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