# ======================================================================
#  $Id: common.gmk,v 1.4 2003/10/06 13:32:05 goiwai Exp $
#  $Name: CLDAQ-1-11-00 $
#  $Log: common.gmk,v $
#  Revision 1.4  2003/10/06 13:32:05  goiwai
#  すこし修整しました.
#
#  Revision 1.3  2003/08/12 11:06:14  goiwai
#  環境変数 CLDAQ_INCLUDES が設定されていた場合,その値をコンパイル時に使
#  用するようにしました.
#
#  Revision 1.2  2003/07/30 16:12:33  goiwai
#  ファイルにコミットログをつけることにしました.
#
# ======================================================================
ifeq ($(binmakeinclude),1)
  ifeq ($(CLDAQ_SOME_EXE),1)
    ifndef CLDAQ_LIB_NAME
      CLDAQ_LIB_NAME := someexe
    endif
    CLDAQ_CATEGORY_TMP_DIR := $(CLDAQ_TMP_DIR)/$(CLDAQ_LIB_NAME)
  else
    CLDAQ_LIB_NAME := $(name)
  endif
else
  CLDAQ_LIB_NAME := $(name)
endif
CLDAQ_CATEGORY_TMP_DIR := $(CLDAQ_TMP_DIR)/$(CLDAQ_LIB_NAME)


ifeq ($(binmakeinclude),1)
  ifeq ($(CLDAQ_ONLY_EXE),0)
    sources := $(shell $(CLDAQ_SCRIPT_DIR)/definelibsources $(name))
  endif
  objects := $(patsubst %.cc,$(CLDAQ_CATEGORY_TMP_DIR)/%.o,$(sources))
  dependencies := $(patsubst %.cc,$(CLDAQ_CATEGORY_TMP_DIR)/%.d,$(sources))
else
  sources := $(wildcard *.cc)
  objects := $(patsubst %.cc,$(CLDAQ_CATEGORY_TMP_DIR)/%.o,$(sources))
  dependencies := $(patsubst %.cc,$(CLDAQ_CATEGORY_TMP_DIR)/%.d,$(sources))
endif


ifeq ($(binmakeinclude),1)
  ifeq ($(CLDAQ_ONLY_EXE),0)
    ifeq ($(CLDAQ_SOME_EXE),1)
      ifndef CLDAQ_LIB_NAME
        libraries_to_build = $(CLDAQ_LIB_DIR)/libsomeexe.a
      else
        libraries_to_build = $(CLDAQ_LIB_DIR)/lib$(CLDAQ_LIB_NAME).a
      endif
    else
      libraries_to_build = $(CLDAQ_LIB_DIR)/lib$(name).a
    endif
# ----------------------------------------------------------------------
lib: $(libraries_to_build)
$(libraries_to_build): $(CLDAQ_CATEGORY_TMP_DIR)/obj.last
	@if [ ! -d $(CLDAQ_LIB_DIR) ]; then mkdir -p $(CLDAQ_LIB_DIR); fi
	@$(ECHO) Creating/replacing object files in lib$(CLDAQ_LIB_NAME).a
	@rm -f $(libraries_to_build)
	@$(AR) $(OUT_LIB) $(libraries_to_build) $(CLDAQ_CATEGORY_TMP_DIR)/*.o
	@if [ -f /usr/bin/ranlib -o -f /bin/ranlib ]; then ranlib $(libraries_to_build); fi
# ----------------------------------------------------------------------
  endif
else
# ----------------------------------------------------------------------
libraries_to_build = $(CLDAQ_LIB_DIR)/lib$(name).a
lib: $(libraries_to_build)
$(libraries_to_build): $(CLDAQ_CATEGORY_TMP_DIR)/obj.last
	@if [ ! -d $(CLDAQ_LIB_DIR) ]; then mkdir -p $(CLDAQ_LIB_DIR); fi
	@$(ECHO) Creating/replacing object files in lib$(CLDAQ_LIB_NAME).a
	@rm -f $(libraries_to_build)
	@$(AR) $(OUT_LIB) $(libraries_to_build) $(CLDAQ_CATEGORY_TMP_DIR)/*.o
	@if [ -f /usr/bin/ranlib -o -f /bin/ranlib ]; then ranlib $(libraries_to_build); fi
# ----------------------------------------------------------------------
endif


CPPFLAGS += $(shell $(CLDAQ_SCRIPT_DIR)/cldaq-config --cflags)
CPPFLAGS += -I$(CLDAQ_CLHEP_INCLUDE_DIR)

ifeq ($(CLDAQ_ROOT_USE),1)
  CPPFLAGS += $(shell root-config --cflags) -D__CLDAQ_ROOT_USE
endif

ifeq ($(CLDAQ_GTK_USE),1)
  CPPFLAGS += $(shell gtk-config --cflags) -D__CLDAQ_GTK_USE
endif

ifeq ($(CLDAQ_ZLIB_USE),1)
  CPPFLAGS += -D__CLDAQ_ZLIB_USE
endif


CPPFLAGS += $(X11FLAGS) $(XMFLAGS)
CPPFLAGS += -I.
ifdef CLDAQ_INCLUDES
  CPPFLAGS += $(CLDAQ_INCLUDES)
endif

ifeq ($(binmakeinclude),1)
  ifeq ($(CLDAQ_ONLY_EXE),0)
    libtarget := $(shell $(CLDAQ_SCRIPT_DIR)/definelibtargets $(name))
    CLDAQ_DUMMY_VARIABLE := $(shell $(CLDAQ_SCRIPT_DIR)/makecommondependencies $(CLDAQ_TARGET_TMP_DIR) $(libtarget))
    -include $(CLDAQ_TARGET_TMP_DIR)/commondep.gmk
  endif
else
# ----------------------------------------------------------------------
$(CLDAQ_CATEGORY_TMP_DIR)/%.o: %.cc
	@$(ECHO) Compiling $*.cc ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUT_OBJ)$(CLDAQ_CATEGORY_TMP_DIR)/$(*F).o $*.cc
.PHONY: all obj lib clean
obj: $(CLDAQ_CATEGORY_TMP_DIR)/obj.last
$(CLDAQ_CATEGORY_TMP_DIR)/obj.last: $(objects)
	@touch $@
$(CLDAQ_CATEGORY_TMP_DIR)/%.d: %.cc
	@$(ECHO) Making dependency for file $< ...
	@if [ ! -d $(CLDAQ_CATEGORY_TMP_DIR) ]; then mkdir -p $(CLDAQ_CATEGORY_TMP_DIR); fi
	@($(ECHO) $(CLDAQ_CATEGORY_TMP_DIR)/\\c; $(CXX) $(GPPFLAGS) $(CPPFLAGS) $< ) | sed 's!$(CLDAQ_CATEGORY_TMP_DIR)/$*.o!& $@!' > $@
-include $(dependencies)
# ----------------------------------------------------------------------
endif


ifneq ($(binmakeinclude),1)
# ----------------------------------------------------------------------
clean:
	@$(ECHO) Cleaning up ...
	@rm -f ./core ./*~ a.out
	@ar d $(CLDAQ_LIB_DIR)/lib$(name).a $(objects)
	@if [ -f $(libraries_to_build) -a -x /usr/bin/ranlib -o -x /bin/ranlib ]; then ranlib $(libraries_to_build); fi
	@cd $(CLDAQ_CATEGORY_TMP_DIR) && rm -rf $(objects) $(dependencies)
# ----------------------------------------------------------------------
endif
