#
# Makefile for MIN3P-HPC MPI parallel version
# written by:      Danyang Su
#
# last modified:   Danyang Su - Nov. 13, 2019
#

# Conditional parameters, please leave the variable empty (DO NOT PUT FALSE instead) if the given condition is FALSE.
# e.g., 'with-debug = true' or 'with-debug = false' or 'with-debug = xxx' means debug is turned on.
#       'with-debug = ' means debug is turned off. The provided value does not mean anything here.
with-debug =
with-clean = true

with-intel =
with-intel-after2018 =
with-gcc = true
with-pgi =

with-lis = 
with-metis =
with-cgal = 
with-usg = true
with-mem-monitor = true

with-petsc = true
with-petsc-hdf = true
with-petsc-mumps = true
with-petsc-superlu = true
with-petsc-log = true

with-static-link =

####################################################
######     DO NOT CHANGE ANYTHING BELOW       ######
###### IF YOU ARE NOT SURE WHAT YOU ARE DOING ######
####################################################

################## Debug options ###################
ifdef with-debug
FFLAGS := -g -fcheck=all -fbacktrace -Wall
CXXFLAGS := -g -fcheck=all -fbacktrace -Wall

FPPFLAGS := -DLINUX -DDEBUG -DDEBUG_X64 -DSTANDARD_FORTRAN
CPPFLAGS := -DLINUX -DDEBUG -DDEBUG_X64
else
FFLAGS := -O2
CXXFLAGS := -O2

FPPFLAGS := -DLINUX -DRELEASE -DRELEASE_X64 -DSTANDARD_FORTRAN
CPPFLAGS := -DLINUX -DRELEASE -DRELEASE_X64
endif

################## Compiler options ################
ifdef with-gcc
FPPFLAGS := $(FPPFLAGS) -DGCC
endif

ifdef with-pgi
FPPFLAGS := $(FPPFLAGS) -DPGI
endif

ifdef with-intel
FPPFLAGS := $(FPPFLAGS) -DINTEL

FFLAGS := $(FFLAGS) -qoverride-limits
CXXFLAGS := $(CXXFLAGS) -qoverride-limits
endif


################## LIS options #####################
# To enable LIS solver, set LIS installation path (LIS_DIR) and architecture (LIS_ARCH) 
# before compiling and include LIS_INC and LIS_LIB below, and use -DLIS in FPPFLAGS
ifdef with-lis
LIS_INC := ${LIS_DIR}/${LIS_ARCH}/include
LIS_LIB := -L${LIS_DIR}/${LIS_ARCH}/lib -llis

FFLAGS := $(FFLAGS) -I$(LIS_INC)
CXXFLAGS := $(CXXFLAGS) -I$(LIS_INC)
FPPFLAGS := $(FPPFLAGS) -DLIS
else
LIS_INC :=
LIS_LIB :=
endif

################## metis options ###################
ifdef with-metis
FPPFLAGS := $(FPPFLAGS) -DMETIS
DLIB := -lstdc++ -lm -lmetis
else
DLIB := -lstdc++ -lm
endif

################## CGAL options ####################
# To enable CGAL library, add the following DLIB with CGAL related libraries and use -DCGAL in FPPFLAGS and -DUSECGAL in CPPFLAGS
ifdef with-cgal
DLIB := $(DLIB) -L/usr/local/lib -rdynamic /usr/local/lib/libmpfr.so /usr/local/lib/libgmp.so /usr/local/lib/libCGAL_ImageIO.so.11.0.1 /usr/local/lib/libCGAL.so.11.0.1 /usr/local/lib/libboost_thread.so /usr/local/lib/libboost_system.so -lpthread -lGLU -lGL -lX11 -lXext -lz /usr/local/lib/libCGAL_ImageIO.so.11.0.1 /usr/local/lib/libCGAL.so.11.0.1 /usr/local/lib/libboost_thread.so /usr/local/lib/libboost_system.so -lpthread -lGLU -lGL -lX11 -lXext -lz /usr/local/lib/libmpfr.so /usr/local/lib/libgmp.so /usr/local/lib/libboost_thread.so /usr/local/lib/libboost_system.so -lpthread -Wl,-rpath,/usr/local/lib

FPPFLAGS := $(FPPFLAGS) -DCGAL
CPPFLAGS := -DUSECGAL
endif

################## USG options #####################
ifdef with-usg
FPPFLAGS := $(FPPFLAGS) -DUSG
endif

################## PETSc options ###################
ifdef with-petsc
FPPFLAGS := $(FPPFLAGS) -DMPI -DPETSC -DOUTPUT_DOUBLE

ifdef with-petsc-hdf
FPPFLAGS := $(FPPFLAGS) -DPETSC_HDF
endif

ifdef with-petsc-mumps
FPPFLAGS := $(FPPFLAGS) -DPETSC_HAVE_MUMPS
endif

ifdef with-petsc-superlu
FPPFLAGS := $(FPPFLAGS) -DPETSC_HAVE_SUPERLU
endif

ifdef with-petsc-log
FPPFLAGS := $(FPPFLAGS)  -DPETSC_USE_LOG
endif

endif

################## Static libs #####################
# by default, intel compiler use static link on    #
# windows, PGI compiler does not support static    #
# linking of user binary, GCC compiler may still   #
# link some system dynamic libs                    #
####################################################
ifdef with-static-link
ifdef with-gcc
SLIB := $(SLIB) -static-libgfortran -static-libgcc
endif
ifdef with-intel
SLIB := $(SLIB) -static-intel
endif
endif

################## other options ###################
ifdef with-mem-monitor
FPPFLAGS := $(FPPFLAGS) -DMEMORY_MONITOR
endif


# To use PETSc, set PETSc installation path (PETSC_DIR) and architecture (PETSC_ARCH) before compiling
#PETSc variables for V3.5.3 and previous version
#include ${PETSC_DIR}/conf/variables
#include ${PETSC_DIR}/conf/rules

#PETSc variables for development version, version V3.6.0 and later
include ${PETSC_DIR}/lib/petsc/conf/variables
include ${PETSC_DIR}/lib/petsc/conf/rules

SRC =./

OBJS = $(SRC)hdf5_zero_data.o

all: $(OBJS) chkopts
	-${FLINKER} $(FFLAGS) $(FPPFLAGS) $(CPPFLAGS) -o hdf5_zero_data $(OBJS) ${PETSC_LIB} ${LIS_LIB} ${DLIB} ${SLIB}
ifdef with-clean
	${RM} $(SRC)*.o 
	${RM} $(SRC)*.mod
endif
