SHELL := /bin/bash
RUNDIR := $(CURDIR)
ifndef TOP
TOP := $(shell \
       top=$(RUNDIR); \
       while [ ! -r "$$top/Rules.top" ] && [ "$$top" != "" ]; do \
           top=$${top%/*}; \
       done; \
       echo $$top)
endif

MK := $(TOP)/mk

.PHONY: dir tree all clean clean_dir clean_all clean_tree dist_clean pbd top

# Default target when nothing is given on the command line.  Reasonable
# options are:
# "dir"  - updates only targets from current directory and its dependencies
# "tree" - updates targets (and their dependencies) in whole subtree
#          starting at current directory
# "all"  - updates all targets in the project
.DEFAULT_GOAL := tree


dir : dir_$(RUNDIR)
tree : tree_$(RUNDIR)

clean : clean_$(.DEFAULT_GOAL)
clean_dir : clean_dir_$(RUNDIR)
clean_tree : clean_tree_$(RUNDIR)

# $(d) keeps the path of "current" directory during tree traversal and
# $(dir_stack) is used for backtracking during traversal
d := $(TOP)
dir_stack :=

include $(MK)/header.mk
include $(MK)/footer.mk

# Automatic inclusion of the skel.mk at the top level - that way
# Rules.top has exactly the same structure as other Rules.mk
include $(MK)/skel.mk

.SECONDEXPANSION:
$(eval $(value HEADER))
include $(TOP)/Rules.top
$(eval $(value FOOTER))

# Outputs the current build directory
pbd:
	@echo $(call real_to_build_dir,$(RUNDIR))

# Outputs the top build directory
top:
	@echo $(TOP)

# Marks specified output as precious
.PRECIOUS: $(call get_subtree,PRECIOUS,$(TOP))

# Optional final makefile where you can specify additional targets
-include $(TOP)/final.mk

# This is just a convenience - to let you know when make has stopped
# interpreting make files and started their execution.
$(info Rules generated $(if $(BUILD_MODE),for "$(BUILD_MODE)" mode,)...)
