####################################################################
# Makefile for 
# $Id: Makefile,v 1.30.2.3 2005/12/15 23:22:13 graemem Exp $
####################################################################

# Before using this Makefile, each of the following environment 
# variables must be set to the path to the "include" and "lib" 
# directory for that dependency (eg. BOOST=$HOME/opt/boost):
#
#    BOOST
#    FFTW
#    NETCDF
#
# usage: "make depend" after changes are made to the makefile
#        "make lib" to make a library (.a) file
#        "make" to make an executable file

#external directories
COMMON=../Common 
CONNECTOR=../Connector
MODEL=../Model

# gips libraries
#GIPS_LIBS = -lgips_instrument -lgips_fbf -lgips_clok
GIPS_LIBS=
# source files (*.c *.f)
TEST_SRCS= IfgShiftTest.cc CplxFFTTest.cc SpectrumChopTest.cc IfgToSpectrumTest.cc

SRCS= Cifg2SpectrumMmapAdapter_GIFTSv1.cc
APP = Cifg2SpectrumMmapAdapter


################################################################

PLATFORM=$(shell uname -s)
# Location of FFTW
ifneq (,$(findstring Darwin,$(PLATFORM)))
FFTW=/sw
FFTW_LIBRARIES=-lfftw -lrfftw -ldrfftw -ldfftw -ldrfftw_threads -ldfftw_threads
#BLITZ=/sw
#BLITZ_LIBRARIES=-lblitz
NETCDF=/sw
NETCDF_LIBRARIES=-lnetcdf_c++ -lnetcdf
endif

ifneq (,$(findstring CYGWIN,$(PLATFORM)))
FFTW=/usr/local
FFTW_LIBRARIES=-lrfftw -lfftw -lrfftw_threads -lfftw_threads 

#BLITZ=/usr/local
#BLITZ_LIBRARIES=-lblitz

NETCDF=/usr/local
NETCDF_LIBRARIES=-lnetcdf_c++ -lnetcdf 
endif

ifneq (,$(findstring Linux,$(PLATFORM)))
FFTW_LIBRARIES=-lrfftw -lfftw -lrfftw_threads -lfftw_threads 
#BLITZ_LIBRARIES=-lblitz
NETCDF_LIBRARIES=-lnetcdf_c++ -lnetcdf 
endif

############################################################
# Library (.a files) path for things we're dependent on
LIBRARYPATH= -L$(FFTW)/lib -L$(COMMON) -L$(NETCDF)/lib

# Include files (.h, .fi) path
INCLUDEPATH= -I$(FFTW)/include -I$(BOOST)/include -I$(COMMON) -I$(MODEL) -I$(CONNECTOR) -I$(NETCDF)/include

# libraries to link in
#LIBRARIES= -lstdc++ -lm -lpthread -lc -lm $(FFTW_LIBRARIES) $(GIPS_LIBS)
LIBRARIES= $(GIPS_LIBS) $(FFTW_LIBRARIES) $(NETCDF_LIBRARIES) -lm -lpthread -lstdc++ -lc

############################################################
# compiler and option selection
# optimization: -mpentiumpro for Pentium Pro, Pentium II, III
#               optimization levels -O, -O2, -O3
OPTIMS= 

# set to -g to include symbols used by GDB. else leave blank
GEN_DEBUG=-g

ifneq (,$(findstring Darwin,$(PLATFORM)))
SYS_DEPENDENT_CFLAGS=-Wno-long-double
endif 

CFLAGS= -pipe -Wall -fPIC $(INCLUDEPATH) $(GEN_DEBUG) $(OPTIMS) $(SYS_DEPENDENT_CFLAGS)
FFLAGS= -Wall $(INCLUDEPATH) $(GEN_DEBUG) $(OPTIMS)
LDFLAGS= $(GEN_DEBUG)
FC= g77
LD= gcc

all: test

doc: Doxyfile
	doxygen
####################################################################
####################################################################
# shouldn't have to change anything below this line. 


# object files (*.o): automatic
BASES=$(basename $(SRCS))
OBJS= $(addsuffix .o,$(BASES))

TEST_BASES=$(basename $(TEST_SRCS))
TEST_EXES= $(addsuffix .unittest,$(TEST_BASES))

.SUFFIXES: .c .cc .h .o .cpp .f .unittest


$(EXE): $(OBJS)
	$(LD) -o $(EXE) $(LDFLAGS) $(OBJS) $(LIBRARYPATH) $(LIBRARIES)

$(APP): $(OBJS)
	$(LD) -o $@ $^ $(LDFLAGS) $(LIBRARYPATH) $(LIBRARIES)

app: $(APP)

%.unittest: %.o 
	$(LD) -o $@ $^ $(LDFLAGS) $(LIBRARYPATH) $(LIBRARIES) 

test: $(TEST_EXES) test.sh
	echo "--- test output follows ---"
	./test.sh $(TEST_EXES)

%.o : %.c
	$(CC) $(CFLAGS) -c $<

%.o : %.cc
	$(CXX) $(CFLAGS) -c $<

%.o : %.f
	$(F77) $(FFLAGS) -c $<

info:
	echo BASES=$(BASES) 
	echo SRCS=$(SRCS) 
	echo OBJS=$(OBJS)
	echo TEST_BASES=$(TEST_BASES)
	echo TEST_EXES=$(TEST_EXES)

$(LIB): $(OBJS) 
	ar rcv $(LIB) $(OBJS)
	ranlib $(LIB)

#lib: $(LIB)

clean: 
	-rm -f $(EXE) $(OBJS) $(LIB) $(TEST_EXES) $(APP)
	-rm -rf doc

depend:
	makedepend -- $(CFLAGS) -- $(SRCS)

#------------
