# $Id: common.gmk,v 1.14 1999/12/05 22:41:38 morita Exp $
# ----------------------------------------------------------------
# Common part of GNUmakefile for libraries.  John Allison, 5/7/95.
# ----------------------------------------------------------------
# Libraries are created according to G4SYSTEM. G.Cosmo, 11/6/96.
# Introduced G4LIBDIR and G4TMPDIR. G.Cosmo, 23/6/98.
# Introduced G4SCHEMA_HEADER_DIR and G4SCHEMA_SRC_DIR. Y.Morita, 16/11/99.

ifndef G4LIBDIR
  G4LIBDIR := $(G4LIB)/$(G4SYSTEM)
endif
G4TMPDIR := $(G4TMP)/$(G4SYSTEM)/$(name)

ifdef G4ODBMS_INSTALLATION
  G4OODDLX_SCHEMA_NAME := -schema Geant4
else
  G4OODDLX_SCHEMA_NAME := 
endif
G4SCHEMA_HEADER_DIR := $(G4TMPDIR)
G4SCHEMA_SRC_DIR    := $(G4TMPDIR)

sources := $(wildcard src/*.cc)
schema_ddl := $(wildcard include/*.ddl)
schema_hh := $(patsubst include/%.ddl,$(G4SCHEMA_HEADER_DIR)/%.hh,$(schema_ddl))
schema_ref_hh := $(patsubst include/%.ddl,$(G4SCHEMA_HEADER_DIR)/%_ref.hh,$(schema_ddl))
objects := $(patsubst src/%.cc,$(G4TMPDIR)/%.o,$(sources))
dependencies := $(patsubst src/%.cc,$(G4TMPDIR)/%.d,$(sources))

# output of ooddlx in G4TMPDIR (.hh, _ref.hh, _ddl.cc)
#
temporaries := $(patsubst include/%.ddl,$(G4SCHEMA_HEADER_DIR)/%.hh,$(schema_ddl))

# output of ooddlx in the current directory (if the process does not finish)
#
temporaries2 := $(patsubst include/%.ddl,%.hh,$(schema_ddl))

ifdef G4ODBMS
  temporaries += $(patsubst include/%.ddl,$(G4SCHEMA_SRC_DIR)/%_ddl.cc,$(schema_ddl)) \
                 $(patsubst include/%.ddl,$(G4SCHEMA_HEADER_DIR)/%_ref.hh,$(schema_ddl))
  temporaries2 += $(patsubst include/%.ddl,%_ddl.cc,$(schema_ddl)) \
                  $(patsubst include/%.ddl,%_ref.hh,$(schema_ddl))
  objects += $(patsubst include/%.ddl,$(G4TMPDIR)/%_ddl.o,$(schema_ddl))
endif

ifneq ($(G4MAKESHLIB),)
# Make shared library.
$(G4LIBDIR)/lib$(name).$(SHEXT): $(G4LIBDIR)/lib$(name).a
ifneq ($(G4TMPDIR),$(G4LIBDIR))
	@cp $(G4TMPDIR)/*.o $(G4LIBDIR)/.
	@sh $(G4MAKESHLIB) $(G4LIBDIR) $(name)
	@$(RM) $(G4LIBDIR)/*.o
else
	@sh $(G4MAKESHLIB) $(G4LIBDIR) $(name)
endif
endif

# Make library.
$(G4LIBDIR)/lib$(name).a: $(G4TMPDIR)/obj.last
	@if [ ! -d $(G4LIBDIR) ] ; then mkdir $(G4LIBDIR) ;fi
	@echo Creating/replacing object files in $(G4LIBDIR)/lib$(name).a
	@rm -f $(G4LIBDIR)/lib$(name).a
	@$(AR) $(OUT_LIB)$(G4LIBDIR)/lib$(name).a $(G4TMPDIR)/*.o
	@if [ -f /usr/bin/ranlib -o -f /bin/ranlib ] ; then ranlib $(G4LIBDIR)/lib$(name).a ;fi

# Touch the versioning file
$(G4TMPDIR)/obj.last: $(objects)
	@touch $@

# Make the .o files.
ifdef G4ODBMS
$(G4TMPDIR)/%.o: src/%.cc $(schema_hh)
else
$(G4TMPDIR)/%.o: src/%.cc
endif
ifdef CPPVERBOSE
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUT_OBJ)$(G4TMPDIR)/$(*F).o src/$*.cc
else
	@echo Compiling $*.cc ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUT_OBJ)$(G4TMPDIR)/$(*F).o src/$*.cc
endif
ifdef G4ODBMS
$(G4TMPDIR)/%.o: $(G4SCHEMA_SRC_DIR)/%.cc
ifdef CPPVERBOSE
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $(G4TMPDIR)/$(*F).o $(G4SCHEMA_SRC_DIR)/$*.cc
else
	@echo Compiling $*.cc ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $(G4TMPDIR)/$(*F).o $(G4SCHEMA_SRC_DIR)/$*.cc
endif
endif
# Make the temporary files.
.PRECIOUS: $(G4SCHEMA_SRC_DIR)/%_ddl.cc $(G4SCHEMA_HEADER_DIR)/%.hh $(G4SCHEMA_HEADER_DIR)/%_ref.hh

ifdef G4ODBMS
$(G4SCHEMA_SRC_DIR)/%_ddl.cc $(G4SCHEMA_HEADER_DIR)/%.hh $(G4SCHEMA_HEADER_DIR)/%_ref.hh: include/%.ddl
ifndef G4OODDLX_BOOT
	@echo G4OODDLX_BOOT is not defined.  Stop.
	exit 1
endif
	@if [ ! -d $(G4TMP)/$(G4SYSTEM) ] ; then mkdir $(G4TMP)/$(G4SYSTEM) ;fi
	@if [ ! -d $(G4TMPDIR) ] ; then mkdir $(G4TMPDIR) ;fi
	@if [ ! -d $(G4SCHEMA_HEADER_DIR) ] ; then mkdir $(G4SCHEMA_HEADER_DIR) ;fi
	@if [ ! -d $(G4SCHEMA_SRC_DIR) ] ; then mkdir $(G4SCHEMA_SRC_DIR) ;fi
ifdef CPPVERBOSE
	ooddlx -notitle $(G4OODDLX_SCHEMA_NAME) \
	  -header_suffix .hh -ref_suffix _ref.hh -c++_suffix _ddl.cc \
	  $(G4OODDLX_FLAGS) $(CPPFLAGS) $< $(G4OODDLX_BOOT)
else
	@echo Preprocessing $< ...
	@ooddlx -notitle $(G4OODDLX_SCHEMA_NAME) \
	  -header_suffix .hh -ref_suffix _ref.hh -c++_suffix _ddl.cc \
	  $(G4OODDLX_FLAGS) $(CPPFLAGS) $< $(G4OODDLX_BOOT)
endif
	@mv -f $*.hh $(G4SCHEMA_HEADER_DIR)/
	@mv -f $*_ref.hh $(G4SCHEMA_HEADER_DIR)/
	@mv -f $*_ddl.cc $(G4SCHEMA_SRC_DIR)/
endif

# Make the .d file(s) and include it(them).
# g++ -MM is good at this, except it forgets the subdirectory
# (hence the use of $G4TMP/).

# define G4SKIP_DEPEND for simplicity
ifndef G4ODBMS_DEPEND
ifdef G4ODBMS
  G4SKIP_DEPEND := 1
endif
endif

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

# .PHONY targets are executed regardless of time-stamp of any file of
# same name.
ifdef G4ODBMS_DEPEND
.PHONY: all obj lib clean clean_libs d_files install_schema
else
.PHONY: all obj lib clean clean_libs
endif

obj: $(G4TMPDIR)/obj.last

ifdef G4ODBMS
headers: $(schema_hh)

d_files: $(dependencies)

$(schema_hh): $(schema_ddl)

install_schema:
	@echo installing schema header files ...
	-@(cp -f $(G4TMPDIR)/*.hh $(G4SCHEMA_INCLUDE)/ || \
	  echo "  schema header file does not exist, so ignore the error message...")
endif

ifneq ($(G4MAKESHLIB),)
lib: $(G4LIBDIR)/lib$(name).$(SHEXT)
else
lib: $(G4LIBDIR)/lib$(name).a
endif

ifndef G4EXLIB
clean:
	@echo Cleaning up ...
	@rm -f $(G4LIBDIR)/lib$(name).a
	@rm -f $(schema_hh) $(schema_ref_hh)
	@rm -rf $(G4TMPDIR)
endif

clean_libs:
	@if [ -f $(G4LIBDIR)/lib$(name).a ] ; then \
	$(ECHO) Removing library lib$(name).a ... ; \
	$(RM) -f $(G4LIBDIR)/lib$(name).a ; fi

