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

include $(CLDAQ_INSTALL)/config/architecture.gmk

numberoftargets := $(shell $(CLDAQ_INSTALL)/config/scripts/filecount $(name))
ifeq ($(numberoftargets),1)
  CLDAQ_SOME_EXE = 0
else
  CLDAQ_SOME_EXE = 1
endif
allsources := $(shell $(CLDAQ_INSTALL)/config/scripts/filecount $(wildcard *.cc))
ifeq ($(numberoftargets),$(allsources))
  CLDAQ_ONLY_EXE = 1
else
  CLDAQ_ONLY_EXE = 0
endif

ifndef CLDAQ_TARGET_TMP_DIR
  ifeq ($(CLDAQ_SOME_EXE),1)
    ifndef CLDAQ_LIB_NAME
      CLDAQ_TARGET_TMP_DIR := $(CLDAQ_TMP_DIR)/someexe
    else
      CLDAQ_TARGET_TMP_DIR := $(CLDAQ_TMP_DIR)/$(CLDAQ_LIB_NAME)
    endif
  else
    CLDAQ_TARGET_TMP_DIR := $(CLDAQ_TMP_DIR)/$(name)
  endif
endif
ifndef CLDAQ_TARGET_LIB_DIR
  CLDAQ_TARGET_LIB_DIR := $(CLDAQ_TARGET_TMP_DIR)
endif
CLDAQ_LIB_DIR := $(CLDAQ_TARGET_LIB_DIR)

ifeq ($(CLDAQ_ONLY_EXE),0)
  LDFLAGS += -L$(CLDAQ_LIB_DIR)
  ifeq ($(CLDAQ_SOME_EXE),1)
    ifndef CLDAQ_LIB_NAME
      LDLIBS += -lsomeexe
    else
      LDLIBS += -l$(CLDAQ_LIB_NAME)
    endif
  else
    LDLIBS += -l$(name)
  endif
endif



LDFLAGS += $(shell $(CLDAQ_INSTALL)/config/scripts/cldaq-config --ldflags)
LDLIBS += $(shell $(CLDAQ_INSTALL)/config/scripts/cldaq-config --ldlibs)
LDFLAGS += -L$(CLDAQ_CLHEP_LIB_DIR)
LDLIBS += -l$(CLDAQ_CLHEP_LIB_NAME)
ifeq ($(CLDAQ_ROOT_USE),1)
  LDLIBS += $(shell root-config --libs)
endif
ifeq ($(CLDAQ_GTK_USE),1)
  LDLIBS += $(shell gtk-config --libs)
endif
ifeq ($(CLDAQ_PAW_USE),1)
  LDLIBS += 
endif
ifeq ($(CLDAQ_QT_USE),1)
  LDLIBS += 
endif
ifeq ($(CLDAQ_ZLIB_USE),1)
  LDLIBS += -lz
endif
LDLIBS += $(X11LIBS) $(XMLIBS)

ifdef CLDAQ_LIBS
  LDLIBS += $(CLDAQ_LIBS)
endif





include $(CLDAQ_INSTALL)/config/common.gmk



sources := $(shell $(CLDAQ_INSTALL)/config/scripts/definesources $(name))
ifeq ($(CLDAQ_SOME_EXE),1)
  objects := $(shell $(CLDAQ_INSTALL)/config/scripts/defineobjects $(CLDAQ_TARGET_TMP_DIR) $(sources))
else
  objects := $(patsubst %.cc,$(CLDAQ_TARGET_TMP_DIR)/exe/%.o,$(sources))
endif
dependencies := $(patsubst %.o,%.d,$(objects))


.PHONY: bin clean

bin: $(shell $(CLDAQ_INSTALL)/config/scripts/definebindep $(CLDAQ_BIN_DIR) $(name))


ifeq ($(CLDAQ_SOME_EXE),1)
  CLDAQ_DUMMY_VARIABLE := $(shell $(CLDAQ_INSTALL)/config/scripts/makebinmakedependencies $(CLDAQ_TARGET_TMP_DIR) $(name))
else
$(CLDAQ_BIN_DIR)/$(name): $(CLDAQ_TARGET_TMP_DIR)/exe/obj.last
	@if [ ! -d $(CLDAQ_BIN_DIR) ]; then mkdir -p $(CLDAQ_BIN_DIR); fi
	@$(ECHO) Linking $(name) using granular libraries ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OUT_EXE)$(CLDAQ_BIN_DIR)/$(name) $(objects) $(LDFLAGS) $(LDLIBS)

ifeq ($(CLDAQ_ONLY_EXE),0)
$(CLDAQ_TARGET_TMP_DIR)/exe/obj.last: lib $(objects)
else
$(CLDAQ_TARGET_TMP_DIR)/exe/obj.last: $(objects)
endif
	@touch $@

$(CLDAQ_TARGET_TMP_DIR)/exe/$(name).o: $(name).cc
	@$(ECHO) Compiling $(name).cc ...
	@$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(APPFLAGS) -c $(OUT_OBJ)$(CLDAQ_TARGET_TMP_DIR)/exe/$(name).o $(name).cc

$(CLDAQ_TARGET_TMP_DIR)/exe/$(name).d: $(name).cc
	@if [ ! -d $(CLDAQ_TARGET_TMP_DIR)/exe ]; then mkdir -p $(CLDAQ_TARGET_TMP_DIR)/exe; fi
	@$(ECHO) Making dependency for file $< ...
	@($(ECHO) $(CLDAQ_TARGET_TMP_DIR)/exe/\\c; $(CXX) $(GPPFLAGS) $(CPPFLAGS) $< ) | sed 's!$(CLDAQ_TARGET_TMP_DIR)/exe/$*.o!& $@!' > $@
endif

-include $(CLDAQ_TARGET_TMP_DIR)/bindep.gmk
-include $(dependencies)


clean:
	@$(ECHO) Cleaning up ...
	@rm -f ./core ./*~ a.out
#ifeq ($(CLDAQ_SOME_EXE),1)
#	@cd $(CLDAQ_TARGET_TMP_DIR) && rm -rf $(name) $(objects) $(dependencies)
#else
	@rm -rf $(CLDAQ_TARGET_TMP_DIR)
#endif
	@cd $(CLDAQ_BIN_DIR) && rm -f $(name)
