# SPDX-FileCopyrightText: 2025 Deutsches Elektronen-Synchrotron DESY
# SPDX-License-Identifier: AGPL-3.0-only

# The makefile compiles (type make) and makes an executable called XCASCADE.x

EXEPATH = .

EXE = $(EXEPATH)/xcascade.x
LD = ld

# choose the compiles among gfortran, ifort

# gfortran
ifeq ($(c),gf)
	F90 = gfortran
	# Flags for quick compilation if you compile it as "make c=gf"
	F90FLAGS = -ffree-line-length-0 -fopenmp -Og
else ifeq ($(c),gfd)
	# Flags for debugging if you compile it as "make c=gfd"
	F90 = gfortran
	F90FLAGS = -ffree-line-length-0 -fopenmp -Wextra -Wall -pedantic -fbounds-check -g -fbacktrace -ffpe-trap=zero,overflow,underflow
	F90FLAGS +=  -Wno-tabs -Wno-unused-variable -Wno-unused-dummy-argument
	# F90FLAGS += -fdefault-real-8   # dangerous!
else ifeq ($(c),gfo)
	# Flags for maximum performance if you compile it as "make c=gfo"
	F90 = gfortran
	F90FLAGS = -ffree-line-length-0 -fopenmp -O3 -ffast-math -fexpensive-optimizations
	#F90FLAGS += -static -static-libgfortran 
else 
	# choosing ifort; you can change the next line to F90 = ifx if you use ifx compiler
	F90 = ifort
	ifneq (${OMP},) # OMP=no means no omp
		F90FLAGS = -qmkl
	else  # there is omp by default
		F90FLAGS = -qopenmp -D OMP_inside
		F90FLAGS += -qmkl=parallel
	endif
	ifeq ($(db),y)
		# flags for debugging if run as "make db=y"
		F90FLAGS += -debug all -check all -fpe0 -O0 -g -fp-model precise -traceback -gen-interfaces -warn interfaces -fpp
		#F90FLAGS += -g -fbounds-check
	else
		# Flags for maximum performance if you compile it as just "make"
		F90FLAGS += -O5 -fpp -ipo
	endif
endif

# list the files necessary for the compilation in the order of their dependencies
OBJS = Constants.o Objects_subroutine.o Cross-sections.o Dealing_with_input_files.o Creating_output_files.o  Laser_pulse.o Monte_Carlo.o Cascading_code.o

# Find the libraries if necessary, depending on the compiler
default:
	@echo " "
ifeq ($(c),gf)
	@echo "Compiling XCASCADE with gfortran"
else ifeq ($(c),gfd)
	@echo "Debugging XCASCADE with gfortran"
else ifeq ($(c),gfo)
	@echo "Compiling XCASCADE with gfortran"
else ifeq ($(c),if13)
	@echo "Compiling XCASCADE with ifort"
else ifeq ($(db),y)
	@echo "Debugging XCASCADE with ifort"
endif

# autoclean before the compilation
	rm -f *.o
	rm -f *.mod
	$(MAKE) $(EXE)
# autoclean after the compilation
	rm *.o
	rm *.mod
	
# compile and load
$(EXE):	$(OBJS)
	$(F90) $(F90FLAGS) $(LDFLAGS) -o $(EXE)  $(OBJS)

.SUFFIXES: .f90 .o .mod
.f90.o:
	$(F90) $(F90FLAGS) -c $*.f90

clean:
	rm -f *.o
	rm -f *.mod
	rm -f XCASCADE.x

# cleaning and removing all results - be careful!
cleanall:
	rm -f *.o
	rm -f *.mod
	rm -f xcascade.x
	rm -f *.dat
	rm -f cascades/*.dat
	rm -f *.sh
	rm -f plots/*.eps
	rm -f job*out
	rm -f *esp
	rm -f *eps
