####################################################################
# Makefile for GIPS Radiometric Calibration stage
# $Id: Makefile,v 1.17.2.6 2005/12/15 23:00:12 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. BLITZ=$HOME/opt/blitz):
#
#    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
#	 "make test" to make and execute all the unit tests
# 	 "make clean" to remove all executables, object files and other build artifacts.
#

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

# gips libraries
# GIPS_LIBS = -lgips_clok -lgips_fbf -lgips_instrument

# source and unit test files
PLANCK_S = # Planck.cc
PLANCK_T = PlanckTest.cc

CAL_FRAME_S = #CalibrationFrame.cc
CAL_FRAME_T = #CalibrationFrameTest.cc

STAGE_S = #RadiometricCalibration.cc RadCal_SHISv0.cc
STAGE_T = #RadCal_SHISv0Test.cc

MMAP_ADAPTER_S = RadCalMmapAdapter_SHISv0.cc

RAD_CAL_T = RadiometricCalibrationTest.cc

CAL_STUDY_T = PiecewiseLinearCalibrationContextTest.cc

BIND_CDF_T = BindCdfTest.cc

SIM2_T = SimCal2Test.cc

SRC      = $(PLANCK_S) $(CAL_FRAME_S) $(STAGE_S)
TEST_SRC = $(RAD_CAL_T) $(CAL_STUDY_T) $(BIND_CDF_T) $(PLANCK_T)


#APP_SRC = $(MMAP_ADAPTER_S)
#APP = RadCalMmapAdapter_SHISv0

APP_SRC = $(SIM2_T)
APP = simcal2

# library name (as a result of make lib)
LIB = libgips_calrad.a

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

PLATFORM=$(shell uname -s)
# Location of FFTW
ifneq (,$(findstring Darwin,$(PLATFORM)))
FFTW=/sw
FFTW_LIBRARIES=-lfftw -lrfftw -ldrfftw -ldfftw -ldrfftw_threads -ldfftw_threads

NETCDF=/sw
NETCDF_LIBRARIES=-lnetcdf -lnetcdf_c++

#BLITZ=/sw
#BLITZ_LIBRARIES=-lblitz
endif

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

#BLITZ=/usr/local
#BLITZ_LIBRARIES=-lblitz
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$(NETCDF)/include -I$(COMMON) -I$(CALRAD) -I$(MODEL) -I$(CONNECTOR) -I$(IFG2SPC)

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

####################################################################
# 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)
CC= gcc
CXX= g++
FC= g77
LD= gcc

all: test

doc: Doxyfile
	doxygen

####################################################################
####################################################################
# shouldn't have to change anything below this line. 


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

APP_BASE=$(basename $(APP_SRC))
APP_OBJ= $(addsuffix .o,$(APP_BASE))

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

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


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

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

app: $(APP)

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

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 SRC=$(SRC) 
	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) $(APP_OBJ) $(APP) $(TEST_EXES)
	-rm -rf doc

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

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