##########################################################################################################################################################
#
#
#
#  Makefile for compiling the new updated myXCLASS package
#  Copyright (C) 2012 - 2024  Thomas Moeller
#
#  I. Physikalisches Institut, University of Cologne
#
#
#
#  Versions of the program:
#
#  Who           When        What
#
#  T. Moeller    2012-05-25  Initial version for the modified version
#
#
#
#  License:
#
#    GNU GENERAL PUBLIC LICENSE
#    Version 3, 29 June 2007
#    (Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>)
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
#
#  Makefile options:    - "all":    builds the myXCLASS program used by the myXCLASS function
#
#                                   Used files:     src/GlobalVariables.f90
#                                                   src/CommandLineArguments.f90
#                                                   src/Read_XML.f90
#                                                   src/MainProgram.f90
#
#
#
#
##########################################################################################################################################################


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


# standard MAGIX directories
bindir = bin


# define path to source file of myXCLASS program
srcdir = src


# define path to source file of myXCLASS program
xclassrcsdir = ../../xclass


# 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 path and names of all source and object files for myXCLASS


# source files
FSRC = $(srcdir)/GlobalVariables.f90 \
       $(srcdir)/CommandLineArguments.f90 \
       $(srcdir)/Read_XML.f90 \
       $(srcdir)/MainProgram.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)


#=========================================================================================================================================================
#=========================================================================================================================================================
# general settings
#=========================================================================================================================================================
#=========================================================================================================================================================


# define name of the executable file
EXEC = myNewXCLASS.exe


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


# set GNU Compiler and flags
ifndef XCLASSFC
    FC = gfortran
else
    FC = ${XCLASSFC}
endif
FFLAGSGNU = -O3 -fopenmp -ffree-form -ffree-line-length-none -fbounds-check -frange-check -fPIC -fallow-argument-mismatch
FFLAGSGNUDBG = -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


# standard build
all: myxclass clean move


# for debugging
dbg: myxclassdbg clean move


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# commands


# myXCLASS compilation commands
myxclass:
	$(FC) $(FFLAGSGNU) -I$(INCLUDEDIR) $(FOBJ) $(liball) $(FSRC) -o $(EXEC)


# myXCLASS with debug compilation commands
myxclassdbg:
	$(FC) $(FFLAGSGNUDBG) -I$(INCLUDEDIR) $(FOBJ) $(liball) $(FSRC) -o $(EXEC)


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# clean directories and previous compilations
clean:
	rm -f *.mod *.o


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# move module file to bin directory
move:
	mv $(EXEC) $(bindir)/


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

