# ----- MAKE FILE machine definitions -----
# Mario A. Rodriguez-Meza, Ciudad de Mexico, 5.05.2023
#

MDIR := $(shell pwd)
WRKDIR = $(MDIR)/build

.base:
	if ! [ -e $(WRKDIR) ]; then mkdir $(WRKDIR) ; mkdir $(WRKDIR)/lib; fi;
	touch build/.base

vpath %.c source:general_libs:main
vpath %.o build
vpath .base build

#B LINES TO ADAPT TO YOUR PLATFORM
#
#--------------------------------------------
# Set other local options to this code
# 	INCLUDES :: OPT1
# 	OTHER OPTIONS :: OPT2
# Some of this definitions can be set in a particular machine
#
OPT1 =
#
# This variable is to show settings
#   and set the corresponding definitions to the compiler
OPT2 =
OPT2LIB =
# The one which will be used by CC is CCFLAG. At some point it gets OPT2 definitions...
#
#
#OPT1    +=  -I ./
#OPT1    +=  -I ./general_libs
# Add as many as needed
#
#OPT2   +=  -DOPTIONNAME
# Add as many as needed

#PRECISION = DOUBLEPREC

#
# your C compiler: Comment/uncomment as convenient
#
# GNU c
# default is gcc-15,
# cballys module is working with python 3.13 and gcc-15
#   check with: 'python -c "import cballys"'
CC       = gcc
# tested in MacBook Pro chip: Apple M3 Max
#CC       = gcc-12
#CC       = gcc-15
# With icc (Intel):
#CC       = icc
#

#
#E LINES TO ADAPT TO YOUR PLATFORM

#
# Nothing to do below. Almost... may be some compiler options should be changed
#


#
# Your python interpreter.
# In order to use Python 3, you can manually
# substitute python3 to python in the line below, or you can simply
# add a compilation option on the terminal command line:
# "PYTHON=python3 make all" (THanks to Marius Millea for pyhton3
# compatibility)
PYTHON ?= python
# tested in MacBook Pro chip: Apple M3 Max
#PYTHON = python3.12

# your optimization flag
# clang-12: warning: -O4 is equivalent to -O3 [-Wdeprecated]
#OPTFLAG = -O4 -ffast-math #-march=native
OPTFLAG = -O3 -ffast-math #-march=native
#OPTFLAG = -Ofast -ffast-math #-march=native
#OPTFLAG = -fast

# your openmp flag (comment for compiling without openmp)
OMPFLAG   = -fopenmp
#OMPFLAG   = -mp -mp=nonuma -mp=allcores -g
#OMPFLAG   = -openmp

# all other compilation flags
CCFLAG = -g -fPIC
LDFLAG = -g -fPIC

# pass current working directory to the code
CCFLAG += -D__CTREEBALLSDIR__='"$(MDIR)"'

# where to find include files *.h
INCLUDES = -I../include
HEADERFILES = $(wildcard ./include/*.h)

#INCLUDES += $(OPT1)

#
MLIBS = -lm
#

# your tool for creating static libraries:
AR        = ar rv

# automatically add external programs if needed. First, initialize to blank.
EXTERNAL =

#
ifeq ($(DEFDIMENSION),2)
    DIMCODE = -DTWODIMCODE
    OPT2 += $(DIMCODE)
else
    DIMCODE = -DTHREEDIMCODE
    OPT2 += $(DIMCODE)
endif
#

#
ifeq ($(OPENMPMACHINE),1)
OMPCODE = -DOPENMPCODE
OPT2 += $(OMPCODE)
MLIBS += -lgomp
endif
#


#B correction 2025-04-06
##
#MANUALCHEBYSHEVON = 1
##
## What if mChebyshev is less than 7... correct!
#ifeq ($(MANUALCHEBYSHEVON),1)
#MANUALCHEBYSHEV = -DMANUALCHEBYSHEV
#OPT2 += $(MANUALCHEBYSHEV)
#endif
##
#E correction 2025-04-06


# To activate float real set to 1. Else 0
SINGLEPON = 0
#
ifeq ($(SINGLEPON),1)
SINGLEP = -DSINGLEP
OPT2 += $(SINGLEP)
endif
#

# To activate long integers set to 1. Else 0
LONGINTON = 1
#
ifeq ($(LONGINTON),1)
LONG = -DLONGINT
OPT2 += $(LONG)
endif
#

#
#   will be deprecated...
#PTOPIVOTROTATIONON = 0
#ifeq ($(PTOPIVOTROTATIONON),1)
#OPT2 += -DPTOPIVOTROTATION
#endif
#

# Flags only for Mac...
MACONLYON = 0
ifeq ($(MACONLYON),1)
OPT2 += -DMACONLY
endif

#B ADDONS
#
#B Do not use any of these flags.
#   Are intended for developing and debuging purposes.
#
# To print some useful debuggin info set to 1. Else 0
#   acts also as (thoroughly )DIAGNOSTICS flag...
#   used in some engines, like search_octree_ggg_omp
DEBUGON = 0
DEBUGCOMPILINGON = 0
DEBUGTREEON = 0
DEBUGTRACKINGON = 0
#
#E
#
ifeq ($(DEBUGCOMPILINGON),1)
OPT2 += -Wall
endif
ifeq ($(DEBUGON),1)
OPT2 += -DDEBUG
endif
ifeq ($(DEBUGTREEON),1)
OPT2 += -DDEBUGTREE
endif
ifeq ($(DEBUGTRACKINGON),1)
OPT2 += -DDEBUGTRACKING
endif
#
ifeq ($(ADDONSON),1)
OPT2 += -DADDONS
# Machine definitions
include $(MACHINES_DIR)/addons/Makefile_addons
#
endif

#E ADDONS

#
#B GENERALLIBS section
# Use 0 to get rid off all calls to general_libs routines:
GENERALLIBS_ON = 1

ifeq ($(GENERALLIBS_ON),1)
GENERALLIBS = general_libs
vpath %.c $(GENERALLIBS)
INCLUDES += -I../$(GENERALLIBS)
HEADERFILES += $(wildcard ../general_libs/*.h)
EXTERNAL += clib.o mathfns.o inout.o mathutil.o numrec.o
endif
#
#E

#
#B GETPARAM section
# Use 0  to get rid off all calls to getparam routines:
GETPARAM_ON = 1

ifeq ($(GETPARAM_ON),1)
GETPARAM = getparam
vpath %.c $(GETPARAM)
OPT2 += -DGETPARAM
INCLUDES += -I../getparam
EXTERNAL += getparam.o
endif
#
#E


#---------------------------------------------
# BEGIN GSL and CFITSIO DEFINITIONS (if needed)
#

#
# Definition to set where GSL are located
#
# NagBody dir if needed or adapt it to point
#     the location of GSL
NAGBODYDIR = $(HOME)/NagBody_pkg/local
# In other machines can be:
#NAGBODYDIR = /opt/local
#

#
# Experiment using this lib... apparently is for interfacing with C++
#   therefore look for something similar for C.
#    -L/usr/local/lib/ -lboost_python312
#

# -------------------
#B GSL section
ifeq ($(USEGSL),1)
OPT2 += -DUSEGSL
#GSLON = 1
#ifeq ($(GSLON),1)
ifeq ($(GSLINTERNAL),0)
# Change these lines to fit your GSL installation
    GSL_INCL =  -I$(NAGBODYDIR)/gsl/include
    GSL_LIBS =  -L$(NAGBODYDIR)/gsl/lib -lgsl -lgslcblas
    CCFLAG += $(GSL_INCL)
    LDFLAG += $(GSL_LIBS)
    CCFLAG += -DNOINTERNALGSL
#    OPT2 += -DNOINTERNALGSL
endif
ifeq ($(GSLINTERNAL),1)
include $(MACHINES_DIR)/addons/Makefile_gsl
endif
#endif # ! GSLON
endif # ! USEGSL
#E
# -------------------

# -------------------
#B CFITSIO section
ifeq ($(CFITSIOON),1)
# set to empty string:
    CFITSIO_INCL =
    CFITSIO_LIBS =
ifeq ($(CFITSIOLIBON),0)
# Change these two lines to fit your CFITSIO installation
    CFITSIO_INCL =  -I$(NAGBODYDIR)/cfitsio/include
    CFITSIO_LIBS =  -L$(NAGBODYDIR)/cfitsio/lib -lcfitsio
# May not be needed to include: -lm -lnsl -lsocket
# MLIBS contains already -lm
#
# To be able to use CFITSIO with multi-threaded parallel programs
#   it is needed to ./configure --enable-reentrant
#
#ifeq ($(CFITSIOLIBON),0)
    CCFLAG += $(CFITSIO_INCL)
    LDFLAG += $(CFITSIO_LIBS)
else
    CCFLAG += $(CFITSIO_INCL)
    LDFLAG += $(CFITSIO_LIBS)
endif
endif
# -------------------

#
# END GSL and CFITSIO DEFINITIONS (if needed)
#--------------------------------------------


CCFLAG += $(OPT2)

%.o: %.c .base $(HEADERFILES)
	cd $(WRKDIR);$(CC) $(OPTFLAG) $(OMPFLAG) $(CCFLAG) $(INCLUDES) -c ../$< -o $*.o

