]> rtime.felk.cvut.cz Git - sysless.git/blobdiff - Makefile.rules
cmdproc: Make backspace work even in sterm
[sysless.git] / Makefile.rules
index 1e6105b296f3d98d9954696bae430f21ad66e670..ad7a919da7b309a3bd9909666b46643b17bdea6e 100644 (file)
@@ -1,28 +1,26 @@
-#                   Version for system-less builds.          #OMK@sysless
+#  Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*- #OMK:base.omk
 #
-#  Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
+#  (C) Copyright 2003, 2006, 2007, 2008, 2009  by Pavel Pisa - OCERA team member
+#  (C) Copyright 2006, 2007, 2008, 2009, 2010 by Michal Sojka - Czech Technical University, FEE, DCE
 #
-#  (C) Copyright 2003 by Pavel Pisa - OCERA team member
-#  (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
+#  Homepage: http://rtime.felk.cvut.cz/omk/
+#  Version:  0.2-35-gb9c630c
 #
 # The OMK build system is distributed under the GNU General Public
 # License.  See file COPYING for details.
 #
-# input variables
+#
+#                   Version for system-less builds.                              #OMK:sysless.omk
+#
+#
+# input variables                                                                #OMK:base.omk
 # V                .. if set to 1, full command text is shown else short form is used
 # W                .. whole tree - if set to 1, make is always called from the top-level directory
 # SUBDIRS          .. list of subdirectories intended for make from actual directory
 # default_CONFIG   .. list of default config assignments CONFIG_XXX=y/n ...
-# ALL_OMK_SUBDIRS  .. Expands to all subdirectories (even linked ones) containing Makefile.omk
-# LOCAL_CONFIG_H   .. name of local config.h file generated from values #OMK@config_h
-#                     of options defined in the current directory
-# config_include_HEADERS .. names of global config files (possibly
-#                     with subdirectories)
-# xxx_DEFINES      .. list of config directives to be included in
-#                     config header file of the name <somedir>/xxx.h
-# LN_HEADERS       .. if "y", header files are symbolicaly linked instead of copied. #OMK@include
-#                                                            #OMK@sysless
-# bin_PROGRAMS     .. list of the require binary programs
+# LN_HEADERS       .. if "y", header files are symbolicaly linked instead of copied. #OMK:include.omk
+# bin_PROGRAMS     .. list of the require binary programs                        #OMK:sysless.omk
+# test_PROGRAMS    .. list of the test programs
 # include_HEADERS  .. list of the user-space public header files
 # lib_LIBRARIES    .. list of the user-space libraries
 # lib_LDSCRIPTS    .. list of LD scripts that should be copied to the lib direcotry
 #                     should be used for linking (e.g. ram flash). If this is not
 #                    specified, then the value of DEFAULT_LD_SCRIPT_VARIANT from config.target is used.
 # PREFIX_DIR       .. Prefix to  directories in _compiled and _build. Used in config.omk.
-# local_EVALUATE   .. Makefile hook, which is executed at the end of #OMK@localeval
+# LOCAL_CONFIG_H   .. name of local config.h file generated from values          #OMK:config_h.omk
+#                     of options defined in the current directory
+# config_include_HEADERS .. names of global config files (possibly
+#                     with subdirectories)
+# xxx_DEFINES      .. list of config directives to be included in
+#                     config header file of the name <somedir>/xxx.h
+# DOXYGEN          .. if non-empty, generated headers includes Doxygen's @file
+#                    command, so it is possible to document config
+#                    variables.
+# local_EVALUATE   .. Makefile hook, which is executed at the end of             #OMK:localeval.omk
 #                     the Makefile.rules. Used only for dirty hacks.
-                                                             #OMK@sysless-setup
--include $(MAKERULES_DIR)/config.target
-
-BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
-COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
-
-LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
-#$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
-
-#=====================================================================
-# Common utility rules
-
-link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
-
-
-#=====================================================================
-# Include correct rules for just running pass
-
-USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
+OMK_RULES_TYPE=sysless                                                           #OMK:Makefile.rules.sysless@
+                                                                                 #OMK:base.omk@Makefile.rules.sysless
+# If we are not called by OMK leaf Makefile...
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
+endif
 
-USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
-USER_LIB_DIR     = $(USER_COMPILED_DIR_NAME)/lib
-USER_UTILS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-utils
-USER_BIN_DIR     = $(USER_COMPILED_DIR_NAME)/bin
-USER_OBJS_DIR    = $(LOCAL_BUILD_DIR)
+# OUTPUT_DIR is the place where _compiled, _build and possible other
+# files/directories are created. By default is the same as
+# $(MAKERULES_DIR).
+ifndef OUTPUT_DIR
+OUTPUT_DIR := $(MAKERULES_DIR)
+endif
 
-# Local Variables:
-# mode:makefile
-# End:
-                                                             #OMK@base
 # We need to ensure definition of sources directory first
 ifndef SOURCES_DIR
+# Only shell built-in pwd understands -L
 SOURCES_DIR := $(shell ( pwd -L ) )
+INVOCATION_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
+INVOCATION_DIR := $(INVOCATION_DIR:/%=%)
+INVOCATION_DIR := $(INVOCATION_DIR:\\%=%)
 endif
 
-.PHONY: all default check-make-ver
+.PHONY: all default check-make-ver print-hints omkize
 
 ifdef W
   ifeq ("$(origin W)", "command line")
@@ -89,7 +84,7 @@ ifndef OMK_WHOLE_TREE
 endif
 
 ifneq ($(OMK_WHOLE_TREE),1)
-all: check-make-ver default
+all: check-make-ver print-hints default
        @echo "Compilation finished"
 else
 # Run make in the top-level directory
@@ -97,50 +92,73 @@ all:
        @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
 endif
 
+ifdef OMK_TESTSROOT
+# Usage: $(call canttest,<error message>)
+define canttest
+       ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
+endef
+else
+define canttest
+       echo "$(1)"
+endef
+endif
+
 #=========================
 # Include the config file
 
-ifneq ($(CONFIG_FILE_OK),y)
 ifndef CONFIG_FILE
-CONFIG_FILE      := $(MAKERULES_DIR)/config.omk
+CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
 endif
-ifneq ($(wildcard $(CONFIG_FILE)-default),)
--include $(CONFIG_FILE)-default
-else
-ifneq ($(MAKECMDGOALS),default-config)
-$(warning Please, run "make default-config" first)
+
+$(CONFIG_FILE)-default:
+       $(MAKE) default-config 
+
+ifeq ($(MAKECMDGOALS),default-config)
+export DEFAULT_CONFIG_PASS=1
 endif
+
+ifneq ($(DEFAULT_CONFIG_PASS),1)
+include $(CONFIG_FILE)-default
 endif
 
+-include $(OUTPUT_DIR)/config.target
+
 ifneq ($(wildcard $(CONFIG_FILE)),)
-include $(CONFIG_FILE)
-CONFIG_FILE_OK = y
+-include $(CONFIG_FILE)
 endif
-endif #$(CONFIG_FILE_OK)
 
-export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
-export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
+
+CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
+
+
+export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR INVOCATION_DIR
+export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
 # must to be serialized only in the toplevel make.
 
 ifndef RELATIVE_DIR
-RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
+RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
 endif
+#$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
-#$(warning  RELATIVE_DIR $(RELATIVE_DIR))
-override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
-#$(warning  BACK2TOP_DIR $(BACK2TOP_DIR))
+#$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
+#override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
+#$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
+
+#$(warning SOURCES_DIR = "$(SOURCES_DIR)")
+#$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
+#$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
 
-#$(warning SOURCES_DIR = $(SOURCES_DIR))
-#$(warning MAKERULES_DIR = $(MAKERULES_DIR))
-#$(warning RELATIVE_DIR = $(RELATIVE_DIR))
+# We have to use RELATIVE_PREFIX because of mingw
+override RELATIVE_PREFIX := $(RELATIVE_DIR)/
+override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
 
 #vpath %.c $(SOURCES_DIR)
 #vpath %.cc $(SOURCES_DIR)
 #vpath %.cxx $(SOURCES_DIR)
 
-VPATH = $(SOURCES_DIR)
+# Define srcdir for Automake compatibility
 srcdir = $(SOURCES_DIR)
 
 # Defines for quiet compilation
@@ -152,7 +170,7 @@ endif
 ifndef OMK_VERBOSE
   OMK_VERBOSE = 0
 endif
-ifeq ($(OMK_VERBOSE),1)
+ifneq ($(OMK_VERBOSE),0)
   Q =
 else
   Q = @
@@ -164,14 +182,25 @@ else
   QUIET_CMD_ECHO = echo
 endif
 
+MAKEFILE_OMK=Makefile.omk
+# All subdirectories (even linked ones) containing Makefile.omk
+# Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
+ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
+
 # ===================================================================
 # We have set up all important variables, so we can check and include
 # real OCERA style Makefile.omk now
 ifndef OMK_INCLUDED
-include $(SOURCES_DIR)/Makefile.omk
+include $(SOURCES_DIR)/$(MAKEFILE_OMK)
+ifeq ($(AUTOMATIC_SUBDIRS),y)
+SUBDIRS?=$(ALL_OMK_SUBDIRS)
+endif
 OMK_INCLUDED := 1
 endif
 
+print-hints:
+       @echo 'Use "make V=1" to see the verbose compile lines.'
+
 check-make-ver:
        @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
        if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
@@ -181,7 +210,7 @@ check-make-ver:
 
 distclean dist-clean:
        @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
-       @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)  $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
+       @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
 
 # Common OMK templates
 # ====================
@@ -191,23 +220,77 @@ define mkdir_def
        [ -d $(1) ] || mkdir -p $(1) || exit 1
 endef
 
-ifneq ($(V),2)
+ifneq ($(OMK_VERBOSE),2)
 NO_PRINT_DIRECTORY := --no-print-directory
 endif
 
-# Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>])
+ifeq ($(USE_LEAF_MAKEFILES),n)
+export USE_LEAF_MAKEFILES
+SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
+SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
+else
+SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
+SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
+endif
+
+pass = $(strip $(1))
+
+unexport SUBDIRS
+
+# Call a pass in a subdirectory
+# Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
+define omk_pass_subdir_template
+.PHONY: $(pass)-$(3)-subdir
+$(pass)-submakes: $(pass)-$(3)-subdir
+$(pass)-$(3)-subdir: MAKEOVERRIDES:=$(filter-out SUBDIRS=%,$(MAKEOVERRIDES))
+$(pass)-$(3)-subdir:
+       @$(call mkdir_def,$(2)/$(3))
+       +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
+               RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
+               -f $(SUBDIR_MAKEFILE) $(pass)-submakes
+# In subdirectories we can call submakes directly since passes are
+# already serialized on the toplevel make.
+endef
+
+ifdef OMK_TESTSROOT
+check-target = $(1:%=%-check)
+endif
+
+# Call a pass in a subdirectory
+# Usage: $(call extra_rules_subdir_template,<subdir>)
+define extra_rules_subdir_template
+extra-rules-subdirs: extra-rules-$(1)
+extra-rules-$(1):
+       +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
+               SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
+endef
+
+.PHONY: extra-rules-subdirs
+extra-rules-subdirs:
+
+$(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
+
+# Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
 define omk_pass_template
-.PHNOY: $(1) $(1)-local
-$(1):
-       +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \
-               $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \
-               RELATIVE_DIR=$(RELATIVE_DIR)/$(dir) -C $(2)/$(dir) \
-               -f $(SOURCES_DIR)/$(dir)/Makefile $$@ || exit 1 ;) true
-ifneq ($(4),)
-       @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \
-       $(call mkdir_def,$(2)); \
-       $(MAKE) $(NO_PRINT_DIRECTORY) -C $(2) \
-               -f $(SOURCES_DIR)/Makefile $(3) $$(@:%=%-local)
+.PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
+$(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
+$(pass):
+# Submakes have to be called this way and not as dependecies for pass
+# serialization to work
+       +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
+               RELATIVE_DIR=$(RELATIVE_DIR) \
+               -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
+$(pass)-submakes:
+       @true                   # Do not emit "nothing to be done" messages
+
+ifneq ($(4)$($(pass)_HOOKS),)
+$(pass)-submakes: $(pass)-this-dir
+$(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
+       +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
+       @$(call mkdir_def,$(2))
+       +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
+               -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
+$(pass)-local: $($(pass)_HOOKS)
 endif
 endef
 
@@ -219,25 +302,31 @@ default-config:
        @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
        @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
        @echo >> "$(CONFIG_FILE)-default"
-       @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
-               RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
-               -f $(MAKERULES_DIR)/Makefile default-config-pass
+       @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
+               RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
+               -f $(OUTPUT_DIR)/Makefile default-config-pass
 
 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
 
 default-config-pass-local:
 #      @echo Default config for $(RELATIVE_DIR)
        @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
-       @$(foreach x, $(default_CONFIG), echo $(x) | \
-               sed -e 's/^.*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
+       @$(foreach x, $(default_CONFIG), echo '$(x)' | \
+               sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
 
-# =======================
-# Useful macros
 
-# All subdirectories (even linked ones) containing Makefile.omk
-# Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
-ALL_OMK_SUBDIRS = $(shell find -L $(SOURCES_DIR) -maxdepth 2 -mindepth 2 -name Makefile.omk|sed 's!$(SOURCES_DIR)/\(.*\)/.*!\1!')
-                                                             #OMK@gcc
+omkize:
+       $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
+          echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
+       fi
+       $(Q)for i in `find -L . -name Makefile.omk` ; do \
+          d=`dirname $${i}`; \
+          if ! test -f "$${d}/Makefile.rules" && ( test -f "$${d}/Makefile" && ! cmp --silent Makefile "$${d}/Makefile" ); then \
+             rm -f "$${d}/Makefile"; \
+             cp -v Makefile "$${d}/Makefile"; \
+          fi \
+       done
+                                                                                 #OMK:gcc.omk@Makefile.rules.sysless
 # Rules for compilation of C, C++ and assembler sources using GNU
 # toolchain.
 
@@ -298,6 +387,10 @@ LOADLIBES += $(lib_LOADLIBES:%=-l%)
 
 -include $(OBJS_DIR)/*.d
 
+SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
+
+SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
+
 #%.lo: %.c
 #      $(CC) -o $@ $(LCFLAGS) -c $<
 
@@ -327,7 +420,7 @@ endef # COMPILER_DEFS_template
 
 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_c_o_template
-$(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
+$(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  CC      $$@"
        $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then mv -f "$$@.d.tmp" "$$@.d" ; \
@@ -339,7 +432,7 @@ endef
 
 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_cc_o_template
-$(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
+$(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  CXX     $$@"
        $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then mv -f "$$@.d.tmp" "$$@.d" ; \
@@ -349,7 +442,7 @@ endef
 
 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_S_o_template
-$(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
+$(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  AS      $$@"
        $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
@@ -364,25 +457,34 @@ $(2): $(1)
        $(Q)if [ -n `dirname $$@` ] ; then \
              if [ ! -e `dirname $$@` ] ; then \
                mkdir -p `dirname $$@` ; fi ; fi
-       $(Q)echo >$$@ '/* Automatically generated from $$< */'
-       $(Q)echo >>$$@ '/* Conditionals to control compilation */'
-       $(Q)set -o pipefail ; $(NM) $$< \
+       $(Q)echo >$$@.tmp '/* Automatically generated from $$< */'
+       $(Q)echo >>$$@.tmp '/* Conditionals to control compilation */'
+# Bellow, the tricks with redirection are for shells without set -o pipefail
+# (see http://www.mail-archive.com/dash@vger.kernel.org/msg00149.html)
+       $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
                | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
-               | sort >>$$@
-       $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
-       $(Q)set -o pipefail ; $(NM) $$< \
+               | sort >>$$@.tmp` && exit $$$$status
+       $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in hexadecimal format */'
+       $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
                | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
-               | sort >>$$@
+               | sort >>$$@.tmp` && exit $$$$status
+       $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in decimal format */'
+       $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) -td $$<; echo $$$$? >&4; }\
+               | sed -n 's/^ *0*\(0\|[1-9][0-9]*\) *A *_cmetric2defdec_\([A-Za-z_0-9]*\) */#define \2 \1/p' \
+               | sort >>$$@.tmp` && exit $$$$status
+       $(Q)mv $$@.tmp $$@
 endef
 
 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
 define PROGRAM_template
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
-$(1)_OBJS += $$(filter %.o,$(1)_SOURCES)
-$(1)_OBJS := $$(sort $$($(1)_OBJS))
+
+GEN_SOURCES += $$($(1)_GEN_SOURCES)
+
+$(foreach x, $(SOURCES2OBJS),
+$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
+               $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
+)
+$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
 
 SOURCES += $$($(1)_SOURCES)
 
@@ -396,12 +498,12 @@ $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
          then echo $$(CC)  $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS)   $$(CFLAGS) ; \
          else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
          $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
-         $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \
+         $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
          -o $$@
        @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
        @if [ -n "$(LD_SCRIPT)" ]; then \
          echo "  $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
-       @sed -n -e 's/^LOAD \(.*\)$$$$/  \1  \\/p' $(OBJS_DIR)/$(1)$(3:%=-%).map  >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
+       @sed -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134'  >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
        @echo >>$(OBJS_DIR)/$(1).exe.d
 endef
 
@@ -420,11 +522,14 @@ endef
 
 # Syntax: $(call LIBRARY_template,<library-name>)
 define LIBRARY_template
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
-$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
-$(1)_OBJS := $$(sort $$($(1)_OBJS))
+
+GEN_SOURCES += $$($(1)_GEN_SOURCES)
+
+$(foreach x, $(SOURCES2OBJS),
+$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
+               $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
+)
+$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
 
 SOURCES += $$($(1)_SOURCES)
 
@@ -436,11 +541,14 @@ endef
 
 # Syntax: $(call SOLIB_template,<library-name>)
 define SOLIB_template
-$(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
-$(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
-$(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
-$(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
-$(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
+
+SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
+
+$(foreach x, $(SOURCES2OBJSLO),
+$(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
+               $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
+)
+$(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
 
 SOLIB_OBJS  += $$($(1)_OBJSLO)
 SOLIB_SOURCES += $$($(1)_SOURCES)
@@ -449,74 +557,24 @@ $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
        @$(QUIET_CMD_ECHO) "  LINK    $$@"
        $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
 endef
-
-# Local Variables:
-# mode:makefile
-# End:
-                                                             #OMK@config_h
-# Syntax: $(call BUILD_CONFIG_H_template,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
-define BUILD_CONFIG_H_template
-
-$(1) : $(wildcard $(CONFIG_FILE)) $(wildcard $(CONFIG_FILE)-default)
-       @$(QUIET_CMD_ECHO) "  CONFGEN $$(@:$(MAKERULES_DIR)/%=%)"
-       @if [ ! -d `dirname $(1).tmp` ] ; then \
-               mkdir `dirname $(1).tmp` ; fi
-       @echo "/* Automatically generated from */" > "$(1).tmp"
-       @echo "/* config file: $$< */" >> "$(1).tmp"
-       @echo "#ifndef $(3)" >> "$(1).tmp"
-       @echo "#define $(3)" >> "$(1).tmp"
-       @( $(foreach x, $(shell echo '$($(2))' | sed -e 's/\<\([^ =]*\)\(=[^ ]\+\|\)\>/\1/g' ), \
-               echo '$(x).$($(x))' ; ) echo ; ) | \
-               sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
-               sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
-                 >> "$(1).tmp"
-       @echo "#endif /*$(3)*/" >> "$(1).tmp"
-       @if cmp --quiet "$(1).tmp" "$(1)" ; then rm "$(1).tmp" ; \
-       else mv "$(1).tmp" "$(1)" ; \
-       echo Updated configuration "$(1)" ; fi
-
-endef
-
-ifdef LOCAL_CONFIG_H
-
-CFLAGS += -I.
-$(eval $(call BUILD_CONFIG_H_template,$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
-
-endif
-
-# Special rules for configuration exported headers
-
-#FIXME: The directory for headers should not be specified here.
-$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
-_$(basename $(notdir $(confh)))_H \
-)))
-
-# Add some hooks to standard passes
-include-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS))
-
-clean-local: clean-local-config-h
-
-clean-local-config-h:
-       @$(foreach confh,$(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS)),\
-           if [ -e $(confh) ] ; then touch -t 200001010101 $(confh) ; fi ; \
-       )
-
-
-# Local Variables:
-# mode:makefile
-# End:
-ifeq ($(OMK_VERBOSE),1)                                      #OMK@include
+ifeq ($(OMK_VERBOSE),1)                                                          #OMK:include.omk@Makefile.rules.sysless
 CPHEADER_FLAGS += -v
 LNHEADER_FLAGS += -v
 endif
 
 ifneq ($(LN_HEADERS),y)
 define cp_cmd
-( echo "  CP      $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; cp $(CPHEADER_FLAGS) $(1) $(2) )
+if ! cmp --quiet $(1) $(2); then \
+    echo "  CP      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
+    install -D $(CPHEADER_FLAGS) $(1) $(2) || exit 1; \
+fi
 endef
 else
 define cp_cmd
-( echo "  LN      $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; ln -sf $(LNHEADER_FLAGS) $(1) $(2) )
+if ! cmp --quiet $(1) $(2); then \
+    echo "  LN      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
+    if [ -f $(1) ]; then d=$(2); mkdir -p $${d%/*} && ln -sf $(LNHEADER_FLAGS) $(1) $(2) || exit 1; else exit 1; fi; \
+fi
 endef
 endif
 
@@ -525,20 +583,46 @@ endif
 
 # Syntax: $(call include-pass-template,<include dir>,<keyword>)
 define include-pass-template
-       @$(foreach f, $($(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(notdir $(f)) \
-          || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(notdir $(f))) || exit 1 ; )
-       @$(foreach f, $(nobase_$(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(f) \
-          || ( mkdir -p $(1)/$(dir $(f)) && $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(f)) ) || exit 1 ; )
-       @$(foreach f, $(renamed_$(2)_HEADERS), \
-          srcfname=`echo '$(f)' | sed -e 's/^\(.*\)->.*$$/\1/'` ; destfname=`echo '$(f)' | sed -e 's/^.*->\(.*\)$$/\1/'` ; \
-          cmp --quiet $(SOURCES_DIR)/$${srcfname} $(1)/$${destfname} \
-          || ( mkdir -p `dirname $(1)/$${destfname}` && $(call cp_cmd,$(SOURCES_DIR)/$${srcfname},$(1)/$${destfname}) ) || exit 1 ; )
+include-pass-local: include-pass-local-$(2)
+include-pass-local-$(2): $$($(2)_GEN_HEADERS) $$(foreach f,$$(renamed_$(2)_GEN_HEADERS),$$(shell f='$$(f)'; echo $$$${f%->*}))
+       @$$(foreach f, $$($(2)_HEADERS),$$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
+# FIXME: Use correct build dir, then document it (in the line bellow)
+       @$$(foreach f, $$($(2)_GEN_HEADERS),$$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
+       @$$(foreach f, $$(nobase_$(2)_HEADERS), $$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(f)); )
+       @$$(foreach f, $$(renamed_$(2)_HEADERS), \
+          f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
+          $$(call cp_cmd,$$(SOURCES_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
+       @$$(foreach f, $$(renamed_$(2)_GEN_HEADERS), \
+          f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
+          $$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
+# Suppress "Nothing to be done for `include-pass-local'" message if no headers are defined in Makefile.omk
+       @$$(if $$($(2)_HEADERS)$$($(2)_GEN_HEADERS)$$(nobase_$(2)_HEADERS)$$(renamed_$(2)_HEADERS)$$(renamed_$(2)_GEN_HEADERS),,true)
 endef
+                                                                                 #OMK:sysless.omk@Makefile.rules.sysless
+BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
+COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
+
+LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
+#$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
+
+#=====================================================================
+# Common utility rules
+
+link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
+
+
+#=====================================================================
+# Include correct rules for just running pass
+
+USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
+
+USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
+USER_LIB_DIR     = $(USER_COMPILED_DIR_NAME)/lib
+USER_UTILS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-utils
+USER_TESTS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-tests
+USER_BIN_DIR     = $(USER_COMPILED_DIR_NAME)/bin
+USER_OBJS_DIR    = $(LOCAL_BUILD_DIR)
 
-# Local Variables:
-# mode:makefile
-# End:
-                                                             #OMK@sysless
 .PHONY: check-dir
 
 # Some support to serialize some targets for parallel make
@@ -547,7 +631,8 @@ include-pass: check-dir
 library-pass: include-pass
 binary-pass utils-pass: library-pass
 
-OMK_SERIALIZE_INCLUDED = y
+override OMK_SERIALIZE_INCLUDED = y
+MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
 endif
 
 # -------------------------------------
@@ -584,8 +669,10 @@ GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
 # Generate rules for compilation of programs and libraries
 ifneq ($(link_VARIANTS),)
 $(foreach prog,$(bin_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
+$(foreach prog,$(test_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
 else
 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR))))
+$(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR))))
 endif
 
 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
@@ -606,11 +693,18 @@ $(LIB_DIR)/%$(OBJ_EXT): %$(OBJ_EXT)
 # User-space static libraries and applications object files
 SOURCES := $(sort $(SOURCES))
 #$(warning SOURCES = $(SOURCES))
+GEN_SOURCES := $(sort $(GEN_SOURCES))
 
 # User-space shared libraries object files
 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
+SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES))
 
+# Create _build directories for sources in subdirectories i.e. *_SOURCES=dir/file.c
+_dirs_to_create=$(filter-out ./,$(sort $(dir $(SOURCES) $(SOLIB_SOURCES))))
+ifneq ($(_dirs_to_create),)
+$(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create)))
+endif
 
 # The above generated rules produced $(SOURCES) and $(SOLIB_SOURCES)
 # variables. Now generate rules for compilation of theese sources
@@ -618,22 +712,24 @@ $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOUR
 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
 $(foreach src,$(filter %$(ASM_EXT),$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%$(OBJ_EXT)),)))
+$(foreach src,$(filter %.c,$(GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),)))
 
 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
 $(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS))))
 $(foreach src,$(filter %$(ASM_EXT),$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%.lo),$(SOLIB_PICFLAGS))))
+$(foreach src,$(filter %.c,$(SOLIB_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
 
 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \
                    $(lib_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%$(LIB_EXT)) $(shared_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%.so) \
                    $(addprefix $(LIB_DIR)/,$(lib_OBJS))
 
 ifneq ($(link_VARIANTS),)
-binary-pass-local:  $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)) \
-                   $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of))))
+binary-pass-local:  $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link)) \
+                   $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link).$(of))))
 else
-binary-pass-local:  $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%) \
-                   $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%.$(of)))
+binary-pass-local:  $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%) \
+                   $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%.$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%.$(of)))
 endif
 
 endif # TARGET_RULE_TEMPLATES
@@ -677,6 +773,12 @@ utils-pass-local: $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
 
 endif # HOST_RULE_TEMPLATES
 
+# Checks for OMK tester
+ifdef OMK_TESTSROOT
+default-config-pass-check include-pass-check:
+library-pass-check binary-pass-check:
+       @[ -x "$(shell which $(CC))" ] || $(call canttest,Cannot find compiler: $(CC))
+endif
 
 #=====================================================================
 # Automatic loading of compiled program by issuing "make load"
@@ -690,9 +792,9 @@ endif
 # Used to load program to the target hardware
 define LOAD_PROGRAM_template
 .PHONY: load-$(1)$(3:%=-%)
-load-$(1)$(3:%=-%): $(2)/$(1)$(3:%=-%)$(LOAD_EXTENSION)
+load-$(1)$(3:%=-%): $(2)/$(1)$(3:%=-%)$(if $(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION))
        @$(QUIET_CMD_ECHO) "  LOAD    $$<"
-       @if [ -z "$$(LOAD_CMD$(3:%=-%))" ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
+       @if [ -z '$$(LOAD_CMD$(3:%=-%))' ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
        $(Q) $$(LOAD_CMD$(3:%=-%)) $$<
 endef
 
@@ -701,7 +803,7 @@ endef
 define LOAD_RUN_VARIANT_template
 .PHONY: load-$(1) run-$(1)
 
-load-$(1): load-$(firstword $(bin_PROGRAMS))-$(1)
+load-$(1): load-$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(1)
 
 run-$(1):
        @$(QUIET_CMD_ECHO) "  RUN     $(1)"
@@ -711,10 +813,11 @@ run-$(1):
 endef
 
 $(foreach link,$(link_VARIANTS),$(foreach prog,$(bin_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
+$(foreach link,$(link_VARIANTS),$(foreach prog,$(test_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
 $(foreach link,$(link_VARIANTS),$(eval $(call LOAD_RUN_VARIANT_template,$(link))))
 
 .PHONY: load run
-load: $(addprefix load-,$(firstword $(bin_PROGRAMS))-$(firstword $(link_VARIANTS)))
+load: $(addprefix load-,$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(firstword $(link_VARIANTS)))
 
 run: run-$(firstword $(link_VARIANTS))
 
@@ -722,23 +825,23 @@ run: run-$(firstword $(link_VARIANTS))
 
 #=====================================================================
 # Generate pass rules from generic templates
-OTHER_PASSES = dep clean install
 
-$(eval $(call omk_pass_template, include-pass, $(LOCAL_BUILD_DIR),,$(include_HEADERS)$(nobase_include_HEADERS)$(renamed_include_HEADERS)$(lib_LDSCRIPTS)$(config_include_HEADERS)))
+$(eval $(call omk_pass_template, include-pass, $(LOCAL_BUILD_DIR),,$(include_HEADERS)$(nobase_include_HEADERS)$(renamed_include_HEADERS)$(lib_LDSCRIPTS)$(config_include_HEADERS)$(LOCAL_CONFIG_H)))
 $(eval $(call omk_pass_template, library-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(lib_obj_SOURCES)))
-$(eval $(call omk_pass_template, binary-pass,  $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(bin_PROGRAMS)))
+$(eval $(call omk_pass_template, binary-pass,  $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(bin_PROGRAMS) $(test_PROGRAMS)))
 $(eval $(call omk_pass_template, utils-pass,   $(LOCAL_BUILD_DIR),HOST_RULE_TEMPLATES=y,$(utils_PROGRAMS)))
-
-
-$(eval $(call omk_pass_template,$(OTHER_PASSES),$(LOCAL_BUILD_DIR),,always))
+$(eval $(call omk_pass_template, dep,    $(LOCAL_BUILD_DIR),,always))
+$(eval $(call omk_pass_template, clean,  $(LOCAL_BUILD_DIR),,always))
+$(eval $(call omk_pass_template, install,$(LOCAL_BUILD_DIR),,always))
 
 
 dep-local:
 
 install-local:
 
+$(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
+
 include-pass-local:
-       $(call include-pass-template,$(USER_INCLUDE_DIR),include)
        @$(foreach f, $(lib_LDSCRIPTS), cmp --quiet $(SOURCES_DIR)/$(f) $(USER_LIB_DIR)/$(notdir $(f)) \
           || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(USER_LIB_DIR)/$(notdir $(f))) || exit 1 ; )
 
@@ -750,19 +853,88 @@ clean-local: clean-custom
               $(USER_OBJS_DIR)/*.map \
               $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
 
-check-dir:
+check-dir::
        @$(call mkdir_def,$(USER_INCLUDE_DIR))
        @$(call mkdir_def,$(USER_LIB_DIR))
        @$(call mkdir_def,$(USER_BIN_DIR))
        @$(call mkdir_def,$(USER_UTILS_DIR))
+       @$(call mkdir_def,$(USER_TESTS_DIR))
 
+include-pass-submakes: extra-rules-subdirs
 # Which passes to pass
 default: include-pass library-pass binary-pass utils-pass
+                                                                                 #OMK:config_h.omk@Makefile.rules.sysless
+# Syntax: $(call BUILD_CONFIG_H_template,<stamp_dir>,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
+define BUILD_CONFIG_H_template
+
+$(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(CONFIG_FILES)
+       @$(QUIET_CMD_ECHO) "  CONFGEN $(notdir $(2))"
+       @if [ ! -d `dirname $(2).tmp` ] ; then \
+               mkdir -p `dirname $(2).tmp` ; fi
+       @echo "/* Automatically generated from */" > "$(2).tmp"
+       @echo "/* config files: $$(^:$(OUTPUT_DIR)/%=%) */" >> "$(2).tmp"
+       $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp")
+       @echo "#ifndef $(4)" >> "$(2).tmp"
+       @echo "#define $(4)" >> "$(2).tmp"
+       @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | sed -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \
+               echo '$(x).$($(x))' ; ) echo ; ) | \
+               sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
+               sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
+                 >> "$(2).tmp"
+       @echo "#endif /*$(4)*/" >> "$(2).tmp"
+       @touch "$$@"
+       @if cmp --quiet "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \
+       else mv "$(2).tmp" "$(2)" ; \
+       echo "Updated configuration $(2)" ; fi
+
+endef
+
+ifdef LOCAL_CONFIG_H
 
-# Local Variables:
-# mode:makefile
-# End:
-                                                             #OMK@sources-list
+# This must be declared after the default cflags are assigned!
+# Override is used to override command line assignemnt.
+override CFLAGS += -I $(USER_OBJS_DIR)
+override kernel_INCLUDES += -I $(KERN_OBJS_DIR)
+$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
+
+endif
+
+# Special rules for configuration exported headers
+
+#FIXME: The directory for headers should not be specified here.
+$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
+_$(basename $(notdir $(confh)))_H \
+)))
+
+config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
+
+# Add some hooks to standard passes
+include-pass-local: $(config_h_stamp_files)
+
+ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),)
+
+ifdef LOCAL_CONFIG_H
+$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
+endif
+
+$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
+_$(basename $(notdir $(confh)))_H \
+)))
+
+kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
+
+# Add some hooks to standard passes
+include-pass-local: $(kern_config_h_stamp_files)
+
+endif
+
+clean-local: clean-local-config-h
+
+clean-local-config-h:
+       @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\
+           if [ -e $(confh) ] ; then rm $(confh) ; fi ; \
+       )
+                                                                                 #OMK:sources-list.omk@Makefile.rules.sysless
 # Rules that creates the list of files which are used during
 # compilation. The list reflects conditional compilation depending on
 # config.omk and other variables.
@@ -770,17 +942,12 @@ default: include-pass library-pass binary-pass utils-pass
 SOURCES_LIST_FN=sources.txt
 ifndef SOURCES_LIST
 SOURCES_LIST_DIR:=$(RELATIVE_DIR)
-SOURCES_LIST:=$(MAKERULES_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN)
-export SOURCES_LIST SOURCES_LIST_DIR
+SOURCES_LIST:=$(OUTPUT_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN)
+SOURCES_LIST_D := $(LOCAL_BUILD_DIR)/$(SOURCES_LIST_FN).d
+export SOURCES_LIST SOURCES_LIST_DIR SOURCES_LIST_D
 endif
 
-ifeq ($(MAKECMDGOALS),sources-list)
-NEED_SOURCES_LIST=y
-endif
-ifeq ($(MAKECMDGOALS),TAGS)
-NEED_SOURCES_LIST=y
-endif
-ifeq ($(MAKECMDGOALS),tags)
+ifneq ($(filter sources-list TAGS tags cscope,$(MAKECMDGOALS)),)
 NEED_SOURCES_LIST=y
 endif
 
@@ -788,42 +955,51 @@ ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is
 .PHONY: sources-list
 sources-list: $(SOURCES_LIST)
 
-$(SOURCES_LIST): $(CONFIG_FILE) $(CONFIG_FILE)-default $(shell find -name Makefile.omk)
+$(SOURCES_LIST): $(CONFIG_FILES) $(shell find -name $(MAKEFILE_OMK))
+       @$(call mkdir_def,$(dir $(SOURCES_LIST_D)))
        @echo -n "" > "$(SOURCES_LIST).tmp"
+       @echo -n "" > "$(SOURCES_LIST_D).tmp"
        @$(MAKE) --no-print-directory sources-list-pass
        @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2"
        @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2"
        @rm "$(SOURCES_LIST).tmp"
        @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)"
+       @echo "$(SOURCES_LIST): \\" > "$(SOURCES_LIST_D).tmp2"
+       @cat "$(SOURCES_LIST_D).tmp"|grep -v "$(SOURCES_LIST_D).tmp"|sort|uniq|\
+               sed -e 's/$$/\\/' >> "$(SOURCES_LIST_D).tmp2"
+       @rm "$(SOURCES_LIST_D).tmp"
+       @mv "$(SOURCES_LIST_D).tmp2" "$(SOURCES_LIST_D)"
 endif
 
 $(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always))
 
 sources-list-pass-local:
+       @$(foreach m,$(MAKEFILE_LIST),echo '  $(m)' >> "$(SOURCES_LIST_D).tmp";)
        @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\
-         echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)" >> "$(SOURCES_LIST).tmp";)
+         echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(h)" >> "$(SOURCES_LIST).tmp";)
        @$(foreach ch,$(config_include_HEADERS), \
-         echo "$(USER_INCLUDE_DIR:$(MAKERULES_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";)
-       @$(foreach h,$(renamed_include_HEADERS),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)"|sed -e 's/\(.*\)->.*/\1/' >> "$(SOURCES_LIST).tmp";)
-       @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(utils_PROGRAMS) \
+         echo "$(USER_INCLUDE_DIR:$(OUTPUT_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";)
+       @$(foreach h,$(renamed_include_HEADERS),echo '$(h)'|sed -e 's|\(.*\)->.*|$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))\1|' >> "$(SOURCES_LIST).tmp";)
+       @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(test_PROGRAMS) $(utils_PROGRAMS) \
          $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\
-         $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(src)" >> "$(SOURCES_LIST).tmp";))
+         $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(src)" >> "$(SOURCES_LIST).tmp";))
 
 ############ TAGS ###########
 
 ifeq ($(MAKECMDGOALS),TAGS)
 ETAGS=etags
 TAGS_CMD = $(ETAGS)
+TAGS: $(SOURCES_LIST)
+       @$(MAKE) --no-print-directory do-tags
 endif
 ifeq ($(MAKECMDGOALS),tags) 
 CTAGS=ctags -N
 TAGS_CMD = $(CTAGS)
+tags: $(SOURCES_LIST)
+       @$(MAKE) --no-print-directory do-tags
 endif
 export TAGS_CMD
 
-tags TAGS: $(SOURCES_LIST)
-       @$(MAKE) --no-print-directory do-tags
-
 ifeq ($(MAKECMDGOALS),do-tags)
 .PHONY: do-tags
 do-tags: $(shell sed -e '/^\#/d' $(SOURCES_LIST))
@@ -835,18 +1011,11 @@ endif
 
 cscope: $(SOURCES_LIST)
        @$(QUIET_CMD_ECHO) "  CSCOPE  < $(SOURCES_LIST_FN)"
-       $(Q)sed -e '/^#/d' $(SOURCES_LIST)|cscope -b -i-
+       $(Q)sed -e '/^#/d' $(SOURCES_LIST) > cscope.files
+       $(Q)cscope -b -icscope.files
 #FIXME: see doc to -i in cscope(1)
-
-# Local Variables:
-# mode:makefile
-# End:
-                                                             #OMK@localeval
+                                                                                 #OMK:localeval.omk@Makefile.rules.sysless
 ifneq ($(local_EVALUATE),)
 #$(warning $(local_EVALUATE))
 $(eval $(local_EVALUATE))
 endif
-
-# Local Variables:
-# mode:makefile
-# End: