##########################################################################################################################################################
#
#
#
#  Makefile for compiling the XCLASSInterface package for python
#  Copyright (C) 2012 - 2024  Thomas Moeller
#
#  I. Physikalisches Institut, University of Cologne
#
#
#
#  Versions of the program:
#
#  Who           When        What
#
#  T. Moeller    2016-04-19  Initial version
#
#
#
#
#  Makefile options:    - "all":    builds the TinyXCLASS package
#
#                                   Used files:     src/XCLASSInterface.f90
#
#                       - "sig":    creates signature file
#
#
##########################################################################################################################################################


#=========================================================================================================================================================
#=========================================================================================================================================================
# define subdirectories
#=========================================================================================================================================================
#=========================================================================================================================================================


# standard MAGIX directories
bindir = bin


# define path to source file of XCLASS interface
srcdir = src


# define path to source files of XCLASS functions
xclassrcsdir = ..


# define path to external packages
extlibdir = ../../external


#=========================================================================================================================================================
#=========================================================================================================================================================
# define include directories
#=========================================================================================================================================================
#=========================================================================================================================================================


# define path of mod files of XCLASS
INCLUDEDIR = $(xclassrcsdir)/core/obj


#=========================================================================================================================================================
#=========================================================================================================================================================
# define source and objects files
#=========================================================================================================================================================
#=========================================================================================================================================================


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# define names of fortran source files


# define path and name of source file of XCLASS interface
FSRC = $(srcdir)/XCLASSInterface.f90


# define path and name of object files
FOBJ = $(INCLUDEDIR)/Module_FunctionCalling.o \
       $(INCLUDEDIR)/Module_myXCLASS__Core.o \
       $(INCLUDEDIR)/Module_Variables.o


#=========================================================================================================================================================
#=========================================================================================================================================================
# add libraries
#=========================================================================================================================================================
#=========================================================================================================================================================


# add cfitsio library
libcfitsio = -L$(extlibdir)/CFITSIO/bin -lcfitsio


# add fftw3 library
libfftw3 = -L$(extlibdir)/FFTW/bin -lfftw3


# add SQLite3 library
libsqlite3 = -L$(extlibdir)/SQLite/bin -lsqlitex


# add additional library
# -lm:          library of basic mathematical functions
# -lz:          dynamic library libz.so.x.y.z (library for data compression, required by cfitsio)
# -lcurl:       dynamic linking library (the multiprotocol file transfer library)
# -ldl:         dynamic linking library
# -lpthread:    POSIX threads library
# -lgomp:       GCC OpenMP shared support library
libadd = -lm -lz -lcurl -ldl -lpthread -lgomp


# define combined library flags
# environment variables "XCLASSFINCLUDE" and "XCLASSLDFLAGS" are needed to find libraries on arm-MAC machines
liball = ${XCLASSFINCLUDE} ${XCLASSLDFLAGS} $(libcfitsio) $(libfftw3) $(libsqlite3) $(libadd)


#=========================================================================================================================================================
#=========================================================================================================================================================
# define FORTRAN compiler and corresponding flags
#=========================================================================================================================================================
#=========================================================================================================================================================


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# define name of the executable file
EXEC = xclassinterface


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# FORTRAN compiler


# set f2py compiler and flags
FCPY = python3 -m numpy.f2py
# FCPY = f2py
FPYFLAGS = --fcompiler="gnu95" --f90flags="-g -ffree-form -ffree-line-length-none -fbounds-check -frange-check -fPIC"
FPYFLAGSDBG =  --fcompiler="gnu95" --f90flags="-g -O0 -fbacktrace -fopenmp -static -ffree-form -ffree-line-length-none -fbounds-check -frange-check -fPIC -Wall -Wextra -fallow-argument-mismatch"


#=========================================================================================================================================================
#=========================================================================================================================================================
# define rules
#=========================================================================================================================================================
#=========================================================================================================================================================


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# define compilation rules


# define standard method
all: f2py clean move echo


# define standard method optimized for debugging
dbg: f2pyDBG clean move


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# compile f2py


# compile f2py (standard compilation)
f2py:
	$(FCPY) $(FPYFLAGS) -I$(INCLUDEDIR) $(FOBJ) $(liball) -c $(FSRC) -m $(EXEC)


# compile f2py optimized for debugging
f2pyDBG:
	$(FCPY) $(FPYFLAGSDBG) -I$(INCLUDEDIR) $(liball) -c $(FSRC) -m $(EXEC)


# compile f2py to generate sig. file
sig:
	$(FCPY) $(FPYFLAGS) -I$(INCLUDEDIR) $(liball) -c $(FSRC) -m $(EXEC) -h $(EXEC).pyf


# compile f2py to generate sig. file
echo:
	echo $(FCPY) $(FPYFLAGS) -I$(INCLUDEDIR) $(liball) -c $(FSRC) -m $(EXEC)



#---------------------------------------------------------------------------------------------------------------------------------------------------------
# clean directories and previous compilations
clean:
	rm -f *.o *.pyf bin/*


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# move module file to bin directory
move:
	mv *.so bin/$(EXEC).so


#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# done
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------

