#*****************************************************************************
#
# FILE NAME:    $Id: Makefile,v 1.8 2009/07/29 16:13:13 mocchiut Exp $    
# VERSION:      
# AUTHOR:       Nergal S.r.l.
# DATE:
#
# DESCRIPTION:  base Makefile
#
# REVISION HISTORY:
#
# Version     Date      Author         Change Description
#
#*****************************************************************************/


#------------------------------------------------------------------------------
ifneq ($(MAKECMDGOALS),distclean)
include $(BASEDIR)/PamOffLineSW/Make.def
endif
ifeq ($(GCC4),1)
export LG2C=-lgfortran
else	
export	LG2C=-lg2c
endif
#------------------------------------------------------------------------------


#CFLAGS += -I $(PAMOFF_DIR) -I $(TECH_DIR_INC) -I $(ROOT_DIR_INC) -I $(BASEDIR) -I $(EVENT_DIR) -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  -fPIC -DPIC 
CFLAGS += -I $(BASEDIR) -I $(EVENT_DIR) -I $(TECH_DIR) -I $(PAMOFF_DIR) -I $(ROOT_DIR_INC) -fPIC -DPIC 

CXXSRCS =	\
	PamOffLineSW_Main.cpp 	\
	StateManager.cpp 	\
	R0_Init_State.cpp 	\
	R1_FA_State.cpp 	\
	R2_FAFE_State.cpp 	\
	R3_FAFEDE_State.cpp 	\
	R4_HEADER_State.cpp 	\
	CRC16.cpp 		\
	PacketSemanticAnalyzer.cpp \
	PacketUser.cpp 		\
	Logger.cpp


#------------------------------------------------------------------------------

TARGETLIB = $(PAMOFF_A)

#------------------------------------------------------------------------------
EXECUTABLES=chewbacca
#EXECUTABLES=PamOffLineSW

all: $(EXECUTABLES)

$(EXECUTABLES): $(TARGETLIB) $(EXTLIB)
ifeq ($(verbose),)
	@$(CXX) $(PAM_BIT) -o  $@    -L ../event -lyoda ${LG2C} `root-config --libs` -lRMySQL $^
else
	$(CXX) $(PAM_BIT) -o  $@    -L ../event -lyoda ${LG2C} `root-config --libs` -lRMySQL $^
endif

$(EXTLIB): $(EXTDIR)
ifeq ($(verbose),)
	@for sub_dir in $(EXTDIR) ; do \
		(${EGREEN} "Make all in $$sub_dir "; ${RESET});\
		$(MAKE) -s -C $$sub_dir libs ; \
	done
else
	for sub_dir in $(EXTDIR) ; do \
		$(MAKE) -C $$sub_dir libs ; \
	done
endif

libs: $(TARGETLIB)

$(TARGETLIB) : ${OBJS}
ifeq ($(verbose),)
	@$(AR) -scrv $@ ${OBJS} &>/dev/null
else
	$(AR) -scrv $@ ${OBJS}
endif

SUBPACKET_HEADER_FILES = \

INCLUDES = -I. -I$(ROOT_DIR)/include 
DICT_HEADER_FILES = \
	PamelaRun.h		yodaversion.h		RunInfoYoda.h	\
	SubPacket.h		PacketType.h		SubDetector.h	\
	DirectoryStructure.h	Algorithm.h		AlgorithmInfo.h	\
	EventHeader.h		PscuHeader.h		EventCounter.h  \
	endrun/PhysEndRunEvent.h 					\
	endrun/CaloEndRun.h						\
	endrun/TBEndRun.h 						\
	log/LogEvent.h		log/LogRecord.h	\
	varDump/VarDumpEvent.h varDump/VarDumpRecord.h	\
	arrDump/ArrDumpEvent.h arrDump/ArrDumpRecord.h	\
	tabDump/TabDumpEvent.h tabDump/TabDumpRecord.h	\
	tmtc/TmtcEvent.h 	tmtc/TmtcRecord.h	\
	mcmd/McmdEvent.h	mcmd/McmdRecord.h	\
	physics/neutronDetector/NeutronEvent.h		\
	physics/neutronDetector/NeutronRecord.h		\
	physics/anticounter/AnticounterEvent.h		\
	physics/calorimeter/CalorimeterEvent.h		\
	physics/S4/S4Event.h				\
	physics/tof/TofEvent.h				\
	physics/tracker/TrackerEvent.h			\
	physics/trigger/TriggerEvent.h 			\
	tsbt/TsbTEvent.h	tsbt/TsbTRecord.h	\
	tsbb/TsbBEvent.h	tsbb/TsbBRecord.h	\
	PscuEvent.h		\
	CalibCalPulse1Event.h	\
	CalibCalPulse2Event.h	\
	CalibTrkBothEvent.h	\
	CalibTrk1Event.h	\
	CalibTrk2Event.h	\
	CalibTofEvent.h		\
	CalibS4Event.h		\
	CalibCalPedEvent.h	\
	Calib1_Ac1Event.h	\
	Calib2_Ac1Event.h	\
	Calib1_Ac2Event.h   	\
	Calib2_Ac2Event.h   	\
	CalibCalEvent.h   	\
	RunHeaderEvent.h	\
	RunTrailerEvent.h	\
	CalibHeaderEvent.h	\
	CalibTrailerEvent.h	\
	InitHeaderEvent.h	\
	InitTrailerEvent.h	\
	EventTrkEvent.h		\
	ForcedFECmdEvent.h	\
	Ac1InitEvent.h		\
	CalInitEvent.h		\
	TrkInitEvent.h		\
	TofInitEvent.h		\
	TrgInitEvent.h		\
	NdInitEvent.h		\
	S4InitEvent.h		\
	Ac2InitEvent.h		\
	CalAlarmEvent.h		\
	Ac1AlarmEvent.h		\
	TrkAlarmEvent.h 	\
	TrgAlarmEvent.h		\
	TofAlarmEvent.h		\
	S4AlarmEvent.h		\
	Ac2AlarmEvent.h		\
	CRC.h			\
	Exception.h		\
	LinkDef.h

$(ROOTEVENTDICT).cpp: ${DICT_HEADER_FILES} 
ifeq ($(verbose),)
	@echo	"Calling ROOTCINT" ;\
	LD_LIBRARY_PATH=$(ROOT_LIB):$(LD_LIBRARY_PATH); $(ROOTCINT) -f $@ -c ${INCLUDES} $^
else
	echo	$(ROOTCINT) -f $@ -c ${INCLUDES} $^ ;\
	LD_LIBRARY_PATH=$(ROOT_LIB):$(LD_LIBRARY_PATH); $(ROOTCINT) -f $@ -c ${INCLUDES} $^
endif

clean: clean-subdir
ifeq ($(verbose),)
	@rm -f $(TARGETLIB) ${OBJS} $(OBJDIR)/.*.d $(EXECUTABLES)
else
	rm -f $(TARGETLIB) ${OBJS} $(OBJDIR)/.*.d $(EXECUTABLES)
endif

clean-subdir: $(EXTDIR)
ifeq ($(verbose),)
	@for sub_dir in $(EXTDIR) ; do \
		$(MAKE) -s -C $$sub_dir clean ; \
	done
else
	for sub_dir in $(EXTDIR) ; do \
		$(MAKE) -C $$sub_dir clean ; \
	done
endif

distclean: distclean-sub distclean-subdir clean
ifeq ($(verbose),)
	@rm -f $(ROOTEVENTDICT).{cpp,h}
	@$(MAKE) -s -C techmodel/ clean 
else
	rm -f $(ROOTEVENTDICT).{cpp,h}
endif

distclean-subdir:
ifeq ($(verbose),)
	@for sub_dir in $(EXTDIR) ; do \
		$(MAKE) -s -C $$sub_dir distclean-sub ; \
	done
else
	for sub_dir in $(EXTDIR) ; do \
		$(MAKE) -C $$sub_dir distclean-sub ; \
	done
endif

#-include $(DEPEND)


#------------------------------------------------------------------------------
include $(BASEDIR)/PamOffLineSW/Make.def.post
#------------------------------------------------------------------------------
