]> rtime.felk.cvut.cz Git - lincan.git/blob - omk/rules/sysless/Makefile.rules
a197d7af4a54d4fdb327ffc47be888174be8a06d
[lincan.git] / omk / rules / sysless / Makefile.rules
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*- #OMK:base.omk
2 #
3 #  (C) Copyright 2003, 2006, 2007, 2008, 2009  by Pavel Pisa - OCERA team member
4 #  (C) Copyright 2006, 2007, 2008, 2009, 2010 by Michal Sojka - Czech Technical University, FEE, DCE
5 #
6 #  Homepage: http://rtime.felk.cvut.cz/omk/
7 #  Version:  0.2-27-g254fd61
8 #
9 # The OMK build system is distributed under the GNU General Public
10 # License.  See file COPYING for details.
11 #
12 #
13 #                   Version for system-less builds.                              #OMK:sysless.omk
14 #
15 #
16 # input variables                                                                #OMK:base.omk
17 # V                .. if set to 1, full command text is shown else short form is used
18 # W                .. whole tree - if set to 1, make is always called from the top-level directory
19 # SUBDIRS          .. list of subdirectories intended for make from actual directory
20 # default_CONFIG   .. list of default config assignments CONFIG_XXX=y/n ...
21 # LN_HEADERS       .. if "y", header files are symbolicaly linked instead of copied. #OMK:include.omk
22 # bin_PROGRAMS     .. list of the require binary programs                        #OMK:sysless.omk
23 # test_PROGRAMS    .. list of the test programs
24 # include_HEADERS  .. list of the user-space public header files
25 # lib_LIBRARIES    .. list of the user-space libraries
26 # lib_LDSCRIPTS    .. list of LD scripts that should be copied to the lib direcotry
27 # lib_obj_SOURCES  .. list of source files which should be compiled and
28 #                     the produced object file placed to the lib directory (e.g. crt0.S)
29 # shared_LIBRARIES .. list of the user-space shared libraries
30 # nobase_include_HEADERS .. public headers copied even with directory part
31 # renamed_include_HEADERS .. public headers copied to the different target name (xxx.h->yyy.h)
32 # utils_PROGRAMS   .. list of the development utility programs (compiled for host computer, this might change in future)
33 # xxx_SOURCES      .. list of specific target sources
34 # xxx_LIBS         .. list of specific target libraries
35 # INCLUDES         .. additional include directories and defines for user-space
36 # lib_LOADLIBES    .. list of libraries linked to each executable
37 # link_VARIANTS    .. list of ld script suffixes (after hypen `-') that
38 #                     should be used for linking (e.g. ram flash). If this is not
39 #                     specified, then the value of DEFAULT_LD_SCRIPT_VARIANT from config.target is used.
40 # PREFIX_DIR       .. Prefix to  directories in _compiled and _build. Used in config.omk.
41 # LOCAL_CONFIG_H   .. name of local config.h file generated from values          #OMK:config_h.omk
42 #                     of options defined in the current directory
43 # config_include_HEADERS .. names of global config files (possibly
44 #                     with subdirectories)
45 # xxx_DEFINES      .. list of config directives to be included in
46 #                     config header file of the name <somedir>/xxx.h
47 # DOXYGEN          .. if non-empty, generated headers includes Doxygen's @file
48 #                     command, so it is possible to document config
49 #                     variables.
50 # local_EVALUATE   .. Makefile hook, which is executed at the end of             #OMK:localeval.omk
51 #                     the Makefile.rules. Used only for dirty hacks.
52 OMK_RULES_TYPE=sysless                                                           #OMK:Makefile.rules.sysless@
53                                                                                  #OMK:base.omk@Makefile.rules.sysless
54 # If we are not called by OMK leaf Makefile...
55 ifndef MAKERULES_DIR
56 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
57 endif
58
59 # OUTPUT_DIR is the place where _compiled, _build and possible other
60 # files/directories are created. By default is the same as
61 # $(MAKERULES_DIR).
62 ifndef OUTPUT_DIR
63 OUTPUT_DIR := $(MAKERULES_DIR)
64 endif
65
66 # We need to ensure definition of sources directory first
67 ifndef SOURCES_DIR
68 # Only shell built-in pwd understands -L
69 SOURCES_DIR := $(shell ( pwd -L ) )
70 INVOCATION_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
71 INVOCATION_DIR := $(INVOCATION_DIR:/%=%)
72 INVOCATION_DIR := $(INVOCATION_DIR:\\%=%)
73 endif
74
75 .PHONY: all default check-make-ver print-hints omkize
76
77 ifdef W
78   ifeq ("$(origin W)", "command line")
79     OMK_WHOLE_TREE:=$(W)
80   endif
81 endif
82 ifndef OMK_WHOLE_TREE
83   OMK_WHOLE_TREE:=0
84 endif
85
86 ifneq ($(OMK_WHOLE_TREE),1)
87 all: check-make-ver print-hints default
88         @echo "Compilation finished"
89 else
90 # Run make in the top-level directory
91 all:
92         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
93 endif
94
95 ifdef OMK_TESTSROOT
96 # Usage: $(call canttest,<error message>)
97 define canttest
98         ( echo "$(1)" > $(OUTPUT_DIR)/_canttest; echo "$(1)"; exit 1 )
99 endef
100 else
101 define canttest
102         echo "$(1)"
103 endef
104 endif
105
106 #=========================
107 # Include the config file
108
109 ifndef CONFIG_FILE
110 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
111 endif
112
113 $(CONFIG_FILE)-default:
114         $(MAKE) default-config 
115
116 ifeq ($(MAKECMDGOALS),default-config)
117 export DEFAULT_CONFIG_PASS=1
118 endif
119
120 ifneq ($(DEFAULT_CONFIG_PASS),1)
121 include $(CONFIG_FILE)-default
122 endif
123
124 -include $(OUTPUT_DIR)/config.target
125
126 ifneq ($(wildcard $(CONFIG_FILE)),)
127 -include $(CONFIG_FILE)
128 endif
129
130
131 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
132
133
134 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR INVOCATION_DIR
135 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
136 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
137 # must to be serialized only in the toplevel make.
138
139 ifndef RELATIVE_DIR
140 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
141 endif
142 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
143 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
144 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
145 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
146 #override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
147 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
148
149 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
150 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
151 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
152
153 # We have to use RELATIVE_PREFIX because of mingw
154 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
155 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
156
157 #vpath %.c $(SOURCES_DIR)
158 #vpath %.cc $(SOURCES_DIR)
159 #vpath %.cxx $(SOURCES_DIR)
160
161 # Define srcdir for Automake compatibility
162 srcdir = $(SOURCES_DIR)
163
164 # Defines for quiet compilation
165 ifdef V
166   ifeq ("$(origin V)", "command line")
167     OMK_VERBOSE = $(V)
168   endif
169 endif
170 ifndef OMK_VERBOSE
171   OMK_VERBOSE = 0
172 endif
173 ifneq ($(OMK_VERBOSE),0)
174   Q =
175 else
176   Q = @
177 endif
178 ifneq ($(findstring s,$(MAKEFLAGS)),)
179   QUIET_CMD_ECHO = true
180   OMK_SILENT = 1
181 else
182   QUIET_CMD_ECHO = echo
183 endif
184
185 MAKEFILE_OMK=Makefile.omk
186 # All subdirectories (even linked ones) containing Makefile.omk
187 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
188 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
189
190 # ===================================================================
191 # We have set up all important variables, so we can check and include
192 # real OCERA style Makefile.omk now
193 ifndef OMK_INCLUDED
194 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
195 ifeq ($(AUTOMATIC_SUBDIRS),y)
196 SUBDIRS?=$(ALL_OMK_SUBDIRS)
197 endif
198 OMK_INCLUDED := 1
199 endif
200
201 print-hints:
202         @echo 'Use "make V=1" to see the verbose compile lines.'
203
204 check-make-ver:
205         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
206         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
207                 echo "Your make program version is too old and does not support OMK system." ; \
208                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
209         fi
210
211 distclean dist-clean:
212         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
213         @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
214
215 # Common OMK templates
216 # ====================
217
218 # Syntax: $(call mkdir,<dir name>)
219 define mkdir_def
220         [ -d $(1) ] || mkdir -p $(1) || exit 1
221 endef
222
223 ifneq ($(OMK_VERBOSE),2)
224 NO_PRINT_DIRECTORY := --no-print-directory
225 endif
226
227 ifeq ($(USE_LEAF_MAKEFILES),n)
228 export USE_LEAF_MAKEFILES
229 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
230 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
231 else
232 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
233 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
234 endif
235
236 pass = $(strip $(1))
237
238 unexport SUBDIRS
239
240 # Call a pass in a subdirectory
241 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
242 define omk_pass_subdir_template
243 .PHONY: $(pass)-$(3)-subdir
244 $(pass)-submakes: $(pass)-$(3)-subdir
245 $(pass)-$(3)-subdir: MAKEOVERRIDES:=$(filter-out SUBDIRS=%,$(MAKEOVERRIDES))
246 $(pass)-$(3)-subdir:
247         @$(call mkdir_def,$(2)/$(3))
248         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
249                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
250                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
251 # In subdirectories we can call submakes directly since passes are
252 # already serialized on the toplevel make.
253 endef
254
255 ifdef OMK_TESTSROOT
256 check-target = $(1:%=%-check)
257 endif
258
259 # Call a pass in a subdirectory
260 # Usage: $(call extra_rules_subdir_template,<subdir>)
261 define extra_rules_subdir_template
262 extra-rules-subdirs: extra-rules-$(1)
263 extra-rules-$(1):
264         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
265                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
266 endef
267
268 .PHONY: extra-rules-subdirs
269 extra-rules-subdirs:
270
271 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
272
273 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
274 define omk_pass_template
275 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
276 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
277 $(pass):
278 # Submakes have to be called this way and not as dependecies for pass
279 # serialization to work
280         +@$(MAKE) SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
281                 RELATIVE_DIR=$(RELATIVE_DIR) \
282                 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
283 $(pass)-submakes:
284         @true                   # Do not emit "nothing to be done" messages
285
286 ifneq ($(4)$($(pass)_HOOKS),)
287 $(pass)-submakes: $(pass)-this-dir
288 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
289         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
290         @$(call mkdir_def,$(2))
291         +@$(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
292                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
293 $(pass)-local: $($(pass)_HOOKS)
294 endif
295 endef
296
297 # =======================
298 # DEFAULT CONFIG PASS
299
300 default-config:
301         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
302         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
303         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
304         @echo >> "$(CONFIG_FILE)-default"
305         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
306                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
307                 -f $(OUTPUT_DIR)/Makefile default-config-pass
308
309 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
310
311 default-config-pass-local:
312 #       @echo Default config for $(RELATIVE_DIR)
313         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
314         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
315                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
316
317
318 omkize:
319         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
320            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
321         fi
322         $(Q)for i in `find -L . -name Makefile.omk` ; do \
323            d=`dirname $${i}`; \
324            if ! test -f "$${d}/Makefile.rules" && ( test -f "$${d}/Makefile" && ! cmp --silent Makefile "$${d}/Makefile" ); then \
325               rm -f "$${d}/Makefile"; \
326               cp -v Makefile "$${d}/Makefile"; \
327            fi \
328         done
329                                                                                  #OMK:gcc.omk@Makefile.rules.sysless
330 # Rules for compilation of C, C++ and assembler sources using GNU
331 # toolchain.
332
333 # Interface to other rules:
334
335 # Input variables:
336 # LIB_DIR - directory where compiled libraries are stored
337 # OBJS_DIR - directory where intermediate files (.o, .map, ...) are stored
338 # INCLUDE_DIR - where includes can be found
339 # from config.omk or Makefile.omk
340 # CROSS_COMPILE - 
341 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
342 # from base: SOURCES_DIR
343 # from Makefile.omk: lib_LOADLIBES
344
345 # Output variables:
346 # SOURCES - all the source files that needs to be compiled (except for shared library sources)
347 # SOLIB_SOURCES - all the source files that needs to be compiled for a shared library
348 # OBJ_EXT - extension of object files
349 # LIB_EXT - extension of library files
350 # LIB_PREF - prefix for library files
351 # ASM_EXT - extension of assembler sources
352
353 # Templates:
354 # COMPILER_DEFS_template - definitions that should be defined before
355 # the following templates can be used. The input variables needs to be
356 # defined before evaluating this template
357
358 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
359 # templates that create rules for compilation of sources
360
361 # CMETRIC_o_h_template - FIXME
362
363 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
364 # create rules for compilation of a program, library and shared
365 # library. The rules can use rules produced by COMPILE_xxx_template.
366
367 define COMPILER_DEFS_template
368 OBJ_EXT = .o
369 LIB_EXT = .a
370 LIB_PREF = lib
371 ASM_EXT = .S
372
373 CC = $(CROSS_COMPILE)gcc
374 LINK = $(CROSS_COMPILE)ld
375 AR = $(CROSS_COMPILE)ar
376 OBJCOPY = $(CROSS_COMPILE)objcopy
377 NM = $(CROSS_COMPILE)nm
378
379 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
380 CFLAGS  += -Wall
381 CFLAGS += -I$(SOURCES_DIR)
382 CFLAGS += -I$(INCLUDE_DIR)
383
384 LOADLIBES += -L$(LIB_DIR)
385 LOADLIBES += $(lib_LOADLIBES:%=-l%)
386
387
388 -include $(OBJS_DIR)/*.d
389
390 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
391
392 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
393
394 #%.lo: %.c
395 #       $(CC) -o $@ $(LCFLAGS) -c $<
396
397 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
398         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
399
400 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
401         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
402
403 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
404         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
405
406
407 # Check GCC version for user build
408 ifndef CC_MAJOR_VERSION
409 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
410 endif
411 # Prepare suitable define for dependency building
412 ifeq ($$(CC_MAJOR_VERSION),2)
413 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
414 else
415 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
416 endif
417
418 endef # COMPILER_DEFS_template
419
420
421 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
422 define COMPILE_c_o_template
423 $(2): $(1) $$(GEN_HEADERS)
424         @$(QUIET_CMD_ECHO) "  CC      $$@"
425         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
426         then mv -f "$$@.d.tmp" "$$@.d" ; \
427         else rm -f "$$@.d.tmp" ; exit 1; \
428         fi
429 endef
430
431
432
433 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
434 define COMPILE_cc_o_template
435 $(2): $(1) $$(GEN_HEADERS)
436         @$(QUIET_CMD_ECHO) "  CXX     $$@"
437         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
438         then mv -f "$$@.d.tmp" "$$@.d" ; \
439         else rm -f "$$@.d.tmp" ; exit 1; \
440         fi
441 endef
442
443 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
444 define COMPILE_S_o_template
445 $(2): $(1) $$(GEN_HEADERS)
446         @$(QUIET_CMD_ECHO) "  AS      $$@"
447         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
448         then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
449         else rm -f "$$@.d.tmp" ; exit 1; \
450         fi
451 endef
452
453 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
454 define CMETRIC_o_h_template
455 $(2): $(1)
456         @$(QUIET_CMD_ECHO) "  CMETRIC $$@"
457         $(Q)if [ -n `dirname $$@` ] ; then \
458               if [ ! -e `dirname $$@` ] ; then \
459                 mkdir -p `dirname $$@` ; fi ; fi
460         $(Q)echo >$$@.tmp '/* Automatically generated from $$< */'
461         $(Q)echo >>$$@.tmp '/* Conditionals to control compilation */'
462 # Bellow, the tricks with redirection are for shells without set -o pipefail
463 # (see http://www.mail-archive.com/dash@vger.kernel.org/msg00149.html)
464         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
465                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
466                 | sort >>$$@.tmp` && exit $$$$status
467         $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in hexadecimal format */'
468         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
469                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
470                 | sort >>$$@.tmp` && exit $$$$status
471         $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in decimal format */'
472         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) -td $$<; echo $$$$? >&4; }\
473                 | sed -n 's/^ *0*\(0\|[1-9][0-9]*\) *A *_cmetric2defdec_\([A-Za-z_0-9]*\) */#define \2 \1/p' \
474                 | sort >>$$@.tmp` && exit $$$$status
475         $(Q)mv $$@.tmp $$@
476 endef
477
478 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
479 define PROGRAM_template
480
481 GEN_SOURCES += $$($(1)_GEN_SOURCES)
482
483 $(foreach x, $(SOURCES2OBJS),
484 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
485                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
486 )
487 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
488
489 SOURCES += $$($(1)_SOURCES)
490
491 ifneq ($(LD_SCRIPT),)
492 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
493 endif
494
495 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
496         @$(QUIET_CMD_ECHO) "  LINK    $$@"
497         $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
498           then echo $$(CC)  $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS)   $$(CFLAGS) ; \
499           else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
500           $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
501           $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
502           -o $$@
503         @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
504         @if [ -n "$(LD_SCRIPT)" ]; then \
505           echo "  $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
506         @sed -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134'  >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
507         @echo >>$(OBJS_DIR)/$(1).exe.d
508 endef
509
510 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
511 %.bin: %
512         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
513         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
514
515 %.hex: %
516         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
517         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
518
519 %.srec: %
520         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
521         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
522
523 # Syntax: $(call LIBRARY_template,<library-name>)
524 define LIBRARY_template
525
526 GEN_SOURCES += $$($(1)_GEN_SOURCES)
527
528 $(foreach x, $(SOURCES2OBJS),
529 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
530                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
531 )
532 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
533
534 SOURCES += $$($(1)_SOURCES)
535
536 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
537         @$(QUIET_CMD_ECHO) "  AR      $$@"
538         $(Q) $(AR) rcs $$@ $$^
539 endef
540
541
542 # Syntax: $(call SOLIB_template,<library-name>)
543 define SOLIB_template
544
545 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
546
547 $(foreach x, $(SOURCES2OBJSLO),
548 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
549                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
550 )
551 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
552
553 SOLIB_OBJS  += $$($(1)_OBJSLO)
554 SOLIB_SOURCES += $$($(1)_SOURCES)
555
556 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
557         @$(QUIET_CMD_ECHO) "  LINK    $$@"
558         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
559 endef
560 ifeq ($(OMK_VERBOSE),1)                                                          #OMK:include.omk@Makefile.rules.sysless
561 CPHEADER_FLAGS += -v
562 LNHEADER_FLAGS += -v
563 endif
564
565 ifneq ($(LN_HEADERS),y)
566 define cp_cmd
567 if ! cmp --quiet $(1) $(2); then \
568     echo "  CP      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
569     install -C -D $(CPHEADER_FLAGS) $(1) $(2) || exit 1; \
570 fi
571 endef
572 else
573 define cp_cmd
574 if ! cmp --quiet $(1) $(2); then \
575     echo "  LN      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
576     if [ -f $(1) ]; then d=$(2); mkdir -p $${d%/*} && ln -sf $(LNHEADER_FLAGS) $(1) $(2) || exit 1; else exit 1; fi; \
577 fi
578 endef
579 endif
580
581 # TODO: Check modification date of changed header files. If it is
582 # newer that in source dir, show a warning.
583
584 # Syntax: $(call include-pass-template,<include dir>,<keyword>)
585 define include-pass-template
586 include-pass-local: include-pass-local-$(2)
587 include-pass-local-$(2): $$($(2)_GEN_HEADERS) $$(foreach f,$$(renamed_$(2)_GEN_HEADERS),$$(shell f='$$(f)'; echo $$$${f%->*}))
588         @$$(foreach f, $$($(2)_HEADERS),$$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
589 # FIXME: Use correct build dir, then document it (in the line bellow)
590         @$$(foreach f, $$($(2)_GEN_HEADERS),$$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
591         @$$(foreach f, $$(nobase_$(2)_HEADERS), $$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(f)); )
592         @$$(foreach f, $$(renamed_$(2)_HEADERS), \
593            f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
594            $$(call cp_cmd,$$(SOURCES_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
595         @$$(foreach f, $$(renamed_$(2)_GEN_HEADERS), \
596            f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
597            $$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
598 # Suppress "Nothing to be done for `include-pass-local'" message if no headers are defined in Makefile.omk
599         @$$(if $$($(2)_HEADERS)$$($(2)_GEN_HEADERS)$$(nobase_$(2)_HEADERS)$$(renamed_$(2)_HEADERS)$$(renamed_$(2)_GEN_HEADERS),,true)
600 endef
601                                                                                  #OMK:sysless.omk@Makefile.rules.sysless
602 BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
603 COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
604
605 LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
606 #$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
607
608 #=====================================================================
609 # Common utility rules
610
611 link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
612
613
614 #=====================================================================
615 # Include correct rules for just running pass
616
617 USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
618
619 USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
620 USER_LIB_DIR     = $(USER_COMPILED_DIR_NAME)/lib
621 USER_UTILS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-utils
622 USER_TESTS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-tests
623 USER_BIN_DIR     = $(USER_COMPILED_DIR_NAME)/bin
624 USER_OBJS_DIR    = $(LOCAL_BUILD_DIR)
625
626 .PHONY: check-dir
627
628 # Some support to serialize some targets for parallel make
629 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
630 include-pass: check-dir
631 library-pass: include-pass
632 binary-pass utils-pass: library-pass
633
634 override OMK_SERIALIZE_INCLUDED = y
635 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
636 endif
637
638 # -------------------------------------
639 # Rules for compilation for target
640 ifdef TARGET_RULE_TEMPLATES
641
642 LDFLAGS += -nostartfiles
643
644  # FIXME: These are not used. What they are good for?
645 LIB_CPPFLAGS += $(CPPFLAGS)
646 LIB_CFLAGS   += $(CFLAGS)
647
648 SOLIB_PICFLAGS += -shared -fpic
649
650 CFLAGS += -DOMK_FOR_TARGET
651
652 INCLUDE_DIR := $(USER_INCLUDE_DIR)
653 LIB_DIR     := $(USER_LIB_DIR)
654 OBJS_DIR    := $(USER_OBJS_DIR)
655
656 $(eval $(COMPILER_DEFS_template))
657
658 # Special rules for CMETRIC generated headers
659
660 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\
661                 $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\
662                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),)))
663 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
664                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\
665                 $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh)))))
666
667 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
668
669 # Generate rules for compilation of programs and libraries
670 ifneq ($(link_VARIANTS),)
671 $(foreach prog,$(bin_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
672 $(foreach prog,$(test_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
673 else
674 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR))))
675 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR))))
676 endif
677
678 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
679 $(foreach src,$(lib_obj_SOURCES),$(eval $(call LIBOBJ_template,$(addsuffix $(OBJ_EXT),$(basename $(src))))))
680 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
681
682
683 # lib_obj_SOURCES handling
684 lib_OBJS = $(addsuffix $(OBJ_EXT),$(basename $(lib_obj_SOURCES)))
685 #$(warning lib_OBJS = $(lib_OBJS))
686 SOURCES += $(filter-out %$(OBJ_EXT),$(lib_obj_SOURCES))
687
688 $(LIB_DIR)/%$(OBJ_EXT): %$(OBJ_EXT)
689         @echo "  CP      $(^:$(MAKERULES_DIR)/%=%) -> $(@:$(MAKERULES_DIR)/%=%)"
690         $(Q)cp $(CP_FLAGS) $< $@
691
692
693 # User-space static libraries and applications object files
694 SOURCES := $(sort $(SOURCES))
695 #$(warning SOURCES = $(SOURCES))
696 GEN_SOURCES := $(sort $(GEN_SOURCES))
697
698 # User-space shared libraries object files
699 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
700 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
701 SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES))
702
703 # Create _build directories for sources in subdirectories i.e. *_SOURCES=dir/file.c
704 _dirs_to_create=$(filter-out ./,$(sort $(dir $(SOURCES) $(SOLIB_SOURCES))))
705 ifneq ($(_dirs_to_create),)
706 $(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create)))
707 endif
708
709 # The above generated rules produced $(SOURCES) and $(SOLIB_SOURCES)
710 # variables. Now generate rules for compilation of theese sources
711 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%$(OBJ_EXT)),)))
712 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
713 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
714 $(foreach src,$(filter %$(ASM_EXT),$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%$(OBJ_EXT)),)))
715 $(foreach src,$(filter %.c,$(GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),)))
716
717 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
718 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
719 $(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS))))
720 $(foreach src,$(filter %$(ASM_EXT),$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%.lo),$(SOLIB_PICFLAGS))))
721 $(foreach src,$(filter %.c,$(SOLIB_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
722
723 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \
724                     $(lib_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%$(LIB_EXT)) $(shared_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%.so) \
725                     $(addprefix $(LIB_DIR)/,$(lib_OBJS))
726
727 ifneq ($(link_VARIANTS),)
728 binary-pass-local:  $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link)) \
729                     $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link).$(of))))
730 else
731 binary-pass-local:  $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%) \
732                     $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%.$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%.$(of)))
733 endif
734
735 endif # TARGET_RULE_TEMPLATES
736
737
738 # -------------------------------------
739 # Rules for compilation utilities for host (user space)
740 ifdef HOST_RULE_TEMPLATES
741
742 CROSS_COMPILE =
743 TARGET_ARCH =
744
745 SOLIB_PICFLAGS += -shared -fpic
746
747 # For host compilation, we don't use a specfic ld script
748 LD_SCRIPT =
749
750 # TODO: It is probably better to use different directories for host
751 # includes, libraries and objects
752 INCLUDE_DIR := $(USER_INCLUDE_DIR)
753 LIB_DIR     := $(USER_LIB_DIR)
754 OBJS_DIR    := $(USER_OBJS_DIR)
755
756 $(eval $(COMPILER_DEFS_template))
757
758  #User-space static libraries and applications object files
759 #SOURCES := $(sort $(SOURCES))
760 #$(warning SOURCES = $(SOURCES))
761
762 # Generate rules for compilation of utility programs
763 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),)))
764
765 # The above generated rule produced $(SOURCES) variable. Now generate
766 # rules for compilation of theese sources
767 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%$(OBJ_EXT)),)))
768 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
769 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
770 $(foreach src,$(filter %$(ASM_EXT),$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%$(OBJ_EXT)),)))
771
772 utils-pass-local: $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
773
774 endif # HOST_RULE_TEMPLATES
775
776 # Checks for OMK tester
777 ifdef OMK_TESTSROOT
778 default-config-pass-check include-pass-check:
779 library-pass-check binary-pass-check:
780         @[ -x "$(shell which $(CC))" ] || $(call canttest,Cannot find compiler: $(CC))
781 endif
782
783 #=====================================================================
784 # Automatic loading of compiled program by issuing "make load"
785
786 ifneq ($(OUTPUT_FORMATS),)
787 # Select a file extension (e.g. .bin) for "make load" command to load.
788 LOAD_EXTENSION = .$(firstword $(OUTPUT_FORMATS))
789 endif
790
791 # Syntax: $(call LOAD_PROGRAM_template,<executable-name>,<dir>,<link-variant>)
792 # Used to load program to the target hardware
793 define LOAD_PROGRAM_template
794 .PHONY: load-$(1)$(3:%=-%)
795 load-$(1)$(3:%=-%): $(2)/$(1)$(3:%=-%)$(if $(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION))
796         @$(QUIET_CMD_ECHO) "  LOAD    $$<"
797         @if [ -z '$$(LOAD_CMD$(3:%=-%))' ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
798         $(Q) $$(LOAD_CMD$(3:%=-%)) $$<
799 endef
800
801 # Syntax: $(call LOAD__RUN_VARIANT_template,<link-variant>)
802 # Used to load and/or run non-default variant of the default program
803 define LOAD_RUN_VARIANT_template
804 .PHONY: load-$(1) run-$(1)
805
806 load-$(1): load-$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(1)
807
808 run-$(1):
809         @$(QUIET_CMD_ECHO) "  RUN     $(1)"
810         @if [ -z "$(RUN_CMD-$(1))" ]; then echo "No command for running '$(1)' variant is specified."; exit 1; fi
811         $(Q) $(RUN_CMD-$(1))
812
813 endef
814
815 $(foreach link,$(link_VARIANTS),$(foreach prog,$(bin_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
816 $(foreach link,$(link_VARIANTS),$(foreach prog,$(test_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
817 $(foreach link,$(link_VARIANTS),$(eval $(call LOAD_RUN_VARIANT_template,$(link))))
818
819 .PHONY: load run
820 load: $(addprefix load-,$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(firstword $(link_VARIANTS)))
821
822 run: run-$(firstword $(link_VARIANTS))
823
824
825
826 #=====================================================================
827 # Generate pass rules from generic templates
828
829 $(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)))
830 $(eval $(call omk_pass_template, library-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(lib_obj_SOURCES)))
831 $(eval $(call omk_pass_template, binary-pass,  $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(bin_PROGRAMS) $(test_PROGRAMS)))
832 $(eval $(call omk_pass_template, utils-pass,   $(LOCAL_BUILD_DIR),HOST_RULE_TEMPLATES=y,$(utils_PROGRAMS)))
833 $(eval $(call omk_pass_template, dep,    $(LOCAL_BUILD_DIR),,always))
834 $(eval $(call omk_pass_template, clean,  $(LOCAL_BUILD_DIR),,always))
835 $(eval $(call omk_pass_template, install,$(LOCAL_BUILD_DIR),,always))
836
837
838 dep-local:
839
840 install-local:
841
842 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
843
844 include-pass-local:
845         @$(foreach f, $(lib_LDSCRIPTS), cmp --quiet $(SOURCES_DIR)/$(f) $(USER_LIB_DIR)/$(notdir $(f)) \
846            || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(USER_LIB_DIR)/$(notdir $(f))) || exit 1 ; )
847
848
849 .PHONY: clean-custom
850 clean-local: clean-custom
851         $(Q)rm -f $(USER_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.lo \
852                $(USER_OBJS_DIR)/*.d \
853                $(USER_OBJS_DIR)/*.map \
854                $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
855
856 check-dir::
857         @$(call mkdir_def,$(USER_INCLUDE_DIR))
858         @$(call mkdir_def,$(USER_LIB_DIR))
859         @$(call mkdir_def,$(USER_BIN_DIR))
860         @$(call mkdir_def,$(USER_UTILS_DIR))
861         @$(call mkdir_def,$(USER_TESTS_DIR))
862
863 include-pass-submakes: extra-rules-subdirs
864 # Which passes to pass
865 default: include-pass library-pass binary-pass utils-pass
866                                                                                  #OMK:config_h.omk@Makefile.rules.sysless
867 # Syntax: $(call BUILD_CONFIG_H_template,<stamp_dir>,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
868 define BUILD_CONFIG_H_template
869
870 $(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(CONFIG_FILES)
871         @$(QUIET_CMD_ECHO) "  CONFGEN $(notdir $(2))"
872         @if [ ! -d `dirname $(2).tmp` ] ; then \
873                 mkdir -p `dirname $(2).tmp` ; fi
874         @echo "/* Automatically generated from */" > "$(2).tmp"
875         @echo "/* config files: $$(^:$(OUTPUT_DIR)/%=%) */" >> "$(2).tmp"
876         $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp")
877         @echo "#ifndef $(4)" >> "$(2).tmp"
878         @echo "#define $(4)" >> "$(2).tmp"
879         @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | sed -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \
880                 echo '$(x).$($(x))' ; ) echo ; ) | \
881                 sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
882                 sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
883                   >> "$(2).tmp"
884         @echo "#endif /*$(4)*/" >> "$(2).tmp"
885         @touch "$$@"
886         @if cmp --quiet "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \
887         else mv "$(2).tmp" "$(2)" ; \
888         echo "Updated configuration $(2)" ; fi
889
890 endef
891
892 ifdef LOCAL_CONFIG_H
893
894 # This must be declared after the default cflags are assigned!
895 # Override is used to override command line assignemnt.
896 override CFLAGS += -I $(USER_OBJS_DIR)
897 override kernel_INCLUDES += -I $(KERN_OBJS_DIR)
898 $(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
899
900 endif
901
902 # Special rules for configuration exported headers
903
904 #FIXME: The directory for headers should not be specified here.
905 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
906 _$(basename $(notdir $(confh)))_H \
907 )))
908
909 config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
910
911 # Add some hooks to standard passes
912 include-pass-local: $(config_h_stamp_files)
913
914 ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),)
915
916 ifdef LOCAL_CONFIG_H
917 $(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
918 endif
919
920 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
921 _$(basename $(notdir $(confh)))_H \
922 )))
923
924 kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
925
926 # Add some hooks to standard passes
927 include-pass-local: $(kern_config_h_stamp_files)
928
929 endif
930
931 clean-local: clean-local-config-h
932
933 clean-local-config-h:
934         @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\
935             if [ -e $(confh) ] ; then rm $(confh) ; fi ; \
936         )
937                                                                                  #OMK:sources-list.omk@Makefile.rules.sysless
938 # Rules that creates the list of files which are used during
939 # compilation. The list reflects conditional compilation depending on
940 # config.omk and other variables.
941
942 SOURCES_LIST_FN=sources.txt
943 ifndef SOURCES_LIST
944 SOURCES_LIST_DIR:=$(RELATIVE_DIR)
945 SOURCES_LIST:=$(OUTPUT_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN)
946 SOURCES_LIST_D := $(LOCAL_BUILD_DIR)/$(SOURCES_LIST_FN).d
947 export SOURCES_LIST SOURCES_LIST_DIR SOURCES_LIST_D
948 endif
949
950 ifneq ($(filter sources-list TAGS tags cscope,$(MAKECMDGOALS)),)
951 NEED_SOURCES_LIST=y
952 endif
953
954 ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is not useful
955 .PHONY: sources-list
956 sources-list: $(SOURCES_LIST)
957
958 $(SOURCES_LIST): $(CONFIG_FILES) $(shell find -name $(MAKEFILE_OMK))
959         @$(call mkdir_def,$(dir $(SOURCES_LIST_D)))
960         @echo -n "" > "$(SOURCES_LIST).tmp"
961         @echo -n "" > "$(SOURCES_LIST_D).tmp"
962         @$(MAKE) --no-print-directory sources-list-pass
963         @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2"
964         @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2"
965         @rm "$(SOURCES_LIST).tmp"
966         @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)"
967         @echo "$(SOURCES_LIST): \\" > "$(SOURCES_LIST_D).tmp2"
968         @cat "$(SOURCES_LIST_D).tmp"|grep -v "$(SOURCES_LIST_D).tmp"|sort|uniq|\
969                 sed -e 's/$$/\\/' >> "$(SOURCES_LIST_D).tmp2"
970         @rm "$(SOURCES_LIST_D).tmp"
971         @mv "$(SOURCES_LIST_D).tmp2" "$(SOURCES_LIST_D)"
972 endif
973
974 $(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always))
975
976 sources-list-pass-local:
977         @$(foreach m,$(MAKEFILE_LIST),echo '  $(m)' >> "$(SOURCES_LIST_D).tmp";)
978         @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\
979           echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(h)" >> "$(SOURCES_LIST).tmp";)
980         @$(foreach ch,$(config_include_HEADERS), \
981           echo "$(USER_INCLUDE_DIR:$(OUTPUT_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";)
982         @$(foreach h,$(renamed_include_HEADERS),echo '$(h)'|sed -e 's|\(.*\)->.*|$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))\1|' >> "$(SOURCES_LIST).tmp";)
983         @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(test_PROGRAMS) $(utils_PROGRAMS) \
984           $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\
985           $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(src)" >> "$(SOURCES_LIST).tmp";))
986
987 ############ TAGS ###########
988
989 ifeq ($(MAKECMDGOALS),TAGS)
990 ETAGS=etags
991 TAGS_CMD = $(ETAGS)
992 TAGS: $(SOURCES_LIST)
993         @$(MAKE) --no-print-directory do-tags
994 endif
995 ifeq ($(MAKECMDGOALS),tags) 
996 CTAGS=ctags -N
997 TAGS_CMD = $(CTAGS)
998 tags: $(SOURCES_LIST)
999         @$(MAKE) --no-print-directory do-tags
1000 endif
1001 export TAGS_CMD
1002
1003 ifeq ($(MAKECMDGOALS),do-tags)
1004 .PHONY: do-tags
1005 do-tags: $(shell sed -e '/^\#/d' $(SOURCES_LIST))
1006         @$(QUIET_CMD_ECHO) "  TAGS    $(SOURCES_LIST_FN)"
1007         $(Q)$(TAGS_CMD) $^
1008 endif
1009
1010 ############ CSCOPE ###########
1011
1012 cscope: $(SOURCES_LIST)
1013         @$(QUIET_CMD_ECHO) "  CSCOPE  < $(SOURCES_LIST_FN)"
1014         $(Q)sed -e '/^#/d' $(SOURCES_LIST) > cscope.files
1015         $(Q)cscope -b -icscope.files
1016 #FIXME: see doc to -i in cscope(1)
1017                                                                                  #OMK:localeval.omk@Makefile.rules.sysless
1018 ifneq ($(local_EVALUATE),)
1019 #$(warning $(local_EVALUATE))
1020 $(eval $(local_EVALUATE))
1021 endif