# GLOBAL Makefile for all packages
#
# DO NOT EDIT -- Automatically generated by Adam's configuration system!!!
#

IGNORE_OBJDIR_TARGETS := up update diff

L4DIR ?= ..
ifneq ($(S),)
  ALL_SUBDIRS := $(shell echo $(S) | tr ':,' ' ')
endif

all::

include $(L4DIR)/mk/Makeconf

ifneq ($(OBJ_BASE),)
include $(L4DIR)/mk/project.mk
PKGDEPS_IGNORE_MISSING :=
endif

#####################
# Rules begin.

#####################
# default make action is "all" - build all packages
#
# We build all l4env headers (idl+includes), then all l4env libs, then the
# l4env servers and examples. Then we build the other headers
# (idl+includes), the other libs, the other servers and examples. We use
# dependencies for this, witch might allow parallel build processes.
#

examples: $(filter-out examples,$(BUILD_SUBDIRS))

.PHONY: all ptest
.PHONY: rm-old-links

PURGE_INC_MESSAGE =echo "=== Purging pending links in $(L4DIR)/include ==="
PURGE_LIB_MESSAGE =echo "=== Purging pending links in $(L4DIR)/lib ==="
UPDATE_MESSAGE    =echo -e $(EMPHSTART)"=== Updating \"$(1)\" ==="$(EMPHSTOP)
DIFF_MESSAGE      =echo -e $(EMPHSTART)"=== Diffing \"$(1)\" ==="$(EMPHSTOP)
PTEST_MESSAGE     =echo "=== Testing Package \"$(1)\" ==="

# Check package dependencies.
#   CHECK_PKGS: if undefined, check all packages,
#               otherwise gives a list of space separated packages to check
#   CHECK_FULL: if set rebuild for every new package check
#                 (ccache recommended when using this flag)
depcheck:
	$(VERBOSE)set -e; \
	$(RM) -r $(filter-out $(OBJ_BASE)/pkg/Makefile,       \
	                      $(wildcard $(OBJ_BASE)/pkg/*)); \
	for checkpkg in $(filter-out $(CHECK_GOOD),$(if $(CHECK_PKGS),$(CHECK_PKGS),$(BUILD_SUBDIRS))); do \
	  echo ==========================================================;     \
	  echo "Deleting all pkgs in build-dir";                               \
	  $(RM) -r $(OBJ_BASE)/lib $(OBJ_BASE)/bin $(OBJ_BASE)/include         \
	           $(OBJ_BASE)/pc $(OBJ_BASE)/pkg/.Package.deps                \
	           $(OBJ_BASE)/pkg/.Package.deps.pkgs;                         \
	  $(if $(CHECK_FULL),$(RM) -r $(OBJ_BASE)/pkg;,)                       \
	  echo "Done";                                                         \
	  echo ----------------------------------------------------------;     \
	  echo "Trying standalone build of '$$checkpkg'";                      \
	  $(MAKE) -C $(L4DIR) O=$(OBJ_BASE)                                    \
	    --no-print-directory syncconfig;                                   \
	  echo $(MAKE) O=$(OBJ_BASE) --no-print-directory $$checkpkg;          \
	  $(MAKE) O=$(OBJ_BASE) --no-print-directory $$checkpkg                \
	    || (echo "Failed package: $$checkpkg";                             \
	        echo "Good package(s): CHECK_GOOD=\"$(CHECK_GOOD) $$PKGDONE\"";\
	        exit 1) || exit 1;                                             \
	  PKGDONE="$$PKGDONE $$checkpkg";                                      \
	done;                                                                  \
	echo "Packages     tested: $(CHECK_GOOD) $$PKGDONE"

depgraph: depsrcgraph depobjgraph
depsrcgraph: $(PKGDIR_OBJ)/depsrc.svg $(PKGDIR_OBJ)/depsrc.ps $(PKGDIR_OBJ)/depsrc.pdf $(PKGDIR_OBJ)/depsrc.reduced.svg $(PKGDIR_OBJ)/depsrc.reduced.ps
depobjgraph: $(PKGDIR_OBJ)/depobj.svg $(PKGDIR_OBJ)/depobj.ps $(PKGDIR_OBJ)/depobj.pdf $(PKGDIR_OBJ)/depobj.reduced.svg $(PKGDIR_OBJ)/depobj.reduced.ps

%.ps: %.dot
	@$(GEN_MESSAGE)
	$(VERBOSE)dot -Tps -o $@ $<

%.pdf: %.dot
	@$(GEN_MESSAGE)
	$(VERBOSE)dot -Tpdf -o $@ $<

%.svg: %.dot
	@$(GEN_MESSAGE)
	$(VERBOSE)dot -Tsvg -o $@ $<

%.reduced.dot: %.dot
	@$(GEN_MESSAGE)
	$(VERBOSE)tred $< > $@

$(PKGDIR_OBJ)/depsrc.dot: FORCE 
	@$(GEN_MESSAGE)
	$(VERBOSE)$(PKGDEPS_CMD) dot $(L4DIR)/pkg > $@

$(PKGDIR_OBJ)/depobj.dot: FORCE
	@$(GEN_MESSAGE)
	$(VERBOSE)$(L4DIR)/tool/bin/pkgdepsobj $(L4DIR_ABS) $(OBJ_BASE) $@

ptest:
	$(VERBOSE)set -e; \
	for d in $(BUILD_SUBDIRS); do \
	  if [ -n "$$(find $$d -type d -name $@)" ]; then \
	    $(call PTEST_MESSAGE,$(BID_DOLLARQUOTE)$$d); \
	    PWD=$(PWD)/$$d $(MAKE) -C $$d $@; \
	  fi; \
	done

TAGS:
	$(ETAGS)

tags:
	$(CTAGS)

.PHONY: TAGS tags depsrcgraph depobjgraph depgraph depcheck
.PHONY: $(foreach ext, .bin .lib .include .idl .headers,addsuffix $(ext), $(BUILD_SUBDIRS))

clean cleanall::
	$(VERBOSE)$(MAKE) rm-old-links

del-backup:
	$(VERBOSE)rm -i `find . -name "*~"` `find . -name "#*#"`

rm-old-links:
	@$(PURGE_INC_MESSAGE)
	$(VERBOSE)test ! -d $(L4DIR)/include || \
	  find $(L4DIR)/include -type l -a ! -xtype f -a ! -xtype d \
	    -exec rm $(if $(VERBOSE),,-v) '{}' ';'
	@$(PURGE_LIB_MESSAGE)
	$(VERBOSE)test ! -d $(L4DIR)/lib || \
	  find $(L4DIR)/lib -type l -a ! -xtype f -a ! -xtype d \
	    -exec rm $(if $(VERBOSE),,-v) '{}' ';'


# Update Makefile on update.
ifneq ($(filter update up,$(MAKECMDGOALS)),)
.PHONY: Makefile
Makefile: update.log
	$(SVN) up $@ 2>&1 | tee -a $(UPDATE_LOG)
endif

UPDATE_LOG = $(call absfilename, $(L4DIR)/pkg/update.log)
.PHONY: update.log
update.log:
	$(VERBOSE)$(RM) $(UPDATE_LOG)

update up: update.log
	$(VERBOSE)$(call UPDATE_MESSAGE,Packages)
	$(VERBOSE)set -e; \
	  $(SVN) update $(ALL_SUBDIRS) 2>&1 | tee -a $(UPDATE_LOG)

# global defines

.PHONY: kernels

help::
	@echo "Specify one of the following targets:"
	@echo "del-backup      - remove temporary file in this tree"
	@echo 'rm-old-links    - remove invalid symlinks in $$(L4DIR)/lib, $$(L4DIR)/include'
	@echo "update          - update the packages currently checked out"
	@echo "help            - this help"

