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


#------------------------------------------------------------------------------
ifneq ($(MAKECMDGOALS),distclean)
include $(BASEDIR)/event/Make.def
endif
#------------------------------------------------------------------------------

CFLAGS += -I $(EVNT_DIR_INC) -I $(ROOT_DIR_INC) -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  -fPIC -DPIC 

ROOTEVENTDICT =  EventDict

CXXSRCS =  	NullAlgorithm.cpp          \
		yodaversion.cpp            \
		PamelaRun.cpp              \
		RunInfoYoda.cpp                \
		$(ROOTEVENTDICT).cpp       \
		SubPacket.cpp              \
		PacketType.cpp             \
		SubDetector.cpp            \
		Algorithm.cpp              \
		AlgorithmInfo.cpp          \
		EventHeader.cpp            \
		PscuHeader.cpp             \
		EventCounter.cpp           \
		PscuEvent.cpp              \
		CalibCalPulse1Event.cpp    \
		CalibCalPulse2Event.cpp    \
		CalibTrkBothEvent.cpp      \
		CalibTrk1Event.cpp         \
		CalibTrk2Event.cpp         \
		CalibTofEvent.cpp          \
		CalibS4Event.cpp           \
		CalibCalPedEvent.cpp       \
		Calib1_Ac1Event.cpp        \
		Calib1_Ac2Event.cpp        \
		Calib2_Ac1Event.cpp        \
		Calib2_Ac2Event.cpp        \
		CalibCalEvent.cpp          \
		RunHeaderEvent.cpp         \
		RunTrailerEvent.cpp        \
		CalibHeaderEvent.cpp       \
		CalibTrailerEvent.cpp      \
		InitHeaderEvent.cpp        \
		InitTrailerEvent.cpp       \
		EventTrkEvent.cpp          \
		ForcedFECmdEvent.cpp       \
		Ac1InitEvent.cpp           \
		CalInitEvent.cpp           \
		TrkInitEvent.cpp           \
		TofInitEvent.cpp           \
		TrgInitEvent.cpp           \
		NdInitEvent.cpp            \
		S4InitEvent.cpp            \
		Ac2InitEvent.cpp           \
		CalAlarmEvent.cpp          \
		Ac1AlarmEvent.cpp          \
		TrkAlarmEvent.cpp          \
		TrgAlarmEvent.cpp          \
		TofAlarmEvent.cpp          \
		S4AlarmEvent.cpp           \
		Ac2AlarmEvent.cpp          \

CSRCS=		CRC.c			   \
		DirectoryStructure.c       


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

TARGETLIB = $(EVNT_A)

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


all: dynamic 
#static


dynamic: libyoda.so.0.0.0
ifeq ($(verbose),)
	@ln -sf libyoda.so.0.0.0 libyoda.so.0 ;\
	ln -sf libyoda.so.0.0.0 libyoda.so
else
	ln -sf libyoda.so.0.0.0 libyoda.so.0 ;\
	ln -sf libyoda.so.0.0.0 libyoda.so
endif

static: libyoda.a

libyoda.so.0.0.0: $(TARGETLIB) $(EXTLIB)
ifeq ($(verbose),)
	@$(CXX) \
	$(PAM_BIT) -o $@ \
	-shared \
        -Wl,-soname \
	-Wl,libyoda.so.0 \
	-Wl,--whole-archive \
	$^ \
	-Wl,--no-whole-archive
else
	$(CXX) \
	$(PAM_BIT) -o $@ \
	-shared \
        -Wl,-soname \
	-Wl,libyoda.so.0 \
	-Wl,--whole-archive \
	$^ \
	-Wl,--no-whole-archive
endif

libyoda.a: $(TARGETLIB) $(EXTLIB)
ifeq ($(verbose),)
	@cd $(OBJDIR) &>/dev/null ; \
	for sub_lib in $(TARGETLIB) $(EXTLIB) ; do \
		$(AR) -x $$sub_lib ; \
	done ;\
	$(AR) -scrv libyoda.a *.o  &>/dev/null
else
	cd $(OBJDIR) ; \
	pwd ;\
	for sub_lib in $(TARGETLIB) $(EXTLIB) ; do \
		$(AR) -x $$sub_lib ; \
	done ;\
	$(AR) -scrv libyoda.a *.o 
endif

$(TARGETLIB) : ${OBJS}
ifeq ($(verbose),)
	@$(AR) -scrv $@ $? &>/dev/null
else
	$(AR) -scrv $@ $?
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

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),)
	@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 libyoda*
else
	rm -f $(TARGETLIB) ${OBJS} $(OBJDIR)/.*.d libyoda*
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}
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)/event/Make.def.post
#------------------------------------------------------------------------------
