# Makefile for GNU-make
# INTEL Fortran: ifort 13.x to 19.x // 2023 to 2024
#
# É. Canot -- IPR/CNRS -- 26 Jan 2024
#
# this Makefile has no option. See the main Makefile.
#

include ../../make_opt.inc

include ../../Makefile.config

# configurable options (OS dependent)

# for optimization (both for ifort, icc, icpc)
# [BUG ifort: '-ipo' leads to Fatal Error]
OPT_FLAGS = -O3

#=======================================================================

# special tag : vendor / f90 abbrev
F90C_TAG=INTEL_IFC

# memorize MAKE local options
ifeq '$(CFG)' 'debug'
  MAKE_CFG=CFG=debug
else # 'release'
  MAKE_CFG=CFG=release
endif
ifeq '$(MEMTR)' 'yes'
  MAKE_MEMTR=MEMTR=yes
else
  ifeq '$(MEMTR)' 'full'
    MAKE_MEMTR=MEMTR=yes
    MEMTR_FULL=yes
  else
    MAKE_MEMTR=MEMTR=no
  endif
endif

# compiler -------------------------------------------------------------

F90C = $(F90_COMP)

# OpenMP flag (specific for each compiler)
OMP_FLAG = #-qopenmp

ifeq '$(CFG)' 'debug'

  CFG_FLAGS = -g -O0 -CB -traceback #-pg
  LD_FLAGS = #-Vaxlib #-pg

else # 'release'

  CFG_FLAGS = $(OPT_FLAGS)
  LD_FLAGS = #-Vaxlib

endif

ifeq '$(IM_CONVERT)' '"yes"'

  FPP_FLAGS += -D_HAS_IM_CONVERT

endif

ifeq '$(DARWIN)' '"yes"'

  FPP_FLAGS += -D_DARWIN

endif

INCLUDES = -I../../include

F90_FLAGS = $(INCLUDES) $(CFG_FLAGS) $(OMP_FLAG)

LD_FLAGS += $(OMP_FLAG)

#----------------------------- libraries -------------------------------

ifeq '$(CFG)' 'debug'

  MUESLI_LIBS = ../../lib/libfgl.a ../../lib/libfml.a

  ifeq '$(HDF5)' '"yes"'
    HDF5_F90_LIBS = $(HDF5_F90_DIR)/libhdf5_fortran.a
  else
    HDF5_F90_LIBS =
  endif

  MFPLOT_LIB = ../../lib/libmfplot.a

else # 'release' (absolute path, comma after -rpath)
     # fix: doublequotes around `pwd` are required to take into account
     # some blanks in the path...

  MUESLI_LIBS = -Wl,-rpath,"`pwd`"/../../lib -L"`pwd`"/../../lib -lfgl -lfml

  ifeq '$(HDF5)' '"yes"'
    HDF5_F90_LIBS = -Wl,-rpath,$(HDF5_F90_DIR) -L$(HDF5_F90_DIR) -lhdf5_fortran
  else
    HDF5_F90_LIBS =
  endif

  # mfplot is already linked in the shared versions of FML
  MFPLOT_LIB =

endif

# BLAS/LAPACK
ifeq '$(BLAS_LAPACK_VENDOR)' '"yes"'
  LAPACK_LIBS = -qmkl
else
  LAPACK_LIBS = -Wl,-rpath,$(LAPACK_DIR) -L$(LAPACK_DIR) -llapack \
                -Wl,-rpath,$(BLAS_DIR) -L$(BLAS_DIR) -lblas \
                $(ADD_LIB)
endif

# PAPI (PerfCtr)
ifeq '$(PAPI)' 'yes'
  PAPI_LIBS = $(PAPI_DIR)/libpapi.a
else # linux kernel not patched with PERFCTR, or PAPI not installed
  PAPI_LIBS =
endif

ifeq '$(NEED_TERMCAP)' '"yes"'
  TERMCAP_LIB = -ltermcap
else # recent READLINE library doesn't include TERMCAP
  TERMCAP_LIB =
endif

ifeq '$(HDF5)' '"yes"'
  HDF5_LIBS = -L$(HDF5_1_6) -lhdf5
else
  HDF5_LIBS =
endif

ZLIB_LIBS = -L$(ZLIB_DIR) -lz

LIBS = $(MUESLI_LIBS) $(LIBSTDC++) $(PAPI_LIBS) $(LAPACK_LIBS) \
        $(MFPLOT_LIB) \
       $(HDF5_F90_LIBS) $(HDF5_LIBS) \
       -L$(PNGLIB) -lpng $(ZLIB_LIBS) -lpthread \
       -lreadline -lhistory $(TERMCAP_LIB) -lsvml

ifeq '$(NO_X11)' '"no"'
  LIBS += -L$(X11_DIR) -lfreetype -lfontconfig -lXrender -lX11
endif

ifeq '$(CFG)' 'debug'
  ifeq '$(NEED_LRT)' '"yes"'
    LIBS += -lrt
  endif
endif

ifneq '$(CFG)' 'debug'
  # OPTIM mode, using shared libraries: most of external libraries are
  # already linked in fml and fgl
  LIBS = $(MUESLI_LIBS)
endif

#=======================================================================

include ../../../src/make/fgl_tests/Makefile_progs.inc

all : $(PROGS)

include ../../../src/make/fgl_tests/Makefile_rules.inc

#------------------------------- rules ---------------------------------

.SUFFIXES :
.SUFFIXES : .F90 .f90 .o

obj/%.o : %.F90
	$(F90C) $(FPP_FLAGS) -D_$(F90C_TAG) $(F90_FLAGS) -c $< -o $@

obj/%.o : %.f90
	$(F90C) $(F90_FLAGS) -c $< -o $@

clean :
	rm -f obj/* *.mod make_options \
         *.out *.prof *.eps *.png *.xpm *.pdf

distclean : clean
	rm -f $(PROGS) .*mf_win_pos_db

