#-----------------------------------------------------------
# Copyright Christian Arnault LAL-Orsay CNRS
# arnault@lal.in2p3.fr
# See the complete license in cmt_license.txt "http://www.cecill.info". 
#-----------------------------------------------------------

include ${CMTROOT}/src/Makefile.core

#
#  Basic tag management
#
ifdef tag
CMTEXTRATAGS = $(tag)
else
tag       = $(CMTCONFIG)
endif

tags      = $(tag),$(CMTEXTRATAGS)

ifdef READONLY
cmt_local_tagfile = /tmp/CMT_$(tag).make$$$$
else
cmt_local_tagfile = $(tag).make
endif

#
#  Generic entry points for all targets
#
all :: check_config

check_config ::
	@$(cmtexe) check configuration; status=$$?; \
	if test $${status} = 0; then \
	  /bin/rm -f $(cmt_local_tagfile); \
	  $(MAKE) --no-print-directory $(MAKEFLAGS) target="first config all" common_target; \
	else \
	  echo "#CMT> There are configuration errors. Please correct them before running gmake"; \
	fi

binclean ::
	@if test ! "$(bin)" = "./"; then \
	  if test -d $(bin) ; then \
	    echo "------> (Makefile.header) Cleaning all from $(bin)"; \
	    /bin/rm -rf $(bin) ; \
	  fi ; \
	fi

clean ::
	@$(cmtexe) check configuration
	@$(MAKE) --no-print-directory target="$@" common_target

.DEFAULT ::
	@$(cmtexe) check configuration
	@$(MAKE) --no-print-directory target="config $@" common_target

#
#  Capture all possible targets
#
common_target ::
	@tmpdir=/tmp; \
	  if test ! "${TMPDIR}" = ""; then tmpdir=${TMPDIR}; fi; \
	  cmt_lock_pid=`mkdir -p $${tmpdir}; mktemp $${tmpdir}/CMT_XXXXXX | sed -e 's#'"$${tmpdir}"'/CMT_##'`; \
	  temp_marker=$${tmpdir}/CMT_$${cmt_lock_pid}; \
	  temp_lock=$${tmpdir}/CMT_lock$${cmt_lock_pid}.make; \
	  temp_constituents=$${tmpdir}/CMT_constituents$${cmt_lock_pid}; \
	  if test ! "${CMTMAKEDEBUG}" = 1; then\
	    trap '/bin/rm -f $${temp_lock} $${temp_constituents} $${temp_marker}; exit $${status}' 0 1 2 15; \
	  fi; \
	  /bin/rm -f $${temp_marker}; \
	  /bin/rm -f $${temp_lock}; \
	  echo "------> (Makefile.header) Rebuilding constituents.make"; \
	  $(cmtexe) -quiet -tag=$(tags) build constituents_makefile -out=$${temp_constituents}; status=$$?; \
	  if test $${status} = 0; then \
	    /bin/rm -f $(cmt_local_tagfile); \
	    $(MAKE) --no-print-directory -f $${temp_constituents} cmt_lock_pid=$${cmt_lock_pid} $(target); \
	    status=$$?; \
	  else \
	    echo "#CMT> Error generating constituents makefile"; \
	  fi; \
	  exit $${status}


#
#  Ancient mechanism for remote launchers (kept for backward compatibility)
#
$(gmake_hosts) ::
	rsh $@ "sh -c '(cd `$(cmtexe) show pwd -quiet`; . setup.sh; gmake $(MAKEFLAGS) $(target))'"

$(make_hosts) ::
	rsh $@ "sh -c '(cd `$(cmtexe) show pwd`; . setup.sh; make $(MAKEFLAGS) $(target))'"


everywhere :: $(everywhere)

#
#   Display all targets
#
help ::
	@echo '--------------------------------------------------------------------------'
	@echo 'You may select one of the following make targets :'
	@echo ' '
	@echo 'help        : this help'
	@echo 'all         : rebuild all constituents according to the $$(constituents) macro'
	@echo '              (this is the default target)'
	@echo 'clean       : remove everything that can be rebuilt'
	@echo 'binclean    : fast erase of the binary directory'
	@echo ' '
	@groups=`$(cmtexe) show groups -quiet -tag=$(tags)`; \
	  for g in $${groups}; do \
	    echo "$${g} : rebuild all constituents of group $${g}"; \
	  done
	@echo "Constituents:"; \
	  for c in `$(cmtexe) show constituent_names -quiet -tag=$(tags)` ; do \
	    echo "  $${c}  $${c}clean"; \
	  done
	@echo ' '
	@echo 'check :'
	@echo '              run all applications defined with the -check option'
	@echo ' '


