#
#   ************ PamCut makefile ************
#    
#     Author: Nicola Mori
#   
#    		This makefile will produce a cut library libPamCut.so. In the "Build options"
#       section the various compilation options can be set; they will propagate to the
#       whole project.
#    


# ------------------------------- Build options ----------------------------#
#
# Normally you will not have the necessity to modify what lies below, if you
# correctly specified the names for the submakefiles in include.mk. Modify
# the options below to set optimization flags, compiler name and so on. 
#

# C++ compiler and linker
C++ = g++

# Optimization flags.
# 1) Use -O, -O1, -O2, -O3 to optimize for speed or -g, -g1, -g2, -g3
# for debugging (slower execution).
# 2) Uncomment -DDEBUGPAMCUT to enable debug sections in the .cpp code (these sections must be included between
# "#ifdef DEBUGPAMCUT" and "#endif"). Note that if you
# use debug instructions also in the .h code (headers) you will also have to add -DDEBUGPAMCUT
# to the compiler options for the main analysis program.
# 3) Since this is a general compiler flag it can also be used, eg., to set the -m32 flag
OPTIMIZE = -O3 ${PAM_BIT} #-DDEBUGPAMCUT

# Linker flags
# The flags defined here will be directly inserted into the linker invocation. Place here the
# -m32 flag, for example.
LINKERFLAGS = ${PAM_BIT}

# Library flags
# Pamela software is modular, so some libraries may not be available in some istallations.
# To avoid linking problems, here you can define flags to exclude those parts of the
# PamCut software which make use of unavailable libraries. Remember to place
# the code to be excluded between proper preprocessor directives. See the documentation or the cut
# TofNucleiZCut for an example.
# Since PamCut headers may refer to the optional libraries, it's important to define the same 
# flags when compiling the analysis code. Otherwise, analysis code will include PamCut headers which,
# if no exclusion flag is set, will try to include optional software headers.
# Conversely, if you use optional libraries remember to tell it to the linker.
# 
# Excluded cuts and actions:
# NO_CALONUCLEI: CaloNucleiZCut
# NO_TRKNUCLEI: TrkNucleiZCut
# NO_TOFNUCLEI: TofNucleiZCut
# NO_CALOPRESAMPLER: ReprocessCaloAction
# NO_CALOAXIS: CaloAngleCut, CaloCrossCut, CaloGeomCut, CaloGeomYSensCut, CaloIonCut, CaloNHitCut,
#              CaloNotIntCut (reduced functionality), CaloTrackChi2Cut, CaloTrackCut, 
#              TrackRedGeomCut (reduced functionality), CaloChi2HistoAction, CaloAxisChi2VsRigHistoAction
EXCLUSIONFLAGS = -DNO_CALONUCLEI -DNO_TRKNUCLEI -DNO_TOFNUCLEI -DNO_CALOPRESAMPLER -DNO_CALOAXIS

# Put below the files on which every .cpp  must depend on, ie., those files that,
# if modified, will trigger a complete recompilation of the project. 
COMMONDEPS = makefile

# The file include.mk contains the inclusion of all the sub-makefiles (subdir.mk)
# in the project. Includes for new cuts must be added there and not below... 
-include include.mk

#-------------------------------- Make body --------------------------------#
#
# Below the make commands are defined. There are no options to set in this section,
# so it has to be modified only in case of radical changes to the make procedure.

# Remove command for clean
RM := rm -rf

# Additional dependencies from headers of other software (PAMELA, ROOT)
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
endif

# All Target
all: libPamCut.so
	@echo
	@echo "**** Compiler version and compilation host ****"
	@$(C++) --version | grep GCC
	@hostname
	@echo "**** Compilation flags ****"
	@echo OPTIMIZE: $(OPTIMIZE)
	@echo LINKERFLAGS: $(LINKERFLAGS)
	@echo EXCLUSIONFLAGS: $(EXCLUSIONFLAGS)
	@echo

# Tool invocations
libPamCut.so: $(OBJS) $(USER_OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: GCC C++ Linker'
	$(C++) -shared $(LINKERFLAGS) -o"libPamCut.so" $(OBJS)
	@echo 'Finished building target: $@'
	@echo ' '

# Other Targets
clean:
	-$(RM) $(CPP_DEPS) $(OBJS) libPamCut.so
	-@echo ' '

distclean:
	@rm -f ${PAM_LIB}/libPamCut.so
	@rm -rf ${PAM_INC}/PamCut
	@echo Installed files successfully removed. 

install:
	@if [[ "${PAM_INC}" == ""  || "${PAM_LIB}" == "" ]]; then \
	    echo Pamela environment not set. ; \
	else \
	  rootdir=`pwd`; \
	  for file in `find -name "*.h"`; do \
	  	dir=`dirname $${file} | sed 's/.\///'`; \
	  	mkdir -p ${PAM_INC}/PamCut/$${dir}; \
	    cp -u $${file} ${PAM_INC}/PamCut/$${dir}; \
	  done; \
	  cp -u libPamCut.so ${PAM_LIB}; \
	fi
	@echo PamCut installed.

.PHONY: all clean distclean install

