#****************************
#*** Copyright Notice ***
#IMPACT-Z,Copyright (c) 2016, The Regents of the University of California, through
#Lawrence Berkeley National Laboratory (subject to receipt of any required approvals
#from the U.S. Dept. of Energy).  All rights reserved.
#If you have questions about your rights to use or distribute this software,
#please contact Berkeley Lab's Innovation & Partnerships Office at  IPO@lbl.gov.
#NOTICE.  This Software was developed under funding from the U.S. Department of Energy
#and the U.S. Government consequently retains certain rights. As such, the U.S. Government
#has been granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable,
#worldwide license in the Software to reproduce, distribute copies to the public, prepare
#derivative works, and perform publicly and display publicly, and to permit other to do so.
#****************************

#*****************************************************
#  General Makefile
#
#*****************************************************

# The path to this Makefile
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

#**************************************************************************
# Macros defining the Fortran, C/C++ compiler and linker.

# Uncomment for MPI build
USE_MPI ?=
FC ?= ifort
LINK ?= ifort
FFLAGS += -O3 -cpp

ifdef USE_MPI
	# Flag for the preprocessor to avoid using mpistub on Fortran files
	FFLAGS += -DUSE_MPI
else
	# Flag for compiler to use the mpif.h generated by .PrepareMPI rule
	FFLAGS += -I$(ROOT_DIR)
endif

#**************************************************************************
# List of .o files that EXENAME depends on.  Edit as appropriate for MP.

OBJS = \
	mpistub.o DataStruct/NumConst.o DataStruct/PhysConst.o DataStruct/Pgrid.o \
	DataStruct/Data.o \
	Func/Timer.o Func/Transpose.o Func/Fldmger.o Func/Ptclmger.o  Func/FFT.o \
        Appl/BPM.o Appl/CCL.o Appl/CCDTL.o Appl/DTL.o Appl/SC.o Appl/Wiggler.o \
	Appl/DriftTube.o Appl/Quadrupole.o Appl/ConstFoc.o Appl/SolRF.o \
	Appl/Sol.o Appl/Dipole.o Appl/Multipole.o Appl/EMfld.o Appl/TWS.o Appl/BeamLineElem.o \
	Appl/CompDom.o Appl/Field.o Appl/BeamBunch.o  Appl/Distribution.o \
	Contrl/Input.o Contrl/Output.o Contrl/AccSimulator.o Contrl/main.o

OBJS2 = \
	mpistub.o NumConst.o PhysConst.o Pgrid.o Data.o \
        Timer.o Transpose.o Fldmger.o Ptclmger.o FFT.o \
	BPM.o CCL.o CCDTL.o DTL.o SC.o DriftTube.o Quadrupole.o Wiggler.o ConstFoc.o \
	SolRF.o Sol.o Dipole.o Multipole.o EMfld.o TWS.o BeamLineElem.o CompDom.o Field.o BeamBunch.o Distribution.o \
	Input.o Output.o AccSimulator.o main.o	


#**************************************************************************
# Change this line if you don't like 'a.out'.

ifdef USE_MPI
EXENAME = ImpactZexe-mpi
else
EXENAME = ImpactZexe
endif

#************************************************************************
# disable predefined suffixes and define your own set of allowed suffixes
 .SUFFIXES:
 .SUFFIXES: .o .f .F .c .f90 .F90

#*************************************************************************
# inference rules (how to compile object files that have no explicit rules)
#  $* = name part of target
#  $@ = full target name
#  $< = dependent name

.f90.o:
	$(FC) -c $(FFLAGS) $<


ifdef USE_MPI
.PrepareMPI:
	- rm -f mpif.h
else
.PrepareMPI:
	- cp mpif_single.h mpif.h
endif
#**************************************************************************
# Rules for building EXENAME from OBJS and OBJS from your source.

all: .PrepareMPI $(EXENAME)

.DEFAULT_GOAL := all

$(EXENAME):  $(OBJS)
	$(LINK) -o $(EXENAME) $(OBJS2)
#************************************************************************
# if you wish to compile a certain object with different flags
# or in some special way, then specify the target & dependency explicitly
# the following line say Timer.o is depended on Timer.f90
#Timer.o: Timer.f90
#	$(CC) -c -O3 Timer.f90

	cp  AccSimulator.o main.o Input.o Output.o Contrl
	cp  BPM.o CCL.o CCDTL.o DTL.o SC.o DriftTube.o Quadrupole.o Wiggler.o \
	    ConstFoc.o BeamLineElem.o BeamBunch.o Field.o CompDom.o \
	    Multipole.o Distribution.o SolRF.o Sol.o Dipole.o TWS.o EMfld.o Appl
	cp  Timer.o Transpose.o Fldmger.o Ptclmger.o FFT.o Func
	cp  NumConst.o PhysConst.o Data.o Pgrid.o DataStruct 
#***********************************************************************
clean:
	-rm *.o $(EXENAME) *.mod */*.o mpif.h
