
#Files containing evolution equations initial power spectrum module
EQUATIONS     ?= equations
POWERSPECTRUM ?= power_tilt
REIONIZATION ?= reionization
RECOMBINATION ?= recfast



BISPECTRUM ?= SeparableBispectrum

#Module doing non-linear scaling
NONLINEAR     ?= halofit

#Driver program
DRIVER        ?= inidriver.F90
#DRIVER       ?= sigma8.f90
#DRIVER       ?= tester.f90


CAMBLIB       = libcamb_$(RECOMBINATION).a

#Shouldn't need to change anything else...

F90FLAGS      = $(FFLAGS)
HEALPIXLD     = -L$(HEALPIXDIR)/lib -lhealpix -L$(FITSDIR) -l$(FITSLIB)

CAMBOBJ       = constants.o utils.o $(EXTCAMBFILES) subroutines.o inifile.o $(POWERSPECTRUM).o $(RECOMBINATION).o \
	$(REIONIZATION).o modules.o bessels.o $(EQUATIONS).o $(NONLINEAR).o lensing.o $(BISPECTRUM).o cmbmain.o camb.o

F90CRLINK ?= -lstdc++

## CosmoRec
ifeq ($(RECOMBINATION),cosmorec)
COSMOREC_PATH ?=../CosmoRec/
GSL_LINK ?=-lgsl -lgslcblas
camb: libCosmoRec.a
$(CAMBLIB): libCosmoRec.a
F90CRLINK += -L$(COSMOREC_PATH) -lCosmoRec $(GSL_LINK)
endif


ifeq ($(RECOMBINATION),hyrec)
HYREC_PATH ?= ../HyRec/
camb: libhyrec.a
$(CAMBLIB): libhyrec.a
F90CRLINK += -L$(HYREC_PATH) -lhyrec
endif


default: camb

all: camb $(CAMBLIB)


subroutines.o: constants.o utils.o
$(POWERSPECTRUM).o: subroutines.o  inifile.o
$(RECOMBINATION).o: subroutines.o inifile.o
$(REIONIZATION).o: constants.o inifile.o
modules.o: $(REIONIZATION).o $(POWERSPECTRUM).o $(RECOMBINATION).o
bessels.o: modules.o
$(EQUATIONS).o: bessels.o
$(NONLINEAR).o:  modules.o
lensing.o: bessels.o
$(BISPECTRUM).o: lensing.o modules.o
cmbmain.o: lensing.o $(NONLINEAR).o $(EQUATIONS).o $(BISPECTRUM).o
camb.o: cmbmain.o
Matrix_utils.o: utils.o
camb: $(CAMBOBJ) $(DRIVER)
	$(F90C) $(F90FLAGS) $(CAMBOBJ) $(DRIVER) $(F90CRLINK) -o $@

$(CAMBLIB): $(CAMBOBJ)
	ar -r $@ $(CAMBOBJ)

camb_fits: writefits.f90 $(CAMBOBJ) $(DRIVER)
	$(F90C) $(F90FLAGS) -I$(HEALPIXDIR)/include $(CAMBOBJ) writefits.f90 $(DRIVER) $(HEALPIXLD) -DWRITE_FITS -o $@

%.o: %.f90
	$(F90C) $(F90FLAGS) -c $*.f90

%.o: %.F90
	$(F90C) $(F90FLAGS) -c $*.F90


clean:
	-rm -f *.o *.a *.d core *.mod


## CosmoRec make parts
cleanCR:
	cd $(COSMOREC_PATH); make tidy;

libCosmoRec.a:
	cd $(COSMOREC_PATH); make lib;


libhyrec.a:
	cd $(HYREC_PATH); make libhyrec.a;


