#-----------------------------------------------------------------------
# Makefile for SOFT_JGR
# Fortran 90 programs only
# P. Robert, LPP, 2021
#-----------------------------------------------------------------------


# ====================
# Choice of compiler
# ====================

FC = gfortran

#Gfortran creates a file name.mod for each module name.
#This confuses the gnu make, which thinks that .mod files are Modula2 source code files.
#You must override this built-in rule by adding the following line somewhere in your Makefile.

%.o : %.mod


# ======================
# optimize or debug mode
# ======================

#MODE=O
 MODE=D

# ================================
# Choice of compilation options
# ================================


# gfortran linux and Windows/MinGW compiler

ifeq ($(FC),gfortran)
	ifeq ($(MODE),D)
	FCOMP = -c -O -g -Wall -fcheck=all -fbacktrace -ffpe-trap=zero,underflow,overflow,invalid -fbounds-check -ftrapv -finit-local-zero -Jmod
	FLINK =	   -O -g -Wall -fcheck=all -fbacktrace -ffpe-trap=zero,underflow,overflow,invalid -fbounds-check -ftrapv -finit-local-zero -Jmod
	else
	FCOMP = -c -O2 -static -Wall -Jmod	-ffpe-summary=none	-finit-local-zero
	FLINK =	   -O2 -static -Wall -Jmod	-ffpe-summary=none	-finit-local-zero
	endif
endif
#-O -Wall -fcheck=all -g -fbacktrace

# ==========================
# Directories definition
# ==========================

SRC_DIR = src/
OBJ_DIR = obj/
BIN_DIR = bin/
MOD_DIR = mod/

# =====================
# List of executables
# =====================


EXEC =	\
	$(BIN_DIR)fgm_cef_to_dat.exe \
	$(BIN_DIR)alitime_fgmpos.exe \
	$(BIN_DIR)cocurl_from_fgmpos_ali.exe \
	$(BIN_DIR)read_curl_div.exe \
	$(BIN_DIR)read_bin_dat.exe \
	$(BIN_DIR)curl_div_gse_to_gsm.exe \
	$(BIN_DIR)crea_3D_grids.exe \
	$(BIN_DIR)crea_grid_B_ini.exe \
	$(BIN_DIR)crea_grid_B_ini-igrf.exe \
	$(BIN_DIR)plot_3D_grid.exe \
	$(BIN_DIR)c_curl_from_B3D_grid.exe

# ========
# make all
# ========

# Create all exe files in bin directory.
# The library read_3D_grid.o is created first because she use modules used by the most part of all programs.

all : $(OBJ_DIR)read_3D_grid.o  $(EXEC)
	@echo mv *.mod $(MOD_DIR)
	@echo	" "
	@echo "-> compilation done."
	@echo "-> Read	lines above to check success."



# ================================================== ==
# Definition of dependencies for the creation of exe
# ================================================== ==

# The objects are taken from $ (OBJ_DIR),
# The executables are put in $ (BIN_DIR)
#
# We define here the name of each target (the .exe) and the list
# of its dependencies.
# Then, we run the compiler to link all the dependencies
# (the $ ^) and build the target (the $ @ which is the .exe).

$(BIN_DIR)fgm_cef_to_dat.exe            : $(OBJ_DIR)fgm_cef_to_dat.o 
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)alitime_fgmpos.exe            : $(OBJ_DIR)alitime_fgmpos.o \
                                          $(OBJ_DIR)lib_alitime_fgmpos.o \
                                          $(OBJ_DIR)lib_time.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)cocurl_from_fgmpos_ali.exe    : $(OBJ_DIR)cocurl_from_fgmpos_ali.o \
                                          $(OBJ_DIR)rocotlib_V3p3.o \
                                          $(OBJ_DIR)lib_alitime_fgmpos.o \
                                          $(OBJ_DIR)lib_time.o \
                                          $(OBJ_DIR)geopack_2008.o \
                                          $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@
    
$(BIN_DIR)read_curl_div.exe       :  $(OBJ_DIR)read_curl_div.o 
	$(FC) $(FLINK) $^ -o $@
    
$(BIN_DIR)read_bin_dat.exe        :  $(OBJ_DIR)read_bin_dat.o 
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)curl_div_gse_to_gsm.exe :  $(OBJ_DIR)curl_div_gse_to_gsm.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)crea_3D_grids.exe       :  $(OBJ_DIR)crea_3D_grids.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)crea_grid_B_ini.exe     :  $(OBJ_DIR)crea_grid_B_ini.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@


$(BIN_DIR)crea_grid_B_ini-igrf.exe :  $(OBJ_DIR)crea_grid_B_ini-igrf.o \
                                      $(OBJ_DIR)rocotlib_V3p3.o \
                                      $(OBJ_DIR)lib_time.o \
                                      $(OBJ_DIR)geopack_2008.o \
                                      $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)c_curl_from_B3D_grid.exe : $(OBJ_DIR)c_curl_from_B3D_grid.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)plot_3D_grid.exe        :  $(OBJ_DIR)plot_3D_grid.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)rogralib_V9p7.o \
                                     $(OBJ_DIR)rograsuplib.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)mag_bow.o
	$(FC) $(FLINK) $^ -o $@
	
# ==========================
# Library compilation
# ==========================
 
# Here we define the name of each target (the .o of each library)
# and its source (the .f90)
#
# Then we run the compiler to compile the source
# (which is the first $ <)
# and make the target ("@" = the .o object)
# Warning: the libraries which are not used by the .exe
# will not be compiled.

$(OBJ_DIR)read_3D_grid.o	  : $(SRC_DIR)read_3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@
      
$(OBJ_DIR)mag_bow.o	          : $(SRC_DIR)mag_bow.f90
	  $(FC) $(FCOMP) $< -o $@
      
$(OBJ_DIR)rocotlib_V3p3.o	  : $(SRC_DIR)rocotlib_V3p3.f90
	  $(FC) $(FCOMP) $< -o $@
	  
$(OBJ_DIR)rogralib_V9p7.o	  : $(SRC_DIR)rogralib_V9p7.f90
	  $(FC) $(FCOMP) $< -o $@
	  
$(OBJ_DIR)rograsuplib.o	      : $(SRC_DIR)rograsuplib.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)lib_time.o x   	  : $(SRC_DIR)lib_time.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)lib_alitime_fgmpos.o : $(SRC_DIR)lib_alitime_fgmpos.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)lib_tools_CLUSTER.o  : $(SRC_DIR)lib_tools_CLUSTER.f90
	  $(FC) $(FCOMP) $< -o $@  

$(OBJ_DIR)geopack_2008.o       : $(SRC_DIR)geopack_2008.f
	  $(FC) $(FCOMP) $< -o $@  
      
# ==========================
# Programmes compilation
# ==========================
	  
$(OBJ_DIR)fgm_cef_to_dat.o		 : $(SRC_DIR)fgm_cef_to_dat.f90
	  $(FC) $(FCOMP) $< -o $@
	  
$(OBJ_DIR)alitime_fgmpos.o		 : $(SRC_DIR)alitime_fgmpos.f90
	  $(FC) $(FCOMP) $< -o $@
	  
$(OBJ_DIR)cocurl_from_fgmpos_ali.o 	 : $(SRC_DIR)cocurl_from_fgmpos_ali.f90
	  $(FC) $(FCOMP) $< -o $@
	  	  
$(OBJ_DIR)read_curl_div.o		 : $(SRC_DIR)read_curl_div.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)read_bin_dat.o		 : $(SRC_DIR)read_bin_dat.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)curl_div_gse_to_gsm.o	 	 : $(SRC_DIR)curl_div_gse_to_gsm.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)crea_3D_grids.o		 : $(SRC_DIR)crea_3D_grids.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)crea_grid_B_ini.o		 : $(SRC_DIR)crea_grid_B_ini.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)crea_grid_B_ini-igrf.o 	 : $(SRC_DIR)crea_grid_B_ini-igrf.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)c_curl_from_B3D_grid.o 	 : $(SRC_DIR)c_curl_from_B3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)plot_3D_grid.o		 : $(SRC_DIR)plot_3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@	  
		  	  
# ==========
# make clean
# ==========

# Delete all .exe , .o and .mod
# Delete all.tmp,	*.core, *.~

clean :
	@echo "***	Removing *.exe *.o, .mod files..."

	@find ./bin -name "*.exe"	-exec rm -f {} \;
	@find ./obj -name "*.o"		-exec rm -f {} \;
	@find ./mod -name "*.mod"	-exec rm -f {} \;
	
	@rm -f *.tmp *core *~ 
	
	@echo "clean done."

#-----------------------------------------------------------------------
# End of Makefile for all SOFT_JGR
#-----------------------------------------------------------------------
