#
# Make.include for GENIE Object-Oriented Neutrino Generator
#
# Costas Andreopoulos (Rutherford Lab.) <costas.andreopoulos \at stfc.ac.uk>
#

#-------------------------------------------------------------------
#                 Read in configure script output
#-------------------------------------------------------------------
# Include file generated by the configure script
# Type './configure --help' for more information

include $(GENIE)/src/make/Make.config

ifndef GOPT_WITH_CXX_USERDEF_FLAGS
 GOPT_WITH_CXX_USERDEF_FLAGS=
endif

#-------------------------------------------------------------------
#                           ARCHITECTURE 
#-------------------------------------------------------------------
# As defined by ROOT
ARCH    = $(shell root-config --arch)  
ARCH_OK = 

#-------------------------------------------------------------------
#                              GENIE
#-------------------------------------------------------------------

# GENIE release info
#
GVERSION  = $(shell awk '{print $0}' $(GENIE)/VERSION)

# GENIE paths
#
GENIE_LIB_PATH    := $(GENIE)/lib
GENIE_BIN_PATH    := $(GENIE)/bin
GENIE_SRC_PATH    := $(GENIE)/src
GENIE_CONFIG_PATH := $(GENIE)/config

# GENIE installation paths used with 'make install'
# The $GENIE_INSTALLATION_PATH is determined from ./configure --prefix=...
#
GENIE_LIB_INSTALLATION_PATH     := $(GENIE_INSTALLATION_PATH)/lib
GENIE_BIN_INSTALLATION_PATH     := $(GENIE_INSTALLATION_PATH)/bin
GENIE_INCBASE_INSTALLATION_PATH := $(GENIE_INSTALLATION_PATH)/include
GENIE_INC_INSTALLATION_PATH     := $(GENIE_INCBASE_INSTALLATION_PATH)/GENIE

# GENIE & NuValidator header files base dir
#
GENIE_INCLUDES := -I$(GENIE)/src/

# The following will be appended in the library name 
#
GLIBVERSION =
ifeq ($(strip $(GOPT_ENABLE_DYLIBVERSION)),YES) 
  GLIBVERSION = -$(GVERSION)
endif

# Look for traces of a previous installation at the specified
# installation location
#
ifeq ($(shell ls $(GENIE_INC_INSTALLATION_PATH) 2>/dev/null ),)
  GENIE_PREVIOUS_INSTALLATION = NO
else
  GENIE_PREVIOUS_INSTALLATION = YES
endif

#-------------------------------------------------------------------
#                             libxml2
#-------------------------------------------------------------------

# XML lib & include directories 

ifdef GOPT_WITH_LIBXML2_INC
XML_INC_DIR = $(GOPT_WITH_LIBXML2_INC)
else
XML_INC_DIR = /usr/include/libxml2
endif

ifdef GOPT_WITH_LIBXML2_LIB
XML_LIB_DIR = $(GOPT_WITH_LIBXML2_LIB)
else
XML_LIB_DIR = /usr/lib
endif

# XML libraries and headers

XML_INCLUDES  := -I$(XML_INC_DIR) 
XML_LIBRARIES := -L$(XML_LIB_DIR) -lxml2

#-------------------------------------------------------------------
#                             log4cpp
#-------------------------------------------------------------------

# log4cpp lib & include directory

ifdef GOPT_WITH_LOG4CPP_INC
LOG_INC_DIR = $(GOPT_WITH_LOG4CPP_INC)
else
LOG_INC_DIR = /usr/local/src/log4cpp/include
endif

ifdef GOPT_WITH_LOG4CPP_LIB
LOG_LIB_DIR = $(GOPT_WITH_LOG4CPP_LIB)
else
LOG_LIB_DIR = /usr/local/lib
endif

# log4cpp libraries and headers

LOG_INCLUDES  := -I$(LOG_INC_DIR)
LOG_LIBRARIES := -L$(LOG_LIB_DIR) -llog4cpp

#-------------------------------------------------------------------
#                             CERNLIB
#-------------------------------------------------------------------

ifeq ($(strip $(GOPT_ENABLE_CERNLIB)),YES) 

CERN_LIB_DIR = $(GOPT_WITH_CERN_LIB)

# figure out which pdflib to use (name keeps changing)

#-- find all libraries named libpdflib*.a and remove the .a part
PPDFLIBS = $(subst .a,,$(wildcard $(CERN_LIB_DIR)/libpdflib*.a))
#-- if many were found use the first one
PPDFLIB  = $(shell awk 'BEGIN {str="$(PPDFLIBS)"; \
			split(str, tokens, " "); print tokens[1]}')
#-- remove the path name
PDFLIB   = $(shell awk 'BEGIN {str="$(PPDFLIB)"; \
			n=split(str, tokens, "/"); print tokens[n]}')
#-- replace the first lib with -l so libpdflib -> -lpdflib
LPDFLIB  = $(shell awk 'BEGIN { str="$(PDFLIB)"; \
				sub(/lib/, "-l", str); print str}')

CERN_LIBRARIES = -L$(CERN_LIB_DIR) \
		 $(LPDFLIB) -lkernlib -lmathlib -lpacklib -lg2c
ifeq ($(strip $(ARCH)),macosx) 
CERN_LIBRARIES = -L$(CERN_LIB_DIR) \
		 $(LPDFLIB) -lkernlib -lmathlib -lpacklib /usr/local/lib/libgfortran.dylib 

have_g2c := $wildcard(/usr/local/lib/libg2c.dylib)
ifeq ($(strip $(have_g2c)),)
else
CERN_LIBRARIES := $(CERN_LIBRARIES) /usr/local/lib/libg2c.dylib 
endif

endif # macosx

endif # enable-cern

#-------------------------------------------------------------------
#                             LHAPDF
#-------------------------------------------------------------------

LHAPDF_LIBRARIES = 
LHAPDF_INCLUDES  =

ifeq ($(strip $(GOPT_ENABLE_LHAPDF)),YES) 

LHAPDF_INCLUDES = -I$(GOPT_WITH_LHAPDF_INC)

ifeq ($(strip $(ARCH)),macosx) 
LHAPDF_LIBRARIES = -L$(GOPT_WITH_LHAPDF_LIB) -lLHAPDF /usr/local/lib/libgfortran.dylib 

have_g2c := $wildcard(/usr/local/lib/libg2c.dylib)
ifeq ($(strip $(have_g2c)),)
else
LHAPDF_LIBRARIES := $(LHAPDF_LIBRARIES) /usr/local/lib/libg2c.dylib 
endif

else
LHAPDF_LIBRARIES = -L$(GOPT_WITH_LHAPDF_LIB) -lLHAPDF -lg2c
endif

endif

#-------------------------------------------------------------------
#                             PYTHIA6
#-------------------------------------------------------------------

# PYTHIA6 dir/lib/object file

PYTHIA_DIR = $(GOPT_WITH_PYTHIA6_LIB)
PYTHIA_O   = $(wildcard $(PYTHIA_DIR)/pythia*.o) # match all vrs

PYTHIA_LIBRARIES  = -L$(PYTHIA_DIR) -lPythia6 $(PYTHIA_O)

ifeq ($(strip $(ARCH)),macosx) 
PYTHIA_LIBRARIES  = -L$(PYTHIA_DIR) -lPythia6 
endif

#-------------------------------------------------------------------
#                             NEUGEN3
#-------------------------------------------------------------------

ifeq ($(strip $(GOPT_ENABLE_NEUGEN)),NO) 

# if NeuGEN was not enabled a dummy NeuGEN library will be built so 
# that the NuValidator's NeuGEN facade can compile. In this case, 
# set NEUGEN3PATH=GENIE
#1  NEUGEN3PATH=$(GENIE)
#1  NEUGEN_LIBRARIES = -L$(NEUGEN3PATH)/lib -lneugen3 
else
#
# In case the actual NeuGEN library is used, add CERNLIB's stdhep
# which is used by NeuGEN (in addition to the 'std' CERNLIBs)
#1  NEUGEN3PATH=$(GOPT_WITH_NEUGEN_PATH)
#1  NEUGEN_LIBRARIES = -L$(CERN_LIB_DIR) -lstdhep \
#1			-L$(NEUGEN3PATH)/lib -lneugen3 
endif

#1ifeq ($(strip $(ARCH)),macosx) 
#1  NEUGEN_LIBRARIES := $(NEUGEN_LIBRARIES) /usr/local/lib/libgfortran.dylib
#1else
#1  NEUGEN_LIBRARIES := $(NEUGEN_LIBRARIES) -lg2c
#1endif

#-------------------------------------------------------------------
#                              NEUT
#-------------------------------------------------------------------

ifeq ($(strip $(GOPT_ENABLE_NEUT)),NO) 
  NEUT_LIB_DIR   =
  NEUT_LIBRARIES =
else
  NEUT_LIB_DIR   = $(GOPT_WITH_NEUT_LIB)

# I need to grab the following libraries:
#   libneutcore_X.Y.Z.a    
#   libnuccorrspl_X.Y.Z.a  
#   libnuceff_X.Y.Z.a        
#   libpartnuck_X.Y.Z.a    
#   libskmcsvc_X.Y.Z.a  
# with whatever version number (different for each library) they are 
# coming with. 
# For each one of the above, I will:
# - find all libraries named lib<name>*.a and remove the .a part
# - if many were found use the first one
# - strip out thepath name
# - replace the first lib with -l so lib<name> -> -l<name>

# libneutcore:
  PNEUTCORES = $(subst .a,,$(wildcard $(NEUT_LIB_DIR)/libneutcore*.a))
  PNEUTCORE  = $(shell awk 'BEGIN {str="$(PNEUTCORES)"; \
               split(str, tokens, " "); print tokens[1]}') 
  NEUTCORE   = $(shell awk 'BEGIN {str="$(PNEUTCORE)"; \
               n=split(str, tokens, "/"); print tokens[n]}')
  LNEUTCORE  = $(shell awk 'BEGIN { str="$(NEUTCORE)"; \
               sub(/lib/, "-l", str); print str}')
# libnuccorrspl:
  PNUCCORS = $(subst .a,,$(wildcard $(NEUT_LIB_DIR)/libnuccorrspl*.a))
  PNUCCOR  = $(shell awk 'BEGIN {str="$(PNUCCORS)"; \
               split(str, tokens, " "); print tokens[1]}') 
  NUCCOR   = $(shell awk 'BEGIN {str="$(PNUCCOR)"; \
               n=split(str, tokens, "/"); print tokens[n]}')
  LNUCCOR  = $(shell awk 'BEGIN { str="$(NUCCOR)"; \
               sub(/lib/, "-l", str); print str}')
# libnuceff:
  PNUCEFFS = $(subst .a,,$(wildcard $(NEUT_LIB_DIR)/libnuceff*.a))
  PNUCEFF  = $(shell awk 'BEGIN {str="$(PNUCEFFS)"; \
               split(str, tokens, " "); print tokens[1]}') 
  NUCEFF   = $(shell awk 'BEGIN {str="$(PNUCEFF)"; \
               n=split(str, tokens, "/"); print tokens[n]}')
  LNUCEFF  = $(shell awk 'BEGIN { str="$(NUCEFF)"; \
               sub(/lib/, "-l", str); print str}')
# libpartnuck:
  PPARTNUCS = $(subst .a,,$(wildcard $(NEUT_LIB_DIR)/libpartnuck*.a))
  PPARTNUC  = $(shell awk 'BEGIN {str="$(PPARTNUCS)"; \
               split(str, tokens, " "); print tokens[1]}') 
  PARTNUC   = $(shell awk 'BEGIN {str="$(PPARTNUC)"; \
               n=split(str, tokens, "/"); print tokens[n]}')
  LPARTNUC  = $(shell awk 'BEGIN { str="$(PARTNUC)"; \
               sub(/lib/, "-l", str); print str}')
# libskmcsvc:
  PSKMCSVCS = $(subst .a,,$(wildcard $(NEUT_LIB_DIR)/libskmcsvc*.a))
  PSKMCSVC  = $(shell awk 'BEGIN {str="$(PSKMCSVCS)"; \
               split(str, tokens, " "); print tokens[1]}') 
  SKMCSVC   = $(shell awk 'BEGIN {str="$(PSKMCSVC)"; \
               n=split(str, tokens, "/"); print tokens[n]}')
  LSKMCSVC  = $(shell awk 'BEGIN { str="$(SKMCSVC)"; \
               sub(/lib/, "-l", str); print str}')

  NEUT_LIBRARIES = -L$(NEUT_LIB_DIR) \
      $(LNEUTCORE) $(LNUCCORRSPL) $(LNUCEFF) $(LPARTNUC) $(LSKMCSVC)
endif

#-------------------------------------------------------------------
#                             GIBUU
#-------------------------------------------------------------------

ifeq ($(strip $(GOPT_ENABLE_GIBUU)),NO) 

# GiBUU was not enabled 
  GIBUU_LIB       =
  GIBUU_LIBRARIES = 
else

# In case the actual fortran90 GIBUU library is used
  GIBUU_LIBRARIES = -L$(GOPT_WITH_GIBUU_LIB) -lGiBUU

ifeq ($(strip $(ARCH)),macosx) 
  GIBUU_LIBRARIES := $(GIBUU_LIBRARIES) /usr/local/lib/libgfortran.dylib
else
  GIBUU_LIBRARIES := $(GIBUU_LIBRARIES) -lg2c
endif

endif

#-------------------------------------------------------------------
#                              ROOT
#-------------------------------------------------------------------
# ROOT headers and libraries
ROOT_INCLUDES  = -I$(shell root-config --incdir)
ROOT_LIBRARIES = $(shell root-config --glibs) \
                        -lMinuit -lGeom -lEG -lEGPythia6

#-------------------------------------------------------------------
#                   PROFILING / DEBUGING OPTIONS
#-------------------------------------------------------------------
# Linking in the Google Profiling Tool library
#
GPROF_LIBRARIES = 
ifeq ($(strip $(GOPT_ENABLE_PROFILER)),YES)
  ifdef GOPT_WITH_PROFILER_LIB
    GPROF_LIBRARIES += -L$(GOPT_WITH_PROFILER_LIB) -lprofiler
  else
    GPROF_LIBRARIES += -L/usr/local/lib -lprofiler
  endif
endif

#-------------------------------------------------------------------
#                               DOXYGEN
#-------------------------------------------------------------------
# if doc was enabled but DOXYGEN was not set, try a default
#
ifdef GOPT_ENABLE_DOXYGEN_DOC
  ifdef GOPT_WITH_DOXYGEN_PATH
    DOXYGEN=$(GOPT_WITH_DOXYGEN_PATH)/doxygen
  else
    DOXYGEN=doxygen
  endif
endif

#-------------------------------------------------------------------
#                       Some global variables
#-------------------------------------------------------------------
MAKE   := gmake
RM      = rm -f
MKDIR   = mkdir -p
CAT     = cat
SYMLINK = ln -sf
COPY    = cp
INSTALL = @INSTALL@

# AR, ARFLAGS and RANLIB are for manipulating an archive
AR        = ar
ARFLAGS   = rv
RANLIB    = ranlib

#-------------------------------------------------------------------
#                        COMPILER OPTIONS
#-------------------------------------------------------------------

# -- gcc

CXX = g++
LD  = g++

# get gcc version & split in '.' delimited tokens (major/minor/revison num.)
CXXVRS      = $(shell $(CXX) -dumpversion)
CXXVRS_MAJ  = $(shell awk 'BEGIN {\
		str="$(CXXVRS)"; split(str, tk, "."); print tk[1]}')
CXXVRS_MIN  = $(shell awk 'BEGIN {\
		str="$(CXXVRS)"; split(str, tk, "."); print tk[2]}')
CXXVRS_REV  = $(shell awk 'BEGIN {\
		str="$(CXXVRS)"; split(str, tk, "."); print tk[3]}')

# gcc version >= 4.1.0 ?
GCC_GE_4_1_0  = $(shell awk 'BEGIN {\
		if($(CXXVRS_MAJ)>=4 && $(CXXVRS_MIN)>0) print "YES"}') 

# MAC OS X with gcc
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ifeq ($(strip $(ARCH)),macosx) 
ARCH_OK       = YES
CXXFLAGS      = -pipe -W -Wall -Wno-long-double \
		-fsigned-char -fno-common -flat_namespace \
		$(GOPT_WITH_CXX_DEBUG_FLAG) \
		$(GOPT_WITH_CXX_OPTIMIZ_FLAG) \
		$(GOPT_WITH_CXX_USERDEF_FLAGS) 
ifeq ($(strip $(GCC_GE_4_1_0)), YES) 
  CXXFLAGS += -Wno-strict-aliasing -ffriend-injection
endif
LDFLAGS       = -bind_at_load
SOFLAGS       = -dynamiclib -flat_namespace \
		-single_module -undefined dynamic_lookup
DllSuf       := dylib
DllLinkSuf   := so
StaticLibSuf := a
ObjSuf       := o
SrcSuf       := cxx
FORT         := g77
FORTOPTS     := $(FFLAGS) -g -c -O -DLINUX $(F77INCS) -fno-second-underscore
RANLIB       := ranlib
SOCMD         = $(LD)
OutPutOpt     = -o
SOMINF =
endif

# LINUX / 32-bit x86 / with gcc
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ifeq ($(strip $(ARCH)),linux) 
ARCH_OK       = YES
CXXFLAGS      = -Wall -fPIC \
		$(GOPT_WITH_CXX_DEBUG_FLAG) \
		$(GOPT_WITH_CXX_OPTIMIZ_FLAG) \
		$(GOPT_WITH_CXX_USERDEF_FLAGS) 
ifeq ($(strip $(GCC_GE_4_1_0)), YES) 
  CXXFLAGS += -Wno-strict-aliasing -ffriend-injection
endif
LDFLAGS       = -g
SOFLAGS       = -shared
DllSuf       := so
DllLinkSuf   := 
StaticLibSuf := a
ObjSuf       := o
SrcSuf       := cxx
FORT         := g77
FORTOPTS     := $(FFLAGS) -g -c -O -DLINUX $(F77INCS) -fno-second-underscore
RANLIB       := ranlib
SOCMD         = $(LD)
OutPutOpt     = -o
SOMINF =
endif

# LINUX / 64-bit x86 / with gcc
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ifeq ($(strip $(ARCH)),linuxx8664gcc) 
ARCH_OK       = YES
CXXFLAGS      = -Wall -fPIC \
		$(GOPT_WITH_CXX_DEBUG_FLAG) \
		$(GOPT_WITH_CXX_OPTIMIZ_FLAG) \
		$(GOPT_WITH_CXX_USERDEF_FLAGS) 
ifeq ($(strip $(GCC_GE_4_1_0)), YES) 
  CXXFLAGS += -Wno-strict-aliasing -ffriend-injection
endif
LDFLAGS       = -g
SOFLAGS       = -shared
DllSuf       := so
DllLinkSuf   := 
StaticLibSuf := a
ObjSuf       := o
SrcSuf       := cxx
FORT         := g77
FORTOPTS     := $(FFLAGS) -g -c -O -DLINUX $(F77INCS) -fno-second-underscore
RANLIB       := ranlib
SOCMD         = $(LD)
OutPutOpt     = -o
SOMINF =
endif

#-------------------------------------------------------------------
#                            SUMMING-UP
#-------------------------------------------------------------------

# LIBS should contain general libraries needed to compile programs. 

LIBRARIES := $(SYSLIBS) \
	     $(ROOT_LIBRARIES) \
	     $(PYTHIA_LIBRARIES) \
	     $(LHAPDF_LIBRARIES) \
             $(XML_LIBRARIES) \
	     $(LOG_LIBRARIES) \
	     $(GPROF_LIBRARIES) -lnsl 

ifeq ($(strip $(ARCH)),macosx) 
LIBRARIES := $(SYSLIBS) \
	     $(ROOT_LIBRARIES) \
	     $(PYTHIA_LIBRARIES) \
	     $(LHAPDF_LIBRARIES) \
             $(XML_LIBRARIES) \
	     $(LOG_LIBRARIES) \
	     $(GPROF_LIBRARIES)
endif

INCLUDES  = $(ROOT_INCLUDES) $(XML_INCLUDES) $(LOG_INCLUDES) $(LHAPDF_INCLUDES) \
	    $(GENIE_INCLUDES) $(LINUX_SYS_INCLUDES)

# Default compiler and preprocessor flags

CXXFLAGS := $(ENV_CXXFLAGS) $(CXXFLAGS)
CPPFLAGS  = $(INCLUDES)
CFLAGS    = -g
#LDFLAGS  := $(LDFLAGS) $(LIBRARIES)

#-------------------------------------------------------------------
#                            MAKE RULES
#-------------------------------------------------------------------

.SUFFIXES : .inc .cxx

#.cc.o:
#	$(CXX) -c -o $@ $(CXXFLAGS) $(CPPFLAGS) $<

#.cxx.o:
#	$(CXX) -c -o $@ $(CXXFLAGS) $(CPPFLAGS) $<

.cc.o:
	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $<

.cxx.o:
	$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $<

.C.o:
	$(CXX) -c -o $@ $(CXXFLAGS) $(CPPFLAGS) $<

.c.o:
	$(CC) -c -o $@ $(CXXFLAGS) $(CPPFLAGS) $<

.F.o :
	$(FORT) $(FORTOPTS) -o $@ $<

.f.o :
	$(FORT) $(FORTOPTS) -o $@ $<

.F.a :
	$(FORT) $(FORTOPTS) -o $*.o $<
	$(AR) $(ARFLAGS) $@ $*.o
	$(RANLIB) $@
	$(RM) $*.o

.f.a :
	$(FORT) $(FORTOPTS) -o $*.o $<
	$(AR) $(ARFLAGS) $@ $*.o
	$(RANLIB) $@
	$(RM) $*.o
