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