# Makefile for GNU-make
# Fortran: GNU gfortran 4.8 to 9
#
# É. Canot -- IPR/CNRS --  8 Oct 2024
#
# this Makefile has no option. See the main Makefile.
#

include ../../make_opt.inc

include ../../Makefile.config

# configurable options (OS dependent)

# for optimization
OPT_FLAGS = -O3 -funroll-loops

# path to shared libraries of GNU compilers
#F90_LIB_DIR = $(GCC_LIB)

ifdef F90_LIB_DIR
  F90_LIB = -Wl,-rpath,$(F90_LIB_DIR) -L$(F90_LIB_DIR) -lgfortran

  # for linking (SPQR is written in C++)
  # usually '-lstdc++' would be sufficient if you have only one version
  # of GCC on your system.
  #LIBSTDC++ = -lstdc++
  # number so.6 used for release from 4.5 to 7
  LIBSTDC++ = -Wl,-rpath,$(F90_LIB_DIR) -L$(F90_LIB_DIR) -lstdc++
endif

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

# special tag : vendor / f90 abbrev
F90C_TAG = GNU_GFC

# 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 # 'no'
  MAKE_MEMTR=MEMTR=no
endif

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

F90C = $(F90_COMP)

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

ifeq '$(CFG)' 'debug'

  CFG_FLAGS = -fno-range-check \
              -g -fbounds-check -fbacktrace #-pg
  ifeq '$(STATIC_LIBGFORTRAN)' '"yes"'
    LD_FLAGS = -static-libgfortran #-pg
  endif

else # 'release'

  CFG_FLAGS = -fno-range-check $(OPT_FLAGS)
  LD_FLAGS = -s

endif

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

  FPP_FLAGS += -D_HAS_IM_CONVERT

endif

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

  FPP_FLAGS += -D_DARWIN

endif

ifeq '$(NO_X11)' '"yes"'

  FPP_FLAGS += -D_NO_X11

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 '$(DARWIN)' '"yes"'
  LAPACK_LIBS =
else
  LAPACK_LIBS = -Wl,-rpath,$(LAPACK_DIR) -Wl,-rpath,$(BLAS_DIR)
endif
LAPACK_LIBS += -L$(LAPACK_DIR) -llapack -L$(BLAS_DIR) -lblas $(ADD_LIB)

# 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) $(ZLIB_LIBS) \
        -lpthread $(TERMCAP_LIB) $(F90_LIB)

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

ifeq '$(CFG)' 'debug'
  ifeq '$(READLINE)' '"yes"'
    ifeq '$(DARWIN)' '"yes"'
      LIBS += -lreadline
    else
      LIBS += -lreadline -lhistory
    endif
  endif
endif

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

ifneq '$(CFG)' 'debug'
  # OPTIM mode, using shared libraries: most of external libraries are
  # already linked in fml and fgl
  LIBS = $(MUESLI_LIBS) $(F90_LIB)
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 *.smod \
         *.out *.prof *.eps *.xpm *.pdf

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

