###############################################################################
#
# 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++
ifeq ($(GCC4),1)
F77             = gfortran
else
F77             = g77
endif
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
CFLAGS         += -Wall -shared
FFLAGS         += -O0
LDFLAGS        +=  

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

OPT_FLAG        = -O3

#------------------------------------------------------------------------------
# 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   = \
	${FORSRCS:%.for=$(OBJDIR)/%.o}  \
	${FSRCS:%.f=$(OBJDIR)/%.o}  \
	${CSRCS:%.c=$(OBJDIR)/%.o}  \
	${CXXSRCS:%.cpp=$(OBJDIR)/%.o} 

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


$(OBJDIR)/.%.d: %.f
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 $@"; \
	$(F77) -M $(FFLAGS) $< 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 $@"; \
	$(F77) -M $(FFLAGS) $< 2>/dev/null\
	        | sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
endif

$(OBJDIR)/.%.d: %.for
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 $@"; \
	$(F77) -M $(FFLAGS) $< 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 $@"; \
	$(F77) -M $(FFLAGS) $< 2>/dev/null\
	        | sed -e 's%$*.o%$(OBJDIR)/$*.o%g' > $(OBJDIR)/.$*.d
endif

$(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: %.for
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(F77) -c $(FFLAGS) -o $@ $<
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(F77) -c $(FFLAGS) -o $@ $<
endif

$(OBJDIR)/%.o: %.f
ifeq ($(verbose),)
	@if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(F77) -c $(FFLAGS) -o $@ $<
else
	if [ ! -d $(OBJDIR) ]; then mkdir -p $(OBJDIR); fi; \
	$(F77) -c $(FFLAGS) -o $@ $<
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
#------------------------------------------------------------------------------

PAMOFF_DIR		= $(BASEDIR)/PamOffLineSW
EVENT_DIR		= $(BASEDIR)/event

FORROU_DIR		= $(PAMOFF_DIR)/forroutines
TRAK_DIR		= $(FORROU_DIR)/tracker
RRAW_DIR		= $(TRAK_DIR)/readraw
ACTR_DIR		= $(FORROU_DIR)/anticounter
CALO_DIR		= $(FORROU_DIR)/calorimeter
TOF_DIR 		= $(FORROU_DIR)/tof
TRGR_DIR 		= $(FORROU_DIR)/trigger

FORROUEXTDIR			= $(TRAK_DIR) $(RRAW_DIR) $(ACTR_DIR) $(CALO_DIR) $(TOF_DIR) $(TRGR_DIR)

PHYS_DIR 		= $(PAMOFF_DIR)/physics
TECH_DIR 		= $(PAMOFF_DIR)/techmodel

EXTDIR			= $(PHYS_DIR) $(TECH_DIR) $(FORROU_DIR)

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

ROOT_DIR_INC             = $(ROOT_DIR)/include

FORROU_DIR_INC		= $(FORROU_DIR)/include
RRAW_DIR_INC		= $(RRAW_DIR)/include
TRAK_DIR_INC		= $(TRAK_DIR)/include
ACTR_DIR_INC		= $(ACTR_DIR)/include
CALO_DIR_INC		= $(CALO_DIR)/include
TOF_DIR_INC		= $(TOF_DIR)/include
TRGR_DIR_INC		= $(TRGR_DIR)/include

PHYS_DIR_INC		= $(PHYS_DIR)/include
TECH_DIR_INC		= $(TECH_DIR)/include

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

FORROU_OBJ         =  $(FORROU_DIR)/$(OBJDIR)
RRAW_OBJ           =  $(RRAW_DIR)/$(OBJDIR)
TRAK_OBJ           =  $(TRAK_DIR)/$(OBJDIR)
ACTR_OBJ           =  $(ACTR_DIR)/$(OBJDIR)
CALO_OBJ           =  $(CALO_DIR)/$(OBJDIR)
TOF_OBJ            =  $(TOF_DIR)/$(OBJDIR)
TRGR_OBJ           =  $(TRGR_DIR)/$(OBJDIR)

PHYS_OBJ           =  $(PHYS_DIR)/$(OBJDIR)
TECH_OBJ           =  $(TECH_DIR)/$(OBJDIR)
PAMOFF_OBJ         =  $(PAMOFF_DIR)/$(OBJDIR)


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

FORROU_A     = $(FORROU_OBJ)/libforrou.a
FORROUINT_A  = $(FORROU_OBJ)/libforrouinternal.a
RRAW_A       = $(RRAW_OBJ)/libforreadraw.a
TRAK_A       = $(RRAW_OBJ)/libfortracker.a
ACTR_A       = $(ACTR_OBJ)/libanticounter.a
CALO_A       = $(CALO_OBJ)/libforcalor.a
TOF_A        = $(TOF_OBJ)/libfortof.a
TRGR_A       = $(TRGR_OBJ)/libfortrigger.a

FORROUEXTLIB	     = $(RRAW_A) $(TRAK_A) $(ACTR_A) $(CALO_A) $(TOF_A) $(TRGR_A)

PHYS_A       = $(PHYS_OBJ)/libdevreaders.a
TECH_A       = $(TECH_OBJ)/libtechmodel.a
PAMOFF_A     = $(PAMOFF_OBJ)/libpamoff.a

EXTLIB	     = $(TECH_A) $(PHYS_A) $(FORROU_A)
