Index: trunk/kBuild/Examples/Program/Makefile
===================================================================
--- trunk/kBuild/Examples/Program/Makefile	(revision 74)
+++ trunk/kBuild/Examples/Program/Makefile	(revision 74)
@@ -0,0 +1,15 @@
+# $Id$
+
+#
+# This is an example makefile.
+#
+
+TOOL = GCC
+PROGRAMS = helloworld hello world
+helloworld_SOURCES = hello.c
+hello_SOURCES = hello.c
+world_SOURCES = hello.c
+
+DEPTH = ../../..
+include $(DEPTH)/kBuild/rules.kMk
+
Index: trunk/kBuild/Examples/Program/hello.c
===================================================================
--- trunk/kBuild/Examples/Program/hello.c	(revision 74)
+++ trunk/kBuild/Examples/Program/hello.c	(revision 74)
@@ -0,0 +1,6 @@
+#include <stdio.h>
+int main()
+{
+    printf("hello world\n");
+    return 0;
+}
Index: trunk/kBuild/footer.kmk
===================================================================
--- trunk/kBuild/footer.kmk	(revision 73)
+++ trunk/kBuild/footer.kmk	(revision 74)
@@ -60,5 +60,5 @@
 # @param    $1      source file
 # @param    $2      normalized main target
-_SOURCE_TOOL = $(firstword $($1_$2_TOOL) $($1_TOOL) $($2_TOOL) )
+_SOURCE_TOOL = $(firstword $($1_$2_TOOL) $($1_TOOL) $($2_TOOL) $(TOOL))
 
 ## Figure out where to put object files.
@@ -92,5 +92,5 @@
 endef
 $(foreach template, $(_TEMPLATES), $(eval $(def_templates)))
-	
+
 
 #
@@ -102,7 +102,7 @@
 # @param    $(target)   Target name.
 define def_inherit_defaults_one
-ifndef $1_$(prop)
+ifndef $(target)_$(prop)
 ifdef $(prop)
-$1_$(prop) := $($(prop))
+$(target)_$(prop) := $($(prop))
 endif
 endif
@@ -215,21 +215,51 @@
 $(eval tool:=$(call _SOURCE_TOOL,$(source),$(target)))
 $(eval defs        :=\
+	$(TOOL_$(tool)_$(type)DEFS)\
+	$(TOOL_$(tool)_$(type)DEFS.$(BUILD_TYPE))\
+	$(DEFS)\
+	$(DEFS.$(BUILD_TYPE))\
+	$($(target)_DEFS)\
+	$($(target)_DEFS.$(BUILD_TYPE))\
+	$($(target)_DEFS.$(BUILD_TARGET))\
+	$($(source)_DEFS)\
+	$($(source)_DEFS.$(BUILD_TYPE))\
+	$($(source)_DEFS.$(BUILD_TARGET))\
+	$($(source)_$(type)DEFS)\
+	$($(source)_$(type)DEFS.$(BUILD_TYPE))\
+	$($(source)_$(type)DEFS.$(BUILD_TARGET))\
+	$($(target)_$(type)DEFS)\
+	$($(target)_$(type)DEFS.$(BUILD_TYPE))\
+	$($(target)_$(type)DEFS.$(BUILD_TARGET))\
+	$($(target)_$(source)_DEFS)\
+	$($(target)_$(source)_DEFS.$(BUILD_TYPE))\
+	$($(target)_$(source)_DEFS.$(BUILD_TARGET))\
 	$($(target)_$(source)_$(type)DEFS)\
-	$($(target)_$(source)_DEFS)\
-	$($(source)_$(type)DEFS)\
-	$($(source)_DEFS)\
-	$($(target)_$(type)DEFS)\
-	$($(target)_DEFS)\
-	$(DEFS)\
-	$(TOOL_$(tool)_$(type)DEFS)\
+	$($(target)_$(source)_$(type)DEFS.$(BUILD_TYPE))\
+	$($(target)_$(source)_$(type)DEFS.$(BUILD_TARGET))\
 	)
 $(eval incs        :=\
+	$($(target)_$(source)_$(type)INCS.$(BUILD_TARGET))\
+	$($(target)_$(source)_$(type)INCS.$(BUILD_TYPE))\
 	$($(target)_$(source)_$(type)INCS)\
+	$($(target)_$(source)_INCS.$(BUILD_TARGET))\
+	$($(target)_$(source)_INCS.$(BUILD_TYPE))\
 	$($(target)_$(source)_INCS)\
+	$($(source)_$(type)INCS.$(BUILD_TARGET))\
+	$($(source)_$(type)INCS.$(BUILD_TYPE))\
 	$($(source)_$(type)INCS)\
+	$($(source)_INCS.$(BUILD_TARGET))\
+	$($(source)_INCS.$(BUILD_TYPE))\
 	$($(source)_INCS)\
+	$($(target)_$(type)INCS.$(BUILD_TARGET))\
+	$($(target)_$(type)INCS.$(BUILD_TYPE))\
 	$($(target)_$(type)INCS)\
+	$($(target)_INCS.$(BUILD_TARGET))\
+	$($(target)_INCS.$(BUILD_TYPE))\
 	$($(target)_INCS)\
+	$(INCS.$(BUILD_TARGET))\
+	$(INCS.$(BUILD_TYPE))\
 	$(INCS)\
+	$(TOOL_$(tool)_$(type)INCS.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_$(type)INCS.$(BUILD_TYPE))\
 	$(TOOL_$(tool)_$(type)INCS)\
 	)
@@ -237,18 +267,33 @@
 	$(TOOL_$(tool)_$(type)FLAGS)\
 	$(TOOL_$(tool)_$(type)FLAGS.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_$(type)FLAGS.$(BUILD_TARGET))\
 	$($(type)FLAGS)\
 	$($(type)FLAGS.$(BUILD_TYPE))\
+	$($(type)FLAGS.$(BUILD_TARGET))\
 	$($(target)_$(type)FLAGS)\
 	$($(target)_$(type)FLAGS.$(BUILD_TYPE))\
+	$($(target)_$(type)FLAGS.$(BUILD_TARGET))\
 	$($(source)_$(type)FLAGS)\
 	$($(source)_$(type)FLAGS.$(BUILD_TYPE))\
+	$($(source)_$(type)FLAGS.$(BUILD_TARGET))\
 	$($(target)_$(source)_$(type)FLAGS)\
 	$($(target)_$(source)_$(type)FLAGS.$(BUILD_TYPE))\
+	$($(target)_$(source)_$(type)FLAGS.$(BUILD_TARGET))\
 	)
 $(eval outbase     := $(call _OBJECT_BASE,$(source),$(target)))
 $(eval dirdep      := $(dir $(outbase)).dir_created)
-$(eval objsuff     := $(firstword $($(target)_$(source)_OBJSUFF) $($(source)_OBJSUFF) $($(target)_OBJSUFF) $(TOOL_$(tool)_$(type)OBJSUFF) $(SUFF_OBJ)))
+$(eval objsuff     := $(firstword \
+	$($(target)_$(source)_OBJSUFF.$(BUILD_TARGET))\
+	$($(target)_$(source)_OBJSUFF)\
+	$($(source)_OBJSUFF.$(BUILD_TARGET))\
+	$($(source)_OBJSUFF)\
+	$($(target)_OBJSUFF.$(BUILD_TARGET))\
+	$($(target)_OBJSUFF)\
+	$(TOOL_$(tool)_$(type)OBJSUFF.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_$(type)OBJSUFF)\
+	$(SUFF_OBJ)\
+	))
 $(eval obj         := $(outbase)$(objsuff))
-$(eval depfile     := $(outbase)$(SUFF_DEP))
+$(eval dep         := $(outbase)$(SUFF_DEP))
 
 #$ (warning dbg: target=$(target) source=$(source) tool=$(tool) obj=$(obj) dep=$(dep) flags=$(flags) defs=$(defs) incs=$(incs) dirdep=$(dirdep) outbase=$(outbase) objsuff=$(objsuff))
@@ -261,5 +306,5 @@
 
 ## Generic macro for processing all target sources.
-# @param    $target     Normalized target name.
+# @param    $(target)   Normalized target name.
 define def_target_sources
 #$ (warning def_target_sources)
@@ -283,5 +328,5 @@
 
 ## Library (one).
-# @param    $(lib)  Library name
+# @param    $(target)   Normalized library (target) name.
 define def_lib
 # source -> object
@@ -290,6 +335,19 @@
 # library
 $(eval tool        := $(firstword $($(target)_TOOL) $(TOOL)))
-$(eval flags       := $($(target)_ARFLAGS) $(ARFLAGS) $(TOOL_$(tool)_ARFLAGS))
-$(eval libsuff     := $(firstword $($(target)_LIBSUFF) $($(target)_LIBSUFF) $(TOOL_$(tool)_ARLIBSUFF) $(SUFF_LIB)))
+$(eval flags       :=\
+	$(TOOL_$(tool)_ARFLAGS)\
+	$(TOOL_$(tool)_ARFLAGS.$(BUILD_TYPE))\
+	$(ARFLAGS)\
+	$(ARFLAGS.$(BUILD_TYPE))\
+	$($(target)_ARFLAGS)\
+	$($(target)_ARFLAGS.$(BUILD_TYPE))\
+	)
+$(eval libsuff     := $(firstword\
+	$($(target)_LIBSUFF.$(BUILD_TARGET))\
+	$($(target)_LIBSUFF)\
+	$(TOOL_$(tool)_ARLIBSUFF.$(BUILD_TARGET))\
+	$(TOOL_$(tool)_ARLIBSUFF)\
+	$(SUFF_LIB)\
+))
 ## @todo prefix
 $(eval outbase     := $(call _TARGET_BASE,$(target),$(target)))
@@ -315,4 +373,84 @@
 #
 
+## Program (one).
+# @param    $(target)   Normalized target (program) name.
+define def_program
+# source -> object
+$(eval $(def_target_sources))
+
+# library
+$(eval tool        := $(firstword $($(target)_TOOL) $(TOOL)))
+$(eval flags       :=\
+	$(TOOL_$(tool)_LDFLAGS)\
+	$(TOOL_$(tool)_LDFLAGS.$(BUILD_TYPE))\
+	$(LDFLAGS)\
+	$(LDFLAGS.$(BUILD_TYPE))\
+	$($(target)_LDFLAGS)\
+	$($(target)_LDFLAGS.$(BUILD_TYPE))\
+)
+$(eval exesuff     := $(firstword $($(target)_EXESUFF) $($(target)_EXESUFF) $(TOOL_$(tool)_LDEXESUFF) $(SUFF_EXE)))
+$(eval outbase     := $(call _TARGET_BASE,$(target),$(target)))
+$(eval exe         := $(outbase)$(exesuff))
+$(eval objs         = $(_OBJS_$(target)))
+$(eval libs        :=\
+	$($(target)_LIBS.$(BUILD_TYPE))\
+	$($(target)_LIBS)\
+	$(LIBS.$(BUILD_TYPE))\
+	$(LIBS)\
+	$(TOOL_$(tool)_LIBS.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_LIBS)\
+)
+$(eval libpath    :=\
+	$($(target)_LIBPATH.$(BUILD_TYPE))\
+	$($(target)_LIBPATH)\
+	$(LIBPATH.$(BUILD_TYPE))\
+	$(LIBPATH)\
+	$(TOOL_$(tool)_LIBPATH.$(BUILD_TYPE))\
+	$(TOOL_$(tool)_LIBPATH)\
+)
+$(eval dirdep      := $(dir $(exe)).dir_created)
+## @todo fix dependencies
+$(eval deps        := )
+#
+$(eval deffile    := $(strip $(firstword
+	$($(target)_DEFFILE.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$($(target)_DEFFILE.$(BUILD_TARGET))\
+	$($(target)_DEFFILE.$(BUILD_TYPE))\
+	$($(target)_DEFFILE)\
+	$(DEFFILE.$(BUILD_TARGET).$(BUILD_TYPE))\
+	$(DEFFILE.$(BUILD_TARGET))\
+	$(DEFFILE.$(BUILD_TYPE))\
+	$(DEFFILE)\
+)))
+$(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 $(TOOL_$(tool)_LINK_PROGRAM))
+
+_OBJS += $(_OBJS_$(target))
+_EXES += $(exe)
+endef
+
+# Process libraries
+$(foreach target, $(PROGRAMS), $(eval $(def_program)))
+
 
 
@@ -360,9 +498,12 @@
 
 
-objs: $(_OBJS)
+objects: $(_OBJS)
 	echo "dbg: _OBJS = $(_OBJS)"
 
-libs: $(_LIBS)
+libraries: $(_LIBS)
 	echo "dbg: _LIBS = $(_LIBS)"
+
+programs: $(_EXES)
+	echo "dbg: _EXES = $(_EXES)"
 
 
Index: trunk/kBuild/tools/GCC.kmk
===================================================================
--- trunk/kBuild/tools/GCC.kmk	(revision 73)
+++ trunk/kBuild/tools/GCC.kmk	(revision 74)
@@ -26,24 +26,36 @@
 
 
-TOOL_GCC                := Generic GCC
+TOOL_GCC                    := Generic GCC
 
-TOOL_GCC_CC             := gcc$(HOSTSUFF_EXE)
-TOOL_GCC_COBJSUFF       := .o
-TOOL_GCC_CFLAGS         := -g
-TOOL_GCC_CFLAGS.debug   := -O0
-TOOL_GCC_CFLAGS.release := -O2
-TOOL_GCC_CFLAGS.profile := -pg
-TOOL_GCC_CINCS          :=
-TOOL_GCC_CDEFS          :=
+TOOL_GCC_CC                 := gcc$(HOSTSUFF_EXE)
+TOOL_GCC_COBJSUFF           := .o
+TOOL_GCC_CFLAGS             := -g
+TOOL_GCC_CFLAGS.debug       := -O0
+TOOL_GCC_CFLAGS.release     := -O2
+TOOL_GCC_CFLAGS.profile     := -pg
+TOOL_GCC_CINCS              :=
+TOOL_GCC_CDEFS              :=
 
-TOOL_GCC_CC             := gcc$(HOSTSUFF_EXE)
-TOOL_GCC_CXXOBJSUFF     := .o
+TOOL_GCC_CXX                := g++$(HOSTSUFF_EXE)
+TOOL_GCC_CXXOBJSUFF         := .o
+TOOL_GCC_CXXOBJSUFF         := .o
+TOOL_GCC_CXXFLAGS           := -g
+TOOL_GCC_CXXFLAGS.debug     := -O0
+TOOL_GCC_CXXFLAGS.release   := -O2
+TOOL_GCC_CXXFLAGS.profile   := -pg
+TOOL_GCC_CXXINCS            :=
+TOOL_GCC_CXXDEFS            :=
 
-TOOL_GCC_AS             := as$(HOSTSUFF_EXE)
-TOOL_GCC_ASOBJSUFF      := .o
+TOOL_GCC_AS                 := as$(HOSTSUFF_EXE)
+TOOL_GCC_ASOBJSUFF          := .o
 
-TOOL_GCC_AR             := ar$(HOSTSUFF_EXE)
-TOOL_GCC_ARFLAGS        := cr
-TOOL_GCC_ARLIBSUFF      := .a
+TOOL_GCC_AR                 := ar$(HOSTSUFF_EXE)
+TOOL_GCC_ARFLAGS            := cr
+TOOL_GCC_ARLIBSUFF          := .a
+
+TOOL_GCC_LD                 := g++$(HOSTSUFF_EXE)
+TOOL_GCC_LDFLAGS            :=
+TOOL_GCC_LDFLAGS.debug      := -g
+TOOL_GCC_LDFLAGS.release    := -s
 
 
@@ -61,5 +73,5 @@
 # @param	$(objsuff)  Object suffix.
 define TOOL_GCC_COMPILE_C
-#$ (warning dbg: GCC: target=$(target) source=$(source) obj=$(obj) dep=$(dep) flags=$(flags) defs=$(defs) incs=$(incs) dirdep=$(dirdep) outbase=$(outbase) objsuff=$(objsuff))
+#$ (warning dbg: TOOL_GCC_COMPILE_C: target=$(target) source=$(source) obj=$(obj) dep=$(dep) flags=$(flags) defs=$(defs) incs=$(incs) dirdep=$(dirdep) outbase=$(outbase) objsuff=$(objsuff))
 $(obj): $(dirdep) $(PATH_CURRENT)/$(source)
 	$(call MSG_L2,Compiling $$@ using GCC)
@@ -68,5 +80,5 @@
 		-Wp,-MT,$(dep) -Wp,-MD,$$@\
 		-o $$@\
-        $(PATH_CURRENT)/$(source)		
+		$(PATH_CURRENT)/$(source)		
 		
 endef
@@ -83,5 +95,5 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 define TOOL_GCC_LINK_LIBRARY
-#$ (warning dbg: GCC: target=$(target) lib=$(lib) objs=$(objs) deps=$(deps) flags=$(flags) dirdep=$(dirdep) outbase=$(outbase))
+#$ (warning dbg: TOOL_GCC_LINK_LIBRARY: target=$(target) lib=$(lib) objs=$(objs) deps=$(deps) flags=$(flags) dirdep=$(dirdep) outbase=$(outbase))
 $(lib): $(dirdep) $(objs) $(deps)
 	$(call MSG_L1,Creating Library $$@)
@@ -91,2 +103,32 @@
 endef
 
+
+## Link program
+# @param    $(target)   Normalized main target name.
+# @param    $(exe)		Program name.
+# @param    $(objs)     Object files to link together.
+# @param    $(libs)     Libraries to search.
+# @param    $(libpath)  Library search paths.
+# @param    $(flags)    Flags.
+# @param	$(dirdep)   Directory creation dependency.
+# @param	$(deps)		Other dependencies.
+# @param	$(deffile)  Definition file. (optional, PC only)
+# @param	$(custom_pre)   Custom step invoked before linking.
+# @param	$(custom_post)  Custom step invoked after linking.
+#
+# @param    $(outbase)  Output basename (full). Use this for list files and such.
+define TOOL_GCC_LINK_PROGRAM
+#$ (warning dbg: TOOL_GCC_LINK_PROGRAM: target=$(target) exe=$(exe) objs=$(objs) libs=$(libs) deps=$(deps) flags=$(flags) dirdep=$(dirdep) deffile=$(deffile) outbase=$(outbase))
+$(exe): $(dirdep) $(objs) $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib))) $(deps)
+	$(call MSG_L1,Creating Program $$@)
+ifneq ($(custom_pre),)
+	$(eval $(custom_pre))
+endif	
+	$(TOOL_GCC_LD) $(flags) -o $$@ $(objs) \
+	$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(basename $(lib))), $(lib)))
+ifneq ($(custom_post),)
+	$(eval $(custom_post))
+endif	
+	
+endef
+
