]> rtime.felk.cvut.cz Git - rtems-devel.git/blob - rtems-omk-template/Makefile.rules
OMK rules updated from its respective repository.
[rtems-devel.git] / rtems-omk-template / Makefile.rules
1 #                   This version is adapted for RTEMS build  #OMK@rtems
2 #
3 #  Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
4 #
5 #  (C) Copyright 2003 by Pavel Pisa - OCERA team member
6 #  (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
7 #
8 # The OMK build system is distributed under the GNU General Public
9 # License.  See file COPYING for details.
10 #
11 # input variables
12 # V                .. if set to 1, full command text is shown else short form is used
13 # W                .. whole tree - if set to 1, make is always called from the top-level directory
14 # SUBDIRS          .. list of subdirectories intended for make from actual directory
15 # default_CONFIG   .. list of default config assignments CONFIG_XXX=y/n ...
16 # LN_HEADERS       .. if "y", header files are symbolicaly linked instead of copied. #OMK@include
17 #                                                            #OMK@rtems
18 # lib_LIBRARIES    .. list of the user-space libraries
19 # shared_LIBRARIES .. list of the user-space shared libraries
20 # include_HEADERS  .. list of the user-space public header files
21 # nobase_include_HEADERS .. public headers copied even with directory part
22 # renamed_include_HEADERS .. public headers copied to the different target name
23 # bin_PROGRAMS     .. list of the require binary programs
24 # utils_PROGRAMS   .. list of the development utility programs
25 # xxx_SOURCES      .. list of specific target sources
26 # INCLUDES         .. additional include directories and defines for user-space
27 # tar_EMBEDFILES   .. list of tars with embedded files
28 # LOCAL_CONFIG_H   .. name of local config.h file generated from values #OMK@config_h
29 #                     of options defined in the current directory
30 # config_include_HEADERS .. names of global config files (possibly
31 #                     with subdirectories)
32 # xxx_DEFINES      .. list of config directives to be included in
33 #                     config header file of the name <somedir>/xxx.h
34 # DOXYGEN          .. if non-empty, generated headers includes Doxygen's @file
35 #                     command, so it is possible to document config
36 #                     variables.
37                                                              #OMK@rtems-setup
38 # Include RTEMS target configuration defining RTEMS_MAKEFILE_PATH
39 include $(MAKERULES_DIR)/config.target
40
41 include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
42 include $(RTEMS_CUSTOM)
43 include $(CONFIG.CC)
44
45 BUILD_DIR_NAME = _build/$(RTEMS_BSP)
46 COMPILED_DIR_NAME = _compiled/$(RTEMS_BSP)
47 GROUP_DIR_NAME =
48
49
50 USER_INCLUDE_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/include
51 USER_LIB_DIR     := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/lib
52 USER_UTILS_DIR   := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/bin-utils
53 USER_BIN_DIR     := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/bin
54 USER_BUILD_DIR   := $(MAKERULES_DIR)/$(BUILD_DIR_NAME)/user
55
56 LOCAL_BUILD_DIR  = $(USER_OBJS_DIR)
57                                                              #OMK@base
58 # We need to ensure definition of sources directory first
59 ifndef SOURCES_DIR
60 SOURCES_DIR := $(shell ( pwd -L ) )
61 endif
62
63 .PHONY: all default check-make-ver
64
65 ifdef W
66   ifeq ("$(origin W)", "command line")
67     OMK_WHOLE_TREE:=$(W)
68   endif
69 endif
70 ifndef OMK_WHOLE_TREE
71   OMK_WHOLE_TREE:=0
72 endif
73
74 ifneq ($(OMK_WHOLE_TREE),1)
75 all: check-make-ver default
76         @echo "Compilation finished"
77 else
78 # Run make in the top-level directory
79 all:
80         @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0
81 endif
82
83 #=========================
84 # Include the config file
85
86 ifneq ($(CONFIG_FILE_OK),y)
87 ifndef CONFIG_FILE
88 CONFIG_FILE      := $(MAKERULES_DIR)/config.omk
89 endif
90 ifneq ($(wildcard $(CONFIG_FILE)-default),)
91 -include $(CONFIG_FILE)-default
92 else
93 ifneq ($(MAKECMDGOALS),default-config)
94 $(warning Please, run "make default-config" first)
95 endif
96 endif
97
98 ifneq ($(wildcard $(CONFIG_FILE)),)
99 include $(CONFIG_FILE)
100 CONFIG_FILE_OK = y
101 endif
102 endif #$(CONFIG_FILE_OK)
103
104 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
105 export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
106 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
107 # must to be serialized only in the toplevel make.
108
109 ifndef RELATIVE_DIR
110 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
111 endif
112 #$(warning  === RELATIVE_DIR = "$(RELATIVE_DIR)" ===)
113 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
114 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
115 #$(warning  RELATIVE_DIR = "$(RELATIVE_DIR)")
116 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g'  -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
117 #$(warning  BACK2TOP_DIR = "$(BACK2TOP_DIR)")
118
119 #$(warning SOURCES_DIR = "$(SOURCES_DIR)")
120 #$(warning MAKERULES_DIR = "$(MAKERULES_DIR)")
121 #$(warning RELATIVE_DIR = "$(RELATIVE_DIR)")
122
123 # We have to use RELATIVE_PREFIX because of mingw
124 override RELATIVE_PREFIX := $(RELATIVE_DIR)/
125 override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%)
126
127 #vpath %.c $(SOURCES_DIR)
128 #vpath %.cc $(SOURCES_DIR)
129 #vpath %.cxx $(SOURCES_DIR)
130
131 VPATH = $(SOURCES_DIR)
132 srcdir = $(SOURCES_DIR)
133
134 # Defines for quiet compilation
135 ifdef V
136   ifeq ("$(origin V)", "command line")
137     OMK_VERBOSE = $(V)
138   endif
139 endif
140 ifndef OMK_VERBOSE
141   OMK_VERBOSE = 0
142 endif
143 ifeq ($(OMK_VERBOSE),1)
144   Q =
145 else
146   Q = @
147 endif
148 ifneq ($(findstring s,$(MAKEFLAGS)),)
149   QUIET_CMD_ECHO = true
150   OMK_SILENT = 1
151 else
152   QUIET_CMD_ECHO = echo
153 endif
154
155 MAKEFILE_OMK=Makefile.omk
156 # All subdirectories (even linked ones) containing Makefile.omk
157 # Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS)
158 ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK))))
159
160 # ===================================================================
161 # We have set up all important variables, so we can check and include
162 # real OCERA style Makefile.omk now
163 ifndef OMK_INCLUDED
164 include $(SOURCES_DIR)/$(MAKEFILE_OMK)
165 OMK_INCLUDED := 1
166 endif
167
168 check-make-ver:
169         @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
170         if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
171                 echo "Your make program version is too old and does not support OMK system." ; \
172                 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
173         fi
174
175 distclean dist-clean:
176         @$(QUIET_CMD_ECHO) "  RM      $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
177         @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)  $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
178
179 # Common OMK templates
180 # ====================
181
182 # Syntax: $(call mkdir,<dir name>)
183 define mkdir_def
184         [ -d $(1) ] || mkdir -p $(1) || exit 1
185 endef
186
187 ifneq ($(V),2)
188 NO_PRINT_DIRECTORY := --no-print-directory
189 endif
190
191 # Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>])
192 define omk_pass_template
193 .PHNOY: $(1) $(1)-local
194 $(1):
195         +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \
196                 $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \
197                 RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(2)/$(dir) \
198                 -f $(SOURCES_DIR)/$(dir)/Makefile $$@ || exit 1 ;) true
199 ifneq ($(4),)
200         @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \
201         $(call mkdir_def,$(2)); \
202         $(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \
203                 -f $(SOURCES_DIR)/Makefile $(3) $$(@:%=%-local)
204 endif
205 endef
206
207 # =======================
208 # DEFAULT CONFIG PASS
209
210 default-config:
211         @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
212         @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
213         @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
214         @echo >> "$(CONFIG_FILE)-default"
215         @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \
216                 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
217                 -f $(MAKERULES_DIR)/Makefile default-config-pass
218
219 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always))
220
221 default-config-pass-local:
222 #       @echo Default config for $(RELATIVE_DIR)
223         @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
224         @$(foreach x, $(default_CONFIG), echo '$(x)' | \
225                 sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
226
227 ifeq ($(OMK_VERBOSE),1)                                      #OMK@include
228 CPHEADER_FLAGS += -v
229 LNHEADER_FLAGS += -v
230 endif
231
232 ifneq ($(LN_HEADERS),y)
233 define cp_cmd
234 ( echo "  CP      $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; cp $(CPHEADER_FLAGS) $(1) $(2) )
235 endef
236 else
237 define cp_cmd
238 ( echo "  LN      $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; ln -sf $(LNHEADER_FLAGS) $(1) $(2) )
239 endef
240 endif
241
242 # TODO: Check modification date of changed header files. If it is
243 # newer that in source dir, show a warning.
244
245 # Syntax: $(call include-pass-template,<include dir>,<keyword>)
246 define include-pass-template
247         @$(foreach f, $($(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(notdir $(f)) \
248            || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(notdir $(f))) || exit 1 ; )
249         @$(foreach f, $(nobase_$(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(f) \
250            || ( mkdir -p $(1)/$(dir $(f)) && $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(f)) ) || exit 1 ; )
251         @$(foreach f, $(renamed_$(2)_HEADERS), \
252            srcfname=`echo '$(f)' | sed -e 's/^\(.*\)->.*$$/\1/'` ; destfname=`echo '$(f)' | sed -e 's/^.*->\(.*\)$$/\1/'` ; \
253            cmp --quiet $(SOURCES_DIR)/$${srcfname} $(1)/$${destfname} \
254            || ( mkdir -p `dirname $(1)/$${destfname}` && $(call cp_cmd,$(SOURCES_DIR)/$${srcfname},$(1)/$${destfname}) ) || exit 1 ; )
255 endef
256
257 # Local Variables:
258 # mode:makefile
259 # End:
260                                                              #OMK@rtems
261
262 DEFAULT_INCLUDES = -I $(PROJECT_INCLUDE)
263
264 CPPFLAGS  += -I $(USER_INCLUDE_DIR)
265
266 CPPFLAGS  += $(CONFIG_OMK_DEFINES)
267
268 #CPPFLAGS  += $(AM_CPPFLAGS)
269 #CFLAGS    += $(AM_CFLAGS)
270 #CXXFLAGS  += $(AM_CXXFLAGS)
271 #LDFLAGS   += $(CFLAGS) $(AM_CFLAGS) $(LD_PATHS:%=-L%)
272
273 LOADLIBES += -L$(USER_LIB_DIR)
274
275 LOADLIBES += $(lib_LOADLIBES:%=-l%)
276
277 SOLIB_PICFLAGS += -shared -fpic
278
279 ifndef RELATIVE_DIR
280 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
281 endif
282 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
283 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
284 #$(warning  RELATIVE_DIR $(RELATIVE_DIR))
285
286 #$(warning SOURCES_DIR = $(SOURCES_DIR))
287 #$(warning MAKERULES_DIR = $(MAKERULES_DIR))
288 #$(warning RELATIVE_DIR = $(RELATIVE_DIR))
289
290 #vpath %.c $(SOURCES_DIR)
291 #vpath %.cc $(SOURCES_DIR)
292 #vpath %.cxx $(SOURCES_DIR)
293
294 VPATH = $(SOURCES_DIR)
295 srcdir = $(SOURCES_DIR)
296
297 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
298
299 .PHONY: check-dir
300
301 # Some support to serialize some targets for parallel make
302 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
303 include-pass: check-dir
304 library-pass: include-pass
305 binary-pass: library-pass
306
307 override OMK_SERIALIZE_INCLUDED = y
308 endif
309
310 #=====================================================================
311 # Special rules for target filesystem and data preparation
312
313 # Syntax: $(call TARFILE_template,<filename>,<filename2>,<directory>) FIXME: Is this correct?
314 define TARFILE_template
315
316 .PHONY: $(2)
317
318 $(2):
319         $(Q) ABSOLUTETARFILE=`cd $$(dir $$@) ; pwd`/$$@ ; \
320              ( test -e $$$$ABSOLUTETARFILE && ( cd  $(3) && tar -df $$$$ABSOLUTETARFILE ) ) || \
321              ( cd  $(3) && tar -cf $$$$ABSOLUTETARFILE . )
322
323 $(2).o: $(2)
324         @$(QUIET_CMD_ECHO) "  TARFILE $$@"
325         $(Q) $(OBJCOPY) -I binary -O elf32-littlearm  $$^ $$@
326
327 endef
328
329 #=====================================================================
330 # User-space rules and templates to compile programs, libraries etc.
331
332 ifdef USER_RULE_TEMPLATES
333
334
335 #%.lo: %.c
336 #       $(CC) -o $@ $(LCFLAGS) -c $<
337
338 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(AM_CPPFLAGS) \
339         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(INCLUDES) -DOMK_FOR_USER
340
341 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(AM_CPPFLAGS) \
342         $(CPPFLAGS) $(AM_CXXFLAGS) $(CFLAGS) $(INCLUDES) -DOMK_FOR_USER
343
344 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
345         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER
346
347 # Check GCC version for user build
348 ifndef CC_MAJOR_VERSION
349 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
350 endif
351 # Prepare suitable define for dependency building
352 ifeq ($(CC_MAJOR_VERSION),2)
353 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
354 else
355 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
356 endif
357
358
359 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
360 define COMPILE_c_o_template
361 $(2): $(1) $$(GEN_HEADERS)
362         @$(QUIET_CMD_ECHO) "  CC      $$@"
363         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
364         then mv -f "$$@.d.tmp" "$$@.d" ; \
365         else rm -f "$$@.d.tmp" ; exit 1; \
366         fi
367 endef
368
369
370 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
371 define COMPILE_cc_o_template
372 $(2): $(1) $$(GEN_HEADERS)
373         @$(QUIET_CMD_ECHO) "  CXX     $$@"
374         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
375         then mv -f "$$@.d.tmp" "$$@.d" ; \
376         else rm -f "$$@.d.tmp" ; exit 1; \
377         fi
378 endef
379
380
381 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
382 define COMPILE_S_o_template
383 $(2): $(1) $$(GEN_HEADERS)
384         @$(QUIET_CMD_ECHO) "  AS      $$@"
385         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
386         then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
387         else rm -f "$$@.d.tmp" ; exit 1; \
388         fi
389 endef
390
391 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
392 define CMETRIC_o_h_template
393 $(2): $(1)
394         @$(QUIET_CMD_ECHO) "  CMETRIC $$@"
395         $(Q)if [ -n `dirname $$@` ] ; then \
396               if [ ! -e `dirname $$@` ] ; then \
397                 mkdir -p `dirname $$@` ; fi ; fi
398         $(Q)echo >$$@ '/* Automatically generated from $$< */'
399         $(Q)echo >>$$@ '/* Conditionals to control compilation */'
400         $(Q)set -o pipefail ; $(NM) $$< \
401                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
402                 | sort >>$$@
403         $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
404         $(Q)set -o pipefail ; $(NM) $$< \
405                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
406                 | sort >>$$@
407 endef
408
409 # Syntax: $(call PROGRAM_template,<dir>,<executable-name>,<link-variant>)
410 define PROGRAM_template
411 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
412 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
413 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
414 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
415 $(1)_OBJS += $$($(1)_EMBEDTARFILES:%=%_tarfile.o)
416 $(1)_OBJS := $$(sort $$($(1)_OBJS))
417
418 USER_OBJS  += $$($(1)_OBJS)
419 USER_SOURCES += $$($(1)_SOURCES)
420 USER_EMBEDTARFILES += $$($(1)_EMBEDTARFILES)
421
422 $(2)/$(1): $(USER_LIB_DIR)/timestamp
423
424 $(2)/$(1): $$($(1)_OBJS)
425         @$(QUIET_CMD_ECHO) "  LINK    $$@"
426         $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
427           then echo $$(CC)  $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
428           else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
429           $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
430           -o $(2)/$(1)
431 #@echo "$(2)/$(1): \\" >$(USER_OBJS_DIR)/$(1).exe.d
432 #@sed -n -e 's/^LOAD \(.*\)$$$$/  \1  \\/p' $(USER_OBJS_DIR)/$(1).exe.map  >>$(USER_OBJS_DIR)/$(1).exe.d
433 #@echo >>$(USER_OBJS_DIR)/$(1).exe.d
434 #-Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map
435 # FIXME: Why the map file was commented out?
436 endef
437
438
439
440 # Syntax: $(call LIBRARY_template,<library-name>)
441 define LIBRARY_template
442 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
443 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
444 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
445 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
446 $(1)_OBJS += $$($(1)_EMBEDTARFILES:%=%_tarfile.o)
447 $(1)_OBJS := $$(sort $$($(1)_OBJS))
448
449 USER_OBJS  += $$($(1)_OBJS)
450 USER_SOURCES += $$($(1)_SOURCES)
451 USER_EMBEDTARFILES += $$($(1)_EMBEDTARFILES)
452
453 $(USER_LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
454         @$(QUIET_CMD_ECHO) "  AR      $$@"
455         $(Q) $(AR) rcs $$@ $$^
456         @touch $(USER_LIB_DIR)/timestamp
457 endef
458
459
460
461 # Syntax: $(call SOLIB_template,<library-name>)
462 define SOLIB_template
463 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
464 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
465 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
466 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
467 $(1)_OBJS += $$($(1)_EMBEDTARFILES:%=%_tarfile.o)
468 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
469
470 SOLIB_OBJS  += $$($(1)_OBJSLO)
471 SOLIB_SOURCES += $$($(1)_SOURCES)
472 USER_EMBEDTARFILES += $$($(1)_EMBEDTARFILES)
473
474 $(USER_LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
475         @$(QUIET_CMD_ECHO) "  LINK    $$@"
476         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
477 endef
478
479
480
481 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \
482                     $(lib_LIBRARIES:%=$(USER_LIB_DIR)/lib%.a) $(shared_LIBRARIES:%=$(USER_LIB_DIR)/lib%.so)
483
484 binary-pass-local: $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%) $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
485
486 # Special rules for CMETRIC generated headers
487
488 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\
489                 $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\
490                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),)))
491 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
492                 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\
493                 $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh)))))
494
495 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
496
497 # Generate rules for compilation of programs and libraries
498
499 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR))))
500
501 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR))))
502
503 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
504
505 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
506
507 -include $(USER_OBJS_DIR)/*.d
508
509 endif
510
511 #=====================================================================
512
513
514 $(eval $(call omk_pass_template, include-pass,$(USER_OBJS_DIR),,$(include_HEADERS)$(nobase_include_HEADERS)$(renamed_include_HEADERS)$(config_include_HEADERS)$(LOCAL_CONFIG_H)))
515 $(eval $(call omk_pass_template, library-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)))
516 $(eval $(call omk_pass_template, binary-pass, $(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(utils_PROGRAMS)))
517
518 $(eval $(call omk_pass_template,clean,$(USER_OBJS_DIR),,always))
519
520 check-dir:
521         @$(call mkdir_def,$(USER_INCLUDE_DIR))
522         @$(call mkdir_def,$(USER_LIB_DIR))
523         @$(call mkdir_def,$(USER_BIN_DIR))
524         @$(call mkdir_def,$(USER_UTILS_DIR))
525
526 include-pass-local:
527         $(call include-pass-template,$(USER_INCLUDE_DIR),include)
528
529 ifdef USER_RULE_TEMPLATES
530
531 # User-space static libraries and applications object files
532
533 USER_SOURCES := $(sort $(USER_SOURCES))
534
535 #$(warning USER_SOURCES = $(USER_SOURCES))
536
537 $(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)))
538
539 $(foreach src,$(filter %.cc,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)))
540
541 $(foreach src,$(filter %.cxx,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)))
542
543 $(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)))
544
545 # User-space shared libraries object files
546
547 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
548
549 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
550
551 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
552
553 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
554
555 $(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS))))
556
557 $(foreach src,$(filter %.S,$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.lo),$(SOLIB_PICFLAGS))))
558
559 $(foreach tarfile,$(USER_EMBEDTARFILES),$(eval $(call TARFILE_template,$(tarfile),$(tarfile)_tarfile,$(SOURCES_DIR)/$(tarfile))))
560
561 endif
562
563
564 clean-local:
565         @echo Cleaning in $(USER_OBJS_DIR)
566         @rm -f $(USER_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.lo \
567                $(USER_OBJS_DIR)/*.d \
568                $(USER_OBJS_DIR)/*.map \
569                $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%) \
570                $(tar_EMBEDFILES:%=$(USER_OBJS_DIR)/%_tarfile)
571
572
573 default: include-pass library-pass binary-pass
574
575 # Local Variables:
576 # mode:makefile
577 # End:
578                                                              #OMK@config_h
579 # Syntax: $(call BUILD_CONFIG_H_template,<stamp_dir>,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
580 define BUILD_CONFIG_H_template
581
582 $(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(wildcard $(CONFIG_FILE)) $(wildcard $(CONFIG_FILE)-default)
583         @$(QUIET_CMD_ECHO) "  CONFGEN $$(@:%.stamp=%)"
584         @if [ ! -d `dirname $(2).tmp` ] ; then \
585                 mkdir -p `dirname $(2).tmp` ; fi
586         @echo "/* Automatically generated from */" > "$(2).tmp"
587         @echo "/* config file: $$< */" >> "$(2).tmp"
588         $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp")
589         @echo "#ifndef $(4)" >> "$(2).tmp"
590         @echo "#define $(4)" >> "$(2).tmp"
591         @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | sed -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \
592                 echo '$(x).$($(x))' ; ) echo ; ) | \
593                 sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
594                 sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
595                   >> "$(2).tmp"
596         @echo "#endif /*$(4)*/" >> "$(2).tmp"
597         @touch "$$@"
598         @if cmp --quiet "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \
599         else mv "$(2).tmp" "$(2)" ; \
600         echo "Updated configuration $(2)" ; fi
601
602 endef
603
604 ifdef LOCAL_CONFIG_H
605
606 # This must be declared after the default cflags are assigned!
607 # Override is used to override command line assignemnt.
608 override CFLAGS += -I.
609 $(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
610
611 endif
612
613 # Special rules for configuration exported headers
614
615 #FIXME: The directory for headers should not be specified here.
616 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
617 _$(basename $(notdir $(confh)))_H \
618 )))
619
620 config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
621
622 # Add some hooks to standard passes
623 include-pass-local: $(config_h_stamp_files)
624
625 ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),)
626
627 ifdef LOCAL_CONFIG_H
628 $(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
629 endif
630
631 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
632 _$(basename $(notdir $(confh)))_H \
633 )))
634
635 kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H))))
636
637 # Add some hooks to standard passes
638 include-pass-local: $(kern_config_h_stamp_files)
639
640 endif
641
642 clean-local: clean-local-config-h
643
644 clean-local-config-h:
645         @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\
646             if [ -e $(confh) ] ; then rm $(confh) ; fi ; \
647         )
648
649
650 # Local Variables:
651 # mode:makefile
652 # End:
653                                                              #OMK@sources-list
654 # Rules that creates the list of files which are used during
655 # compilation. The list reflects conditional compilation depending on
656 # config.omk and other variables.
657
658 SOURCES_LIST_FN=sources.txt
659 ifndef SOURCES_LIST
660 SOURCES_LIST_DIR:=$(RELATIVE_DIR)
661 SOURCES_LIST:=$(MAKERULES_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN)
662 export SOURCES_LIST SOURCES_LIST_DIR
663 endif
664
665 ifeq ($(MAKECMDGOALS),sources-list)
666 NEED_SOURCES_LIST=y
667 endif
668 ifeq ($(MAKECMDGOALS),TAGS)
669 NEED_SOURCES_LIST=y
670 endif
671 ifeq ($(MAKECMDGOALS),tags)
672 NEED_SOURCES_LIST=y
673 endif
674
675 ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is not useful
676 .PHONY: sources-list
677 sources-list: $(SOURCES_LIST)
678
679 $(SOURCES_LIST): $(CONFIG_FILE) $(CONFIG_FILE)-default $(shell find -name $(MAKEFILE_OMK))
680         @echo -n "" > "$(SOURCES_LIST).tmp"
681         @$(MAKE) --no-print-directory sources-list-pass
682         @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2"
683         @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2"
684         @rm "$(SOURCES_LIST).tmp"
685         @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)"
686 endif
687
688 $(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always))
689
690 sources-list-pass-local:
691         @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\
692           echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)" >> "$(SOURCES_LIST).tmp";)
693         @$(foreach ch,$(config_include_HEADERS), \
694           echo "$(USER_INCLUDE_DIR:$(MAKERULES_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";)
695         @$(foreach h,$(renamed_include_HEADERS),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)"|sed -e 's/\(.*\)->.*/\1/' >> "$(SOURCES_LIST).tmp";)
696         @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(utils_PROGRAMS) \
697           $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\
698           $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(src)" >> "$(SOURCES_LIST).tmp";))
699
700 ############ TAGS ###########
701
702 ifeq ($(MAKECMDGOALS),TAGS)
703 ETAGS=etags
704 TAGS_CMD = $(ETAGS)
705 TAGS: $(SOURCES_LIST)
706         @$(MAKE) --no-print-directory do-tags
707 endif
708 ifeq ($(MAKECMDGOALS),tags) 
709 CTAGS=ctags -N
710 TAGS_CMD = $(CTAGS)
711 tags: $(SOURCES_LIST)
712         @$(MAKE) --no-print-directory do-tags
713 endif
714 export TAGS_CMD
715
716 ifeq ($(MAKECMDGOALS),do-tags)
717 .PHONY: do-tags
718 do-tags: $(shell sed -e '/^\#/d' $(SOURCES_LIST))
719         @$(QUIET_CMD_ECHO) "  TAGS    $(SOURCES_LIST_FN)"
720         $(Q)$(TAGS_CMD) $^
721 endif
722
723 ############ CSCOPE ###########
724
725 cscope: $(SOURCES_LIST)
726         @$(QUIET_CMD_ECHO) "  CSCOPE  < $(SOURCES_LIST_FN)"
727         $(Q)sed -e '/^#/d' $(SOURCES_LIST)|cscope -b -i-
728 #FIXME: see doc to -i in cscope(1)
729
730 # Local Variables:
731 # mode:makefile
732 # End: