Index: /trunk/kBuild/doc/QuickReference-kmk.txt
===================================================================
--- /trunk/kBuild/doc/QuickReference-kmk.txt	(revision 2474)
+++ /trunk/kBuild/doc/QuickReference-kmk.txt	(revision 2475)
@@ -610,4 +610,9 @@
         $(root names...)
 
+    Extract the non-root part of each file name (a bit complicated on
+    Windows & OS/2) [1]_::
+
+        $(notroot names...)
+
     Append ``suffix`` to each word in ``names``::
 
@@ -830,5 +835,5 @@
     A combination of ``$(eval )`` and ``$(call )`` [1]_::
 
-        $(evalcall var)
+        $(evalcall2 var)
 
     Remove comments and blank lines from the variable ``var``. Expands to
Index: /trunk/kBuild/footer.kmk
===================================================================
--- /trunk/kBuild/footer.kmk	(revision 2474)
+++ /trunk/kBuild/footer.kmk	(revision 2475)
@@ -121,4 +121,6 @@
 _SYSMODS  :=
 _MISCBINS :=
+_STAGE_FILES :=
+_STAGE_DIRS :=
 _INSTALLS :=
 _INSTALLS_FILES :=
@@ -1281,34 +1283,80 @@
 local out := $(outbase)$(suff)
 
-# TARGET_*
+# Object directory target variable.
 $(target)_1_TARGET := $(out)
 $(call KB_FN_ASSIGN_DEPRECATED,TARGET_$(target),$($(target)_1_TARGET), $(target)_1_TARGET)
 
-# INSTARGET_*
-ifeq ($(strip $($(target)_NOINST) $($(target)_NOINST.$(bld_trg)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch).$(bld_type))  $($(target)_NOINST.$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg_cpu)) $($(target)_NOINST.$(bld_type))),)
- ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg)))
- else ifneq ($(strip $($(target)_INST.$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_type)))
- else ifneq ($(strip $($(target)_INST)),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST))
+# Staging and install directory target variables.
+local insttype := $(firstword \
+	$($(target)_INSTTYPE) \
+	$($(target)_INSTTYPE.$(bld_trg)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INSTTYPE.$(bld_trg_arch)) \
+	$($(target)_INSTTYPE.$(bld_trg_cpu)) \
+	$($(target)_INSTTYPE.$(bld_type)) \
+	)
+ifeq ($(insttype),)
+ ifneq ($(firstword \
+	$($(target)_NOINST) \
+	$($(target)_NOINST.$(bld_trg)) \
+	$($(target)_NOINST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_NOINST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_NOINST.$(bld_trg_arch)) \
+	$($(target)_NOINST.$(bld_trg_cpu)) \
+	$($(target)_NOINST.$(bld_type)) ),)
+   local insttype := none
  else
-  local inst := $(definst)/
- endif
- INSTARGET_$(target) := $(patsubst %/,%/$(notdir $(out)),$(inst))
-
-else # NOINST
- INSTARGET_$(target) :=
-endif # NOINST
-
-endef
+   local insttype := both
+ endif
+endif
+ifn1of ($(insttype), none both stage)
+ $(error kBuild: Unknown value '$(insttype)' for '$(target)_INSTTYPE'. Valid values are 'none', 'both' and 'stage'.)
+endif
+$(target)_1_INSTTYPE := $(insttype)
+
+if1of ($(insttype), stage both)
+ local stage := $(strip $(firstword \
+	$($(target)_STAGE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_STAGE.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_STAGE.$(bld_trg).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_type)) \
+	$($(target)_STAGE.$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg_arch)) \
+	$($(target)_STAGE.$(bld_trg)) \
+	$($(target)_INST.$(bld_trg)) \
+	$($(target)_STAGE.$(bld_type)) \
+	$($(target)_INST.$(bld_type)) \
+	$($(target)_STAGE) \
+	$($(target)_INST) \
+	$(definst) ))
+ $(target)_1_STAGE := $(stage)
+ $(target)_1_STAGE_TARGET := $(PATH_STAGE)/$(stage)$(notdir $(out))
+else
+ $(target)_1_STAGE :=
+ $(target)_1_STAGE_TARGET :=
+endif
+INSTARGET_$(target) := $($(target)_1_STAGE_TARGET)
+
+if1of ($(insttype), both)
+ local inst := $(strip $(firstword \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg).$(bld_type)) \
+	$($(target)_INST.$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg)) \
+	$($(target)_INST.$(bld_type)) \
+	$($(target)_INST) \
+	$(definst) ))
+ $(target)_1_INST := $(inst)
+ $(target)_1_INST_TARGET := $(PATH_INS)/$(inst)$(notdir $(out))
+else
+ $(target)_1_INST :=
+ $(target)_1_INST_TARGET :=
+endif
+
+endef # def_pass1_link_common
 $(eval-opt-var def_pass1_link_common)
 
@@ -1320,5 +1368,5 @@
 # set NOINST if not forced installation before doing the usual stuff.
 ifndef $(target)_INST
-$(target)_NOINST := 1
+$(target)_INSTTYPE := none
 endif
 $(evalval def_pass1_link_common)
@@ -1327,5 +1375,5 @@
 EXT     := EXE
 EXTPRE  := HOST
-definst := $(PATH_BIN)
+definst := $(INST_BIN)
 tool_prefix := LD
 bld_trg_base_var := PLATFORM
@@ -1339,5 +1387,5 @@
 EXT     := LIB
 EXTPRE  :=
-definst := $(PATH_LIB)
+definst := $(INST_LIB)
 tool_prefix := AR
 bld_trg_base_var := TARGET
@@ -1351,5 +1399,5 @@
 EXT     := DLL
 EXTPRE  :=
-definst := $(PATH_DLL)
+definst := $(INST_DLL)
 tool_prefix := LD
 bld_trg_base_var := TARGET
@@ -1367,5 +1415,5 @@
  EXT     := LIB
  EXTPRE  :=
- definst := $(PATH_LIB)
+ definst := $(INST_LIB)
  tool_prefix := AR
  bld_trg_base_var := TARGET
@@ -1375,5 +1423,5 @@
  EXT     := DLL
  EXTPRE  :=
- definst := $(PATH_DLL)
+ definst := $(INST_DLL)
  tool_prefix := LD
  bld_trg_base_var := TARGET
@@ -1388,5 +1436,5 @@
 EXT     := EXE
 EXTPRE  :=
-definst := $(PATH_BIN)
+definst := $(INST_BIN)
 tool_prefix := LD
 bld_trg_base_var := TARGET
@@ -1400,5 +1448,5 @@
 EXT     := SYS
 EXTPRE  :=
-definst := $(PATH_SYS)
+definst := $(INST_SYS)
 tool_prefix := LD
 bld_trg_base_var := TARGET
@@ -1412,5 +1460,5 @@
 EXT     := BIN
 EXTPRE  :=
-definst := $(PATH_BIN)
+definst := $(INST_BIN)
 tool_prefix := LD
 bld_trg_base_var := TARGET
@@ -1428,7 +1476,79 @@
 local bld_trg_arch := $(firstword $($(target)_BLD_TRG_ARCH) $(KBUILD_TARGET_ARCH))
 local bld_trg_cpu  := $(firstword $($(target)_BLD_TRG_CPU)  $(KBUILD_TARGET_CPU))
-# TARGET_*
+# _1_TARGET
 $(target)_1_TARGET := $(PATH_TARGET)/$(target).ins
 $(call KB_FN_ASSIGN_DEPRECATED,TARGET_$(target),$($(target)_1_TARGET), $(target)_1_TARGET)
+
+# Determine and set 1_INSTTYPE.
+local insttype := $(firstword \
+	$($(target)_INSTTYPE) \
+	$($(target)_INSTTYPE.$(bld_trg)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INSTTYPE.$(bld_trg_arch)) \
+	$($(target)_INSTTYPE.$(bld_trg_cpu)) \
+	$($(target)_INSTTYPE.$(bld_type)) \
+	)
+ifeq ($(insttype),)
+ ifneq ($(firstword \
+	$($(target)_NOINST) \
+	$($(target)_NOINST.$(bld_trg)) \
+	$($(target)_NOINST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_NOINST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_NOINST.$(bld_trg_arch)) \
+	$($(target)_NOINST.$(bld_trg_cpu)) \
+	$($(target)_NOINST.$(bld_type)) ),)
+   local insttype := none
+ else
+   local insttype := both
+ endif
+endif
+ifn1of ($(insttype), none both stage)
+ $(error kBuild: Unknown value '$(insttype)' for '$(target)_INSTTYPE'. Valid values are 'none', 'both' and 'stage'.)
+endif
+$(target)_1_INSTTYPE    := $(insttype)
+
+# Determine the actual INST and STAGE sub-dirs to use for this target.
+if1of ($(insttype), stage both)
+ local stage := $(strip $(firstword \
+	$($(target)_STAGE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_STAGE.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_STAGE.$(bld_trg).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_type)) \
+	$($(target)_STAGE.$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg_arch)) \
+	$($(target)_STAGE.$(bld_trg)) \
+	$($(target)_INST.$(bld_trg)) \
+	$($(target)_STAGE.$(bld_type)) \
+	$($(target)_INST.$(bld_type)) \
+	$($(target)_STAGE) \
+	$($(target)_INST) \
+	$(definst) ))
+ $(target)_1_STAGE := $(stage)
+else
+ $(target)_1_STAGE = $(error _1_STAGE not used)
+endif
+
+if1of ($(insttype), both)
+ local inst := $(strip $(firstword \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INST.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg).$(bld_type)) \
+	$($(target)_INST.$(bld_trg_arch)) \
+	$($(target)_INST.$(bld_trg)) \
+	$($(target)_INST.$(bld_type)) \
+	$($(target)_INST) \
+	$(definst) ))
+ $(target)_1_INST := $(inst)
+else
+ $(target)_1_INST = $(error _1_INST not used)
+endif
+
+# Block properties that we put off setting until pass 2 for INSTALLS.
+$(target)_1_STAGE_TARGET = $(error The '_1_STAGE_TARGET' property is not present on install targets.  Use '_2_STAGE_TARGETS' instead (set by pass 2!).)
+$(target)_1_INST_TARGET  = $(error The '_1_INST_TARGET' property is not present on install targets.  Use '_2_INST_TARGETS' instead (set by pass 2!).)
+INSTARGET_$(target)      = $(error The 'INSTARGET_' is deprecated and besides, it is being accessed to early.  Consider '_2_STAGE_TARGETS' or '_2_INST_TARGETS'.)
 
 # INSTARGET_ later.
@@ -1439,4 +1559,5 @@
 endef # def_pass1_install
 $(eval-opt-var def_pass1_install)
+
 $(foreach target, $(_ALL_INSTALLS), \
 	$(evalval def_pass1_install))
@@ -1587,4 +1708,5 @@
 $(error kBuild: Bad or missing INST property for source '$(source)' in target '$(target)': $(inst))
 endif
+## @todo Install-revamp: FIXME
 INSTARGET_$(target)_$(srcname) := $(inst)
 local fetchdir := $(firstword \
@@ -1799,4 +1921,5 @@
 define def_fetch
 # common
+## @todo Install-revamp: FIXME
 INSTARGET_$(target) := $($(target)_INST)
 ifneq ($(patsubst %/,ok,$(INSTARGET_$(target))),ok)
@@ -2004,17 +2127,33 @@
 
 ## Generate the link & lib install rule
-# @param    $(target)   Normalized target name.
-# @param    $(out)      The build target.
-# @param    $(INSTARGET_$(target))  The installation targets.
-# @param    $(mode)     The file mode (optional)
+# Implicit parameters: target, out, $(target)_1_STAGE_TARGET, mode,
+#                      pre_install_cmds, post_install_cmds
 define def_link_install_rule
-$(INSTARGET_$(target)) : $(out) | $(call DIRDEP,$(dir $(INSTARGET_$(target))))
+$($(target)_1_INST_TARGET): $(out) | $(dir $($(target)_1_INST_TARGET))
 	%$$(call MSG_INST_TRG,$(target),$(out),$$@)
+	$(pre_install_cmds)
+
 	$$(QUIET)$$(INSTALL) $(if $(mode),-m $(mode)) $(if $(uid),-o $(uid)) $(if $(gid),-g $(gid)) -- $(out) $$@
-
-ifdef KBUILD_DO_AUTO_INSTALL
-$(basename $(notdir $(out))):: $(INSTARGET_$(target))
-endif
-endef # def_link_install_rule
+	$(post_install_cmds)
+
+
+endef
+
+
+## Generate the link & lib stage installation rule
+# Implicit parameters: target, out, $(target)_1_STAGE_TARGET, mode,
+#                      pre_install_cmds, post_install_cmds
+define def_link_stage_rule
+$($(target)_1_STAGE_TARGET): $(out) | $(dir $($(target)_1_STAGE_TARGET))
+	%$$(call MSG_INST_TRG,$(target),$(out),$$@)
+	$(pre_install_cmds)
+
+	$$(QUIET)$$(INSTALL) $(if $(mode),-m $(mode)) $(if $(uid),-o $(uid)) $(if $(gid),-g $(gid)) -- $(out) $$@
+	$(post_install_cmds)
+
+
+$(basename $(notdir $(out))):: $($(target)_1_STAGE_TARGET)
+
+endef
 
 
@@ -2072,5 +2211,5 @@
  othersrc += $(source)
 endif
-endef
+endef # def_src_handler_one
 
 ## Generic macro for processing all target sources.
@@ -2106,7 +2245,46 @@
 
 
-
-## Generic macro for generating the install rule(s) for a target
-# and update the globals with default out.
+##
+# Function for getting the first defined propert value.
+#
+# @param   1            The property name.
+# @note    Implicit parameters: target, bld_trg, bld_trg_arch, bld_trg_cpu, bld_type.
+# @returns Expanded property value.
+#
+## @todo bld_trg_cpu is missing here.
+define def_fn_prop_get_first_defined
+ifdef $(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type)
+ local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type))
+else ifdef $(target)_$(1).$(bld_trg).$(bld_trg_arch)
+ local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_trg_arch))
+else ifdef $(target)_$(1).$(bld_trg).$(bld_type)
+ local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_type))
+else ifdef $(target)_$(1).$(bld_trg_arch)
+ local .RETURN := $($(target)_$(1).$(bld_trg_arch))
+else ifdef $(target)_$(1).$(bld_trg)
+ local .RETURN := $($(target)_$(1).$(bld_trg))
+else ifdef $(target)_$(1).$(bld_type)
+ local .RETURN := $($(target)_$(1).$(bld_type))
+else ifdef $(target)_$(1)
+ local .RETURN := $($(target)_$(1))
+else ifdef $(1).$(bld_trg).$(bld_trg_arch).$(bld_type)
+ local .RETURN := $($(1).$(bld_trg).$(bld_trg_arch).$(bld_type))
+else ifdef $(1).$(bld_trg).$(bld_trg_arch)
+ local .RETURN := $($(1).$(bld_trg).$(bld_trg_arch))
+else ifdef $(1).$(bld_trg).$(bld_type)
+ local .RETURN := $($(1).$(bld_trg).$(bld_type))
+else ifdef $(1).$(bld_trg_arch)
+ local .RETURN := $($(1).$(bld_trg_arch))
+else ifdef $(1).$(bld_trg)
+ local .RETURN := $($(1).$(bld_trg))
+else ifdef $(1).$(bld_type)
+ local .RETURN := $($(1).$(bld_type))
+else
+ local .RETURN := $($(1))
+endif
+endef # def_fn_prop_get_first_defined
+
+
+## Generic macro for generating the install rule(s) for a target.
 #
 # @param	$(target)   Normalized target name.
@@ -2114,25 +2292,6 @@
 # @param	$(definst)  The default _INST value.
 # @param	$(typevar)  The name of the variable with all the root targets of its type.
-# @remark   Only library uses this now.
-define def_target_install_pluss
-ifeq ($(strip $($(target)_NOINST) $($(target)_NOINST.$(bld_trg)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch).$(bld_type))  $($(target)_NOINST.$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg_cpu)) $($(target)_NOINST.$(bld_type))),)
- ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg)))
- else ifneq ($(strip $($(target)_INST.$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_type)))
- else ifneq ($(strip $($(target)_INST)),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST))
- else
-  local inst := $(definst)/
- endif
- INSTARGET_$(target) := $(patsubst %/,%/$(notdir $(out)),$(inst))
+define def_target_install_only
+ifneq ($($(target)_1_INSTTYPE),none)
  local mode := $(firstword \
  	$($(target)_MODE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
@@ -2159,25 +2318,20 @@
  	$($(target)_GID.$(bld_type)) \
  	$($(target)_GID) )
-
- # generate the install rule
+ local pre_install_cmds  := $(evalcall def_fn_prop_get_first_defined,PRE_INST_CMDS)
+ local post_install_cmds := $(evalcall def_fn_prop_get_first_defined,POST_INST_CMDS)
+
+ # Generate the rules
+ ifeq ($($(target)_1_INSTTYPE),both)
 $(eval $(def_link_install_rule))
-
- _INSTALLS_FILES += $(INSTARGET_$(target))
-
- ifdef KBUILD_DO_AUTO_INSTALL
-  $(typevar) += $(INSTARGET_$(target))
- else
-  $(typevar) += $(out)
- endif
-else # _NOINST
+  _INSTALLS_FILES += $($(target)_1_INST_TARGET)
+ endif
+$(eval $(def_link_stage_rule))
+ _STAGE_FILES += $($(target)_1_STAGE_TARGET)
+ $(typevar) += $($(target)_1_STAGE_TARGET)
+else  # INSTTYPE == none
  $(typevar) += $(out)
-endif
-
-_OUT_FILES += $($(target)_2_OUTPUT) $($(target)_2_OUTPUT_MAYBE) $(out)
-_CLEAN_FILES += $($(target)_CLEAN) $($(target)_CLEAN.$(bld_trg)) $($(target)_CLEAN.$(bld_trg).$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_cpu)) $($(target)_CLEAN.$(bld_type))
-_DIRS += $($(target)_BLDDIRS) $($(target)_BLDDIRS.$(bld_trg)) $($(target)_BLDDIRS.$(bld_trg).$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_cpu)) $($(target)_BLDDIRS.$(bld_type))
-_OBJS += $($(target)_2_OBJS)
-
-endef # def_target_install_pluss
+endif # INSTTYPE == none
+
+endef # def_target_install_only
 
 
@@ -2311,65 +2465,6 @@
 
 # Custom pre-link actions.
-## @todo bld_trg_cpu is missing here.
-ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_type))
-else ifdef $(target)_PRE_CMDS.$(bld_trg_arch)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg_arch))
-else ifdef $(target)_PRE_CMDS.$(bld_trg)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg))
-else ifdef $(target)_PRE_CMDS.$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_type))
-else ifdef $(target)_PRE_CMDS
- local pre_cmds := $($(target)_PRE_CMDS)
-else ifdef PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef PRE_CMDS.$(bld_trg).$(bld_trg_arch)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef PRE_CMDS.$(bld_trg).$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_type))
-else ifdef PRE_CMDS.$(bld_trg_arch)
- local pre_cmds := $(PRE_CMDS.$(bld_trg_arch))
-else ifdef PRE_CMDS.$(bld_trg)
- local pre_cmds := $(PRE_CMDS.$(bld_trg))
-else ifdef PRE_CMDS.$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_type))
-else
- local pre_cmds := $(PRE_CMDS)
-endif
-
-# Custom post-link actions.
-ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_type))
-else ifdef $(target)_POST_CMDS.$(bld_trg_arch)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg_arch))
-else ifdef $(target)_POST_CMDS.$(bld_trg)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg))
-else ifdef $(target)_POST_CMDS.$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_type))
-else ifdef $(target)_POST_CMDS
- local post_cmds := $($(target)_POST_CMDS)
-else ifdef POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef POST_CMDS.$(bld_trg).$(bld_trg_arch)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef POST_CMDS.$(bld_trg).$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_type))
-else ifdef POST_CMDS.$(bld_trg_arch)
- local post_cmds := $(POST_CMDS.$(bld_trg_arch))
-else ifdef POST_CMDS.$(bld_trg)
- local post_cmds := $(POST_CMDS.$(bld_trg))
-else ifdef POST_CMDS.$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_type))
-else
- local post_cmds := $(POST_CMDS)
-endif
+local pre_cmds  := $(evalcall def_fn_prop_get_first_defined,PRE_CMDS)
+local post_cmds := $(evalcall def_fn_prop_get_first_defined,POST_CMDS)
 
 # eliminate this guy?
@@ -2421,11 +2516,16 @@
 $(eval $(def_link_rule))
 
-# installing and globals
-$(evalval def_target_install_pluss)
-endef
+# installing and globals.
+$(evalval def_target_install_only)
+
+_OUT_FILES += $($(target)_2_OUTPUT) $($(target)_2_OUTPUT_MAYBE) $(out)
+_CLEAN_FILES += $($(target)_CLEAN) $($(target)_CLEAN.$(bld_trg)) $($(target)_CLEAN.$(bld_trg).$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_cpu)) $($(target)_CLEAN.$(bld_type))
+_DIRS += $($(target)_BLDDIRS) $($(target)_BLDDIRS.$(bld_trg)) $($(target)_BLDDIRS.$(bld_trg).$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_cpu)) $($(target)_BLDDIRS.$(bld_type))
+_OBJS += $($(target)_2_OBJS)
+
+endef # def_lib
 $(eval-opt-var def_lib)
 
 # Process libraries
-definst := $(PATH_LIB)
 typevar := _LIBS
 tool_do := LINK_LIBRARY
@@ -2447,5 +2547,4 @@
 # @param    $(target)   Normalized target name.
 # @param    $(EXT)      EXE,DLL,SYS.
-# @param    $(definst)  The default _INST value.
 # @param    $(typevar)  The name of the variable with all the root targets of its type.
 define def_link_common
@@ -2680,65 +2779,7 @@
 endif
 
-# Custom pre-link actions.
-ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef $(target)_PRE_CMDS.$(bld_trg).$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg).$(bld_type))
-else ifdef $(target)_PRE_CMDS.$(bld_trg_arch)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg_arch))
-else ifdef $(target)_PRE_CMDS.$(bld_trg)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_trg))
-else ifdef $(target)_PRE_CMDS.$(bld_type)
- local pre_cmds := $($(target)_PRE_CMDS.$(bld_type))
-else ifdef $(target)_PRE_CMDS
- local pre_cmds := $($(target)_PRE_CMDS)
-else ifdef PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef PRE_CMDS.$(bld_trg).$(bld_trg_arch)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef PRE_CMDS.$(bld_trg).$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_trg).$(bld_type))
-else ifdef PRE_CMDS.$(bld_trg_arch)
- local pre_cmds := $(PRE_CMDS.$(bld_trg_arch))
-else ifdef PRE_CMDS.$(bld_trg)
- local pre_cmds := $(PRE_CMDS.$(bld_trg))
-else ifdef PRE_CMDS.$(bld_type)
- local pre_cmds := $(PRE_CMDS.$(bld_type))
-else
- local pre_cmds := $(PRE_CMDS)
-endif
-
-# Custom post-link actions.
-ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef $(target)_POST_CMDS.$(bld_trg).$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg).$(bld_type))
-else ifdef $(target)_POST_CMDS.$(bld_trg_arch)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg_arch))
-else ifdef $(target)_POST_CMDS.$(bld_trg)
- local post_cmds := $($(target)_POST_CMDS.$(bld_trg))
-else ifdef $(target)_POST_CMDS.$(bld_type)
- local post_cmds := $($(target)_POST_CMDS.$(bld_type))
-else ifdef $(target)_POST_CMDS
- local post_cmds := $($(target)_POST_CMDS)
-else ifdef POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef POST_CMDS.$(bld_trg).$(bld_trg_arch)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_trg_arch))
-else ifdef POST_CMDS.$(bld_trg).$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_trg).$(bld_type))
-else ifdef POST_CMDS.$(bld_trg_arch)
- local post_cmds := $(POST_CMDS.$(bld_trg_arch))
-else ifdef POST_CMDS.$(bld_trg)
- local post_cmds := $(POST_CMDS.$(bld_trg))
-else ifdef POST_CMDS.$(bld_type)
- local post_cmds := $(POST_CMDS.$(bld_type))
-else
- local post_cmds := $(POST_CMDS)
-endif
+# Custom pre/post-link actions.
+local pre_cmds  := $(evalcall def_fn_prop_get_first_defined,PRE_CMDS)
+local post_cmds := $(evalcall def_fn_prop_get_first_defined,POST_CMDS)
 
 # eliminate this guy?
@@ -2746,60 +2787,5 @@
 
 # installation targets
-ifeq ($(strip $($(target)_NOINST) $($(target)_NOINST.$(bld_trg)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg).$(bld_trg_arch).$(bld_type))  $($(target)_NOINST.$(bld_trg_arch)) $($(target)_NOINST.$(bld_trg_cpu)) $($(target)_NOINST.$(bld_type))),)
- ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg).$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg).$(bld_type)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg_arch))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg_arch)))
- else ifneq ($(strip $($(target)_INST.$(bld_trg))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_trg)))
- else ifneq ($(strip $($(target)_INST.$(bld_type))),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST.$(bld_type)))
- else ifneq ($(strip $($(target)_INST)),)
-  local inst := $(addprefix $(PATH_INS)/,$($(target)_INST))
- else
-  local inst := $(definst)/
- endif
- INSTARGET_$(target) := $(patsubst %/,%/$(notdir $(out)),$(inst))
- ifdef KBUILD_DO_AUTO_INSTALL
-  $(typevar)  += $(INSTARGET_$(target))
- else
-  $(typevar)  += $(out)
- endif
- local mode := $(firstword \
- 	$($(target)_MODE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
- 	$($(target)_MODE.$(bld_trg).$(bld_trg_arch)) \
- 	$($(target)_MODE.$(bld_trg).$(bld_type)) \
- 	$($(target)_MODE.$(bld_trg_arch)) \
- 	$($(target)_MODE.$(bld_trg)) \
- 	$($(target)_MODE.$(bld_type)) \
- 	$($(target)_MODE) )
- local uid := $(firstword \
- 	$($(target)_UID.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
- 	$($(target)_UID.$(bld_trg).$(bld_trg_arch)) \
- 	$($(target)_UID.$(bld_trg).$(bld_type)) \
- 	$($(target)_UID.$(bld_trg_arch)) \
- 	$($(target)_UID.$(bld_trg)) \
- 	$($(target)_UID.$(bld_type)) \
- 	$($(target)_UID) )
- local gid := $(firstword \
- 	$($(target)_GID.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
- 	$($(target)_GID.$(bld_trg).$(bld_trg_arch)) \
- 	$($(target)_GID.$(bld_trg).$(bld_type)) \
- 	$($(target)_GID.$(bld_trg_arch)) \
- 	$($(target)_GID.$(bld_trg)) \
- 	$($(target)_GID.$(bld_type)) \
- 	$($(target)_GID) )
-
- # generate the install rule
-$(eval $(def_link_install_rule))
-
-else # NOINST
- INSTARGET_$(target) :=
- $(typevar)  += $(out)
-endif # NOINST
+$(evalval def_target_install_only)
 
 # dependency file
@@ -2864,27 +2850,12 @@
 #
 
-## Build program (one).
-# @param    $(target)   Normalized target (program) name.
-define def_bldprog
-
-# set NOINST if not forced installation.
-ifndef $(target)_INST
-$(target)_NOINST := 1
-endif
-
-# do the usual stuff.
-$(evalval def_link_common)
-
-endef
-
 # Process build programs.
 EXT     := EXE
 EXTPRE  := HOST
 tool_do := LINK_PROGRAM
-definst := $(PATH_BIN)
 typevar := _BLDPROGS
 mode    := 0755
 bld_trg_base_var := PLATFORM
-$(foreach target, $(_ALL_BLDPROGS), $(evalval def_bldprog))
+$(foreach target, $(_ALL_BLDPROGS), $(evalval def_link_common))
 
 ifdef KBUILD_PROFILE_SELF
@@ -2901,5 +2872,4 @@
 EXTPRE  :=
 tool_do := LINK_DLL
-definst := $(PATH_DLL)
 typevar := _DLLS
 mode    := 0644
@@ -2925,10 +2895,8 @@
  EXT     := LIB
  tool_do := LINK_LIBRARY
- definst := $(PATH_LIB)
  $(foreach target, $(_ALL_IMPORT_LIBS), $(evalval def_lib))
 else
  EXT     := DLL
  tool_do := LINK_DLL
- definst := $(PATH_DLL)
  $(foreach target, $(_ALL_IMPORT_LIBS), $(evalval def_link_common))
 endif
@@ -2947,5 +2915,4 @@
 EXTPRE  :=
 tool_do := LINK_PROGRAM
-definst := $(PATH_BIN)
 typevar := _PROGRAMS
 mode    := 0755
@@ -2966,5 +2933,4 @@
 EXTPRE  :=
 tool_do := LINK_SYSMOD
-definst := $(PATH_SYS)
 typevar := _SYSMODS
 mode    := 0644
@@ -2985,5 +2951,4 @@
 EXTPRE  :=
 tool_do := LINK_MISCBIN
-definst := $(PATH_BIN)
 typevar := _MISCBINS
 mode    := 0644
@@ -3006,14 +2971,24 @@
 #
 
-## generate the install rule
-define def_install_src_rule
-# the install rule
-$(insdst) : $(srcsrc) | $(call DIRDEP,$(dir $(insdst)))
-	%$$(call MSG_INST_FILE,$(srcsrc),$(insdst))
-	$$(QUIET)$(inscmd)
-endef
-$(eval-opt-var def_install_src_rule)
-
-## install one file
+##
+# Generate the staging rules.
+#
+define def_install_src_rule_staging
+$(stagedst) : $(srcsrc) | $(dir $(stagedst))
+	%$$(call MSG_INST_FILE,$(srcsrc),$(stagedst))
+	$$(QUIET)$(stagecmd)
+endef
+$(eval-opt-var def_install_src_rule_staging)
+
+define def_install_src_rule_installing
+$(instdst) : $(srcsrc) | $(dir $(instdst))
+	%$$(call MSG_INST_FILE,$(srcsrc),$(instdst))
+	$$(QUIET)$(instcmd)
+endef
+$(eval-opt-var def_install_src_rule_installing)
+
+##
+# Install one file.
+#
 define def_install_src
 
@@ -3028,30 +3003,33 @@
  $(warning kBuild: Install target '$(target)' has a bad source specifier: $(src))
 endif
-local srcdst := $(subst =>, ,$(src))
-local srcsrc := $(firstword $(srcdst))
+local srcdst    := $(subst =>, ,$(src))
+local srcsrc    := $(firstword $(srcdst))
 local srcdstdir := $(dir $(word 2,$(srcdst)))
-local srcdst := $(word $(words $(srcdst)),$(srcdst))
-
-# _INSTFUN
+local srcdst    := $(word $(words $(srcdst)),$(srcdst))
+
+# Calc destinations.
 ifdef $(srcsrc)_INSTFUN
  local instfun := $(srcsrc)_INSTFUN
-else ifdef $(target)_INSTFUN
- local instfun := $(target)_INSTFUN
 else
- local instfun := _INSTALL_FILE
-endif
-
-# _INST
-ifdef $(srcsrc)_INST
- local inst := $(patsubst %/,%,$($(srcsrc)_INST))/$(dir $(srcdstdir))
-else ifdef $(target)_INST
- local inst := $(patsubst %/,%,$($(target)_INST))/$(dir $(srcdstdir))
-else
- local inst := $(dir $(srcdstdir))
-endif
-
-# calc target
-local insdst := $(call $(instfun),$(srcdst),$(target),$(inst),$(PATH_INS))
-#$(warning instfun=$(instfun) srcdst=$(srcdst) target=$(target) inst=$(inst) => insdst=$(insdst))
+ local instfun := $(top_instfun)
+endif
+
+local stage    := $(firstword $($(srcsrc)_STAGE) $($(target)_1_STAGE))
+ifneq ($(substr $(stage),-1),/)
+ $(warning kBuild: File $(srcsrc) in install target $(target) has a STAGE property without a trailing slash.)
+ local stage   := $(stage)/
+endif
+local stage    := $(stage)/$(dir $(srcdstdir))
+local stagedst := $(call $(instfun),$(srcdst),$(target),$(stage),$(PATH_STAGE))
+
+local inst     := $(firstword $($(srcsrc)_STAGE) $($(target)_1_STAGE))
+ifneq ($(substr $(inst),-1),/)
+ $(warning kBuild: File $(srcsrc) in install target $(target) has a INST property without a trailing slash.)
+ local inst    := $(inst)/
+endif
+local inst     := $(inst)/$(dir $(srcdstdir))
+local instdst  := $(call $(instfun),$(srcdst),$(target),$(inst),$(PATH_INS))
+
+#$(warning instfun=$(instfun) srcdst=$(srcdst) target=$(target) inst=$(inst) => instdst=$(instdst); stage=$(stage) => stagedst=$(stagedst))
 
 # mode, uid and gid
@@ -3069,7 +3047,5 @@
 	$($(srcdst)_MODE.$(bld_trg)) \
 	$($(srcdst)_MODE) \
-	$($(target)_MODE.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_MODE.$(bld_trg)) \
-	$($(target)_MODE))
+	$(top_mode))
 local uid := $(firstword \
 	$($(target)_$(srcsrc)_UID.$(bld_trg).$(bld_trg_arch)) \
@@ -3085,7 +3061,5 @@
 	$($(srcdst)_UID.$(bld_trg)) \
 	$($(srcdst)_UID) \
-	$($(target)_UID.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_UID.$(bld_trg)) \
-	$($(target)_UID))
+	$(top_uid))
 local gid := $(firstword \
 	$($(target)_$(srcsrc)_GID.$(bld_trg).$(bld_trg_arch)) \
@@ -3101,11 +3075,7 @@
 	$($(srcdst)_GID.$(bld_trg)) \
 	$($(srcdst)_GID) \
-	$($(target)_GID.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_GID.$(bld_trg)) \
-	$($(target)_GID))
+	$(top_gid))
 local flags := \
-	$($(target)_IFFLAGS) \
-	$($(target)_IFFLAGS.$(bld_trg)) \
-	$($(target)_IFFLAGS.$(bld_trg).$(bld_trg_arch)) \
+	$(top_ifflags) \
 	$($(srcdst)_IFFLAGS) \
 	$($(srcdst)_IFFLAGS.$(bld_trg)) \
@@ -3136,45 +3106,68 @@
 
 # Adjust the source if we got a default PATH. (This must be done this late!)
-local defpath     := $($(target)_DEFPATH)
-ifeq ($(defpath),)
- local defpath    := $($(target)_PATH)
-endif
+local defpath := $(firstword $($(target)_DEFPATH) $($(target)_PATH))
 ifneq ($(defpath),)
  local srcsrc := $(abspathex $(srcsrc),$(defpath))
 endif
 
-# create the command
+# create the commands
 ifdef $(srcsrc)_INSTALLER
- local inscmd := $(call $(srcsrc)_INSTALLER,$(srcsrc),$(insdst),$(target),$(flags))
+ local stagecmd := $(call $(srcsrc)_INSTALLER,$(srcsrc),$(stagedst),$(target),$(flags))
+ local instcmd  := $(call $(srcsrc)_INSTALLER,$(srcsrc),$(instdst),$(target),$(flags))
 else ifdef $(target)_INSTALLER
- local inscmd := $(call $(target)_INSTALLER,$(srcsrc),$(insdst),$(target),$(flags))
+ local stagecmd := $(call $(target)_INSTALLER,$(srcsrc),$(stagedst),$(target),$(flags))
+ local instcmd  := $(call $(target)_INSTALLER,$(srcsrc),$(instdst),$(target),$(flags))
 else
- local inscmd := $$(INSTALL)\
+ local stagecmd := $$(INSTALL)\
 		$(if $(uid),-o $(uid))\
 		$(if $(gid),-g $(gid))\
 		$(if $(mode),-m $(mode))\
 		$(flags) -- \
-		$(srcsrc) $(insdst)
-endif
-
-# generate the rule (need double evaluation here)
-$(eval $(def_install_src_rule))
-
-INSTARGET_$(target) += $(insdst)
+		$(srcsrc) $(stagedst)
+ local instcmd := $$(INSTALL)\
+		$(if $(uid),-o $(uid))\
+		$(if $(gid),-g $(gid))\
+		$(if $(mode),-m $(mode))\
+		$(flags) -- \
+		$(srcsrc) $(instdst)
+endif
+
+# Generate the staging rule (requires double evaluation).
+$(eval $(def_install_src_rule_staging))
+$(target)_2_STAGE_TARGETS += $(stagedst)
+
+# Generate the install rule
+ifeq ($(insttype),both)
+ $(eval $(def_install_src_rule_installing))
+ $(target)_2_INST_TARGETS += $(instdst)
+endif
+
 endef # def_install_src
 $(eval-opt-var def_install_src)
 
 
-## generate the symlink rule
-define def_install_symlink_rule
-# the install rule
-$(insdst) : | $(call DIRDEP,$(dir $(insdst)))
-	%$$(call MSG_INST_SYM,$(insdst),$(symdst))
+##
+# Generate the symlink rules.
+#
+define def_install_symlink_rule_staging
+$(stagedst) : | $(dir $(stagedst))
+	%$$(call MSG_INST_SYM,$(stagedst),$(symdst))
 	$$(QUIET)$$(RM) -f -- $$@
-	$$(QUIET)$$(LN_SYMLINK) $(symdst) $(insdst)
-endef # def_install_symlink_rule
-$(eval-opt-var def_install_symlink_rule)
-
-## create one symlink
+	$$(QUIET)$$(LN_SYMLINK) $(symdst) $(stagedst)
+endef
+$(eval-opt-var def_install_symlink_rule_staging)
+
+define def_install_symlink_rule_installing
+$(instdst) : | $(dir $(instdst))
+	%$$(call MSG_INST_SYM,$(instdst),$(symdst))
+	$$(QUIET)$$(RM) -f -- $$@
+	$$(QUIET)$$(LN_SYMLINK) $(symdst) $(instdst)
+endef
+$(eval-opt-var def_install_symlink_rule_installing)
+
+
+##
+# Create one symlink.
+#
 define def_install_symlink
 
@@ -3184,35 +3177,46 @@
 local symdst := $(word $(words $(symdst)),$(symdst))
 
-# _INSTFUN
+# Calc destinations
 ifdef $(symlnk)_INSTFUN
  local instfun := $(symlnk)_INSTFUN
-else ifdef $(target)_INSTFUN
- local instfun := $(target)_INSTFUN
 else
- local instfun := _INSTALL_FILE
-endif
-
-# _INST
-ifdef $(symlnk)_INST
- local inst := $(patsubst %/,%,$($(symlnk)_INST))/$(dir $(symlnk))
-else ifdef $(target)_INST
- local inst := $(patsubst %/,%,$($(target)_INST))/$(dir $(symlnk))
-else
- local inst := $(dir $(symlnk))
-endif
-
-# calc target
-local insdst := $(call $(instfun),$(symlnk),$(target),$(inst),$(PATH_INS))
-#$(warning symlnk=$(symlnk) symdst=$(symdst) insdst=$(insdst) instfun=$(instfun) inst='$(inst)')
-
-# generate the rule (need double evaluation here)
-$(eval $(def_install_symlink_rule))
-
-INSTARGET_$(target) += $(insdst)
+ local instfun := $(top_instfun)
+endif
+
+local stage    := $(firstword $($(symlnk)_STAGE) $($(target)_1_STAGE))
+ifneq ($(substr $(stage),-1),/)
+ $(warning kBuild: Symlink $(symlnk) in install target $(target) has a STAGE property without a trailing slash.)
+ local stage   := $(stage)/
+endif
+local stage    := $(stage)/$(dir $(srcdstdir))
+local stagedst := $(call $(instfun),$(symlnk),$(target),$(stage),$(PATH_STAGE))
+
+local inst     := $(firstword $($(symlnk)_STAGE) $($(target)_1_STAGE))
+ifneq ($(substr $(inst),-1),/)
+ $(warning kBuild: Symlnk $(symlnk) in install target $(target) has a INST property without a trailing slash.)
+ local inst    := $(inst)/
+endif
+local inst     := $(inst)/$(dir $(srcdstdir))
+local instdst  := $(call $(instfun),$(symlnk),$(target),$(inst),$(PATH_INS))
+#$(warning symlnk=$(symlnk) symdst=$(symdst) instdst=$(instdst) stagedst=$(stagedst) instfun=$(instfun) inst=$(inst) stage='$(stage)')
+
+# Generate the staging rule (requires double evaluation).
+$(eval $(def_install_symlink_rule_staging))
+$(target)_2_STAGE_TARGETS += $(stagedst)
+
+# Generate the install rule.
+ifeq ($(instmode),both)
+ $(eval $(def_install_symlink_rule_installing))
+ $(target)_2_INST_TARGETS += $(instdst)
+endif
+
 endef # def_install_symlink
 $(optmize def_install_symlink)
 
 
-## generate the install rule
+##
+# Generate an directory installtion rule.
+# Note. Used both for staging and real install rules.
+#
 define def_install_directory_rule
 # the install rule
@@ -3230,15 +3234,27 @@
 $(eval-opt-var def_install_directory_rule)
 
-
-## create one directory
+##
+# Create one directory.
+#
 define def_install_directory
 
-# _INST
+# gather common properties.
 ifdef $(directory)_INST
- local inst := $(PATH_INS)/$(patsubst %/,%,$($(directory)_INST))
-else ifdef $(target)_INST
- local inst := $(PATH_INS)/$(patsubst %/,%,$($(target)_INST))
+ local inst := $($(directory)_INST)
 else
- local inst := $(PATH_INS)
+ local inst := $($(target)_1_INST)
+endif
+ifneq ($(substr $(inst),-1),/)
+ $(warning kBuild: Directory $(directory) in install target $(target) has a INST property without a trailing slash.)
+ local inst := $(inst)/
+endif
+ifdef $(directory)_STAGE
+ local stage := $($(directory)_STAGE)
+else
+ local stage := $($(target)_1_STAGE)
+endif
+ifneq ($(substr $(stage),-1),/)
+ $(warning kBuild: Directory $(directory) in install target $(target) has a INST property without a trailing slash.)
+ local stage := $(stage)/
 endif
 
@@ -3250,7 +3266,5 @@
 	$($(directory)_MODE.$(bld_trg)) \
 	$($(directory)_MODE) \
-	$($(target)_MODE.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_MODE.$(bld_trg)) \
-	$($(target)_MODE))
+	$(top_mode) )
 local uid := $(firstword \
 	$($(target)_$(directory)_UID.$(bld_trg).$(bld_trg_arch)) \
@@ -3260,7 +3274,5 @@
 	$($(directory)_UID.$(bld_trg)) \
 	$($(directory)_UID) \
-	$($(target)_UID.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_UID.$(bld_trg)) \
-	$($(target)_UID))
+	$(top_uid) )
 local gid := $(firstword \
 	$($(target)_$(directory)_GID.$(bld_trg).$(bld_trg_arch)) \
@@ -3270,11 +3282,7 @@
 	$($(directory)_GID.$(bld_trg)) \
 	$($(directory)_GID) \
-	$($(target)_GID.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_GID.$(bld_trg)) \
-	$($(target)_GID))
+	$(top_gid) )
 local flags := \
-	$($(target)_IDFLAGS)\
-	$($(target)_IDFLAGS.$(bld_trg)) \
-	$($(target)_IDFLAGS.$(bld_trg).$(bld_trg_arch)) \
+	$(top_idflags) \
 	$($(directory)_IDFLAGS) \
 	$($(directory)_IDFLAGS.$(bld_trg)) \
@@ -3282,56 +3290,146 @@
 	$($(target)_$(directory)_IDFLAGS) \
 	$($(target)_$(directory)_IDFLAGS.$(bld_trg)) \
-	$($(target)_$(directory)_IDFLAGS.$(bld_trg).$(bld_trg_arch))
-
-local insdst := $(inst)/$(directory)/
-#$(warning directory=$(directory) inst=$(inst) insdst=$(insdst) mode=$(mode) gid=$(gid) uid=$(uid))
-
-# generate the rule (need double evaluation here)
+	$($(target)_$(directory)_IDFLAGS.$(bld_trg).$(bld_trg_arch)) \
+ #$(warning directory=$(directory) inst=$(inst) stage=$(stage) mode=$(mode) gid=$(gid) uid=$(uid))
+
+# The staging rule (requires double evaluation).
+local insdst := $(PATH_STAGE)$(stage)$(directory)/
+$(target)_2_STAGE_DIR_TARGETS += $(insdst)
 $(eval $(def_install_directory_rule))
 
-INSTARGET_DIRS_$(target) += $(insdst)
+# The install rule.
+ifeq ($(insttype),both)
+ local insdst := $(PATH_INS)$(inst)$(directory)/
+ $(target)_2_INST_DIR_TARGETS += $(insdst)
+ $(eval $(def_install_directory_rule))
+endif
+
 endef # def_install_directory
 $(eval-opt-var def_install_directory)
 
 
-## process one install target.
+##
+# Process one install target.
+#
 define def_install
 # the basics.
-local bld_type  := $(firstword $($(target)_BLD_TYPE)     $(KBUILD_TYPE))
-local bld_trg   := $(firstword $($(target)_BLD_TRG)      $(KBUILD_TARGET))
-local bld_trg_arch:= $(firstword $($(target)_BLD_TRG_ARCH) $(KBUILD_TARGET_ARCH))
-local bld_trg_cpu := $(firstword $($(target)_BLD_TRG_CPU)  $(KBUILD_TARGET_CPU))
-
-INSTARGET_$(target) := $($(target)_GOALS)
-INSTARGET_DIRS_$(target) :=
-local outbase := $(call TARGET_BASE,$(target),$(target))
+local bld_type     := $(firstword $($(target)_BLD_TYPE)     $(KBUILD_TYPE))
+local bld_trg      := $(firstword $($(target)_BLD_TRG)      $(KBUILD_TARGET))
+local bld_trg_arch := $(firstword $($(target)_BLD_TRG_ARCH) $(KBUILD_TARGET_ARCH))
+local bld_trg_cpu  := $(firstword $($(target)_BLD_TRG_CPU)  $(KBUILD_TARGET_CPU))
+local insttype     := $($(target)_1_INSTTYPE)
+
+ifneq ($(insttype),none)
+ $(target)_2_STAGE_TARGETS    := $($(target)_GOALS) $($(target)_STAGE_ONLY_GOALS)
+else
+ $(target)_2_STAGE_TARGETS    :=
+endif
+$(target)_2_STAGE_DIR_TARGETS :=
+
+ifeq ($(insttype),both)
+ $(target)_2_INST_TARGETS     := $($(target)_GOALS) $($(target)_INST_ONLY_GOALS)
+else
+ $(target)_2_INST_TARGETS     :=
+endif
+$(target)_2_INST_DIR_TARGETS  :=
+
+
+local outbase      := $(call TARGET_BASE,$(target),$(target))
 $(target)_0_OUTDIR := $(patsubst %/,%,$(dir $(outbase)))
 $(call KB_FN_ASSIGN_DEPRECATED,PATH_$(target),$($(target)_0_OUTDIR), $(target)_0_OUTDIR)
 
-local clean_files := $($(target)_CLEAN) $($(target)_CLEAN.$(bld_trg)) $($(target)_CLEAN.$(bld_trg).$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_arch)) $($(target)_CLEAN.$(bld_trg_cpu)) $($(target)_CLEAN.$(bld_type))
-
-$(foreach directory,$($(target)_DIRS) $($(target)_DIRS.$(bld_trg)) $($(target)_DIRS.$(bld_trg).$(bld_trg_arch)) $($(target)_DIRS.$(bld_trg_arch)) $($(target)_DIRS.$(bld_trg_cpu)) $($(target)_DIRS.$(bld_type)), \
-	$(evalval def_install_directory))
-
-$(foreach src,$($(target)_SOURCES) $($(target)_SOURCES.$(bld_trg)) $($(target)_SOURCES.$(bld_trg).$(bld_trg_arch)) $($(target)_SOURCES.$(bld_trg_arch)) $($(target)_SOURCES.$(bld_trg_cpu)) $($(target)_SOURCES.$(bld_type)), \
-	$(evalval def_install_src))
-
-$(foreach src,$($(target)_SYMLINKS) $($(target)_SYMLINKS.$(bld_trg)) $($(target)_SYMLINKS.$(bld_trg).$(bld_trg_arch)) $($(target)_SYMLINKS.$(bld_trg_arch)) $($(target)_SYMLINKS.$(bld_trg_cpu)) $($(target)_SYMLINKS.$(bld_type)), \
-	$(evalval def_install_symlink))
-
-# the collection target
-local clean_files  += $(PATH_TARGET)/$(target).ins
-$(target)_1_TARGET := $(PATH_TARGET)/$(target).ins
-$(call KB_FN_ASSIGN_DEPRECATED,TARGET_$(target),$($(target)_1_TARGET), $(target)_1_TARGET)
-$($(target)_1_TARGET): $$(INSTARGET_$(target)) | $$(INSTARGET_DIRS_$(target)) $(call DIRDEP,$(PATH_TARGET))
+ifneq ($(insttype),none)
+ # cache top level target properties.
+ local top_mode := $(firstword \
+ 	$($(target)_MODE.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_MODE.$(bld_trg)) \
+ 	$($(target)_MODE) )
+ local top_uid := $(firstword \
+ 	$($(target)_UID.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_UID.$(bld_trg)) \
+ 	$($(target)_UID) )
+ local top_gid := $(firstword \
+ 	$($(target)_GID.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_GID.$(bld_trg)) \
+ 	$($(target)_GID) )
+ local top_ifflags := \
+ 	$($(target)_IFFLAGS) \
+ 	$($(target)_IFFLAGS.$(bld_trg)) \
+ 	$($(target)_IFFLAGS.$(bld_trg).$(bld_trg_arch)) \
+ local top_idflags := \
+ 	$($(target)_IDFLAGS) \
+ 	$($(target)_IDFLAGS.$(bld_trg)) \
+ 	$($(target)_IDFLAGS.$(bld_trg).$(bld_trg_arch))
+ ifdef $(target)_INSTFUN
+   local top_instfun := $(target)_INSTFUN
+ else
+   local top_instfun := _INSTALL_FILE
+ endif
+
+ $(foreach directory, \
+ 	$($(target)_DIRS) \
+ 	$($(target)_DIRS.$(bld_trg)) \
+ 	$($(target)_DIRS.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_DIRS.$(bld_trg_arch)) \
+ 	$($(target)_DIRS.$(bld_trg_cpu)) \
+ 	$($(target)_DIRS.$(bld_type)), \
+ 	$(evalval def_install_directory))
+
+ local clean_files  := \
+ 	$($(target)_CLEAN) \
+ 	$($(target)_CLEAN.$(bld_trg)) \
+ 	$($(target)_CLEAN.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_CLEAN.$(bld_trg_arch)) \
+ 	$($(target)_CLEAN.$(bld_trg_cpu)) \
+ 	$($(target)_CLEAN.$(bld_type))
+
+ $(foreach src,\
+ 	$($(target)_SOURCES) \
+ 	$($(target)_SOURCES.$(bld_trg)) \
+ 	$($(target)_SOURCES.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_SOURCES.$(bld_trg_arch)) \
+ 	$($(target)_SOURCES.$(bld_trg_cpu)) \
+ 	$($(target)_SOURCES.$(bld_type)), \
+ 	$(evalval def_install_src))
+
+ $(foreach src,\
+ 	$($(target)_SYMLINKS) \
+ 	$($(target)_SYMLINKS.$(bld_trg)) \
+ 	$($(target)_SYMLINKS.$(bld_trg).$(bld_trg_arch)) \
+ 	$($(target)_SYMLINKS.$(bld_trg_arch)) \
+ 	$($(target)_SYMLINKS.$(bld_trg_cpu)) \
+ 	$($(target)_SYMLINKS.$(bld_type)), \
+ 	$(evalval def_install_symlink))
+else # none
+ local clean_files  :=
+endif
+
+# The collection targets (staging only).
+local clean_files  += $($(target)_1_TARGET)
+$($(target)_1_TARGET): $$($(target)_2_STAGE_TARGETS) | $$($(target)_2_STAGE_DIR_TARGETS) $$(dir $$@)
 	@$(QUIET2)$(APPEND) $@
 
 $(target): $$($(target)_1_TARGET)
 
-_INSTALLS += $($(target)_1_TARGET)
-_INSTALLS_FILES += $(INSTARGET_$(target))
-_INSTALLS_DIRS += $(INSTARGET_DIRS_$(target))
-_CLEAN_FILES += $(clean_files)
-_DIRS += $($(target)_0_OUTDIR) $($(target)_BLDDIRS) $($(target)_BLDDIRS.$(bld_trg)) $($(target)_BLDDIRS.$(bld_trg).$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_arch)) $($(target)_BLDDIRS.$(bld_trg_cpu)) $($(target)_BLDDIRS.$(bld_type))
+# Update Global lists.
+_INSTALLS       += $($(target)_1_TARGET)
+_STAGE_FILES    += $($(target)_2_STAGE_TARGETS)
+_STAGE_DIRS     += $($(target)_2_STAGE_DIR_TARGETS)
+_INSTALLS_FILES += $($(target)_2_INST_TARGETS)
+_INSTALLS_DIRS  += $($(target)_2_INST_DIR_TARGETS)
+_CLEAN_FILES    += $(clean_files)
+_DIRS           += \
+	$($(target)_0_OUTDIR) \
+	$($(target)_BLDDIRS) \
+	$($(target)_BLDDIRS.$(bld_trg)) \
+	$($(target)_BLDDIRS.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_BLDDIRS.$(bld_trg_arch)) \
+	$($(target)_BLDDIRS.$(bld_trg_cpu)) \
+	$($(target)_BLDDIRS.$(bld_type))
+
+# Deprecated properties.
+INSTARGET_$(target)      := $($(target)_2_STAGE_TARGETS)
+INSTARGET_DIRS_$(target) := $($(target)_2_STAGE_DIR_TARGETS)
+
 endef # def_install
 $(eval-opt-var def_install)
@@ -3345,5 +3443,7 @@
 endif
 
+#
 # Some introspection targets that can be useful for package maintainers.
+#
 .PHONY: kbuild-show-install-files kbuild-show-install-dirs
 kbuild-show-install-files::
@@ -3353,9 +3453,19 @@
 	$(addprefix $(NL)$(TAB)$(QUIET)$(ECHO) , $(subst $(PATH_INS)/,,$(_INSTALLS_DIRS)))
 
+kbuild-show-stage-files::
+	$(addprefix $(NL)$(TAB)$(QUIET)$(ECHO) , $(subst $(PATH_STAGE)/,,$(_STAGE_FILES)))
+
+kbuild-show-stage-dirs::
+	$(addprefix $(NL)$(TAB)$(QUIET)$(ECHO) , $(subst $(PATH_STAGE)/,,$(_STAGE_DIRS)))
+
 
 #
 # TESTING
 #
-_TESTING += $(TESTING) $(TESTING.$(KBUILD_TARGET)) $(TESTING.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH)) $(TESTING.$(KBUILD_TARGET_ARCH)) $(TESTING.$(KBUILD_TARGET_CPU))
+_TESTING += $(TESTING) \
+	$(TESTING.$(KBUILD_TARGET)) \
+	$(TESTING.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH)) \
+	$(TESTING.$(KBUILD_TARGET_ARCH)) \
+	$(TESTING.$(KBUILD_TARGET_CPU))
 
 
@@ -3363,5 +3473,9 @@
 # PACKING
 #
-_PACKING += $(PACKING) $(PACKING.$(KBUILD_TARGET)) $(PACKING.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH)) $(PACKING.$(KBUILD_TARGET_ARCH)) $(PACKING.$(KBUILD_TARGET_CPU))
+_PACKING += $(PACKING) \
+	$(PACKING.$(KBUILD_TARGET)) \
+	$(PACKING.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH)) \
+	$(PACKING.$(KBUILD_TARGET_ARCH)) \
+	$(PACKING.$(KBUILD_TARGET_CPU))
 
 
@@ -3374,6 +3488,6 @@
 # DIRECTORIES
 #
-_DIR_ALL := $(sort $(addsuffix /,$(patsubst %/,%,$(_DIRS))) $(dir $(_OUT_FILES) $(_OBJS) $(_INSTALLS_FILES)))
-$(foreach directory,$(_INSTALLS_DIRS), $(eval _DIR_ALL := $(filter-out $(directory),$(_DIR_ALL))))
+_DIR_ALL := $(sort $(addsuffix /,$(patsubst %/,%,$(_DIRS))) $(dir $(_OUT_FILES) $(_OBJS) $(_INSTALLS_FILES) $(_STAGE_FILES)))
+$(foreach directory,$(_INSTALLS_DIRS) $(_STAGE_DIRS), $(eval _DIR_ALL := $(filter-out $(directory),$(_DIR_ALL))))
 
 
@@ -3409,6 +3523,20 @@
 do-clean:
 	%$(call MSG_CLEAN)
-	$(QUIET)$(RM) -f -- $(_OUT_FILES) $(_OBJS) $(_DEPFILES) $(_DEPFILES_INCLUDED) $(_CLEAN_FILES) $(OTHER_CLEAN)
-	$(QUIET)$(RMDIR) -p --ignore-fail-on-non-empty --ignore-fail-on-not-exist -- $(rsort $(dir $(_OUT_FILES) $(_OBJS) $(_DEPFILES) $(_DEPFILES_INCLUDED) $(_CLEAN_FILES) $(OTHER_CLEAN)))
+	$(QUIET)$(RM) -f -- \
+		$(_OUT_FILES) \
+		$(_OBJS) \
+		$(_DEPFILES) \
+		$(_DEPFILES_INCLUDED) \
+		$(_CLEAN_FILES) \
+		$(OTHER_CLEAN) \
+		$(_STAGE_FILES)
+	$(QUIET)$(RMDIR) -p --ignore-fail-on-non-empty --ignore-fail-on-not-exist -- \
+		$(rsort $(dir   $(_OUT_FILES) \
+				$(_OBJS) \
+				$(_DEPFILES) \
+				$(_DEPFILES_INCLUDED) \
+				$(_CLEAN_FILES) \
+				$(OTHER_CLEAN))\
+			$(_STAGE_DIRS) )
 
 
@@ -3620,4 +3748,7 @@
 nothing: pass_nothing
 
+.PHONY:  staging
+staging: pass_staging
+
 .PHONY:  packing
 packing: pass_packing
@@ -3657,5 +3788,6 @@
 miscbins:  $$(_MISCBINS)
 others:    $$(_OTHERS)
-installs:  $$(_INSTALLS) $$(_INSTALLS_DIRS) $$(_INSTALLS_FILES)
+stagings:  $$(_INSTALLS) $$(_STAGE_DIRS) $$(_STAGE_FILES)
+installs:  $$(_INSTALLS_DIRS) $$(_INSTALLS_FILES)
 
 
@@ -3717,4 +3849,4 @@
 # end-of-file-content
 __footer_kmk__ := target
-endif # __footer_kmk__
-
+endif # !defined(__footer_kmk__)
+
Index: /trunk/kBuild/header.kmk
===================================================================
--- /trunk/kBuild/header.kmk	(revision 2474)
+++ /trunk/kBuild/header.kmk	(revision 2475)
@@ -390,4 +390,34 @@
 endif
 
+# Install directory layout.  Relative to PATH_INS.
+INST_BIN             = bin/
+if1of ($(KBUILD_TARGET), win)
+INST_DLL             = bin/
+else
+INST_DLL             = lib/
+endif
+if1of ($(KBUILD_TARGET), os2 win)
+INST_SYS             = drivers/
+else
+INST_SYS             = kernel/
+endif
+INST_LIB             = lib/
+INST_DOC             = share/doc/
+INST_DEBUG           = debug/
+INST_SBIN            = sbin/
+INST_LIBEXEC         = libexec/
+INST_SHARE           = share/
+
+# Staging directory layout. Relative to PATH_STAGE.
+STAGE_BIN            = $(INST_BIN)
+STAGE_DLL            = $(INST_DLL)
+STAGE_SYS            = $(INST_SYS)
+STAGE_LIB            = $(INST_LIB)
+STAGE_DOC            = $(INST_DOC)
+STAGE_DEBUG          = $(INST_DEBUG)
+STAGE_SBIN           = $(INST_SBIN)
+STAGE_LIBEXEC        = $(INST_LIBEXEC)
+STAGE_SHARE          = $(INST_SHARE)
+
 # Output directories.
 ifndef PATH_OUT_BASE
@@ -404,10 +434,12 @@
 PATH_OBJ             = $(PATH_OUT)/obj
 PATH_TARGET          = $(PATH_OBJ)/$(CURSUBDIR)
-PATH_INS             = $(PATH_OUT)
-PATH_BIN             = $(PATH_INS)/bin
-PATH_DLL             = $(PATH_INS)/bin
-PATH_SYS             = $(PATH_INS)/bin
-PATH_LIB             = $(PATH_INS)/lib
-PATH_DOC             = $(PATH_INS)/doc
+PATH_STAGE           = $(PATH_OUT)/stage
+ifndef PATH_INS
+ ifdef DESTROOT
+PATH_INS             = $(DESTROOT)
+ else
+PATH_INS             = $(PATH_OUT)/dist
+ endif
+endif
 
 # Development tool tree.
@@ -840,5 +872,5 @@
 # A Config.kmk file can add it's own properties to this list and kBuild
 # will do the necessary inheritance for templates, sdks, tools and targets.
-PROPS_SINGLE := $(PROPS_TOOLS) TEMPLATE INST NOINST BLD_TYPE BLD_TRG BLD_TRG_ARCH BLD_TRG_CPU FETCHDIR \
+PROPS_SINGLE := $(PROPS_TOOLS) TEMPLATE INSTTYPE INST STAGE NOINST BLD_TYPE BLD_TRG BLD_TRG_ARCH BLD_TRG_CPU FETCHDIR \
 	OBJSUFF COBJSUFF CXXOBJSUFF OBJCOBJSUFF OBJCXXOBJSUFF ASOBJSUFF RCOBJSUFF SYSSUFF BINSUFF EXESUFF DLLSUFF LIBSUFF ARLIBSUFF \
 	MODE UID GID
@@ -853,5 +885,5 @@
 # This list of non-accumulative target properties which are or may be
 # functions, and thus should not be expanded until the very last moment.
-PROPS_DEFERRED := INSTFUN INSTALLER PRE_CMDS POST_CMDS NAME SONAME
+PROPS_DEFERRED := INSTFUN INSTALLER PRE_CMDS POST_CMDS PRE_INST_CMDS POST_INST_CMDS NAME SONAME
 
 ## PROPS_ACCUMULATE_R
@@ -949,9 +981,9 @@
 PROPS_INSTALLS_SINGLE        := TOOL TEMPLATE INST NOINST
 PROPS_INSTALLS_DEFERRED      := INSTFUN INSTALLER
-PROPS_INSTALLS_ACCUMULATE_R  := DEPS ORDERDEPS
+PROPS_INSTALLS_ACCUMULATE_R  := DEPS ORDERDEPS GOALS INST_ONLY_GOALS STAGE_ONLY_GOALS
 PROPS_INSTALLS_ACCUMULATE_L  := SOURCES DIRS CLEAN
 ## @}
 
-## @name Properties valid on fetches (INSTALLS)
+## @name Properties valid on fetches (FETCHES)
 ## @{
 PROPS_FETCHES_SINGLE         := TOOL TEMPLATE FETCHTOOL UNPACKTOOL PATCHTOOL INST FETCHDIR
@@ -1069,10 +1101,18 @@
 PASS_OTHERS_pass    := others
 
-## PASS: install
+## PASS: staging
 # This pass installs the built entities to a sandbox area.
 ## @todo split this up into build install (to sandbox) and real installation.
+PASS_STAGING        := Staging
+PASS_STAGING_trgs   :=
+PASS_STAGING_vars   := _STAGING_DIRS _INSTALLS _STAGING_FILES
+PASS_STAGING_pass   := staging
+
+## PASS: install
+# This pass installs the built entities to where they will be used (using
+# DESTROOT or PATH_INS to indicate where this is).
 PASS_INSTALLS       := Install
 PASS_INSTALLS_trgs  :=
-PASS_INSTALLS_vars  := _INSTALLS_DIRS _INSTALLS _INSTALLS_FILES
+PASS_INSTALLS_vars  := _INSTALLS_DIRS _INSTALLS_FILES
 PASS_INSTALLS_pass  := installs
 
@@ -1107,10 +1147,10 @@
 ## DEFAULT_PASSES
 # The default passes and their order.
-DEFAULT_PASSES := BLDPROGS LIBRARIES DLLS BINARIES OTHERS INSTALLS
+DEFAULT_PASSES := BLDPROGS LIBRARIES DLLS BINARIES OTHERS STAGING
 
 ## PASSES
 # The passes that should be defined. This must include
 # all passes mentioned by DEFAULT_PASSES.
-PASSES := FETCHES PATCHES $(DEFAULT_PASSES) TESTING PACKING CLEAN NOTHING
+PASSES := FETCHES PATCHES $(DEFAULT_PASSES) INSTALLS TESTING PACKING CLEAN NOTHING
 
 
@@ -1178,13 +1218,34 @@
 #
 PATH_OUT_BASE := $(abspath $(PATH_OUT_BASE))
-PATH_OUT    := $(abspath $(PATH_OUT))
-PATH_OBJ    := $(abspath $(PATH_OBJ))
-PATH_TARGET := $(abspath $(PATH_TARGET))
-PATH_INS    := $(abspath $(PATH_INS))
-PATH_BIN    := $(abspath $(PATH_BIN))
-PATH_DLL    := $(abspath $(PATH_DLL))
-PATH_SYS    := $(abspath $(PATH_SYS))
-PATH_LIB    := $(abspath $(PATH_LIB))
-PATH_DOC    := $(abspath $(PATH_DOC))
+PATH_OUT      := $(abspath $(PATH_OUT))
+PATH_OBJ      := $(abspath $(PATH_OBJ))
+PATH_TARGET   := $(abspath $(PATH_TARGET))
+PATH_INS      := $(abspath $(PATH_INS))
+PATH_STAGE    := $(abspath $(PATH_STAGE))
+
+# Finalize the install and staging directory layouts.
+define def_kbuild_finalize_inst
+local val := $(strip $($(y)_$(x)))
+ifeq ($(val),)
+ $(error kBuild: '$(y)_$(x)' is set to an empty value.)
+endif
+ifneq ($(words $(val)),1)
+ $(error kBuild: The '$(y)_$(x)' value '$(val)' should not contain spaces.)
+endif
+ifneq ($(pos \,$(val)), 0)
+ $(error kBuild: The '$(y)_$(x)' value '$(val)' contains DOS slashes: not allowed.)
+endif
+ifneq ($(pos $(COLON),$(val)), 0)
+ $(error kBuild: The '$(y)_$(x)' value '$(val)' contains colon: not allowed.)
+endif
+ifneq ($(substr $(val),-1), /)
+ $(error kBuild: The '$(y)_$(x)' value '$(val)' has no trailing slash.)
+endif
+if $(pos /../,$(val)) != 0 || "$(substr $(val), 3)" == "../"
+ $(error kBuild: The '$(y)_$(x)' value '$(val)' contains dot-dot escape.)
+endif
+$($(y)_$(x) := $(val)
+endef
+$(foreach y, INST STAGE, $(foreach x, BIN DLL SYS LIB DOC SBIN LIBEXEC SHARE DEBUG, $(evalcall def_kbuild_finalize_inst)))
 
 
