# Meta makefile rules.

# Delete the default suffixes
.SUFFIXES:

# Define macro to strip characters preceding '-' in the target variable ($*)
strip-target = `echo $@ | sed 's/^[^-]*-//'`

ifeq (,$(CTPROJROOT))
CTPROJROOT := $(shell $(DTOOL)/bin/ctproj -root)
endif
export CTPROJROOT 

# Make sure that CT_INCLUDE_PATH is set.
ifeq (,$(CT_INCLUDE_PATH))
export CT_INCLUDE_PATH = .
endif

# Make sure CT_LIBRARY_PATH is set.
ifeq (,$(CT_LIBRARY_PATH))
ifneq (,$(LD_LIBRARY_PATH))
export CT_LIBRARY_PATH := $(LD_LIBRARY_PATH)
else
export CT_LIBRARY_PATH = .
endif
endif

# export the OPTIMIZE variable
ifneq (,$(OPTIMIZE))
export OPTIMIZE
endif

ifeq (,$(INSTALL))
INSTALL := $(SUBMAKES)
endif

all: $(SUBMAKES)

install: $(INSTALL)

ifeq (yes,$(HAVE_ATRIA))
realinstall:
	@cd `ctproj -r` ; clearmake -C gnu install-`ctproj -p`
else
realinstall:
	@proj=`ctproj -p` ; cd `ctproj -r` ; $(MAKE) install-$$proj
endif

ifneq (,$(SUBMAKES))
.PHONY: $(SUBMAKES)
.PHONY: $(addprefix egg-,$(SUBMAKES))
.PHONY: $(addprefix unopt-,$(SUBMAKES))
.PHONY: $(addprefix pfb-,$(SUBMAKES))
endif

ifneq (,$(NUMBER_OF_PROCESSORS))
# on NT, parallelize using NUMBER_OF_PROCESSORS env-var
PARALLEL_MAKE = -j $(NUMBER_OF_PROCESSORS)
endif

$(SUBMAKES):
	@echo '***' Executing $(MAKE) for sub-target $@...
	@echo $(MAKE) $(PARALLEL_MAKE) -f $(MAKEDIR)/Makefile.$@
	@$(MAKE) $(PARALLEL_MAKE) -f $(MAKEDIR)/Makefile.$@

clean: $(addprefix clean-,$(SUBMAKES))

$(addprefix clean-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) clean for sub-target $(strip-target)...
	@$(MAKE) $(PARALLEL_MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) clean
	@rm -f *~

cleanall: $(addprefix cleanall-,$(SUBMAKES))

$(addprefix cleanall-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) cleanall for sub-target $(strip-target)...
	@$(MAKE) $(PARALLEL_MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) cleanall

list:
	@echo $(SUBMAKES)

# These are used in src/all/model subdirectories.

egg: $(addprefix egg-,$(SUBMAKES))
$(addprefix egg-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) egg for sub-target $(strip-target)...
	@$(MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) egg

unopt: $(addprefix unopt-,$(SUBMAKES))
$(addprefix unopt-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) unopt for sub-target $(strip-target)...
	@$(MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) unopt

pfb: $(addprefix pfb-,$(SUBMAKES))
$(addprefix pfb-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) pfb for sub-target $(strip-target)...
	@$(MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) pfb

uninstall: $(addprefix uninstall-,$(SUBMAKES))
$(addprefix uninstall-,$(SUBMAKES)):
	@echo '***' Executing $(MAKE) uninstall for sub-target $(strip-target)...
	@$(MAKE) -f $(MAKEDIR)/Makefile.$(strip-target) uninstall

