Index: trunk/kBuild/footer.kmk
===================================================================
--- trunk/kBuild/footer.kmk	(revision 216)
+++ trunk/kBuild/footer.kmk	(revision 219)
@@ -51,10 +51,10 @@
 
 # all of a type
-_OBJS   :=
-_LIBS   :=
-_DLLS   :=
-_EXES   :=
-_SYSMODS:=
-_DIRS   :=
+_OBJS     :=
+_LIBS     :=
+_DLLS     :=
+_PROGRAMS :=
+_SYSMODS  :=
+_DIRS     :=
 _IMPORT_LIBS :=
 
@@ -632,7 +632,7 @@
 
 $$(basename $$(notdir $$(obj))).o::
-	$$(MAKE) -B -f $$(MAKEFILE) $(obj)
+	+ $$(MAKE) -B -f $$(MAKEFILE) $(obj)
 $$(basename $$(notdir $$(obj))).obj::
-	$$(MAKE) -B -f $$(MAKEFILE) $(obj)
+	+ $$(MAKE) -B -f $$(MAKEFILE) $(obj)
 
 $(eval _OBJS_$(target) += $(obj))
@@ -657,4 +657,156 @@
 	,$(eval $(def_target_source_c_cpp_asm)) ))
 endef
+
+
+#
+# NEEDED
+#
+
+## Needed (one).
+# @param    $(target)   Normalized target (program) name.
+define def_needed
+# basics
+$(eval tool        := $(call _TARGET_TOOL,$(target),LD))
+$(eval outbase     := $(call _TARGET_BASE,$(target),$(target)))
+$(eval PATH_$(target) := $(patsubst %/,%,$(dir $(outbase))))
+$(eval exesuff     := $(firstword $($(target)_EXESUFF) $($(target)_EXESUFF) $(TOOL_$(tool)_LDEXESUFF) $(HOSTSUFF_EXE)))
+$(eval exe         := $(outbase)$(exesuff))
+$(eval TARGET_$(target) := $(exe))
+
+# source -> object
+$(eval $(def_target_sources))
+
+# more library stuff.
+$(eval tool        := $(call _TARGET_TOOL,$(target),LD))
+$(eval outbase     := $(call _TARGET_BASE,$(target),$(target)))
+$(eval objs         = $(_OBJS_$(target)))
+$(eval flags       :=\
+	$(TOOL_$(tool)_LDFLAGS)\
+	$(TOOL_$(tool)_LDFLAGS.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_LDFLAGS.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_LDFLAGS.$(BUILD_TARGET_ARCH))\
+	$(TOOL_$(tool)_LDFLAGS.$(BUILD_TARGET_CPU))\
+	$(LDFLAGS)\
+	$(LDFLAGS.$(BUILD_TYPE))\
+	$(LDFLAGS.$(BUILD_TARGET))\
+	$(LDFLAGS.$(BUILD_TARGET_ARCH))\
+	$(LDFLAGS.$(BUILD_TARGET_CPU))\
+	$($(target)_LDFLAGS)\
+	$($(target)_LDFLAGS.$(BUILD_TYPE))\
+	$($(target)_LDFLAGS.$(BUILD_TARGET))\
+	$($(target)_LDFLAGS.$(BUILD_TARGET_ARCH))\
+	$($(target)_LDFLAGS.$(BUILD_TARGET_CPU))\
+)
+$(eval libs        :=\
+	$($(target)_LIBS.$(BUILD_TARGET_CPU))\
+	$($(target)_LIBS.$(BUILD_TARGET_ARCH))\
+	$($(target)_LIBS.$(BUILD_TARGET))\
+	$($(target)_LIBS.$(BUILD_TYPE))\
+	$($(target)_LIBS)\
+	$(foreach sdk, $($(target)_SDKS.$(BUILD_TARGET)) \
+				   $($(target)_SDKS.$(BUILD_TYPE)) \
+				   $($(target)_SDKS),\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET_CPU))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET_ARCH))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TYPE))\
+	$(SDK_$(sdk)_LIBS))\
+	$(LIBS.$(BUILD_TARGET_CPU))\
+	$(LIBS.$(BUILD_TARGET_ARCH))\
+	$(LIBS.$(BUILD_TARGET))\
+	$(LIBS.$(BUILD_TYPE))\
+	$(LIBS)\
+	$(foreach sdk, $(SDKS.$(BUILD_TARGET)) \
+				   $(SDKS.$(BUILD_TYPE)) \
+				   $(SDKS),\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET_CPU))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET_ARCH))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TARGET))\
+	$(SDK_$(sdk)_LIBS.$(BUILD_TYPE))\
+	$(SDK_$(sdk)_LIBS))\
+	$(TOOL_$(tool)_LIBS.$(BUILD_TARGET_CPU))\
+	$(TOOL_$(tool)_LIBS.$(BUILD_TARGET_ARCH))\
+	$(TOOL_$(tool)_LIBS.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_LIBS.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_LIBS)\
+)
+$(eval libpath    :=\
+	$($(target)_LIBPATH.$(BUILD_TARGET_CPU))\
+	$($(target)_LIBPATH.$(BUILD_TARGET_ARCH))\
+	$($(target)_LIBPATH.$(BUILD_TARGET))\
+	$($(target)_LIBPATH.$(BUILD_TYPE))\
+	$($(target)_LIBPATH)\
+	$(foreach sdk, $($(target)_SDKS.$(BUILD_TARGET)) \
+				   $($(target)_SDKS.$(BUILD_TYPE)) \
+				   $($(target)_SDKS),\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET_CPU))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET_ARCH))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TYPE))\
+	$(SDK_$(sdk)_LIBPATH))\
+	$(LIBPATH.$(BUILD_TARGET_CPU))\
+	$(LIBPATH.$(BUILD_TARGET_ARCH))\
+	$(LIBPATH.$(BUILD_TARGET))\
+	$(LIBPATH.$(BUILD_TYPE))\
+	$(LIBPATH)\
+	$(foreach sdk, $(SDKS.$(BUILD_TARGET)) \
+				   $(SDKS.$(BUILD_TYPE)) \
+				   $(SDKS),\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET_CPU))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET_ARCH))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TARGET))\
+	$(SDK_$(sdk)_LIBPATH.$(BUILD_TYPE))\
+	$(SDK_$(sdk)_LIBPATH))\
+	$(TOOL_$(tool)_LIBPATH.$(BUILD_TARGET_CPU))\
+	$(TOOL_$(tool)_LIBPATH.$(BUILD_TARGET_ARCH))\
+	$(TOOL_$(tool)_LIBPATH.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_LIBPATH.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_LIBPATH)\
+)
+$(eval dirdep      := $(dir $(exe)).dir_created)
+## @todo fix dependencies
+$(eval deps        := $($(target)_DEPS))
+#
+$(eval custom_pre    := $(strip $(firstword
+	$($(target)_CUSTOM_PRE.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$($(target)_CUSTOM_PRE.$(BUILD_TARGET))\
+	$($(target)_CUSTOM_PRE.$(BUILD_TYPE))\
+	$($(target)_CUSTOM_PRE)\
+	$(CUSTOM_PRE.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$(CUSTOM_PRE.$(BUILD_TARGET))\
+	$(CUSTOM_PRE.$(BUILD_TYPE))\
+	$(CUSTOM_PRE)\
+)))
+$(eval custom_post    := $(strip $(firstword
+	$($(target)_CUSTOM_POST.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$($(target)_CUSTOM_POST.$(BUILD_TARGET))\
+	$($(target)_CUSTOM_POST.$(BUILD_TYPE))\
+	$($(target)_CUSTOM_POST)\
+	$(CUSTOM_POST.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$(CUSTOM_POST.$(BUILD_TARGET))\
+	$(CUSTOM_POST.$(BUILD_TYPE))\
+	$(CUSTOM_POST)\
+)))
+$(eval othersrc    := $(filter-out %.c %.cpp %.cxx %.cc %.s %.S %.asm,$($(target)_SOURCES) $($(target)_SOURCES.$(BUILD_TARGET)) $($(target)_SOURCES.$(BUILD_MODE))))
+
+$(eval $(TOOL_$(tool)_LINK_PROGRAM))
+# publish rule (still need work)
+ifndef $(target)_INST
+$(eval pubexe := $(PATH_BIN)/$(notdir $(exe)))
+$(pubexe) : $(exe) $(dir $(pubexe)).dir_created
+	$(CP) $(exe) $(pubexe)
+
+_NEEDED    += $(pubexe)
+_OUT_FILES += $(exe) $(pubexe)
+else
+_NEEDED    += $(exe)
+_OUT_FILES += $(exe)
+endif
+_OBJS += $(_OBJS_$(target))
+endef
+
+# Process needed.
+$(foreach target, $(NEEDED) $(NEEDED.$(BUILD_TARGET)), $(eval $(def_needed)))
+
 
 
@@ -897,5 +1049,5 @@
 # Process import libraries.
 #
-#	- On OS/2 and windows these are libraries.
+#   - On OS/2 and windows these are libraries.
 #   - On other platforms they are fake DLLs.
 ifeq ($(subst win32,os2,$(BUILD_TARGET)),os2)
@@ -906,4 +1058,5 @@
 $(foreach target, $(IMPORT_LIBS) $(IMPORT_LIBS.$(BUILD_TARGET)), $(eval _IMPORT_LIBS += $(firstword $(TARGET_PUB_$(target)) $(TARGET_$(target)))))
 
+    
 
 #
@@ -1045,8 +1198,8 @@
 	$(CP) $(exe) $(pubexe)
 
-_DLLS += $(pubexe)
+_PROGRAMS  += $(pubexe)
 _OUT_FILES += $(exe) $(pubexe)
 else
-_DLLS += $(exe)
+_PROGRAMS  += $(exe)
 _OUT_FILES += $(exe)
 endif
@@ -1244,5 +1397,5 @@
 define def_createdir
 $(dirfile):
-	$(call MSG_Ltarget,Creating directory $$(@D))
+	$(call MSG_L1,Creating directory $$(@D))
 	$(MKDIR) -p $$(@D)
 	@echo dir_created > $$@
@@ -1276,6 +1429,6 @@
 # @param	$(tag)		tag to attach to the rule name.
 define def_pass_subdir
-pass_$(pass)$(tag)::
-	$(QUIET)$$(MAKE) -C $(subdir) -f $$(notdir $$(firstword $$(wildcard $$(addprefix $(subdir)/,$$(DEFAULT_MAKEFILE))))) pass_$(pass)
+pass_$(pass)$(tag):: $(dep)
+	+ $(QUIET)$$(MAKE) -C $(subdir) -f $$(notdir $$(firstword $$(wildcard $$(addprefix $(subdir)/,$$(DEFAULT_MAKEFILE))))) pass_$(pass)
 endef
 
@@ -1286,6 +1439,6 @@
 # @param	$(tag)		tag to attach to the rule name.
 define def_pass_makefile
-pass_$(pass)$(tag)::
-	$(QUIET)$$(MAKE) -C $(patsubst %/,%,$(dir $(makefile))) -f $(notdir $(makefile)) pass_$(pass)
+pass_$(pass)$(tag):: $(dep)
+	+ $(QUIET)$$(MAKE) -C $(patsubst %/,%,$(dir $(makefile))) -f $(notdir $(makefile)) pass_$(pass)
 endef
 
@@ -1301,8 +1454,10 @@
 
 $(eval tag:=_before)
+$(eval dep:=)
 $(foreach subdir,$(SUBDIRS_$(PASS))            $(SUBDIRS_$(PASS).$(BUILD_TARGET))         ,$(eval $(def_pass_subdir)))
 $(foreach makefile,$(MAKEFILES_BEFORE_$(PASS)) $(MAKEFILES_BEFORE_$(PASS).$(BUILD_TARGET)),$(eval $(def_pass_makefile)))
 
 $(eval tag:=_after)
+$(eval dep:=pass_$(pass)_doit)
 $(foreach subdir,$(SUBDIRS_AFTER_$(PASS))      $(SUBDIRS_AFTER_$(PASS).$(BUILD_TARGET))   ,$(eval $(def_pass_subdir)))
 $(foreach makefile,$(MAKEFILES_AFTER_$(PASS))  $(MAKEFILES_AFTER_$(PASS).$(BUILD_TARGET)) ,$(eval $(def_pass_makefile)))
@@ -1310,8 +1465,7 @@
 .NOTPARALLEL: pass_$(pass)_before pass_$(pass)_after
 .PHONY: pass_$(pass) pass_$(pass)_before pass_$(pass)_doit pass_$(pass)_after
-pass_$(pass): \
-	pass_$(pass)_before \
-	pass_$(pass)_doit \
-	pass_$(pass)_after
+pass_$(pass)_doit:      pass_$(pass)_before
+pass_$(pass)_after::    pass_$(pass)_doit
+pass_$(pass):           pass_$(pass)_after
 
 endef
@@ -1341,5 +1495,5 @@
 ## PASS: binaries
 # This pass builds binary targets, i.e. programs, system modules and stuff.
-pass_binaries_doit: $(_EXES) $(_SYSMODS) $(_OTHER_BINARIES)
+pass_binaries_doit: $(_PROGRAMS) $(_SYSMODS) $(_OTHER_BINARIES)
 pass := binaries
 PASS := BINARIES
@@ -1386,5 +1540,5 @@
 # This pass just walks the tree.
 pass_nothing_doit:
-	$(call MSG_Ltarget,Did nothing in $(CURDIR))
+	$(call MSG_L1,Did nothing in $(CURDIR))
 pass := nothing
 PASS := NOTHING
@@ -1393,12 +1547,28 @@
 
 
-
+## Pass order
+# @param	$(pass)		Current pass name.
+# @param	$(prev_pass)	The previous pass name.
+define def_pass_order
+pass_$(pass)_order: $(pass_prev)
+	$(call MSG_L1,Pass $(pass))
+	+ $(QUIET)$$(MAKE) -f $$(MAKEFILE) pass_$(pass)
+.NOTPARALLEL: pass_$(pass)_order
+$(eval pass_prev := pass_$(pass)_order)
+endef
+
+## PASS: order
+# Use dependencies to ensure correct pass order.
+pass_prev :=
+$(foreach pass,$(PASSES),$(eval $(def_pass_order)))
+
+     
 #
 # THE MAIN RULES
 #
-all_recursive: $(foreach pass,$(PASSES),pass_$(pass))
+all_recursive: $(pass_prev)
 
 rebuild: clean
-	$(MAKE) -f $(firstword $(MAKEFILE_LIST)) all_recursive
+	+ $(MAKE) -f $(firstword $(MAKEFILE_LIST)) all_recursive
 
 
@@ -1408,5 +1578,5 @@
 libraries: $(_LIBS) $(_IMPORT_LIBS) $(_OTHER_LIBRARIES)
 dlls:      $(_DLLS)
-programs:  $(_EXES)
+programs:  $(_PROGRAMS)
 sysmods:   $(_SYSMODS)
 
