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