]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control-pxmc.git/blob - src/Makefile.rules
RPi PXMC Test: Adapt GPIO mapping to support Raspberry Pi model 2.
[fpga/rpi-motor-control-pxmc.git] / src / 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, 2011, 2013 by Michal Sojka - Czech Technical University, FEE, DCE
5 #
6 #  Homepage: http://rtime.felk.cvut.cz/omk/
7 #  Version:  0.2-161-g098e555
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 Linux/RTLinux builds.                            #OMK:linux.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 # wvtest_SCRIPTS    .. list of scripts producing wvtest output                   #OMK:wvtest.omk
22 # wvtest_PROGRAMS   .. list of the testing programs producing wvtest output
23 # LN_HEADERS       .. if "y", header files are symbolicaly linked instead of copied. #OMK:include.omk
24 # input variables                                                                #OMK:linux.omk
25 # lib_LIBRARIES    .. list of the user-space libraries
26 # shared_LIBRARIES .. list of the user-space shared libraries
27 # include_HEADERS  .. list of the user-space public header files
28 # nobase_include_HEADERS .. public headers copied even with directory part
29 # renamed_include_HEADERS .. public headers copied to the different target name
30 # bin_PROGRAMS     .. list of the require binary programs
31 # utils_PROGRAMS   .. list of the development utility programs
32 # test_PROGRAMS    .. list of the testing programs
33 # bin_SCRIPTS      .. list of scripts to be copied to _compiled/bin
34 # xxx_SOURCES      .. list of specific target sources
35 # xxx_GEN_SOURCES  .. list of specific target sources that are generated in the build directory
36 # xxx_LIBS         .. list of specific target libraries (-l prefix is automatically added)
37 # xxx_LDFLAGS      .. list of specific target LDFLAGS
38 # lib_LOADLIBES    .. list of libraries linked to each executable
39 # INCLUDES         .. additional include directories and defines for user-space
40 #
41 # OMK_CFLAGS        .. C compiler flags
42 # OMK_CXXFLAGS      .. C++ compiler flags
43 # OMK_CPPFLAGS     .. C preprocessor flags
44 # LDFLAGS          .. linker flags for programs linking
45 # kernel_LIBRARIES .. list of the kernel-space libraries                         #OMK:linux-kernel.omk
46 # rtlinux_LIBRARIES.. list of the RT-Linux kernel-space libraries
47 # kernel_HEADERS   .. list of the kernel-space public header files
48 # rtlinux_HEADERS  .. list of the RT-Linux kernel-space public header files
49 # kernel_MODULES   .. list of the kernel side modules/applications
50 # rtlinux_MODULES  .. list of RT-Linux the kernel side modules/applications
51 # kernel_INCLUDES  .. additional include directories and defines for kernel-space
52 # rtlinux_INCLUDES .. additional include directories and defines for RT-Linux
53 # OMIT_KERNEL_PASSES  if defined, all kernel passes are omited
54 # LINUX_DIR        .. location of Linux kernel sources
55 # RTL_DIR          .. location of RT-Linux sources
56 # cmetric_include_HEADERS    .. generate header file with offsets and sizes of structure fields #OMK:cmetric.omk
57 # xxx_CMETRIC_SOURCES        .. the source cmetric generation
58 # LOCAL_CONFIG_H   .. name of local config.h file generated from values          #OMK:config_h.omk
59 #                     of options defined in the current directory
60 # config_include_HEADERS .. names of global config files (possibly
61 #                     with subdirectories)
62 # xxx_DEFINES      .. list of config directives to be included in
63 #                     config header file of the name <somedir>/xxx.h
64 # DOXYGEN          .. if non-empty, generated headers includes Doxygen's @file
65 #                     command, so it is possible to document config
66 #                     variables.
67 # QT_PROJECTS      .. list of QT .pro file to use for compilation                  #OMK:qt.omk
68 # QT_SUBDIRS       .. subdirectories where to build QT applications using qmake (depricated)
69 # QTDIR            .. where QT resides
70 OMK_RULES_TYPE=linux                                                             #OMK:Makefile.rules.linux@
71                                                                                  #OMK:base.omk@Makefile.rules.linux
72 # If we are not called by OMK leaf Makefile...
73 ifndef MAKERULES_DIR
74 MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST))))
75 endif
76
77 # The $(SED4OMK) command for BSD based systems requires -E option to allow
78 # extended regular expressions
79
80 SED4OMK ?= sed
81 ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
82   SED4OMK := $(SED4OMK) -E
83   ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
84     SED4OMK := gsed
85   endif
86   ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
87     SED4OMK := gsed -E
88   endif
89   ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y)
90     $(error No SED program suitable for OMK found)
91   endif
92 endif
93
94 # OUTPUT_DIR is the place where _compiled, _build and possible other
95 # files/directories are created. By default is the same as
96 # $(MAKERULES_DIR).
97 ifndef OUTPUT_DIR
98 OUTPUT_DIR := $(MAKERULES_DIR)
99 endif
100
101 # We need to ensure definition of sources directory first
102 ifndef SOURCES_DIR
103 # Only shell built-in pwd understands -L
104 SOURCES_DIR := $(shell ( pwd -L ) )
105 INVOCATION_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
106 INVOCATION_DIR := $(INVOCATION_DIR:/%=%)
107 INVOCATION_DIR := $(INVOCATION_DIR:\\%=%)
108 endif
109
110 .PHONY: all default check-make-ver print-hints omkize
111
112 ifdef W
113   ifeq ("$(origin W)", "command line")
114     OMK_WHOLE_TREE:=$(W)
115   endif
116 endif
117 ifndef OMK_WHOLE_TREE
118   OMK_WHOLE_TREE:=0
119 endif
120
121 ifneq ($(OMK_WHOLE_TREE),1)
122 all: check-make-ver print-hints default
123         @echo "Compilation finished"
124 else
125 # Run make in the top-level directory
126 all:
127         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
128 endif
129
130 # omk-get-var target allows external scripts/programs to determine the
131 # values of OMK variables such as RELATIVE_DIR etc.
132 .PHONY: omk-get-var
133 omk-get-var:
134         @$(foreach var,$(VAR),echo $(var)=$($(var));)
135
136 #=========================
137 # Include the config file
138
139 ifndef CONFIG_FILE
140 CONFIG_FILE      := $(OUTPUT_DIR)/config.omk
141 endif
142
143 $(CONFIG_FILE)-default:
144         $(MAKE) default-config 
145
146 ifeq ($(MAKECMDGOALS),default-config)
147 export DEFAULT_CONFIG_PASS=1
148 endif
149
150 ifneq ($(DEFAULT_CONFIG_PASS),1)
151 include $(CONFIG_FILE)-default
152 endif
153
154 -include $(OUTPUT_DIR)/config.target
155
156 ifneq ($(wildcard $(CONFIG_FILE)),)
157 -include $(CONFIG_FILE)
158 endif
159
160
161 CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE))
162
163
164 export SED4OMK SOURCES_DIR MAKERULES_DIR RELATIVE_DIR INVOCATION_DIR
165 export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
166 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
167 # must to be serialized only in the toplevel make.
168
169 ifndef RELATIVE_DIR
170 RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%)
171 endif
172 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
173 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
174 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
175 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
176 #override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | $(SED4OMK) -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
177 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
178
179 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
180 #$(warning MAKERULES_DIR = "$(OUTPUT_DIR)")
181 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
182
183 # We have to use RELATIVE_PREFIX because of mingw
184 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
185 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
186
187 #vpath %.c $(SOURCES_DIR)
188 #vpath %.cc $(SOURCES_DIR)
189 #vpath %.cxx $(SOURCES_DIR)
190
191 # Define srcdir for Automake compatibility
192 srcdir = $(SOURCES_DIR)
193
194 # Defines for quiet compilation
195 ifdef V
196   ifeq ("$(origin V)", "command line")
197     OMK_VERBOSE = $(V)
198   endif
199 endif
200 ifndef OMK_VERBOSE
201   OMK_VERBOSE = 0
202 endif
203 ifneq ($(OMK_VERBOSE),0)
204   Q =
205 else
206   Q = @
207 endif
208 ifneq ($(findstring s,$(MAKEFLAGS)),)
209   QUIET_CMD_ECHO = true
210   OMK_SILENT = 1
211 else
212   QUIET_CMD_ECHO = echo
213 endif
214
215 MAKEFILE_OMK=Makefile.omk
216 # All subdirectories (even linked ones) containing Makefile.omk
217 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
218 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
219
220 # ===================================================================
221 # We have set up all important variables, so we can check and include
222 # real OCERA style Makefile.omk now
223 ifndef OMK_INCLUDED
224 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
225 ifeq ($(AUTOMATIC_SUBDIRS),y)
226 SUBDIRS?=$(ALL_OMK_SUBDIRS)
227 endif
228 OMK_INCLUDED := 1
229 endif
230
231 print-hints:
232         @echo 'Use "make V=1" to see the verbose compile lines.'
233
234 check-make-ver:
235         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | $(SED4OMK) -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
236         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
237                 echo "Your make program version ($(MAKE_VERSION)) is too old and does not support OMK system." ; \
238                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
239         fi
240
241 distclean dist-clean:
242         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
243         @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)  $(OUTPUT_DIR)/$(BUILD_DIR_NAME)
244
245 # Common OMK templates
246 # ====================
247
248 # Syntax: $(call mkdir,<dir name>)
249 define mkdir_def
250         [ -d $(1) ] || mkdir -p $(1) || exit 1
251 endef
252
253 ifneq ($(OMK_VERBOSE),2)
254 NO_PRINT_DIRECTORY := --no-print-directory
255 endif
256
257 ifeq ($(USE_LEAF_MAKEFILES),n)
258 export USE_LEAF_MAKEFILES
259 SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
260 SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules
261 else
262 SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile
263 SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile
264 endif
265
266 pass = $(strip $(1))
267
268 unexport SUBDIRS
269
270 # Call a pass in a subdirectory
271 # Usage: $(call omk_pass_subdir_template,<pass name>,<build dir>,<subdir>)
272 define omk_pass_subdir_template
273 .PHONY: $(pass)-$(3)-subdir
274 $(pass)-submakes: $(pass)-$(3)-subdir
275 $(pass)-$(3)-subdir: MAKEOVERRIDES:=$(filter-out SUBDIRS=%,$(MAKEOVERRIDES))
276 $(pass)-$(3)-subdir:
277         @$(call mkdir_def,$(2)/$(3))
278         +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \
279                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \
280                 -f $(SUBDIR_MAKEFILE) $(pass)-submakes
281 # In subdirectories we can call submakes directly since passes are
282 # already serialized on the toplevel make.
283 endef
284
285 ifdef OMK_TESTSROOT
286 check-target = $(1:%=%-check)
287 endif
288
289 # Call a pass in a subdirectory
290 # Usage: $(call extra_rules_subdir_template,<subdir>)
291 define extra_rules_subdir_template
292 extra-rules-subdirs: extra-rules-$(1)
293 extra-rules-$(1):
294         +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \
295                 SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1)
296 endef
297
298 .PHONY: extra-rules-subdirs
299 extra-rules-subdirs:
300
301 $(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir))))
302
303 # Usage: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local enable condition>])
304 define omk_pass_template
305 .PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes
306 $(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir))))
307 $(pass):
308 # Submakes have to be called this way and not as dependecies for pass
309 # serialization to work
310         +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \
311                 RELATIVE_DIR=$(RELATIVE_DIR) \
312                 -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes
313 $(pass)-submakes:
314         @true                   # Do not emit "nothing to be done" messages
315
316 ifneq ($(4)$($(pass)_HOOKS),)
317 $(pass)-submakes: $(pass)-this-dir
318 $(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir)
319         +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)"
320         @$(call mkdir_def,$(2))
321         +@$(MAKE) --no-builtin-rules $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
322                 -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local)
323 ifneq ($(pass),clean)
324 $(pass)-local: $($(pass)_HOOKS)
325 endif
326 endif
327 endef
328
329 # =======================
330 # DEFAULT CONFIG PASS
331
332 default-config:
333         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
334         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
335         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
336         @echo >> "$(CONFIG_FILE)-default"
337         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \
338                 RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \
339                 -f $(OUTPUT_DIR)/Makefile default-config-pass
340
341 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
342
343 default-config-pass-local:
344 #       @echo Default config for $(RELATIVE_DIR)
345         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
346         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
347                 $(SED4OMK) -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
348
349
350 omkize:
351         $(Q)if ! grep -q MAKERULES_DIR Makefile; then \
352            echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \
353         fi
354         $(Q)for i in `find -L . -name Makefile.omk` ; do \
355            d=`dirname $${i}`; \
356            if ! test -f "$${d}/Makefile.rules" && ! cmp -s Makefile "$${d}/Makefile"; then \
357               rm -f "$${d}/Makefile"; \
358               cp -v Makefile "$${d}/Makefile"; \
359            fi \
360         done
361                                                                                  #OMK:wvtest.omk@Makefile.rules.linux
362 ifndef WVTEST_LIBRARY
363 WVTEST_LIBRARY = wvtest
364 endif
365
366 # Documentation: wvtest_PROGRAMS is amost equivalent to test_PROGRAMS.
367 # The two differences are that the program is automatically linked
368 # with $(WVTEST_LIBRARY) and it is run during "make wvtest".
369 test_PROGRAMS += $(wvtest_PROGRAMS)
370
371 # Documentation: If your project uses wvtest, it is recomended to put
372 # the "test: wvtest" rule to config.target.
373 wvtest:
374         $(Q)$(MAKERULES_DIR)/wvtestrun $(MAKE) wvtest-pass
375
376 .PHONY: wvtest
377
378 $(eval $(call omk_pass_template,wvtest-pass,$$(LOCAL_BUILD_DIR),,$(wvtest_SCRIPTS)$(wvtest_PROGRAMS)))
379
380 # Usage: $(call wvtest_template,<shell command>)
381 define wvtest_template
382 wvtest-pass-local: wvtest-run-$(1)
383 .PHONY: wvtest-run-$(1)
384 wvtest-run-$(1):
385         $(Q)echo "Testing \"Run $(1)\" in Makefile.rules:"
386         $(Q)mkdir -p $(1).wvtest
387         $(Q)cd $(1).wvtest && \
388           PATH=$$(USER_BIN_DIR):$$$$PATH LD_LIBRARY_PATH=$$(USER_LIB_DIR):$$$$LD_LIBRARY_PATH \
389           $(1)
390 $(notdir $(1))_LIBS += $$(WVTEST_LIBRARY)
391 endef
392
393 # Documentation: Write the test so, that it can be run from arbitrary
394 # directory, i.e. in case of a script ensure that the wvtest library
395 # is sourced like this:
396 #
397 # . $(dirname $0)/wvtest.sh
398
399 $(foreach script,$(wvtest_SCRIPTS),$(eval $(call wvtest_template,$(SOURCES_DIR)/$(script))))
400 # Hack!!!
401 USER_TESTS_DIR := $(OUTPUT_DIR)/_compiled/bin-tests
402 $(foreach prog,$(wvtest_PROGRAMS),$(eval $(call wvtest_template,$(USER_TESTS_DIR)/$(prog))))
403 ifeq ($(OMK_VERBOSE),1)                                                          #OMK:include.omk@Makefile.rules.linux
404 CPHEADER_FLAGS += -v
405 LNHEADER_FLAGS += -v
406 endif
407
408 ifneq ($(LN_HEADERS),y)
409 define cp_cmd
410 if ! cmp -s $(1) $(2); then \
411     echo "  CP      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
412     install -d $(CPHEADER_FLAGS) `dirname $(2)` && \
413     install $(CPHEADER_FLAGS) $(1) $(2) || exit 1; \
414 fi
415 endef
416 else
417 define cp_cmd
418 if ! cmp -s $(1) $(2); then \
419     echo "  LN      $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \
420     if [ -f $(1) ]; then d=$(2); mkdir -p $${d%/*} && ln -sf $(LNHEADER_FLAGS) $(1) $(2) || exit 1; else exit 1; fi; \
421 fi
422 endef
423 endif
424
425 # TODO: Check modification date of changed header files. If it is
426 # newer that in source dir, show a warning.
427
428 # Syntax: $(call include-pass-template,<include dir>,<keyword>)
429 define include-pass-template
430 include-pass-local: include-pass-local-$(2)
431 include-pass-local-$(2): $$($(2)_GEN_HEADERS) $$(foreach f,$$(renamed_$(2)_GEN_HEADERS),$$(shell f='$$(f)'; echo $$$${f%->*}))
432         @$$(foreach f, $$($(2)_HEADERS),$$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
433 # FIXME: Use correct build dir, then document it (in the line bellow)
434         @$$(foreach f, $$($(2)_GEN_HEADERS),$$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$(f),$(1)/$$(notdir $$(f))); )
435         @$$(foreach f, $$(nobase_$(2)_HEADERS), $$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(f)); )
436         @$$(foreach f, $$(renamed_$(2)_HEADERS), \
437            f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
438            $$(call cp_cmd,$$(SOURCES_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
439         @$$(foreach f, $$(renamed_$(2)_GEN_HEADERS), \
440            f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \
441            $$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$$${srcfname},$(1)/$$$${destfname}); )
442 # Suppress "Nothing to be done for `include-pass-local'" message if no headers are defined in Makefile.omk
443         @$$(if $$($(2)_HEADERS)$$($(2)_GEN_HEADERS)$$(nobase_$(2)_HEADERS)$$(renamed_$(2)_HEADERS)$$(renamed_$(2)_GEN_HEADERS),,true)
444 endef
445                                                                                  #OMK:linux.omk@Makefile.rules.linux
446 BUILD_DIR_NAME = _build
447 COMPILED_DIR_NAME = _compiled
448 ifndef GROUP_DIR_NAME
449 GROUP_DIR_NAME = nogroup
450 endif
451
452 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
453 USER_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
454 USER_UTILS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
455 USER_TESTS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
456 USER_BIN_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
457 USER_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
458
459 ifeq ($(BUILD_OS),)
460   # Check for target
461   ifeq ($(OS),Windows_NT)
462     BUILD_OS := win32
463   else
464     BUILD_OS := $(shell uname | tr '[A-Z]' '[a-z]' )
465     #$(warning BUILD_OS=$(BUILD_OS))
466   endif
467 endif
468
469 ifeq ($(TARGET_OS),)
470   TARGET_OS := $(BUILD_OS)
471 endif
472
473 export TARGET_OS
474 export BUILD_OS
475
476 LOCAL_BUILD_DIR  = $(USER_OBJS_DIR)
477
478 # Assign default values to CFLAGS variable. If the variable is defined
479 # earlier (i.g. in config.omk), it is not overriden here.
480 CFLAGS ?= -O2 -Wall
481 CXXFLAGS ?= -O2 -Wall
482
483
484 CPPFLAGS  += -I $(USER_INCLUDE_DIR)
485
486 LOADLIBES += -L$(USER_LIB_DIR) 
487
488 LOADLIBES += $(lib_LOADLIBES:%=-l%)
489
490 LIB_CPPFLAGS += $(CPPFLAGS)
491 LIB_CFLAGS   += $(CFLAGS)
492
493 ifeq ($(TARGET_OS),win32)
494   EXE_SUFFIX = .exe
495   SOLIB_EXT = dll
496 else
497   SOLIB_EXT = so
498   SOLIB_PICFLAGS += -fpic
499 endif
500
501 #vpath %.c $(SOURCES_DIR)
502 #vpath %.cc $(SOURCES_DIR)
503 #vpath %.cxx $(SOURCES_DIR)
504
505 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
506 OMK_WORK_DIR  = $(USER_OBJS_DIR)
507
508 .PHONY: dep subdirs clean clean-custom cleandepend check-dir
509
510 # Some support to serialize some targets for parallel make
511 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
512 include-pass: check-dir
513 library-pass: include-pass
514 link-pseudo-pass: library-pass
515 binary-pass: link-pseudo-pass
516
517 override OMK_SERIALIZE_INCLUDED = y
518 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
519 endif
520
521 #=====================================================================
522 # User-space rules and templates to compile programs, libraries etc.
523
524 ifdef USER_RULE_TEMPLATES
525
526 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
527         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
528
529 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
530         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(OMK_CXXFLAGS) $(CXXFLAGS) -DOMK_FOR_USER
531
532 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
533         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER
534
535 idl_COMPILE = $(IDL_COMPILER)
536
537 # Check GCC version for user build
538 ifndef CC_MAJOR_VERSION
539 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
540 endif
541 # Prepare suitable define for dependency building
542 ifeq ($(CC_MAJOR_VERSION),2)
543 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
544 else
545 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
546 endif
547
548
549 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
550 define COMPILE_c_o_template
551 ifeq ($$($(2)_C_TARGET),)
552 $(2)_C_TARGET=1
553 $(2): $(1) $$(GEN_HEADERS)
554         @$(QUIET_CMD_ECHO) "  CC      $$@"
555         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
556         then mv -f "$$@.d.tmp" "$$@.d" ; \
557         else rm -f "$$@.d.tmp" ; exit 1; \
558         fi
559 endif
560 endef
561
562
563 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
564 define COMPILE_cc_o_template
565 ifeq ($$($(2)_CC_TARGET),)
566 $(2)_CC_TARGET=1
567 $(2): $(1) $$(GEN_HEADERS)
568         @$(QUIET_CMD_ECHO) "  CXX     $$@"
569         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
570         then mv -f "$$@.d.tmp" "$$@.d" ; \
571         else rm -f "$$@.d.tmp" ; exit 1; \
572         fi
573 endif
574 endef
575
576
577 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
578 define COMPILE_S_o_template
579 ifeq ($$($(2)_S_TARGET),)
580 $(2)_S_TARGET=1
581 $(2): $(1) $$(GEN_HEADERS)
582         @$(QUIET_CMD_ECHO) "  AS      $$@"
583         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
584         then mv -f "$$@.d.tmp" "$$@.d" ; \
585         else rm -f "$$@.d.tmp" ; exit 1; \
586         fi
587 endif
588 endef
589
590 # Syntax: $(call COMPILE_s_o_template,<source>,<target>,<additional c-flags>)
591 define COMPILE_s_o_template
592 ifeq ($$($(2)_s_TARGET),)
593 $(2)_s_TARGET=1
594 $(2): $(1) $$(GEN_HEADERS)
595         @$(QUIET_CMD_ECHO) "  AS      $$@"
596         $(Q)$$(S_o_COMPILE) $(3) -o $$@ -c $$<
597 endif
598 endef
599
600 # Syntax: $(call COMPILE_idl_template,</path/to/src.idl>,<basename>)
601 define COMPILE_idl_template
602
603 ifeq ($$($(2)_IDL_TARGET),)
604 $(2)_IDL_TARGET=1
605 GEN_HEADERS+=$(filter %.h,$(notdir $(1:%.idl=%.h))) # Do we need this global variable?
606
607 $(2).c $(2)-stubs.c $(2)-skels.c $(2)-common.c $(2).h: $(1) $$(wildcard $$(firstword $$(idl_COMPILE)))
608         @$(QUIET_CMD_ECHO) "  IDL     $$@"
609         $(Q) $$(idl_COMPILE) $$($(2)_IDLFLAGS) $(1)
610 endif
611 endef
612
613 # GCC recognizes files matching this pattern as C++
614 CXX_PATTERN = %.cc %.cp %.cxx %.cpp %.CPP %.c++ %.C
615
616 # Syntax: $(call COMPILE_templates,<sources_abs>,<suffix>,<obj-prefix>)
617 # Note: The newlines after $(call ) are IMPORTANT!!!
618 define COMPILE_templates
619 $(foreach src,$(filter %.c,$(1)),$(call COMPILE_c_o_template,$(src),$(3)$(notdir $(src:%.c=%$(2))),)
620 )
621 $(foreach src,$(filter $(CXX_PATTERN),$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
622 )
623 $(foreach src,$(filter %.S,$(1)),$(call COMPILE_S_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
624 )
625 $(foreach src,$(filter %.s,$(1)),$(call COMPILE_s_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
626 )
627 endef
628
629 TARGET_IDL_SOURCES =  $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-skels.c)) \
630                        $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-common.c)) \
631                        $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) \
632                        $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-common.c)) \
633                        $(filter %.c,$($(1)_IDL:%.idl=%.c))
634 TARGET_SOURCES = $($(1)_SOURCES) $($(1)_GEN_SOURCES) $(TARGET_IDL_SOURCES)
635 TARGET_SOURCES_ABS = $($(1)_SOURCES:%=$(SOURCES_DIR)/%) $($(1)_GEN_SOURCES) $(TARGET_IDL_SOURCES)
636 TARGET_OBJ_PREFIX = $(if $($(1)_CFLAGS)$($(1)_CXXFLAGS)$($(1)_CPPFLAGS),$(1)-,)
637 TARGET_OBJS  = $(sort $(addprefix $(TARGET_OBJ_PREFIX),$(addsuffix .o,$(basename $(notdir $(TARGET_SOURCES))))))
638 TARGET_LOBJS = $(sort $(addprefix $(TARGET_OBJ_PREFIX),$(addsuffix .lo,$(basename $(notdir $(TARGET_SOURCES))))))
639 TARGET_IDLS = $($(1)_SERVER_IDL) $($(1)_CLIENT_IDL) $($(1)_IDL)
640 TARGET_CFLAGS   = $(if $($(1)_CFLAGS),$($(1)_CFLAGS),$(OMK_CFLAGS))
641 TARGET_CXXFLAGS = $(if $($(1)_CXXFLAGS),$($(1)_CXXFLAGS),$(OMK_CXXFLAGS))
642 TARGET_CPPFLAGS = $(if $($(1)_CPPFLAGS),$($(1)_CPPFLAGS),$(OMK_CPPFLAGS))
643
644 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<executable-suffix>,<linker-sript>)
645 define PROGRAM_template
646 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
647
648 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
649
650 $(2)/$(1)$(3): OMK_CFLAGS=$(TARGET_CFLAGS)
651 $(2)/$(1)$(3): OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
652 $(2)/$(1)$(3): OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
653 $(2)/$(1)$(3): $(TARGET_OBJS)
654         @$(QUIET_CMD_ECHO) "  LINK    $$@"
655         $(Q) $(if $(filter $(CXX_PATTERN),$(TARGET_SOURCES)),$$(CXX),$$(CC)) \
656           $(TARGET_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)_LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
657         @echo "$(2)/$(1)$(3): \\" >$(USER_OBJS_DIR)/$(1).exe.d
658         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(USER_OBJS_DIR)/$(1).exe.map|tr '&' '\134'  >>$(USER_OBJS_DIR)/$(1).exe.d
659         @echo >>$(USER_OBJS_DIR)/$(1).exe.d
660
661 binary-pass-local: $(2)/$(1)$(3)
662 endef
663
664 # Usage: $(call SCRIPT_template,<target-directory>,<script-name>)
665 define SCRIPT_template
666 $(2)/$(1): $$(SOURCES_DIR)/$(1)
667         @$(QUIET_CMD_ECHO) "  CP      $$@"
668         $(Q)cp $$^ $$@
669
670 binary-pass-local: $(2)/$(1)
671 endef
672
673
674 # Syntax: $(call LIBRARY_template,<library-name>)
675 define LIBRARY_template
676
677 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
678
679 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
680
681 $(USER_LIB_DIR)/lib$(1).a: OMK_CFLAGS=$(TARGET_CFLAGS)
682 $(USER_LIB_DIR)/lib$(1).a: OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
683 $(USER_LIB_DIR)/lib$(1).a: OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
684 $(USER_LIB_DIR)/lib$(1).a: $(TARGET_OBJS)
685         @$(QUIET_CMD_ECHO) "  AR      $$@"
686         $(Q) $(AR) rcs $$@ $$^
687
688 library-pass-local: $(USER_LIB_DIR)/lib$(1).a
689 endef
690
691
692 # Syntax: $(call SOLIB_template,<library-name>)
693 define SOLIB_template
694
695 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
696
697 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.lo,$(TARGET_OBJ_PREFIX))
698
699 .PHONY: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
700 $(2)/$(1)$(3): OMK_CFLAGS=$(TARGET_CFLAGS)
701 $(2)/$(1)$(3): OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
702 $(2)/$(1)$(3): OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
703 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CFLAGS = $(TARGET_CFLAGS) $(SOLIB_PICFLAGS)
704 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CXXFLAGS = $(TARGET_CXXFLAGS) $(SOLIB_PICFLAGS)
705 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CPPFLAGS = $(TARGET_CPPFLAGS)
706 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: $(TARGET_LOBJS)
707         $(Q)echo '$(1)_objslo += $$$$(addprefix $(USER_OBJS_DIR)/,$$^)' > $$@.tmp; \
708             echo '$(1)_libs += $$($(1)_LIBS) $$(lib_LOADLIBES)' >> $$@.tmp; \
709             echo '$(1)_ldflags += $$($(1)_LDFLAGS) $$(lib_LDFLAGS)' >> $$@.tmp; \
710             echo 'shared_libs := $$$$(sort $(1) $$$$(shared_libs))' >> $$@.tmp; \
711             if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
712
713 library-pass-local: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
714 endef
715
716 # Generate rules for compilation of programs and libraries
717
718 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
719
720 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX))))
721
722 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX))))
723
724 $(foreach script,$(bin_SCRIPTS),$(eval $(call SCRIPT_template,$(script),$(USER_BIN_DIR))))
725
726
727 # $(foreach lib,$(lib_LIBRARIES),$(info $(call LIBRARY_template,$(lib))))
728 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
729
730 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
731
732 -include $(USER_OBJS_DIR)/*.d
733 #FIXME: This doesn't include dependencies of source files from
734 #subdirectories (i.s. *_SOURCES=dir/file.c. I'm currently not sure,
735 #how to handle this.
736
737 endif # USER_RULE_TEMPLATES
738
739 .PHONY: link-pseudo-pass
740 link-pseudo-pass:
741         $(Q)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(USER_BUILD_DIR) -f $(SOURCESDIR_MAKEFILE) link-shared-libs
742
743 ifeq ($(MAKECMDGOALS),link-shared-libs)
744
745
746
747 # Syntax: $(call solib_link_template,<library-name>)
748 define solib_link_template
749 $(1)_shared_libs = $$(patsubst %,$(USER_LIB_DIR)/lib%.$(SOLIB_EXT),$$(filter $$(shared_libs),$$($(1)_libs)))
750 #$$(warning $(1)_shared_libs = $$($(1)_shared_libs))
751 $(USER_LIB_DIR)/lib$(1).$(SOLIB_EXT): $$($(1)_shared_libs) $$($(1)_objslo)
752         @$(QUIET_CMD_ECHO) "  LINK    $$@"
753         $(Q)$(CC) --shared -Xlinker -soname=lib$(1).$(SOLIB_EXT) -Wl,-Map,$(USER_OBJS_DIR)/lib$(1).$(SOLIB_EXT).map -o $$@ $$($(1)_objslo) $$(LOADLIBES) $$($(1)_libs:%=-l%) $$(lib_ldflags) $$($(1)_ldflags)
754 endef
755
756 -include $(shell true; find $(USER_BUILD_DIR) -name 'lib*.omkvar') # `true' is a hack for MinGW
757 #$(warning $(shared_libs))
758 $(foreach lib,$(shared_libs),$(eval $(call solib_link_template,$(lib))))
759
760 .PHONY: link-shared-libs
761 link-shared-libs: $(shared_libs:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT))
762 endif # link-shared-libs
763
764 $(eval $(call omk_pass_template, library-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(cmetric_include_HEADERS)))
765 $(eval $(call omk_pass_template, binary-pass, $(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(utils_PROGRAMS)$(test_PROGRAMS)$(bin_SCRIPTS)))
766
767 $(eval $(call omk_pass_template,clean,$(USER_OBJS_DIR),,always))
768 $(eval $(call omk_pass_template,install,$(USER_OBJS_DIR),,always))
769 $(eval $(call omk_pass_template,include-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,always))
770
771 check-dir::
772         @$(call mkdir_def,$(USER_BUILD_DIR))
773         @$(call mkdir_def,$(USER_INCLUDE_DIR))
774         @$(call mkdir_def,$(USER_LIB_DIR))
775         @$(call mkdir_def,$(USER_BIN_DIR))
776         @$(call mkdir_def,$(USER_UTILS_DIR))
777         @$(call mkdir_def,$(USER_TESTS_DIR))
778
779 install-local:                  # TODO
780
781 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
782
783 clean-local::
784         @echo Cleaning in $(USER_OBJS_DIR)
785         @rm -f $(USER_OBJS_DIR)/*.[och] \
786                $(USER_OBJS_DIR)/*.lo \
787                $(USER_OBJS_DIR)/*.d \
788                $(USER_OBJS_DIR)/*.map \
789                $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
790
791 include-pass-submakes: extra-rules-subdirs
792
793 # We must go to EXTRA_RULES_SUBDIRS before going to any other
794 # directory, since the executables compiled in EXTRA_RULES_SUBDIRS
795 # might be needed there.
796 include-pass-this-dir $(foreach subdir,$(SUBDIRS),include-pass-$(subdir)-subdir): extra-rules-subdirs
797
798 default: include-pass library-pass binary-pass
799                                                                                  #OMK:linux-kernel.omk@Makefile.rules.linux
800 KERN_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include-kern
801 KERN_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib-kern
802 KERN_MODULES_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/modules
803 KERN_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/kern
804 KERN_MODPOST_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/kern-modpost
805
806 ifndef LINUX_VERSION
807 LINUX_VERSION=$(shell uname -r)
808 endif
809 ifndef LINUX_DIR
810 LINUX_DIR=/lib/modules/$(LINUX_VERSION)/build
811 endif
812
813 KERN_OBJS_DIR = $(KERN_BUILD_DIR)/$(RELATIVE_DIR)
814
815 kernel-lib-pass: include-pass
816 kernel-mod-pass: kernel-lib-pass
817 kernel-modpost-pass: kernel-mod-pass
818 kernel-pass: kernel-mod-pass kernel-modpost-pass
819
820 #=====================================================================
821 # Kernel-space rules and templates to compile modules, libraries etc.
822
823 ifdef KERN_RULE_TEMPLATES
824
825 $(KERN_LIB_DIR)/kernel.mk: $(LINUX_DIR)/.config $(MAKERULES_DIR)/kernelcfg2mk
826         @$(QUIET_CMD_ECHO) "  KCFG2MK $@"
827         $(Q) $(MAKERULES_DIR)/kernelcfg2mk $(LINUX_DIR) $(KERN_LIB_DIR)
828
829 ifeq ($(CONFIG_RTLINUX),y)
830 include $(RTL_DIR)/rtl.mk
831
832 KERN_CC = $(CC)
833 kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(CC) -print-search-dirs | $(SED4OMK) -n -e 's/^install: \(.*\)$$/\1/p' )
834 INCLUDES := -I $(KERN_INCLUDE_DIR) $(INCLUDE) $(rtlinux_INCLUDES) $(kernel_INCLUDES)
835 #-DEXPORT_NO_SYMBOLS
836 c_o_kern_COMPILE = $(KERN_CC) -idirafter $(kern_GCCLIB_DIR)/include $(INCLUDES)  $(CFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc
837 cc_o_kern_COMPILE = $(CXX) $(INCLUDES) $(CXXFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB
838 KERN_EXE_SUFFIX := .o
839 KERN_ARCH = $(ARCH)
840 KERN_LD = $(LD)
841 KERN_AR = $(AR)
842
843 else # CONFIG_RTLINUX
844
845 include $(KERN_LIB_DIR)/kernel.mk
846
847 ifeq ($(LINUX_SRC),)
848 LINUX_SRC = $(LINUX_DIR)
849 endif
850 kernel_INCLUDES += -I $(KERN_INCLUDE_DIR) -I $(LINUX_DIR) -idirafter $(LINUX_SRC)/include/linux
851
852 ifdef LINUX_CC
853 KERN_CC = $(LINUX_CC)
854 kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(LINUX_CC) -print-search-dirs | $(SED4OMK) -n -e 's/^install: \(.*\)$$/\1/p' )
855 else
856 KERN_CC = echo KERN_CC not defined - compilation skipped
857 endif
858 c_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc
859 cc_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB
860 S_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_AFLAGS) $(LINUX_AFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc
861 KERN_EXE_SUFFIX := $(LINUX_MODULE_EXT)
862 KERN_LDFLAGS = $(LINUX_LDFLAGS) $(LINUX_LDFLAGS_MODULE)
863 ifdef LINUX_ARCH
864 KERN_ARCH = $(LINUX_ARCH)
865 else
866 KERN_ARCH = echo KERN_ARCH not defined - skipped
867 endif
868 ifdef LINUX_LD
869 KERN_LD = $(LINUX_LD)
870 else
871 KERN_LD = echo KERN_LD  not defined - skipped
872 endif
873 ifneq ($(LINUX_AR),)
874 KERN_AR = $(LINUX_AR)
875 else
876 KERN_AR = $(AR)
877 endif
878 ifeq ($(LINUX_QUOTE_MODNAME),y)
879 KERN_MQ=\"
880 KERN_KBUILD_MODNAME=-D"KBUILD_MODNAME=((THIS_MODULE)!=NULL?(THIS_MODULE)->name:NULL)"
881 endif
882 endif # CONFIG_RTLINUX
883
884 KERN_LOADLIBES += -L$(KERN_LIB_DIR)
885
886 KERN_LOADLIBES += $(rtlinux_LOADLIBES:%=-l%)
887 KERN_LOADLIBES += $(kernel_LOADLIBES:%=-l%)
888
889
890
891 # Check GCC version for kernel part of build
892 ifndef kern_CC_MAJOR_VERSION
893 kern_CC_MAJOR_VERSION := $(shell $(KERN_CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
894 endif
895 # Prepare suitable define for dependency building
896 ifeq ($(kern_CC_MAJOR_VERSION),2)
897 kern_CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
898 else
899 kern_CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
900 endif
901
902 ifeq ($(KERN_EXE_SUFFIX),.ko)
903 ifeq ($(wildcard $(LINUX_DIR)/scripts/mod/modpost),)
904 KERN_MODPOST = $(LINUX_DIR)/scripts/modpost
905 else
906 KERN_MODPOST = $(LINUX_DIR)/scripts/mod/modpost
907 endif
908 KERN_MODULES_LINK_DIR = $(KERN_MODPOST_DIR)
909 KERN_LINK_SUFFIX = .o
910 else
911 KERN_MODULES_LINK_DIR = $(KERN_MODULES_DIR)
912 KERN_LINK_SUFFIX = $(KERN_EXE_SUFFIX)
913 endif
914
915 ifeq ($(LINUX_CONFIG_MODVERSIONS),y)
916 MODPOST_OPTS += -m
917 MODPOST_OPTS += -i $(LINUX_DIR)/Module.symvers
918 ifneq ($(LINUX_BUILDHOST),) # this is not correct point, it should look for 2.6.17 kernel
919 MODPOST_OPTS += -I $(KERN_LIB_DIR)/Module.symvers
920 endif
921 MODPOST_OPTS += -o $(KERN_LIB_DIR)/Module.symvers
922 endif
923
924 ifeq ($(LINUX_CONFIG_DEBUG_SECTION_MISMATCH),y)
925 MODPOST_OPTS += -S
926 endif
927
928 ifeq ($(LINUX_CONFIG_MARKERS),y)
929 MODPOST_OPTS += -K $(LINUX_DIR)/Module.markers
930 MODPOST_OPTS += -M $(KERN_LIB_DIR)/Module.markers
931 endif
932
933 ifeq ($(LINUX_KBUILD_MODPOST_WARN),y)
934 MODPOST_OPTS += -w
935 endif
936
937 ifneq ($(LINUX_BUILDHOST),)
938 ifneq ($(LINUX_BUILDHOST),$(LINUX_ARCH))
939 MODPOST_OPTS += -c
940 endif
941 endif
942
943 define COMPILE_c_o_kern_template
944
945 $(2): $(1)
946         @$(QUIET_CMD_ECHO) "  CC [K]  $$@"
947         $(Q) if $$(c_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \
948         -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \
949         -o $$@ -c $$< ; \
950         then mv -f "$$@.d.tmp" "$$@.d" ; \
951         else rm -f "$$@.d.tmp" ; exit 1; \
952         fi
953 endef
954
955
956
957 define COMPILE_cc_o_kern_template
958
959 $(2): $(1)
960         @$(QUIET_CMD_ECHO) "  CXX [K] $$@"
961         $(Q) if $$(cc_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \
962         -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \
963         -o $$@ -c $$< ; \
964         then mv -f "$$@.d.tmp" "$$@.d" ; \
965         else rm -f "$$@.d.tmp" ; exit 1; \
966         fi
967 endef
968
969
970
971 define COMPILE_S_o_kern_template
972
973 $(2): $(1)
974         @$(QUIET_CMD_ECHO) "  AS [K]  $$@"
975         $(Q) if $$(S_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \
976         -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \
977         -o $$@ -c $$< ; \
978         then mv -f "$$@.d.tmp" "$$@.d" ; \
979         else rm -f "$$@.d.tmp" ; exit 1; \
980         fi
981 endef
982
983
984
985 define MODULE_kern_template
986 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
987 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
988 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
989 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cpp=%.o))
990 $(1)_OBJS := $$(sort $$($(1)_OBJS))
991
992 KERN_OBJS  += $$($(1)_OBJS)
993 KERN_SOURCES += $$($(1)_SOURCES)
994
995 # this is hack to build "__this_module" structure for 2.6.x kernels
996 # modpost is used for that purpose now
997
998 #$(1).mod.c:
999 #       echo  "\
1000 #       #include <linux/version.h>@\
1001 #       #include <linux/module.h>@\
1002 #       #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,5))@\
1003 #       #include <linux/vermagic.h>@\
1004 #       #include <linux/compiler.h>@\
1005 #       @\
1006 #       MODULE_INFO(vermagic, VERMAGIC_STRING);@\
1007 #       @\
1008 #       #undef unix@\
1009 #       struct module __this_module@\
1010 #       __attribute__((section(\".gnu.linkonce.this_module\"))) = {@\
1011 #        .name = __stringify(KBUILD_MODNAME),@\
1012 #        .init = init_module,@\
1013 #       #ifdef CONFIG_MODULE_UNLOAD@\
1014 #        .exit = cleanup_module,@\
1015 #       #endif@\
1016 #       };@\
1017 #       #endif@\
1018 #       " | tr @ \\n >$$@
1019
1020
1021 #$(eval $(call COMPILE_c_o_kern_template,$(1).mod.c,$(1).mod.o,-DKBUILD_MODNAME=$(1)))
1022
1023 $(2)/$(1)$(KERN_LINK_SUFFIX): $$($(1)_OBJS)
1024         @$(QUIET_CMD_ECHO) "  LD [K]  $$@"
1025         $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) -r $$($(1)_OBJS) -L$$(kern_GCCLIB_DIR) $$($(1)_LIBS:%=-l%) $$(KERN_LOADLIBES) -Map $(KERN_OBJS_DIR)/$(1).mod.map -o $$@
1026         @echo "$(2)/$(1)$(KERN_LINK_SUFFIX): \\" >$(KERN_OBJS_DIR)/$(1).mod.d
1027         @$(SED4OMK) -n -e 's/^LOAD \(.*\)$$$$/  \1  \\/p' $(KERN_OBJS_DIR)/$(1).mod.map  >>$(KERN_OBJS_DIR)/$(1).mod.d
1028         @echo >>$(KERN_OBJS_DIR)/$(1).mod.d
1029         @if [ "$(KERN_EXE_SUFFIX)" = ".ko" ] ; then \
1030           echo $(1) >>$(KERN_MODPOST_DIR)/module-changes ; \
1031           echo $(1) >$(KERN_MODPOST_DIR)/$(1).mod.stamp ; \
1032         fi
1033
1034 endef
1035
1036
1037
1038 define LIBRARY_kern_template
1039 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
1040 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
1041 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
1042 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cpp=%.o))
1043 $(1)_OBJS := $$(sort $$($(1)_OBJS))
1044
1045 KERN_OBJS  += $$($(1)_OBJS)
1046 KERN_SOURCES += $$($(1)_SOURCES)
1047
1048 $(KERN_LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
1049         @$(QUIET_CMD_ECHO) "  AR [K]  $$@"
1050         $(Q) $(KERN_AR) rcs $$@ $$^
1051 endef
1052
1053
1054 ifdef LINUX_DIR
1055
1056 kernel-lib-pass-local: $(kernel_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a)
1057
1058 kernel-mod-pass-local: $(kernel_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX))
1059
1060 $(foreach module,$(kernel_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR))))
1061
1062 $(foreach lib,$(kernel_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib))))
1063
1064 endif
1065
1066
1067 ifeq ($(CONFIG_RTLINUX),y)
1068
1069 kernel-mod-pass-local: $(rtlinux_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX))
1070
1071 kernel-lib-pass-local: $(rtlinux_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a)
1072
1073 $(foreach module,$(rtlinux_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR))))
1074
1075 $(foreach lib,$(rtlinux_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib))))
1076
1077 endif
1078
1079 ifeq ($(KERN_MODPOST_PASS),y)
1080
1081 MODULES_LIST := $(wildcard *.mod.stamp)
1082 MODULES_LIST := $(MODULES_LIST:%.mod.stamp=%)
1083
1084 define MODPOST_kern_template
1085 $(2) : $(1)$(KERN_LINK_SUFFIX) $(1).mod.c
1086         @$(QUIET_CMD_ECHO) "  LD [M]  $$@"
1087         $(Q) $$(c_o_kern_COMPILE) -D"KBUILD_BASENAME=$(KERN_MQ)$(1)$(KERN_MQ)" \
1088                 -D"KBUILD_MODNAME=$(KERN_MQ)$(1)$(KERN_MQ)" \
1089                 -o $(1).mod.o -c $(1).mod.c
1090         $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) $(1)$(KERN_LINK_SUFFIX) $(1).mod.o -r -o $$@
1091 endef
1092
1093 kernel-modpost-versions: $(wildcard $(LINUX_DIR)/Module.symvers)
1094         @$(QUIET_CMD_ECHO) "  MODPOST    $(KERN_MODPOST_DIR)"
1095         @echo  >$(KERN_MODPOST_DIR)/modpost-running
1096         @rm -f $(KERN_MODPOST_DIR)/module-changes
1097         $(Q) $(KERN_MODPOST) $(MODPOST_OPTS) $(MODULES_LIST:%=%$(KERN_LINK_SUFFIX))
1098
1099 $(MODULES_LIST:%=%.mod.c) : kernel-modpost-versions
1100
1101 kernel-modpost-pass-local: $(MODULES_LIST:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX))
1102         @rm -f $(KERN_MODPOST_DIR)/modpost-running
1103
1104 $(foreach module,$(MODULES_LIST),$(eval $(call MODPOST_kern_template,$(module),$(module:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX)))))
1105
1106 endif
1107
1108 -include $(KERN_OBJS_DIR)/*.d
1109
1110 endif
1111
1112 #=====================================================================
1113
1114 # Kernel requires its own set of configuration header-files
1115 ifneq ($(kernel_LIBRARIES)$(rtlinux_LIBRARIES)$(kernel_MODULES)$(rtlinux_MODULES)$(kernel_HEADERS)$(rtlinux_HEADERS)$(kernel_HEADERS)$(rtlinux_HEADERS)$(nobase_kernel_HEADERS)$(nobase_rtlinux_HEADERS)$(renamed_kernel_HEADERS)$(renamed_rtlinux_HEADERS),)
1116 KERN_CONFIG_HEADERS_REQUIRED = y
1117 endif
1118
1119 $(eval $(call omk_pass_template, kernel-lib-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_LIBRARIES)$(rtlinux_LIBRARIES)))
1120 $(eval $(call omk_pass_template, kernel-mod-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_MODULES)$(rtlinux_MODULES)))
1121
1122 kernel-modpost-pass:
1123         +@if [ -e "$(KERN_MODPOST_DIR)/module-changes" -o -e "$(KERN_MODPOST_DIR)/modpost-running" ] ; \
1124         then \
1125             $(MAKE) --no-print-directory -C $(KERN_MODPOST_DIR) \
1126                -f $(SOURCES_DIR)/Makefile KERN_RULE_TEMPLATES=y KERN_MODPOST_PASS=y $(@:%=%-local) ; \
1127         fi
1128
1129 check-dir::
1130         @$(call mkdir_def,$(KERN_BUILD_DIR))
1131         @$(call mkdir_def,$(KERN_INCLUDE_DIR))
1132         @$(call mkdir_def,$(KERN_LIB_DIR))
1133         @$(call mkdir_def,$(KERN_MODULES_DIR))
1134         @$(call mkdir_def,$(KERN_MODPOST_DIR))
1135
1136 $(eval $(call include-pass-template,$(KERN_INCLUDE_DIR),kernel))
1137 ifeq ($(CONFIG_RTLINUX),y)
1138 $(eval $(call include-pass-template,$(KERN_INCLUDE_DIR),rtlinux))
1139 endif
1140
1141 ifdef KERN_RULE_TEMPLATES
1142
1143 KERN_SOURCES := $(sort $(KERN_SOURCES))
1144
1145 #$(warning KERN_SOURCES = $(KERN_SOURCES))
1146
1147 $(foreach src,$(filter %.c,$(KERN_SOURCES)),$(eval $(call COMPILE_c_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)))
1148
1149 $(foreach src,$(filter %.cc,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)))
1150
1151 $(foreach src,$(filter %.cxx,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)))
1152
1153 $(foreach src,$(filter %.cpp,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cpp=%.o),)))
1154
1155 $(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)))
1156 endif
1157
1158 clean-local::
1159         @echo Cleaning in $(KERN_OBJS_DIR)
1160         @rm -f $(KERN_OBJS_DIR)/*.o \
1161                $(KERN_OBJS_DIR)/*.d \
1162                $(KERN_OBJS_DIR)/*.map \
1163                $(KERN_OBJS_DIR)/*.mod.c \
1164                $(kernel_MODULES:%=$(KERN_MODPOST_DIR)/%.*) \
1165                $(LOCAL_CONFIG_H:%=$(KERN_OBJS_DIR)/%)
1166         @if [ -e $(KERN_LIB_DIR)/kernel.mk ] ; then \
1167             touch -t 200001010101 $(KERN_LIB_DIR)/kernel.mk ; \
1168         fi
1169
1170 ifndef OMIT_KERNEL_PASSES
1171 # Also make kernel passes if not disabled
1172 default: kernel-lib-pass kernel-pass
1173 endif
1174                                                                                  #OMK:cmetric.omk@Makefile.rules.linux
1175 NM ?= nm
1176
1177 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
1178 define CMETRIC_o_h_template
1179 $(2): $(1)
1180         @$(QUIET_CMD_ECHO) "  CMETRIC $$@"
1181         $(Q)if [ -n `dirname $$@` ] ; then \
1182               if [ ! -e `dirname $$@` ] ; then \
1183                 mkdir -p `dirname $$@` ; fi ; fi
1184         $(Q)echo >$$@.tmp '/* Automatically generated from $$< */'
1185         $(Q)echo >>$$@.tmp '/* Conditionals to control compilation */'
1186 # Bellow, the tricks with redirection are for shells without set -o pipefail
1187 # (see http://www.mail-archive.com/dash@vger.kernel.org/msg00149.html)
1188         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
1189                 | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
1190                 | sort >>$$@.tmp` && exit $$$$status
1191         $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in hexadecimal format */'
1192         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
1193                 | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
1194                 | sort >>$$@.tmp` && exit $$$$status
1195         $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in decimal format */'
1196         $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) -td $$<; echo $$$$? >&4; }\
1197                 | $(SED4OMK) -n 's/^ *0*\(0\|[1-9][0-9]*\) *A *_cmetric2defdec_\([A-Za-z_0-9]*\) */#define \2 \1/p' \
1198                 | sort >>$$@.tmp` && exit $$$$status
1199         $(Q)mv $$@.tmp $$@
1200 endef
1201
1202 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS))
1203
1204 # Special rules for CMETRIC generated headers
1205
1206 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\
1207                 $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\
1208                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),)))
1209 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
1210                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\
1211                 $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh)))))
1212
1213 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
1214                                                                                  #OMK:config_h.omk@Makefile.rules.linux
1215 # Syntax: $(call BUILD_CONFIG_H_template,<stamp_dir>,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
1216 define BUILD_CONFIG_H_template
1217
1218 $(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(CONFIG_FILES)
1219         @$(QUIET_CMD_ECHO) "  CONFGEN $(notdir $(2))"
1220         @if [ ! -d `dirname $(2).tmp` ] ; then \
1221                 mkdir -p `dirname $(2).tmp` ; fi
1222         @echo "/* Automatically generated from */" > "$(2).tmp"
1223         @echo "/* config files: $$(^:$(OUTPUT_DIR)/%=%) */" >> "$(2).tmp"
1224         $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp")
1225         @echo "#ifndef $(4)" >> "$(2).tmp"
1226         @echo "#define $(4)" >> "$(2).tmp"
1227         @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | $(SED4OMK) -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \
1228                 echo '$(x).$($(x))' ; ) echo ; ) | \
1229                 $(SED4OMK) -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
1230                 $(SED4OMK) -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
1231                   >> "$(2).tmp"
1232         @echo "#endif /*$(4)*/" >> "$(2).tmp"
1233         @touch "$$@"
1234         @if cmp -s "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \
1235         else mv "$(2).tmp" "$(2)" ; \
1236         echo "Updated configuration $(2)" ; fi
1237
1238 endef
1239
1240 ifdef LOCAL_CONFIG_H
1241
1242 # This must be declared after the default cflags are assigned!
1243 # Override is used to override command line assignemnt.
1244 override CFLAGS += -I $(USER_OBJS_DIR)
1245 override kernel_INCLUDES += -I $(KERN_OBJS_DIR)
1246 $(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
1247
1248 endif
1249
1250 # Special rules for configuration exported headers
1251
1252 #FIXME: The directory for headers should not be specified here.
1253 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
1254 _$(basename $(notdir $(confh)))_H \
1255 )))
1256
1257 config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
1258
1259 # Add some hooks to standard passes
1260 include-pass-local: $(config_h_stamp_files)
1261
1262 ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),)
1263
1264 ifdef LOCAL_CONFIG_H
1265 $(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
1266 endif
1267
1268 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
1269 _$(basename $(notdir $(confh)))_H \
1270 )))
1271
1272 kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
1273
1274 # Add some hooks to standard passes
1275 include-pass-local: $(kern_config_h_stamp_files)
1276
1277 endif
1278
1279 clean-local::
1280         @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\
1281             if [ -e $(confh) ] ; then rm $(confh) ; fi ; \
1282         )
1283                                                                                  #OMK:qt.omk@Makefile.rules.linux
1284 ifneq ($(QT_SUBDIRS)$(QT_PROJECTS),)
1285
1286 # Usage: $(call qt_project_template,<.pro_file relative to SOURCES_DIR>)
1287 define qt_project_template
1288
1289 .PHONY: qt-subpass-$(1) clean-qt-$(dir $(1))
1290
1291 # FIXME: Handle multiple .pro files correctly
1292 $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile: $(SOURCES_DIR)/$(1)
1293         $(Q)mkdir -p $$(dir $$(@)) && cd $$(dir $$(@)) &&               \
1294         $(if $(QMAKE),$(QMAKE),$(QTDIR:%=%/bin/)qmake)                  \
1295              TOP_DIR=$(OUTPUT_DIR)                                      \
1296              RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1))                 \
1297              $(QTDIR:%=QTDIR=%) CC=$(CC) CXX=$(CXX)                     \
1298              LIBS+="-L$(USER_LIB_DIR)" DESTDIR=$(USER_BIN_DIR)          \
1299              INCLUDEPATH+="$(USER_INCLUDE_DIR)"                         \
1300              DEPENDPATH+="$(USER_INCLUDE_DIR)"                          \
1301              QMAKE_LFLAGS="-Wl,-rpath-link,$(USER_LIB_DIR) $$(QMAKE_LFLAGS)" \
1302              $(SOURCES_DIR)/$(1)
1303
1304 # This horrible substitution is here to properly escape
1305 # -Wl,-rpath,$ORIGIN flags. It includes escaping for make, shell,
1306 # qmake and again make and shell run on qmake generated makefile.
1307 QMAKE_LFLAGS = $$(subst $$$$,\\\\\\$$$$\$$$$,$$(LDFLAGS))
1308
1309 # Hook to binary pass
1310 binary-pass-submakes: qt-subpass-$(1)
1311 qt-subpass-$(1): $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile
1312         $(Q)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir $(1)) \
1313                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1)) -C $(LOCAL_BUILD_DIR)/$(dir $(1)) || exit 1 ;
1314
1315 # Hook to clean pass
1316 clean-local:: $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile
1317         @$(QUIET_CMD_ECHO) "  QT CLEAN $(dir $(1))"
1318         $(Q)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir $(1)) \
1319           RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1)) \
1320           -C $(LOCAL_BUILD_DIR)/$(dir $(1)) clean
1321         $(Q)rm $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile
1322 endef
1323
1324 $(foreach pro,$(QT_PROJECTS), $(eval $(call qt_project_template,$(pro))))
1325 $(foreach pro,$(foreach dir,$(QT_SUBDIRS), $(wildcard $(SOURCES_DIR)/$(dir)/*.pro)), $(eval $(call qt_project_template,$(pro:$(SOURCES_DIR)/%=%))))
1326
1327 endif
1328                                                                                  #OMK:sources-list.omk@Makefile.rules.linux
1329 # Rules that creates the list of files which are used during
1330 # compilation. The list reflects conditional compilation depending on
1331 # config.omk and other variables.
1332
1333 SOURCES_LIST_FN=sources.txt
1334 ifndef SOURCES_LIST
1335 SOURCES_LIST_DIR:=$(RELATIVE_DIR)
1336 SOURCES_LIST:=$(OUTPUT_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN)
1337 SOURCES_LIST_D := $(LOCAL_BUILD_DIR)/$(SOURCES_LIST_FN).d
1338 export SOURCES_LIST SOURCES_LIST_DIR SOURCES_LIST_D
1339 endif
1340
1341 ifneq ($(filter sources-list TAGS tags cscope,$(MAKECMDGOALS)),)
1342 NEED_SOURCES_LIST=y
1343 endif
1344
1345 ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is not useful
1346 .PHONY: sources-list
1347 sources-list: $(SOURCES_LIST)
1348
1349 $(SOURCES_LIST): $(CONFIG_FILES) $(shell find -name $(MAKEFILE_OMK))
1350         @$(call mkdir_def,$(dir $(SOURCES_LIST_D)))
1351         @echo -n "" > "$(SOURCES_LIST).tmp"
1352         @echo -n "" > "$(SOURCES_LIST_D).tmp"
1353         @$(MAKE) --no-print-directory sources-list-pass
1354         @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2"
1355         @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2"
1356         @rm "$(SOURCES_LIST).tmp"
1357         @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)"
1358         @echo "$(SOURCES_LIST): \\" > "$(SOURCES_LIST_D).tmp2"
1359         @cat "$(SOURCES_LIST_D).tmp"|grep -v "$(SOURCES_LIST_D).tmp"|sort|uniq|\
1360                 $(SED4OMK) -e 's/$$/\\/' >> "$(SOURCES_LIST_D).tmp2"
1361         @rm "$(SOURCES_LIST_D).tmp"
1362         @mv "$(SOURCES_LIST_D).tmp2" "$(SOURCES_LIST_D)"
1363 endif
1364
1365 $(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always))
1366
1367 sources-list-pass-local:
1368         @$(foreach m,$(MAKEFILE_LIST),echo '  $(m)' >> "$(SOURCES_LIST_D).tmp";)
1369         @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\
1370           echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(h)" >> "$(SOURCES_LIST).tmp";)
1371         @$(foreach ch,$(config_include_HEADERS), \
1372           echo "$(USER_INCLUDE_DIR:$(OUTPUT_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";)
1373         @$(foreach h,$(renamed_include_HEADERS),echo '$(h)'|$(SED4OMK) -e 's|\(.*\)->.*|$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))\1|' >> "$(SOURCES_LIST).tmp";)
1374         @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(test_PROGRAMS) $(utils_PROGRAMS) \
1375           $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\
1376           $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(src)" >> "$(SOURCES_LIST).tmp";))
1377
1378 ############ TAGS ###########
1379
1380 ifeq ($(MAKECMDGOALS),TAGS)
1381 ETAGS=etags
1382 TAGS_CMD = $(ETAGS)
1383 TAGS: $(SOURCES_LIST)
1384         @$(MAKE) --no-print-directory do-tags
1385 endif
1386 ifeq ($(MAKECMDGOALS),tags) 
1387 CTAGS=ctags -N
1388 TAGS_CMD = $(CTAGS)
1389 tags: $(SOURCES_LIST)
1390         @$(MAKE) --no-print-directory do-tags
1391 endif
1392 export TAGS_CMD
1393
1394 ifeq ($(MAKECMDGOALS),do-tags)
1395 .PHONY: do-tags
1396 do-tags: $(shell $(SED4OMK) -e '/^\#/d' $(SOURCES_LIST))
1397         @$(QUIET_CMD_ECHO) "  TAGS    $(SOURCES_LIST_FN)"
1398         $(Q)$(TAGS_CMD) $^
1399 endif
1400
1401 ############ CSCOPE ###########
1402
1403 cscope: $(SOURCES_LIST)
1404         @$(QUIET_CMD_ECHO) "  CSCOPE  < $(SOURCES_LIST_FN)"
1405         $(Q)$(SED4OMK) -e '/^#/d' $(SOURCES_LIST) > cscope.files
1406         $(Q)cscope -b -icscope.files
1407 #FIXME: see doc to -i in cscope(1)