# R's default compiler flags are hidden in Makeconf. To find your Makeconf,
#
# eval $(grep R_HOME_DIR $(which R) | head -1)
# cd $R_HOME_DIR/etc
# find . |grep Makeconf
#
# Now edit to Makeconf to adjust the default compiler flags.

CXX_STD = CXX17

NO_INLINE=-fno-inline -fno-inline-small-functions -fno-default-inline

# UMFPACK is a fast sparse LU decomposition library
UMFPACK_CXXFLAGS=-I/usr/include/suitesparse
UMFPACK_LIBS=-lumfpack -lamd

# Uncomment the following lines to turn on debugging output in the backend
# DEBUG_CXXFLAGS+=-DDEBUGMX                     # DEBUGMX gives Setup, cleanup, and per-iteration details
# DEBUG_CXXFLAGS+=-DDEBUGMX_ALGEBRA             # DEBUGMX_ALGEBRA gives per-compute calculation output (be careful with FIML)
# DEBUG_CXXFLAGS+=-DDEBUGMX_MATRIX              # turn on matrix debugging
# DEBUG_CXXFLAGS+=-DDEBUGMX_ROWS                # DEBUGMX_ROWS gives per-row output for selected FIML rows
# DEBUG_CXXFLAGS+=$(xcheck_CXXFLAGS) -DOMX_BOUNDS_CHECK -D_GLIBCXX_DEBUG -g3 $(NO_INLINE) -O0 -fmax-errors=5  # basic debugging without extra logging
# DEBUG_CXXFLAGS+=-fno-omit-frame-pointer $(NO_INLINE)  # oprofile
# DEBUG_CXXFLAGS+=-UNDEBUG                      # always uncomment this when you debug
# DEBUG_LDFLAGS += $(xcheck_LDFLAGS)            # pass debug flags to linker, i.e., -fsanitize=undefined

# To use gcc's address sanitizer, uncomment:
# COMMON_FLAGS += -fsanitize=address -fno-omit-frame-pointer -fno-common -fsanitize-address-use-after-scope
# then run with env LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/11/libasan.so ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1 R -f ...

# Uncomment if you want to link OpenMx against Intel's MKL, if you installed the MKL directly from an Intel product, under Linux/GNU:
# MKL_CXXFLAGS= -D EIGEN_USE_MKL_ALL -I"/opt/intel/mkl/include"
# Uncomment the following line to statically link against the multithreaded MKL (MKLROOT should already have been defined by an Intel script):
# MKL_LIBS= -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a \
	${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -liomp5 -lpthread -lm -ldl
# Uncomment the following line to statically link against the single-threaded MKL (MKLROOT should already have been defined by an Intel script):
# MKL_LIBS=  -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_sequential.a \
	${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl

# Uncomment if you want to link OpenMx against Debian's package of the Intel MKL
# (you may need to change the paths in MKL_CXXFLAGS and MKL_LIBS if using a different OS, or if you installed the MKL directly from Intel):
# MKL_CXXFLAGS= -D EIGEN_USE_MKL_ALL -I"/usr/include/mkl"
# Uncomment the following line to statically link the single-threaded MKL:
# MKL_LIBS= -Wl,--start-group /usr/lib/x86_64-linux-gnu/libmkl_intel_lp64.a \
	/usr/lib/x86_64-linux-gnu/libmkl_sequential.a /usr/lib/x86_64-linux-gnu/libmkl_core.a \
	-Wl,--end-group -lpthread -lm -ldl
# Uncomment the following line to statically link the multithreaded MKL:
# MKL_LIBS= -Wl,--start-group /usr/lib/x86_64-linux-gnu/libmkl_intel_lp64.a \
	/usr/lib/x86_64-linux-gnu/libmkl_intel_thread.a /usr/lib/x86_64-linux-gnu/libmkl_core.a \
	-Wl,--end-group -liomp5 -lpthread -lm -ldl
# Uncomment the following line to dynamically link the single-threaded MKL:
# MKL_LIBS= -L /usr/lib/x86_64-linux-gnu -lmkl_gf_lp64 -lmkl_intel_lp64 -lmkl_rt -lmkl_sequential -lmkl_core -lmkl_mc -lmkl_def -lpthread -lm -ldl
# Uncomment the following line to dynamically link the multithreaded MKL:
# MKL_LIBS= -L /usr/lib/x86_64-linux-gnu -lmkl_gf_lp64 -lmkl_intel_lp64 -lmkl_rt -lmkl_intel_thread -lmkl_core -liomp5 -lmkl_mc -lmkl_def -lpthread -lm -ldl

# Uncomment next line to link OpenMx against a non-MKL BLAS implementation (assumes BLAS_LIBS is already set appropriately):
# MKL_CXXFLAGS= -D EIGEN_USE_BLAS
# Uncomment next TWO lines to additionally link OpenMx against a non-MKL LAPACK (you may need to appropriately change the path):
# MKL_CXXFLAGS+= -D EIGEN_USE_LAPACK
# MKL_LIBS= -L/usr/lib/x86_64-linux-gnu -llapacke

PKG_CXXFLAGS= $(OPENMP_CXXFLAGS) $(MKL_CXXFLAGS) \
 $(COMPILER_CXXFLAGS) $(ARCH_SPECIFIC_COMPILER_FLAGS) $(DEBUG_CXXFLAGS) $(NLOPT_CXXFLAGS) \
 $(COMMON_FLAGS) -DSTRICT_R_HEADERS -D_HAS_AUTO_PTR_ETC=0 \
 $(shell ${R_HOME}/bin/Rscript -e "StanHeaders:::CxxFlags()")

PKG_CFLAGS=$(COMMON_FLAGS) -DSTRICT_R_HEADERS

PKG_LIBS= $(OPENMP_CXXFLAGS) \
  $(COMPILER_LDFLAGS) $(NPSOL_LDFLAGS) $(NLOPT_LDFLAGS) $(LAPACK_LIBS) \
	$(BLAS_LIBS) $(MKL_LIBS) $(FLIBS) $(ARCH_SPECIFIC_LINKER_FLAGS) $(DEBUG_LDFLAGS) \
  $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")

all: check-fflags $(SHLIB) save-gcno

check-fflags:
	$(if $(findstring fopenmp,$(ALL_FFLAGS)$(ALL_FCFLAGS)), \
  $(error You must remove -fopenmp from your FFLAGS and FCFLAGS see https://github.com/OpenMx/OpenMx/issues/284),)

save-gcno: $(SHLIB)
	mkdir -p ../inst/debug
	-cp *.gcno ../inst/debug/

-include autodep
