#  cluggey makefile to compile all the programs in scr. Needed to deviate from
#   standard Makefile method for est_noise8; with the standard, Makefile format, the blas files
#    would not link; but if I compile and Link in one step, the est_noise would compile with no errors
#
########IMPRTANT##############
#  Need to 'source /opt/intel/oneapi/setvars --force'  ##########


FC = ifort

# useful for debuggin
FFLAGS =  -I${MKLROOT}/include/intel64/ilp64 -i8  -I"${MKLROOT}/include" -Ofast  -w -std=f2008   -g -fcheck=all  #  -mcmodel=medium

FFLAGS1 = -Ofast -w -std=legacy
#  fft --- https://www.netlib.org/fftpack/
#          https://fortran-lang.github.io/fftpack/ 
#FFT = ~/proglib/fftpack/build/gfortran_50F62D7499E64B65/fftpack/libfftpack.a
FFT = dfft.o
BLAS = -I${MKLROOT}/include/intel64/ilp64 -i8  -I"${MKLROOT}/include" ${MKLROOT}/lib/intel64/libmkl_blas95_ilp64.a ${MKLROOT}/lib/intel64/libmkl_lapack95_ilp64.a \
      -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a \
     ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group \
      -liomp5 -lpthread -lm -ldl 

NBLAS = -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -liomp5 -lpthread -lm -ldl

SRC = time_mod.f90  alloc_mod.f90 est_noise_mod.f90 filterfunc_mod.f90  psd_calc8.f90 bust_mod.f90  GetStuff_mod.f90  bust8.f90 \
  adjust8.f90 genNoise_mod.f90  randnor.f gen_noise8.f90  mle_mod.f90   NedlerMeadSimplex_mod.f90  model_fit_A_mod.f90 est_noise8.22.f90 \
  compare_wander_mod.f90 compare_wander8.01.f90

#OBJ = ${SRC:.f90=.o} #substitute .f90 with .o



%.o: %.f90  #wildcard rule, creation of *.o depends on *.f90
	$(FC) $(FFLAGS)  -c $<

dfft:
	ifort -c dfft.f -std=legacy -w
	
OBJ1 = time_mod.o alloc_mod.o filterfunc_mod.o  psd_calc8.o
#  need to include the fft library 	
psd_calc8: $(OBJ1)
	$(FC) $(FFT) $(BLAS) -o $@ $(OBJ1) 

OBJ2 = bust_mod.o time_mod.o GetStuff_mod.o  bust8.o    
bust8: $(OBJ2)
	$(FC) $(NBLAS) -o $@ $(OBJ2)
	
OBJ3 = GetStuff_mod.o time_mod.o adjust8.o
adjust8: $(OBJ3)
	$(FC) $(NBLAS) -o $@ $(OBJ3)
	
OBJ4 = alloc_mod.o genNoise_mod.o time_mod.o randnor.o filterfunc_mod.o  gen_noise8.o
gen_noise8: $(OBJ4)
	$(FC) $(FFT) $(NBLAS) -o $@ $(OBJ4)

OBJ5 =   alloc_mod.o  est_noise_mod.o model_fit_A_mod.o  mle_mod.o filterfunc_mod.o  genNoise_mod.o randnor.o GetStuff_mod.o \
        time_mod.o  NedlerMeadSimplex_mod.o est_noise8.22.o	
##est_noise8: $(OBJ5)
#	$(FC) $(BLAS) $(FFT) -o $@ $(OBJ5)

#  none standard --- compiles and links in one step
#  using Intel open MP threading as that gives best performance (cpu speed)
est_noise8:
	ifort -o est_noise8 alloc_mod.f90 est_noise_mod.f90 model_fit_A_mod.f90  mle_mod.f90 filterfunc_mod.f90  genNoise_mod.f90 randnor.f GetStuff_mod.f90 \
        time_mod.f90  NedlerMeadSimplex_mod.f90 est_noise8.22.f90 dfft.o \
	-Ofast \
	 -I${MKLROOT}/include/intel64/ilp64 -i8  -I"${MKLROOT}/include" \
      ${MKLROOT}/lib/intel64/libmkl_blas95_ilp64.a ${MKLROOT}/lib/intel64/libmkl_lapack95_ilp64.a \
      -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a \
     ${MKLROOT}/lib/intel64/libmkl_core.a  \
      -liomp5 -lpthread -lm -ldl  -Wl,--end-group

#  TBB threading
#	-I${MKLROOT}/include/intel64/ilp64 -i8  -I"${MKLROOT}/include" \
#	 ${MKLROOT}/lib/intel64/libmkl_blas95_ilp64.a -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a \
#	${MKLROOT}/lib/intel64/libmkl_tbb_thread.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group \
##	-L${TBBROOT}/lib/intel64/gcc4.8 -ltbb -lstdc++ -lpthread -lm -ldl  

#  sequential threading
#	-I${MKLROOT}/include/intel64/ilp64 -i8  -I"${MKLROOT}/include" \
#	 ${MKLROOT}/lib/intel64/libmkl_blas95_ilp64.a -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_ilp64.a \
#	${MKLROOT}/lib/intel64/libmkl_sequential.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl

	
OBJ6= time_mod.o alloc_mod.o GetStuff_mod.o compare_wander_mod.o randnor.o filterfunc_mod.o genNoise_mod.o compare_wander8.01.o
compare_wander8: $(OBJ6)
	$(FC) $(FFT) $(NBLAS) -o $@ $(OBJ6)
	
all:  dfft psd_calc8 bust8 adjust8 gen_noise8 est_noise8 compare_wander8
	@echo "Ready to run make install"
	
install:
	mv psd_calc8 bust8 adjust8 gen_noise8 est_noise8 compare_wander8 ../bin
	@echo "Ready to run make clean"
clean:
	rm -fr *.o *.mod *.dSYM core psd_calc bust8 adjust8 gen_noise8 est_noise8 compare_wander8 *.out *.jrn


###

