1 # Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
3 # (C) Copyright 2003 by Pavel Pisa - OCERA team member
4 # (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
6 # Version for system-less builds. #OMK@sysless
10 # V .. if set to 1, full command text is shown else short form is used
11 # SUBDIRS .. list of subdirectories intended for make from actual directory
12 # default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ...
13 # LOCAL_CONFIG_H .. name of local config.h file generated from values #OMK@config_h
14 # of options defined in the current directory
15 # config_include_HEADERS .. names of global config files (possibly
16 # with subdirectories)
17 # xxx_DEFINES .. list of config directives to be included in
18 # config header file of the name <somedir>/xxx.h
19 # LN_HEADERS .. if defined, header files are symbolicaly linked instead of copied. #OMK@include
21 # bin_PROGRAMS .. list of the require binary programs
22 # include_HEADERS .. list of the user-space public header files
23 # lib_LIBRARIES .. list of the user-space libraries
24 # lib_LDSCRIPTS .. list of LD scripts that should be copied to the lib direcotry
25 # lib_obj_SOURCES .. list of source files which should be compiled and
26 # the produced object file placed to the lib directory (e.g. crt0.S)
27 # shared_LIBRARIES .. list of the user-space shared libraries
28 # nobase_include_HEADERS .. public headers copied even with directory part
29 # renamed_include_HEADERS .. public headers copied to the different target name (xxx.h->yyy.h)
30 # utils_PROGRAMS .. list of the development utility programs (compiled for host computer, this might change in future)
31 # xxx_SOURCES .. list of specific target sources
32 # xxx_LIBS .. list of specific target libraries
33 # INCLUDES .. additional include directories and defines for user-space
34 # lib_LOADLIBES .. list of libraries linked to each executable
35 # link_VARIANTS .. list of ld script suffixes (after hypen `-') that
36 # should be used for linking (e.g. ram flash). If this is not
37 # specified, then the value of DEFAULT_LD_SCRIPT_VARIANT from config.target is used.
38 # PREFIX_DIR .. Prefix to directories in _compiled and _build. Used in config.omk.
39 # local_EVALUATE .. Makefile hook, which is executed at the end of #OMK@localeval
40 # the Makefile.rules. Used only for dirty hacks.
42 BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
43 COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
45 LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
46 #$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
48 #=====================================================================
49 # Common utility rules
51 link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
54 #=====================================================================
55 # Include correct rules for just running pass
57 USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
59 USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
60 USER_LIB_DIR = $(USER_COMPILED_DIR_NAME)/lib
61 USER_UTILS_DIR = $(USER_COMPILED_DIR_NAME)/bin-utils
62 USER_BIN_DIR = $(USER_COMPILED_DIR_NAME)/bin
63 USER_OBJS_DIR = $(LOCAL_BUILD_DIR)
69 # We need to ensure definition of sources directory first
71 SOURCES_DIR := $(shell ( pwd -L ) )
74 .PHONY: all default check-make-ver
76 all: check-make-ver default
78 #=========================
79 # Include the config file
81 ifneq ($(CONFIG_FILE_OK),y)
83 CONFIG_FILE := $(MAKERULES_DIR)/config.omk
85 ifneq ($(wildcard $(CONFIG_FILE)-default),)
86 -include $(CONFIG_FILE)-default
88 $(warning Please, run "make default-config" first)
91 ifneq ($(wildcard $(CONFIG_FILE)),)
92 include $(CONFIG_FILE)
95 endif #$(CONFIG_FILE_OK)
97 export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR
98 export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT
99 # OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes
100 # must to be serialized only in the toplevel make.
103 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
105 override RELATIVE_DIR := $(RELATIVE_DIR:/%=%)
106 override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%)
107 #$(warning RELATIVE_DIR $(RELATIVE_DIR))
108 override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__')
109 #$(warning BACK2TOP_DIR $(BACK2TOP_DIR))
111 #$(warning SOURCES_DIR = $(SOURCES_DIR))
112 #$(warning MAKERULES_DIR = $(MAKERULES_DIR))
113 #$(warning RELATIVE_DIR = $(RELATIVE_DIR))
115 #vpath %.c $(SOURCES_DIR)
116 #vpath %.cc $(SOURCES_DIR)
117 #vpath %.cxx $(SOURCES_DIR)
119 VPATH = $(SOURCES_DIR)
120 srcdir = $(SOURCES_DIR)
122 # Defines for quiet compilation
124 ifeq ("$(origin V)", "command line")
131 ifeq ($(OMK_VERBOSE),1)
136 ifneq ($(findstring s,$(MAKEFLAGS)),)
137 QUIET_CMD_ECHO = true
140 QUIET_CMD_ECHO = echo
143 # ===================================================================
144 # We have set up all important variables, so we can check and include
145 # real OCERA style Makefile.omk now
147 include $(SOURCES_DIR)/Makefile.omk
153 @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \
154 if [ x$$GOOD_MAKE_VERSION != xy ] ; then \
155 echo "Your make program version is too old and does not support OMK system." ; \
156 echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \
159 distclean dist-clean:
160 @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)"
161 @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME) $(MAKERULES_DIR)/$(BUILD_DIR_NAME)
163 # Common OMK templates
164 # ====================
166 # Syntax: $(call mkdir,<dir name>)
168 [ -d $(1) ] || mkdir -p $(1) || exit 1
171 # Syntax: $(call omk_pass_template,<pass name>,<build dir>,[<local make flags>],[<local condition>],[<subpass>])
172 define omk_pass_template
173 .PHNOY: $(1) $(1)-local
175 +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)) ; \
176 $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) --no-print-directory \
177 RELATIVE_DIR=$(RELATIVE_DIR)/$(dir) -C $(2)/$(dir) \
178 -f $(SOURCES_DIR)/$(dir)/Makefile $$@ || exit 1 ;)
180 +@if [ $(4) ] || [ -z "$(subst ",\",$(4))" ]; then \
181 echo " MAKE $$@ in $(RELATIVE_DIR)"; \
182 $(call mkdir_def,$(2)); \
183 $(MAKE) --no-print-directory -C $(2) \
184 -f $(SOURCES_DIR)/Makefile $(3) $$(@:%=%-local); \
189 # =======================
190 # DEFAULT CONFIG PASS
193 @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default"
194 @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default"
195 @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default"
196 @echo >> "$(CONFIG_FILE)-default"
197 @$(MAKE) --no-print-directory -C $(MAKERULES_DIR) \
198 RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \
199 -f $(MAKERULES_DIR)/Makefile default-config-pass
201 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR)))
203 default-config-pass-local:
204 # @echo Default config for $(RELATIVE_DIR)
205 @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default"
206 @$(foreach x, $(default_CONFIG), echo $(x) | \
207 sed -e 's/^.*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; )
209 # Rules for compilation of C, C++ and assembler sources using GNU
212 # Interface to other rules:
215 # LIB_DIR - directory where compiled libraries are stored
216 # OBJS_DIR - directory where intermediate files (.o, .map, ...) are stored
217 # INCLUDE_DIR - where includes can be found
218 # from config.omk or Makefile.omk
220 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
221 # from base: SOURCES_DIR
222 # from Makefile.omk: lib_LOADLIBES
225 # SOURCES - all the source files that needs to be compiled (except for shared library sources)
226 # SOLIB_SOURCES - all the source files that needs to be compiled for a shared library
229 # COMPILER_DEFS_template - definitions that should be defined before
230 # the following templates can be used. The input variables needs to be
231 # defined before evaluating this template
233 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
234 # templates that create rules for compilation of sources
236 # CMETRIC_o_h_template - FIXME
238 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
239 # create rules for compilation of a program, library and shared
240 # library. The rules can use rules produced by COMPILE_xxx_template.
242 define COMPILER_DEFS_template
243 CC = $(CROSS_COMPILE)gcc
244 LINK = $(CROSS_COMPILE)ld
245 AR = $(CROSS_COMPILE)ar
246 OBJCOPY = $(CROSS_COMPILE)objcopy
247 NM = $(CROSS_COMPILE)nm
249 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
251 CFLAGS += -I$(SOURCES_DIR)
252 CFLAGS += -I$(INCLUDE_DIR)
254 LOADLIBES += -L$(LIB_DIR)
255 LOADLIBES += $(lib_LOADLIBES:%=-l%)
258 -include $(OBJS_DIR)/*.d
261 # $(CC) -o $@ $(LCFLAGS) -c $<
263 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
264 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
266 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
267 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
269 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
270 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
273 # Check GCC version for user build
274 ifndef CC_MAJOR_VERSION
275 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
277 # Prepare suitable define for dependency building
278 ifeq ($$(CC_MAJOR_VERSION),2)
279 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
281 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
284 # lib_obj_SOURCES handling
286 @cp $$(CP_FLAGS) $$< $$@
287 endef # COMPILER_DEFS_template
290 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
291 define COMPILE_c_o_template
292 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
293 @$(QUIET_CMD_ECHO) " CC $$@"
294 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
295 then mv -f "$$@.d.tmp" "$$@.d" ; \
296 else rm -f "$$@.d.tmp" ; exit 1; \
302 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
303 define COMPILE_cc_o_template
304 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
305 @$(QUIET_CMD_ECHO) " CXX $$@"
306 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
307 then mv -f "$$@.d.tmp" "$$@.d" ; \
308 else rm -f "$$@.d.tmp" ; exit 1; \
312 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
313 define COMPILE_S_o_template
314 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
315 @$(QUIET_CMD_ECHO) " AS $$@"
316 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
317 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
318 else rm -f "$$@.d.tmp" ; exit 1; \
322 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
323 define CMETRIC_o_h_template
325 @$(QUIET_CMD_ECHO) " CMETRIC $$@"
326 $(Q)if [ -n `dirname $$@` ] ; then \
327 if [ ! -e `dirname $$@` ] ; then \
328 mkdir -p `dirname $$@` ; fi ; fi
329 $(Q)echo >$$@ '/* Automatically generated from $$< */'
330 $(Q)echo >>$$@ '/* Conditionals to control compilation */'
331 $(Q)set -o pipefail ; $(NM) $$< \
332 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
334 $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
335 $(Q)set -o pipefail ; $(NM) $$< \
336 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
340 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
341 define PROGRAM_template
342 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
343 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
344 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
345 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
346 $(1)_OBJS += $$(filter %.o,$(1)_SOURCES)
347 $(1)_OBJS := $$(sort $$($(1)_OBJS))
349 SOURCES += $$($(1)_SOURCES)
351 ifneq ($(LD_SCRIPT),)
352 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
355 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
356 @$(QUIET_CMD_ECHO) " LINK $$@"
357 $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
358 then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
359 else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
360 $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
361 $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \
363 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
364 @if [ -n "$(LD_SCRIPT)" ]; then \
365 echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
366 @sed -n -e 's/^LOAD \(.*\)$$$$/ \1 \\/p' $(OBJS_DIR)/$(1)$(3:%=-%).map >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
367 @echo >>$(OBJS_DIR)/$(1).exe.d
370 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
372 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
373 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
376 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
377 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
380 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
381 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
383 # Syntax: $(call LIBRARY_template,<library-name>)
384 define LIBRARY_template
385 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
386 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
387 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
388 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
389 $(1)_OBJS := $$(sort $$($(1)_OBJS))
391 SOURCES += $$($(1)_SOURCES)
393 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
394 @$(QUIET_CMD_ECHO) " AR $$@"
395 $(Q) $(AR) rcs $$@ $$^
399 # Syntax: $(call SOLIB_template,<library-name>)
400 define SOLIB_template
401 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
402 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
403 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
404 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
405 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
407 SOLIB_OBJS += $$($(1)_OBJSLO)
408 SOLIB_SOURCES += $$($(1)_SOURCES)
410 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
411 @$(QUIET_CMD_ECHO) " LINK $$@"
412 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
416 # lib_obj_SOURCES handling
417 lib_OBJS = $(addsuffix .o,$(basename $(lib_obj_SOURCES)))
418 SOURCES += $(filter-out %.o,$(lib_obj_SOURCES))
419 #$(warning lib_OBJS = $(lib_OBJS))
421 # # Syntax: $(call LIBOBJ_template,<object-name>)
422 # define LIBOBJ_template
423 # $(LIB_DIR)/$(1): $(1)
424 # @cp $(CP_FLAGS) $$< $$@
431 # Syntax: $(call BUILD_CONFIG_H_template,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
432 define BUILD_CONFIG_H_template
434 $(1) : $(wildcard $(CONFIG_FILE)) $(wildcard $(CONFIG_FILE)-default)
435 @$(QUIET_CMD_ECHO) " CONFGEN $$@ in `pwd`"
436 @if [ ! -d `dirname $(1).tmp` ] ; then \
437 mkdir `dirname $(1).tmp` ; fi
438 @echo "/* Automatically generated from */" > "$(1).tmp"
439 @echo "/* config file: $$< */" >> "$(1).tmp"
440 @echo "#ifndef $(3)" >> "$(1).tmp"
441 @echo "#define $(3)" >> "$(1).tmp"
442 @( $(foreach x, $(shell echo '$($(2))' | sed -e 's/\<\([^ =]*\)\(=[^ ]\+\|\)\>/\1/g' ), \
443 echo '$(x).$($(x))' ; ) echo ; ) | \
444 sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \
445 sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \
447 @echo "#endif /*$(3)*/" >> "$(1).tmp"
448 @if cmp --quiet "$(1).tmp" "$(1)" ; then \
449 echo rm "$(1).tmp" ; \
450 else mv "$(1).tmp" "$(1)" ; \
451 echo Updated configuration "$(1)" ; fi
458 $(eval $(call BUILD_CONFIG_H_template,$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
462 # Special rules for configuration exported headers
464 #FIXME: The directory for headers should not be specified here.
465 $(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\
466 _$(basename $(notdir $(confh)))_H \
469 # Add some hooks to standard passes
470 include-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS))
472 clean-local: clean-local-config-h
474 clean-local-config-h:
475 @$(foreach confh,$(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS)),\
476 if [ -e $(confh) ] ; then touch -t 200001010101 $(confh) ; fi ; \
483 ifeq ($(OMK_VERBOSE),1) #OMK@include
490 cp $(CPHEADER_FLAGS) $(1) $(2)
494 ln -sf $(LNHEADER_FLAGS) $(1) $(2)
498 # TODO: Check modification date of changed header files. If it is
499 # newer that in source dir, show a warning.
501 # Syntax: $(call include-pass-template,<include dir>,<keyword>)
502 define include-pass-template
503 @$(foreach f, $($(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(notdir $(f)) \
504 || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(notdir $(f))) || exit 1 ; )
505 @$(foreach f, $(nobase_$(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(f) \
506 || ( mkdir -p $(1)/$(dir $(f)) && $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(f)) ) || exit 1 ; )
507 @$(foreach f, $(renamed_$(2)_HEADERS), \
508 srcfname=`echo '$(f)' | sed -e 's/^\(.*\)->.*$$/\1/'` ; destfname=`echo '$(f)' | sed -e 's/^.*->\(.*\)$$/\1/'` ; \
509 cmp --quiet $(SOURCES_DIR)/$${srcfname} $(1)/$${destfname} \
510 || ( mkdir -p `dirname $(1)/$${destfname}` && $(call cp_cmd,$(SOURCES_DIR)/$${srcfname},$(1)/$${destfname}) ) || exit 1 ; )
517 # Some support to serialize some targets for parallel make
518 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
519 include-pass: $(check-dir)
520 library-pass: include-pass
521 binary-pass utils-pass: library-pass
523 OMK_SERIALIZE_INCLUDED = y
526 ifeq ($(wildcard $(MAKERULES_DIR)/config.target),$(MAKERULES_DIR)/config.target)
527 $(error Please, rename $(MAKERULES_DIR)/config.target to config.omk!)
532 # -------------------------------------
533 # Rules for compilation for target
534 ifdef TARGET_RULE_TEMPLATES
536 LDFLAGS += -nostartfiles
538 # FIXME: These are not used. What they are good for?
539 LIB_CPPFLAGS += $(CPPFLAGS)
540 LIB_CFLAGS += $(CFLAGS)
542 SOLIB_PICFLAGS += -shared -fpic
544 CFLAGS += -DOMK_FOR_TARGET
546 INCLUDE_DIR := $(USER_INCLUDE_DIR)
547 LIB_DIR := $(USER_LIB_DIR)
548 OBJS_DIR := $(USER_OBJS_DIR)
550 $(eval $(COMPILER_DEFS_template))
552 # Special rules for CMETRIC generated headers
554 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\
555 $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\
556 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),)))
557 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
558 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\
559 $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh)))))
561 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
563 # Generate rules for compilation of programs and libraries
564 $(foreach prog,$(bin_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
565 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
566 #$(foreach src,$(lib_obj_SOURCES),$(eval $(call LIBOBJ_template,$(addsuffix .o,$(basename $(src)))))) # FIXME: This can't work LIBOBJ_template is not defined
567 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
569 # User-space static libraries and applications object files
570 SOURCES := $(sort $(SOURCES))
571 #$(warning SOURCES = $(SOURCES))
573 # User-space shared libraries object files
574 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
575 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
578 # The above generated rules produced $(SOURCES) and $(SOLIB_SOURCES)
579 # variables. Now generate rules for compilation of theese sources
580 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)))
581 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)))
582 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)))
583 $(foreach src,$(filter %.S,$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)))
585 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
586 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
587 $(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS))))
588 $(foreach src,$(filter %.S,$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.lo),$(SOLIB_PICFLAGS))))
590 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \
591 $(lib_LIBRARIES:%=$(LIB_DIR)/lib%.a) $(shared_LIBRARIES:%=$(LIB_DIR)/lib%.so) \
592 $(addprefix $(LIB_DIR)/,$(lib_OBJS))
594 binary-pass-local: $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)) \
595 $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of))))
598 endif # TARGET_RULE_TEMPLATES
601 # -------------------------------------
602 # Rules for compilation utilities for host (user space)
603 ifdef HOST_RULE_TEMPLATES
608 SOLIB_PICFLAGS += -shared -fpic
610 # For host compilation, we don't use a specfic ld script
613 # TODO: It is probably better to use different directories for host
614 # includes, libraries and objects
615 INCLUDE_DIR := $(USER_INCLUDE_DIR)
616 LIB_DIR := $(USER_LIB_DIR)
617 OBJS_DIR := $(USER_OBJS_DIR)
619 $(eval $(COMPILER_DEFS_template))
621 #User-space static libraries and applications object files
622 #SOURCES := $(sort $(SOURCES))
623 #$(warning SOURCES = $(SOURCES))
625 # Generate rules for compilation of utility programs
626 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),)))
628 # The above generated rule produced $(SOURCES) variable. Now generate
629 # rules for compilation of theese sources
630 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)))
631 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)))
632 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)))
633 $(foreach src,$(filter %.S,$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)))
635 utils-pass-local: $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
637 endif # HOST_RULE_TEMPLATES
640 #=====================================================================
641 # Automatic loading of compiled program by issuing "make load"
643 ifneq ($(OUTPUT_FORMATS),)
644 # Select a file extension (e.g. .bin) for "make load" command to load.
645 LOAD_EXTENSION = .$(firstword $(OUTPUT_FORMATS))
648 # Syntax: $(call LOAD_PROGRAM_template,<executable-name>,<dir>,<link-variant>)
649 # Used to load program to the target hardware
650 define LOAD_PROGRAM_template
651 .PHONY: load-$(1)$(3:%=-%)
652 load-$(1)$(3:%=-%): $(2)/$(1)$(3:%=-%)$(LOAD_EXTENSION)
653 @$(QUIET_CMD_ECHO) " LOAD $$<"
654 @if [ -z "$$(LOAD_CMD$(3:%=-%))" ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
655 $(Q) $$(LOAD_CMD$(3:%=-%)) $$<
658 # Syntax: $(call LOAD__RUN_VARIANT_template,<link-variant>)
659 # Used to load and/or run non-default variant of the default program
660 define LOAD_RUN_VARIANT_template
661 .PHONY: load-$(1) run-$(1)
663 load-$(1): load-$(firstword $(bin_PROGRAMS))-$(1)
666 @$(QUIET_CMD_ECHO) " RUN $(1)"
667 @if [ -z "$(RUN_CMD-$(1))" ]; then echo "No command for running '$(1)' variant is specified."; exit 1; fi
672 $(foreach link,$(link_VARIANTS),$(foreach prog,$(bin_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
673 $(foreach link,$(link_VARIANTS),$(eval $(call LOAD_RUN_VARIANT_template,$(link))))
676 load: $(addprefix load-,$(firstword $(bin_PROGRAMS))-$(firstword $(link_VARIANTS)))
678 run: run-$(firstword $(link_VARIANTS))
682 #=====================================================================
683 # Generate pass rules from generic templates
684 OTHER_PASSES = dep clean install
686 $(eval $(call omk_pass_template, include-pass, $(LOCAL_BUILD_DIR),,-n "$(include_HEADERS)$(nobase_include_HEADERS)$(renamed_include_HEADERS)$(lib_LDSCRIPTS)"))
687 $(eval $(call omk_pass_template, library-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y, -n "$(lib_LIBRARIES)$(shared_LIBRARIES)$(lib_OBJS)"))
688 $(eval $(call omk_pass_template, binary-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y, -n "$(bin_PROGRAMS)"))
689 $(eval $(call omk_pass_template, utils-pass, $(LOCAL_BUILD_DIR),HOST_RULE_TEMPLATES=y,-n "$(utils_PROGRAMS)"))
692 $(eval $(call omk_pass_template,$(OTHER_PASSES),$(LOCAL_BUILD_DIR)))
700 $(call include-pass-template,$(USER_INCLUDE_DIR),include)
701 @$(foreach f, $(lib_LDSCRIPTS), cmp --quiet $(SOURCES_DIR)/$(f) $(USER_LIB_DIR)/$(notdir $(f)) \
702 || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(USER_LIB_DIR)/$(notdir $(f))) || exit 1 ; )
706 clean-local: clean-custom
707 @echo Cleaning in $(USER_OBJS_DIR)
708 @rm -f $(USER_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.lo \
709 $(USER_OBJS_DIR)/*.d \
710 $(USER_OBJS_DIR)/*.map \
711 $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
714 @$(call mkdir_def,$(USER_INCLUDE_DIR))
715 @$(call mkdir_def,$(USER_LIB_DIR))
716 @$(call mkdir_def,$(USER_BIN_DIR))
717 @$(call mkdir_def,$(USER_UTILS_DIR))
719 # Create directories only for the first time
720 ifndef CHECK_DIR_DONE
721 check-dir = check-dir
724 export CHECK_DIR_DONE
726 # Which passes to pass
727 default: $(check-dir) include-pass library-pass binary-pass utils-pass
729 ETAGS = etags -R -f $(MAKERULES_DIR)/TAGS
732 $(ETAGS) $(MAKERULES_DIR)/app
733 $(ETAGS) -a $(MAKERULES_DIR)/arch/$(ARCH)/generic
734 $(ETAGS) -a $(MAKERULES_DIR)/arch/$(ARCH)/mach-$(MACH)
735 $(ETAGS) -a $(MAKERULES_DIR)/board/$(BOARD)
736 $(ETAGS) -a $(MAKERULES_DIR)/libs4c
742 ifneq ($(local_EVALUATE),)
743 #$(warning $(local_EVALUATE))
744 $(eval $(local_EVALUATE))