##########################################################################################################################################################
#
#
#
#  Makefile for compiling the XCLASS core library
#  Copyright (C) 2023 - 2024  Thomas Moeller
#
#  I. Physikalisches Institut, University of Cologne
#
#
#
#  Versions of the program:
#
#  Who           When        What
#
#  T. Moeller    2023-09-01  Initial version
#
#
#
#
#  Makefile options:    - "build":  compiles XCLASS source files
#
#                                   Used files:     src/Module_Variables.f90 \
#                                                   src/Module_FunctionCalling.f90 \
#                                                   src/Module_myXCLASS__Core.f90
#
#
##########################################################################################################################################################


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


# define output directory for final binary file
bindir = bin


# define directory of source files for XCLASS library
coresrcdir = src


# define directory of object files
coreobjdir = obj


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


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


## define names of fortran source files for XCLASS library
ARSRC = $(coresrcdir)/Module_Variables.f90 \
        $(coresrcdir)/Module_FunctionCalling.f90 \
        $(coresrcdir)/Module_myXCLASS__Core.f90


## define names of fortran object files for XCLASS library
AROBJ = Module_Variables.o \
        Module_FunctionCalling.o \
        Module_myXCLASS__Core.o


#=========================================================================================================================================================
#=========================================================================================================================================================
# define C compiler and corresponding flags
#=========================================================================================================================================================
#=========================================================================================================================================================


# set fortran compiler using environment variable 'XCLASS_FORTRAN_COMPILER'
ifndef XCLASS_FORTRAN_COMPILER
    FC = gfortran
else
    FC = ${XCLASS_FORTRAN_COMPILER}
endif


# set fortran compiler and flags
FCFLAGS = -O3 -fopenmp -static -ffree-form -ffree-line-length-none -fbounds-check -frange-check -fPIC -fallow-argument-mismatch
FCFLAGSDBG = -g -O0 -fbacktrace -fopenmp -static -ffree-form -ffree-line-length-none -fbounds-check -frange-check -fPIC -Wall -Wextra -fallow-argument-mismatch


#=========================================================================================================================================================
#=========================================================================================================================================================
# define programs tp create a static library
#=========================================================================================================================================================
#=========================================================================================================================================================


# library for XCLASS
RANLIB = ranlib
AR = ar
ARFLAGS = cr
LIBN = libxclass.a


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


##--------------------------------------------------------------------------------------------------------------------------------------------------------
## define order of commands
# all: xclass libx ranlib moveobj move clean


##--------------------------------------------------------------------------------------------------------------------------------------------------------
## define order of commands
build: clean xclass moveobj clean


##--------------------------------------------------------------------------------------------------------------------------------------------------------
## compile XCLASS source files
xclass:
	$(FC) $(FCFLAGS) -c $(ARSRC)


## build XCLASS library
libx:
	$(AR) $(ARFLAGS) $(LIBN) $(AROBJ)


## run ranlib
ranlib:
	$(RANLIB) $(LIBN)


# clean directories and previous compilations
clean:
	rm -f *.a *.o *.mod $(coresrcdir)/*.mod $(bindir)/*


# move compilation
moveobj:
	mv *.mod $(coreobjdir)/
	mv *.o $(coreobjdir)/


# move compilation
move:
	mv $(LIBN) $(bindir)/


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

