#-- start of constituent_lock ------

cmt_${CONSTITUENT}_${HASTARGETTAG} = 1

#--------------------------------------------------------

ifdef cmt_${CONSTITUENT}_has_target_tag

cmt_local_tagfile_${CONSTITUENT} = $(bin)$(${PACKAGE}_tag)_${CONSTITUENT}.make
cmt_final_setup_${CONSTITUENT} = $(bin)setup_${CONSTITUENT}.make
cmt_local_${CONSTITUENT}_makefile = $(bin)${CONSTITUENT}.make

${CONSTITUENT}_extratags = -tag_add=target_${CONSTITUENT}

else

cmt_local_tagfile_${CONSTITUENT} = $(bin)$(${PACKAGE}_tag).make
cmt_final_setup_${CONSTITUENT} = $(bin)setup.make
cmt_local_${CONSTITUENT}_makefile = $(bin)${CONSTITUENT}.make

endif

not_${CONSTITUENT}_dependencies = { n=0; for p in $?; do m=0; for d in $(${CONSTITUENT}_dependencies); do if [ $$p = $$d ]; then m=1; break; fi; done; if [ $$m -eq 0 ]; then n=1; break; fi; done; [ $$n -eq 1 ]; }

ifdef STRUCTURED_OUTPUT
${CONSTITUENT}dirs :
	@if test ! -d $(bin)${CONSTITUENT}; then $(mkdir) -p $(bin)${CONSTITUENT}; fi
	$(echo) "STRUCTURED_OUTPUT="$(bin)${CONSTITUENT}
else
${CONSTITUENT}dirs : ;
endif

ifdef cmt_${CONSTITUENT}_has_target_tag

ifndef QUICK
$(cmt_local_${CONSTITUENT}_makefile) : $(${CONSTITUENT}_dependencies) build_library_links
	$(echo) "(constituents.make) Building ${CONSTITUENT}.make"; \
	  $(cmtexe) -tag=$(tags) $(${CONSTITUENT}_extratags) build constituent_config -out=$(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}
else
$(cmt_local_${CONSTITUENT}_makefile) : $(${CONSTITUENT}_dependencies) $(cmt_build_library_linksstamp) $(use_requirements)
	@if [ ! -f $@ ] || [ ! -f $(cmt_local_tagfile_${CONSTITUENT}) ] || \
	  [ ! -f $(cmt_final_setup_${CONSTITUENT}) ] || \
	  $(not_${CONSTITUENT}_dependencies) ; then \
	  test -z "$(cmtmsg)" || \
	  echo "$(CMTMSGPREFIX)" "(constituents.make) Building ${CONSTITUENT}.make"; \
	  $(cmtexe) -tag=$(tags) $(${CONSTITUENT}_extratags) build constituent_config -out=$(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}; \
	  fi
endif

else

ifndef QUICK
$(cmt_local_${CONSTITUENT}_makefile) : $(${CONSTITUENT}_dependencies) build_library_links
	$(echo) "(constituents.make) Building ${CONSTITUENT}.make"; \
	  $(cmtexe) -f=$(bin)${CONSTITUENT}.in -tag=$(tags) $(${CONSTITUENT}_extratags) build constituent_makefile -without_cmt -out=$(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}
else
$(cmt_local_${CONSTITUENT}_makefile) : $(${CONSTITUENT}_dependencies) $(cmt_build_library_linksstamp) $(bin)${CONSTITUENT}.in
	@if [ ! -f $@ ] || [ ! -f $(cmt_local_tagfile_${CONSTITUENT}) ] || \
	  [ ! -f $(cmt_final_setup_${CONSTITUENT}) ] || \
	  $(not_${CONSTITUENT}_dependencies) ; then \
	  test -z "$(cmtmsg)" || \
	  echo "$(CMTMSGPREFIX)" "(constituents.make) Building ${CONSTITUENT}.make"; \
	  $(cmtexe) -f=$(bin)${CONSTITUENT}.in -tag=$(tags) $(${CONSTITUENT}_extratags) build constituent_makefile -without_cmt -out=$(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}; \
	  fi
endif

endif

#	  $(cmtexe) -tag=$(tags) $(${CONSTITUENT}_extratags) build constituent_makefile -out=$(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}

${CONSTITUENT} :: $(${CONSTITUENT}_dependencies) $(cmt_local_${CONSTITUENT}_makefile) dirs ${CONSTITUENT}dirs
	$(echo) "(constituents.make) Creating ${CONSTITUENT}${lock_suffix} and Starting ${CONSTITUENT}"
	@${lock_command} ${CONSTITUENT}${lock_suffix} || exit $$?; \
	  retval=$$?; \
	  trap '${unlock_command} ${CONSTITUENT}${lock_suffix}; exit $${retval}' 1 2 15; \
	  if test -f $(cmt_local_${CONSTITUENT}_makefile); then \
	  $(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}; \
	  fi; \
	  retval=$$?; ${unlock_command} ${CONSTITUENT}${lock_suffix}; exit $${retval}
	$(echo) "(constituents.make) ${CONSTITUENT} done"

clean :: ${CONSTITUENT}clean

${CONSTITUENT}clean :: $(${CONSTITUENT}clean_dependencies) ##$(cmt_local_${CONSTITUENT}_makefile)
	$(echo) "(constituents.make) Starting ${CONSTITUENT}clean"
	@-if test -f $(cmt_local_${CONSTITUENT}_makefile); then \
	  $(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}clean; \
	fi
	$(echo) "(constituents.make) ${CONSTITUENT}clean done"
#	@-$(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) ${CONSTITUENT}clean

##	  /bin/rm -f $(cmt_local_${CONSTITUENT}_makefile) $(bin)${CONSTITUENT}_dependencies.make

install :: ${CONSTITUENT}install ;

${CONSTITUENT}install :: $(${CONSTITUENT}_dependencies) $(cmt_local_${CONSTITUENT}_makefile)
	$(echo) "(constituents.make) Starting $@"
	@if test -f $(cmt_local_${CONSTITUENT}_makefile); then \
	  $(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) install; \
	  fi
#	@-$(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) install
	$(echo) "(constituents.make) $@ done"

uninstall : ${CONSTITUENT}uninstall

$(foreach d,$(${CONSTITUENT}_dependencies),$(eval $(d)uninstall_dependencies += ${CONSTITUENT}uninstall))

${CONSTITUENT}uninstall : $(${CONSTITUENT}uninstall_dependencies) ##$(cmt_local_${CONSTITUENT}_makefile)
	$(echo) "(constituents.make) Starting $@"
	@-if test -f $(cmt_local_${CONSTITUENT}_makefile); then \
	  $(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) uninstall; \
	  fi
#	@$(MAKE) -f $(cmt_local_${CONSTITUENT}_makefile) uninstall
	$(echo) "(constituents.make) $@ done"

remove_library_links :: ${CONSTITUENT}uninstall ;

ifndef PEDANTIC
.DEFAULT::
	$(echo) "(constituents.make) Starting $@ ${CONSTITUENT}"
	$(echo) Using default action for $@
	$(echo) "(constituents.make) $@ ${CONSTITUENT} done"
endif

#-- end of constituent_lock ------
