
include ${CMTROOT}/src/Makefile.core

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

tags      = $(CMTCONFIG),$(CMTEXTRATAGS)

#
#  Generic entry points for all targets
#
all :: config
	@$(cmtexe) check configuration
	@FILTEREDMAKEFLAGS=`echo $(MAKEFLAGS) | sed -e 's#^j#-j#' -e 's#[ ]j[ ]# -j #'`; \
	$(MAKE) --no-print-directory $${FILTEREDMAKEFLAGS} target="all" common_target

.DEFAULT ::
	@$(cmtexe) check configuration
	@FILTEREDMAKEFLAGS=`echo $(MAKEFLAGS) | sed -e 's#^j#-j#' -e 's#[ ]j[ ]# -j #'`; \
	$(MAKE) --no-print-directory $${FILTEREDMAKEFLAGS} target="$@" common_target

config ::
	@$(cmtexe) check configuration
	@FILTEREDMAKEFLAGS=`echo $(MAKEFLAGS) | sed -e 's#^j#-j#' -e 's#[ ]j[ ]# -j #'`; \
	$(MAKE) --no-print-directory $${FILTEREDMAKEFLAGS} target="config" common_target

#
#  Capture all possible targets
#
common_target ::
	MMAKEFLAGS=`echo $(MAKEFLAGS) | sed -e 's#^j#-j#' -e 's#[ ]j[ ]# -j #'`; \
	cmt_lock_pid=$$$$; \
	temp_lock=lock$${cmt_lock_pid}.make; \
	temp_constituents=constituents$${cmt_lock_pid}; \
	trap '/bin/rm -f $${temp_lock} $${temp_constituents}; exit $${status}' 0 1 2 15; \
	/bin/rm -f $${temp_lock}; \
	echo "------> (Makefile.header) Rebuilding constituents.make"; \
	$(cmtexe) build constituents_makefile -quiet -tag=$(tags); \
	/bin/mv constituents.make $${temp_constituents}; \
	$(MAKE) --no-print-directory -f $${temp_constituents} cmt_lock_pid=$${cmt_lock_pid} $${MMAKEFLAGS} first; \
	$(MAKE) --no-print-directory -f $${temp_constituents} cmt_lock_pid=$${cmt_lock_pid} $${MMAKEFLAGS} $(target); \
	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 ' '
	@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 ' '


