###############################################################################
#
# FILE NAME: Make.def
# VERSION: 1.2
# AUTHOR:       Nergal S.r.l.
# DATE: 2007-03-14
#
# DESCRIPTION:  base Makefile  
#
# REVISION HISTORY:
#
# Version     Date      Author         Change Description
#
###############################################################################

#------------------------------------------------------------------------------
# default settings
#------------------------------------------------------------------------------

ifndef BASEDIR
        BASEDIR =$(shell pwd)
endif

ifdef release
      debug = 0
      release = 1
endif

ifndef release
     debug = 1
     release = 0
endif
 

CC              = gcc
CXX             = g++
AR              = ar
AS              = as
CXXLD           = g++
OC              = cp
ROOT_DIR        = $(ROOTSYS)
ROOT_LIB        = $(ROOTSYS)/lib
ROOTCINT 	= $(ROOTSYS)/bin/rootcint

SHELL    = /bin/bash
MAKE     = /usr/bin/make BASEDIR=$(BASEDIR) 

.PHONY : clean help var

#------------------------------------------------------------------------------
# default compiler, assembler & linker FLAGS
#------------------------------------------------------------------------------


CFLAGS         += -Wall -shared
LDFLAGS        +=  

#------------------------------------------------------------------------------
# optimization FLAG
#------------------------------------------------------------------------------

OPT_FLAG        = -O4

#------------------------------------------------------------------------------
# object's directories, FLAGS & libs extensions for release & debug version 
#------------------------------------------------------------------------------

OBJDIR_D         = obj/debug
OBJDIR_R         = obj/release

ifeq ("1", "$(debug)")
     CFLAGS         += -g $(OPT_FLAG) -DDEBUG 
     OBJDIR         = obj/debug
endif

ifeq ("1", "$(release)")
     CFLAGS         += $(OPT_FLAG)
     OBJDIR         = obj/release
endif

#------------------------------------------------------------------------------
# Objects & Dependencies
#------------------------------------------------------------------------------

OBJS   = \
	${CSRCS:%.c=$(OBJDIR)/%.o}  \
	${CXXSRCS:%.cpp=$(OBJDIR)/%.o}  \

DEPEND = \
	${CSRCS:%.c=$(OBJDIR)/.%.d}  \
	${CXXSRCS:%.cpp=$(OBJDIR)/.%.d}  \


$(OBJDIR)/.%.d: %.c
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	if [ ! -d $(OBJDIR)/$$(/usr/bin/dirname $<) ]; then mkdir -p $(OBJDIR)/$$(/usr/bin/dirname $<)  $(OBJDIR)/.$$(/usr/bin/dirname $<) ;fi; \
#	echo "generating depend infos for $< into $@"; \
	@$(CC) -M $(CFLAGS) $< 2>/dev/null\
	        | sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	if [ ! -d $(OBJDIR)/$$(/usr/bin/dirname $<) ]; then mkdir -p $(OBJDIR)/$$(/usr/bin/dirname $<)  $(OBJDIR)/.$$(/usr/bin/dirname $<) ;fi; \
	echo "generating depend infos for $< into $@"; \
	$(CC) -M $(CFLAGS) $< 2>/dev/null\
	        | sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
endif

$(OBJDIR)/.%.d: %.cpp
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	if [ ! -d $(OBJDIR)/$$(/usr/bin/dirname $<) ]; then mkdir -p $(OBJDIR)/$$(/usr/bin/dirname $<)  $(OBJDIR)/.$$(/usr/bin/dirname $<) ;fi; \
#	echo "generating depend infos for $< into $@"; \
	@$(CXX) -M $(CFLAGS) $< 2>/dev/null\
		| sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	if [ ! -d $(OBJDIR)/$$(/usr/bin/dirname $<) ]; then mkdir -p $(OBJDIR)/$$(/usr/bin/dirname $<)  $(OBJDIR)/.$$(/usr/bin/dirname $<) ;fi; \
	echo "generating depend infos for $< into $@"; \
	$(CXX) -M $(CFLAGS) $< 2>/dev/null\
		| sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
endif

$(OBJDIR)/%.o: %.c
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(CC) -c $(CFLAGS) -o $@ $<
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(CC) -c $(CFLAGS) -o $@ $<
endif

$(OBJDIR)/%.o: %.cpp
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(CXX) -c $(CFLAGS) -o $@ $<
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(CXX) -c $(CFLAGS) -o $@ $<
endif

#------------------------------------------------------------------------------
# General directory structure, includes, objects
#------------------------------------------------------------------------------

EVENT_DIR		= $(BASEDIR)/event

LOG_DIR			= $(EVENT_DIR)/log
MCMD_DIR		= $(EVENT_DIR)/mcmd
TMTC_DIR		= $(EVENT_DIR)/tmtc
PHYS_DIR		= $(EVENT_DIR)/physics
ACTR_DIR		= $(PHYS_DIR)/anticounter
CALO_DIR		= $(PHYS_DIR)/calorimeter
TRAK_DIR		= $(PHYS_DIR)/tracker
NDTC_DIR		= $(PHYS_DIR)/neutronDetector
S4_DIR			= $(PHYS_DIR)/S4
TOF_DIR			= $(PHYS_DIR)/tof
TRGR_DIR		= $(PHYS_DIR)/trigger
ARRD_DIR		= $(EVENT_DIR)/arrDump
TABD_DIR		= $(EVENT_DIR)/tabDump
VARD_DIR		= $(EVENT_DIR)/varDump
TSBT_DIR		= $(EVENT_DIR)/tsbt
TSBB_DIR		= $(EVENT_DIR)/tsbb
ENDR_DIR		= $(EVENT_DIR)/endrun
EVNT_DIR		= $(EVENT_DIR)
PHYSEXTDIR		= $(ACTR_DIR) $(CALO_DIR) $(TRAK_DIR) $(NDTC_DIR) $(S4_DIR) $(TOF_DIR) $(TRGR_DIR) 
EXTDIR			= $(LOG_DIR) $(MCMD_DIR) $(TMTC_DIR) $(PHYSEXTDIR) $(ARRD_DIR) $(TABD_DIR) $(VARD_DIR) $(TSBT_DIR) $(TSBB_DIR) $(ENDR_DIR)

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

ROOT_DIR_INC             = $(ROOT_DIR)/include

LOG_DIR_INC              = $(LOG_DIR)/include
MCMD_DIR_INC             = $(MCMD_DIR)/include
TMTC_DIR_INC             = $(TMTC_DIR)/include

ACTR_DIR_INC             = $(ACTR_DIR)/include
CALO_DIR_INC             = $(CALO_DIR)/include
TRAK_DIR_INC             = $(TRAK_DIR)/include
NDTC_DIR_INC             = $(NDTC_DIR)/include
S4_DIR_INC	         = $(S4_DIR)/include
TOF_DIR_INC	         = $(TOF_DIR)/include
TRGR_DIR_INC	         = $(TRGR_DIR)/include
ARRD_DIR_INC	         = $(ARRD_DIR)/include
TABD_DIR_INC	         = $(TABD_DIR)/include
VARD_DIR_INC	         = $(VARD_DIR)/include
TSBT_DIR_INC	         = $(TSBT_DIR)/include
TSBB_DIR_INC	         = $(TSBB_DIR)/include
ENDR_DIR_INC	         = $(ENDR_DIR)/include
EVNT_DIR_INC	         = $(EVNT_DIR)/include

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

LOG_OBJ            =  $(LOG_DIR)/$(OBJDIR)
MCMD_OBJ           =  $(MCMD_DIR)/$(OBJDIR)
TMTC_OBJ           =  $(TMTC_DIR)/$(OBJDIR)
ACTR_OBJ           =  $(ACTR_DIR)/$(OBJDIR)
CALO_OBJ           =  $(CALO_DIR)/$(OBJDIR)
TRAK_OBJ           =  $(TRAK_DIR)/$(OBJDIR)
NDTC_OBJ           =  $(NDTC_DIR)/$(OBJDIR)
S4_OBJ             =  $(S4_DIR)/$(OBJDIR)
TOF_OBJ            =  $(TOF_DIR)/$(OBJDIR)
TRGR_OBJ           =  $(TRGR_DIR)/$(OBJDIR)
ARRD_OBJ           =  $(ARRD_DIR)/$(OBJDIR)
TABD_OBJ           =  $(TABD_DIR)/$(OBJDIR)
VARD_OBJ           =  $(VARD_DIR)/$(OBJDIR)
TSBT_OBJ           =  $(TSBT_DIR)/$(OBJDIR)
TSBB_OBJ           =  $(TSBB_DIR)/$(OBJDIR)
ENDR_OBJ           =  $(ENDR_DIR)/$(OBJDIR)
EVNT_OBJ           =  $(EVNT_DIR)/$(OBJDIR)

#------------------------------------------------------------------------------
# Built libraries 
#------------------------------------------------------------------------------

LOG_A        = $(LOG_OBJ)/liblogevent.a
MCMD_A       = $(MCMD_OBJ)/libmcmdevent.a
TMTC_A       = $(TMTC_OBJ)/libtmtcevent.a
ACTR_A       = $(ACTR_OBJ)/libanticounterevent.a
CALO_A       = $(CALO_OBJ)/libcalorimeter.a
TRAK_A       = $(TRAK_OBJ)/libtrackerevent.a
NDTC_A       = $(NDTC_OBJ)/libneutronevent.a
S4_A         = $(S4_OBJ)/libs4event.a
TOF_A        = $(TOF_OBJ)/libtofevent.a
TRGR_A       = $(TRGR_OBJ)/libtriggerevent.a
ARRD_A       = $(ARRD_OBJ)/libarrdumpevent.a
TABD_A       = $(TABD_OBJ)/libtabdumpevent.a
VARD_A       = $(VARD_OBJ)/libvardumpevent.a
TSBT_A       = $(TSBT_OBJ)/libtsbtevent.a
TSBB_A       = $(TSBB_OBJ)/libtsbbevent.a
ENDR_A       = $(ENDR_OBJ)/libendrunevent.a
EVNT_A       = $(EVNT_OBJ)/libevntevent.a
EXTLIB	     = $(LOG_A) $(MCMD_A) $(TMTC_A) $(ACTR_A) $(CALO_A) $(TRAK_A) $(NDTC_A) $(S4_A) $(TOF_A) $(TRGR_A) $(ARRD_A) $(TABD_A) $(VARD_A) $(TSBT_A) $(TSBB_A) $(ENDR_A)
