# $Id: binmake.gmk,v 1.45 2000/05/23 05:32:14 stesting Exp $
# ----------------------------------------------------------
# Script defining rules and paths for making binaries.
#                          Gabriele Cosmo, 25/06/1998.
# ----------------------------------------------------------

ifndef LDFLAGS
  include $(G4INSTALL)/config/architecture.gmk
endif

G4LIBDIR := $(G4LIB)/$(G4SYSTEM)

include $(G4INSTALL)/config/G4UI_USE.gmk
include $(G4INSTALL)/config/G4VIS_USE.gmk
include $(G4INSTALL)/config/interactivity.gmk

ifndef INCFLAGS
  INCFLAGS := \
              -I$(G4BASE)/global/management/include \
              -I$(G4BASE)/global/HEPRandom/include \
              -I$(G4BASE)/global/HEPGeometry/include \
              -I$(G4BASE)/global/HEPNumerics/include \
              -I$(G4BASE)/run/include \
              -I$(G4BASE)/materials/include \
              -I$(G4BASE)/particles/management/include \
              -I$(G4BASE)/particles/bosons/include \
              -I$(G4BASE)/particles/leptons/include \
              -I$(G4BASE)/particles/hadrons/barions/include \
              -I$(G4BASE)/particles/hadrons/ions/include \
              -I$(G4BASE)/particles/hadrons/mesons/include \
              -I$(G4BASE)/particles/shortlived/include \
              -I$(G4BASE)/processes/management/include \
              -I$(G4BASE)/processes/general/include \
              -I$(G4BASE)/processes/decay/include \
              -I$(G4BASE)/processes/optical/include \
              -I$(G4BASE)/processes/transportation/include \
              -I$(G4BASE)/processes/parameterisation/include \
              -I$(G4BASE)/processes/electromagnetic/standard/include \
              -I$(G4BASE)/processes/electromagnetic/muons/include \
              -I$(G4BASE)/processes/electromagnetic/utils/include \
              -I$(G4BASE)/processes/electromagnetic/xrays/include \
              -I$(G4BASE)/processes/electromagnetic/lowenergy/include \
              -I$(G4BASE)/processes/hadronic/management/include \
              -I$(G4BASE)/processes/hadronic/processes/include \
              -I$(G4BASE)/processes/hadronic/util/include \
              -I$(G4BASE)/processes/hadronic/cross_sections/include \
              -I$(G4BASE)/processes/hadronic/models/generator/de_excitation/include \
              -I$(G4BASE)/processes/hadronic/models/generator/diffractive_string/include \
              -I$(G4BASE)/processes/hadronic/models/generator/high_energy/include \
              -I$(G4BASE)/processes/hadronic/models/generator/kinetic_model/include \
              -I$(G4BASE)/processes/hadronic/models/generator/management/include \
              -I$(G4BASE)/processes/hadronic/models/generator/pre_equilibrium/include \
              -I$(G4BASE)/processes/hadronic/models/generator/quark_gluon_string/include \
              -I$(G4BASE)/processes/hadronic/models/generator/string_common/include \
              -I$(G4BASE)/processes/hadronic/models/generator/string_fragmentation/include \
              -I$(G4BASE)/processes/hadronic/models/generator/util/include \
              -I$(G4BASE)/processes/hadronic/models/high_energy/include \
              -I$(G4BASE)/processes/hadronic/models/low_energy/include \
              -I$(G4BASE)/processes/hadronic/models/neutron_hp/include \
              -I$(G4BASE)/processes/hadronic/models/isotope_production/include \
              -I$(G4BASE)/processes/hadronic/models/radiative_decay/include \
              -I$(G4BASE)/processes/hadronic/stopping/include \
              -I$(G4BASE)/geometry/management/include \
              -I$(G4BASE)/geometry/solids/CSG/include \
              -I$(G4BASE)/geometry/solids/specific/include \
              -I$(G4BASE)/geometry/solids/BREPS/include \
              -I$(G4BASE)/geometry/solids/Boolean/include \
              -I$(G4BASE)/geometry/solids/STEP/include \
              -I$(G4BASE)/geometry/solids/STEPinterface/include \
              -I$(G4BASE)/geometry/volumes/include \
              -I$(G4BASE)/geometry/magneticfield/include \
              -I$(G4BASE)/track/include \
              -I$(G4BASE)/tracking/include \
              -I$(G4BASE)/digits+hits/detector/include \
              -I$(G4BASE)/digits+hits/hits/include \
              -I$(G4BASE)/digits+hits/digits/include \
              -I$(G4BASE)/readout/include \
              -I$(G4BASE)/event/include \
              -I$(G4BASE)/graphics_reps/include \
              -I$(G4BASE)/intercoms/include
endif

ifdef G4ODBMS
  INCFLAGS += \
              -I$(G4BASE)/persistency/global/include \
              -I$(G4BASE)/persistency/digits+hits/hits/include \
              -I$(G4BASE)/persistency/digits+hits/digits/include \
              -I$(G4BASE)/persistency/events/include \
              -I$(G4BASE)/persistency/geometry/global/include \
              -I$(G4BASE)/persistency/geometry/solids/CSG/include \
              -I$(G4BASE)/persistency/geometry/solids/Boolean/include \
              -I$(G4BASE)/persistency/geometry/management/include \
              -I$(G4BASE)/persistency/management/include \
              -I$(G4SCHEMA_INCLUDE) \
              -I$(OBJY_DIR)/include \
              -I$(HEP_ODBMS_DIR)/include \
              -DHep_HAS_BUILDIN_BOOL_CONSTANTS
endif

CPPFLAGS += $(INCFLAGS)
LDFLAGS += -L$(G4LIBDIR)

G4TMPDIR := $(G4TMP)/$(G4SYSTEM)/$(G4TARGET)
G4BINDIR := $(G4BIN)/$(G4SYSTEM)

ifdef G4EXLIB
  G4LIBDIR := $(G4TMPDIR)
  LDFLAGS += -L$(G4LIBDIR)
  include $(G4INSTALL)/config/common.gmk
endif

GLOBALLIBS := $(shell test -f $(G4LIB)/$(G4SYSTEM)/libG4global.a && echo yes)

ifdef LDLIBS
  USER_DEFINED_LDLIBS := 1
endif

# Because of the script for granular libraries which replaces part of LDLIBS
# and because of the way user defined LDLIBS was augmented historically I
# have split LDLIBS into 4 parts...
#
ifndef USER_DEFINED_LDLIBS

# LDLIBS1 contains the very high level libraries...
#
ifdef G4EXLIB
  LDLIBS1 := -l$(G4TARGET)
endif
LDLIBS1 += $(EXTRALIBS)

# VISLIBS and UILIBS are now handled by the granular library script...
#
ifdef GLOBALLIBS
  LDLIBS1 += $(VISLIBS) $(UILIBS)
endif

ifdef GLOBALLIBS

  ifdef G4ODBMS
    LDLIBS1 += -lG4persistency
  endif

  LDLIBS2 := -lG4readout \
             -lG4run \
             -lG4event \
             -lG4tracking \
             -lG4processes \
             -lG4digits+hits \
             -lG4track \
             -lG4particles \
             -lG4geometry \
             -lG4materials \
             -lG4graphics_reps \
             -lG4intercoms \
             -lG4global
else
# The next line specifically should read LDLIBS2 = , not LDLIBS2 :=, so
#   that it is not expanded until the directory G4TMPDIR is created.
  LDLIBS2 = $(shell \
        G4TMP=$(G4TMP); export G4TMP; \
	if [ \( -f $(G4LIB)/$(G4SYSTEM)/liblist \
	  -a -f $(G4LIB)/$(G4SYSTEM)/libname.map \) ]; then \
	$(G4LIB)/$(G4SYSTEM)/liblist \
	  -d $(G4TMPDIR) \
	  < $(G4LIB)/$(G4SYSTEM)/libname.map; fi)
# Warning: do not add to LDLIBS2 with += becuase this causes it to be
# expanded too soon.  Hence extra libraries must have a different name...
# Extra libraries to resolve remaining circular dependencies...
#  LDLIBS2EXTRA = 
endif

# LDLIBS3 contains the first set of low level libraries...
#
LDLIBS3 += $(INTYLIBS)

endif # ifndef USER_DEFINED_LDLIBS

# LDLIBS4 contains the next set of low level libraries which historically
# (why?) the user is not supposed to be able to define...
#
ifdef G4ODBMS
  LDFLAGS  += -L$(OBJY_DIR)/lib \
              -L$(HEP_ODBMS_DIR)/lib

#  Note: $(G4ODBMS_LIBS) should be placed
#        _before_ -l$(CLHEP_LIB)

  LDLIBS4 += $(G4ODBMS_LIBS)
endif
ifdef G4USE_OSPACE
  LDLIBS4 += $(OSPACELIBS)
endif

LDLIBS4 += $(LOADLIBS)

# Finally assemble libraries...
#
ifdef USER_DEFINED_LDLIBS
  LDLIBS_PREFINAL := $(LDLIBS)
else
# Again, do not use := or +=.  See note on LDLIBS2 above.
  LDLIBS_PREFINAL = $(LDLIBS1) $(LDLIBS2) $(LDLIBS2EXTRA) $(LDLIBS3)
endif
LDLIBS_PREFINAL += $(LDLIBS4)

ifeq ($(G4SYSTEM),WIN32-VC)
  WIN32TMP := $(patsubst -L%,$(LIB_PATH)%,$(LDFLAGS))
  LDFLAGS  = $(patsubst /,$(PATH_DEL),$(WIN32TMP))
  LDLIBS = $(patsubst -l%,lib%.a,$(LDLIBS_PREFINAL))
else
  LDLIBS = $(LDLIBS_PREFINAL)
endif


sources := $(wildcard $(G4TARGET).cc)
objects := $(patsubst %.cc,$(G4TMPDIR)/exe/%.o,$(sources))
dependencies := $(patsubst %.cc,$(G4TMPDIR)/exe/%.d,$(sources))

LINK_DEPENDENCIES := $(G4TMPDIR)/exe/obj.last $(EXTRA_LINK_DEPENDENCIES)

ifndef G4ODBMS
.PHONY: bin clean clean_bin debug
else
.PHONY: bin clean clean_bin debug cleandb newboot newfd
endif

# Make $(G4TARGET) executable.
bin: $(G4BINDIR)/$(G4TARGET)

$(G4BINDIR)/$(G4TARGET): $(LINK_DEPENDENCIES)
ifndef USER_DEFINED_LDLIBS
  ifndef GLOBALLIBS
	@echo "Using granular libraries ..."
	@if [ ! \( -f $(G4LIB)/$(G4SYSTEM)/liblist \
	  -a -f $(G4LIB)/$(G4SYSTEM)/libname.map \) ]; then \
	  echo "ERROR: No liblist program or library map file."; \
	  echo "       These are needed for building with granular"; \
	  echo "       libraries."; \
	  echo '         cd $$G4INSTALL/source'; \
	  echo "         gmake"; \
	  echo "       or if you are sure you have already made all the"; \
	  echo "       granular libraries:"; \
	  echo "         gmake libmap"; \
	  exit 1; fi
  else
	@echo "Using global libraries."
  endif
endif
	@if [ ! -d $(G4BINDIR) ] ; then mkdir $(G4BINDIR) ;fi
ifdef CPPVERBOSE
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) \
	  -o $(G4BINDIR)/$(G4TARGET) $(objects) $(LDFLAGS) \
	  $(LDLIBS)
else
	@echo Linking $(G4TARGET) ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) \
	  -o $(G4BINDIR)/$(G4TARGET) $(objects) $(LDFLAGS) \
	  $(LDLIBS)
endif

# Touch the versioning file
ifdef G4EXLIB
$(G4TMPDIR)/exe/obj.last: lib $(objects)
else
$(G4TMPDIR)/exe/obj.last: $(objects)
endif
	@touch $@

# Make the $(G4TARGET).o file.
$(G4TMPDIR)/exe/$(G4TARGET).o : $(G4TARGET).cc
ifdef CPPVERBOSE
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(APPFLAGS) \
	 -c $(OUT_OBJ)$(G4TMPDIR)/exe/$(G4TARGET).o $(G4TARGET).cc
else
	@echo Compiling $(G4TARGET).cc ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(APPFLAGS) \
	 -c $(OUT_OBJ)$(G4TMPDIR)/exe/$(G4TARGET).o $(G4TARGET).cc
endif

# Make the $(G4TARGET).d file and include it.
ifdef G4SKIP_DEPEND
$(G4TMPDIR)/exe/$(G4TARGET).d: $(G4TARGET).cc
	@echo Skip dependency file creation for file $< ...
else
$(G4TMPDIR)/exe/$(G4TARGET).d: $(G4TARGET).cc
	@if [ ! -d $(G4TMP)/$(G4SYSTEM) ] ; then mkdir $(G4TMP)/$(G4SYSTEM) ;fi
	@if [ ! -d $(G4TMPDIR) ] ; then mkdir $(G4TMPDIR) ;fi
	@if [ ! -d $(G4TMPDIR)/exe ] ; then mkdir $(G4TMPDIR)/exe ;fi
	@echo Making dependency for file $<...
	@($(ECHO) $(G4TMPDIR)/exe/\\c ; \
	g++ -MM $(CPPFLAGS) $< ) | sed 's!$(G4TMPDIR)/exe/$*.o!& $@!' >$@
-include $(dependencies)
endif

clean:
	@echo Cleaning up ...
	@rm -f ./core
	@rm -rf $(G4TMPDIR)
	@rm -f $(G4LIBDIR)/lib$(G4TARGET).a
	@rm -f $(G4BINDIR)/$(G4TARGET)
ifdef CFRONT
	@rm -rf $(G4TREP)/exec
endif

clean_bin:
	@echo Removing application $(G4TARGET) ...
	@rm -f ./core
	@rm -f $(G4BINDIR)/$(G4TARGET)

############################+ targets for G4ODBMS +############################
ifdef G4ODBMS

cleandb: clean newboot

newboot:
	@echo Copying schema file to $(G4EXAMPLE_BOOT) for FDID $(G4EXAMPLE_FDID)
	@rm -f $(G4EXAMPLE_BOOT_DIR)/$(name)
	@rm -f $(G4EXAMPLE_BOOT_DIR)/$(name).FDDB
	@rm -f $(G4EXAMPLE_BOOT_DIR)/*.$(name).DB
	@$(HEP_ODBMS_DIR)/etc/getdb $(G4SCHEMA_DIR)/G4SCHEMA \
	                            $(G4EXAMPLE_BOOT) $(G4SCHEMA_FDID)

newfd:
ifndef G4EXAMPLE_FDID
	@echo G4EXAMPLE_FDID is not defined.  Stop.
	exit 1
endif
	@echo Creating new runtime federated database $(G4EXAMPLE_BOOT) for FDID $(G4EXAMPLE_FDID)
	@rm -f $(G4EXAMPLE_FD_DIR)/$(name)
	@rm -f $(G4EXAMPLE_FD_DIR)/$(name).FDDB
	@rm -f $(G4EXAMPLE_FD_DIR)/*.$(name).DB
	@$(HEP_ODBMS_DIR)/etc/getdb $(G4EXAMPLE_BOOT) \
	                            $(G4EXAMPLE_FD) $(G4EXAMPLE_FDID)
endif
############################- targets for G4ODBMS -############################

