]> rtime.felk.cvut.cz Git - sysless.git/blob - Makefile.rules
Makefile.rules updated from the standalone OMK project.
[sysless.git] / Makefile.rules
1 #  Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base
2 #
3 #  (C) Copyright 2003 by Pavel Pisa - OCERA team member
4 #  (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE
5 #
6 #                   Version for system-less builds.          #OMK@sysless
7 #
8 #                                                            #OMK@base
9 # input variables
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
20 #                                                            #OMK@sysless
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.
41                                                              #OMK@sysless-setup
42 BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
43 COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
44
45 LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
46 #$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
47
48 #=====================================================================
49 # Common utility rules
50
51 link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
52
53
54 #=====================================================================
55 # Include correct rules for just running pass
56
57 USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
58
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)
64
65 # Local Variables:
66 # mode:makefile
67 # End:
68                                                              #OMK@base
69 # We need to ensure definition of sources directory first
70 ifndef SOURCES_DIR
71 SOURCES_DIR := $(shell ( pwd -L ) )
72 endif
73
74 .PHONY: all default check-make-ver
75
76 all: check-make-ver default
77
78 #=========================
79 # Include the config file
80
81 ifneq ($(CONFIG_FILE_OK),y)
82 ifndef CONFIG_FILE
83 CONFIG_FILE      := $(MAKERULES_DIR)/config.omk
84 endif
85 ifneq ($(wildcard $(CONFIG_FILE)-default),)
86 -include $(CONFIG_FILE)-default
87 else
88 $(warning Please, run "make default-config" first)
89 endif
90
91 ifneq ($(wildcard $(CONFIG_FILE)),)
92 include $(CONFIG_FILE)
93 CONFIG_FILE_OK = y
94 endif
95 endif #$(CONFIG_FILE_OK)
96
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.
101
102 ifndef RELATIVE_DIR
103 RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%)
104 endif
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))
110
111 #$(warning SOURCES_DIR = $(SOURCES_DIR))
112 #$(warning MAKERULES_DIR = $(MAKERULES_DIR))
113 #$(warning RELATIVE_DIR = $(RELATIVE_DIR))
114
115 #vpath %.c $(SOURCES_DIR)
116 #vpath %.cc $(SOURCES_DIR)
117 #vpath %.cxx $(SOURCES_DIR)
118
119 VPATH = $(SOURCES_DIR)
120 srcdir = $(SOURCES_DIR)
121
122 # Defines for quiet compilation
123 ifdef V
124   ifeq ("$(origin V)", "command line")
125     OMK_VERBOSE = $(V)
126   endif
127 endif
128 ifndef OMK_VERBOSE
129   OMK_VERBOSE = 0
130 endif
131 ifeq ($(OMK_VERBOSE),1)
132   Q =
133 else
134   Q = @
135 endif
136 ifneq ($(findstring s,$(MAKEFLAGS)),)
137   QUIET_CMD_ECHO = true
138   OMK_SILENT = 1
139 else
140   QUIET_CMD_ECHO = echo
141 endif
142
143 # ===================================================================
144 # We have set up all important variables, so we can check and include
145 # real OCERA style Makefile.omk now
146 ifndef OMK_INCLUDED
147 include $(SOURCES_DIR)/Makefile.omk
148 OMK_INCLUDED := 1
149 endif
150
151
152 check-make-ver:
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 ; \
157         fi
158
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)
162
163 # Common OMK templates
164 # ====================
165
166 # Syntax: $(call mkdir,<dir name>)
167 define mkdir_def
168         [ -d $(1) ] || mkdir -p $(1) || exit 1
169 endef
170
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
174 $(1):
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 ;)
179         $(5)
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); \
185         fi
186
187 endef
188
189 # =======================
190 # DEFAULT CONFIG PASS
191
192 default-config:
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
200
201 $(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR)))
202
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" ; )
208                                                              #OMK@gcc
209 # Rules for compilation of C, C++ and assembler sources using GNU
210 # toolchain.
211
212 # Interface to other rules:
213
214 # Input variables:
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
219 # CROSS_COMPILE - 
220 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
221 # from base: SOURCES_DIR
222 # from Makefile.omk: lib_LOADLIBES
223
224 # Output variables:
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
227
228 # Templates:
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
232
233 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
234 # templates that create rules for compilation of sources
235
236 # CMETRIC_o_h_template - FIXME
237
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.
241
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
248
249 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
250 CFLAGS  += -Wall
251 CFLAGS += -I$(SOURCES_DIR)
252 CFLAGS += -I$(INCLUDE_DIR)
253
254 LOADLIBES += -L$(LIB_DIR)
255 LOADLIBES += $(lib_LOADLIBES:%=-l%)
256
257
258 -include $(OBJS_DIR)/*.d
259
260 #%.lo: %.c
261 #       $(CC) -o $@ $(LCFLAGS) -c $<
262
263 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
264         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
265
266 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
267         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
268
269 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
270         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
271
272
273 # Check GCC version for user build
274 ifndef CC_MAJOR_VERSION
275 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
276 endif
277 # Prepare suitable define for dependency building
278 ifeq ($$(CC_MAJOR_VERSION),2)
279 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
280 else
281 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
282 endif
283
284 # lib_obj_SOURCES handling
285 $(LIB_DIR)/%.o: %.o
286         @cp $$(CP_FLAGS) $$< $$@
287 endef # COMPILER_DEFS_template
288
289
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; \
297         fi
298 endef
299
300
301
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; \
309         fi
310 endef
311
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; \
319         fi
320 endef
321
322 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
323 define CMETRIC_o_h_template
324 $(2): $(1)
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' \
333                 | sort >>$$@
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' \
337                 | sort >>$$@
338 endef
339
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))
348
349 SOURCES += $$($(1)_SOURCES)
350
351 ifneq ($(LD_SCRIPT),)
352 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
353 endif
354
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%) \
362           -o $$@
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
368 endef
369
370 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
371 %.bin: %
372         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
373         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
374
375 %.hex: %
376         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
377         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
378
379 %.srec: %
380         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
381         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
382
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))
390
391 SOURCES += $$($(1)_SOURCES)
392
393 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
394         @$(QUIET_CMD_ECHO) "  AR      $$@"
395         $(Q) $(AR) rcs $$@ $$^
396 endef
397
398
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))
406
407 SOLIB_OBJS  += $$($(1)_OBJSLO)
408 SOLIB_SOURCES += $$($(1)_SOURCES)
409
410 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
411         @$(QUIET_CMD_ECHO) "  LINK    $$@"
412         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
413 endef
414
415
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))
420
421 # # Syntax: $(call LIBOBJ_template,<object-name>)
422 # define LIBOBJ_template
423 # $(LIB_DIR)/$(1): $(1)
424 #       @cp $(CP_FLAGS) $$< $$@
425 # endef
426
427 # Local Variables:
428 # mode:makefile
429 # End:
430                                                              #OMK@config_h
431 # Syntax: $(call BUILD_CONFIG_H_template,<header_file_path>,<list_of_options_to_export>,<header_barrier>)
432 define BUILD_CONFIG_H_template
433
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' \
446                   >> "$(1).tmp"
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
452
453 endef
454
455 ifdef LOCAL_CONFIG_H
456
457 CFLAGS += -I.
458 $(eval $(call BUILD_CONFIG_H_template,$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) )
459
460 endif
461
462 # Special rules for configuration exported headers
463
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 \
467 )))
468
469 # Add some hooks to standard passes
470 include-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS))
471
472 clean-local: clean-local-config-h
473
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 ; \
477         )
478
479
480 # Local Variables:
481 # mode:makefile
482 # End:
483 ifeq ($(OMK_VERBOSE),1)                                      #OMK@include
484 CPHEADER_FLAGS += -v
485 LNHEADER_FLAGS += -v
486 endif
487
488 ifndef LN_HEADERS
489 define cp_cmd
490 cp $(CPHEADER_FLAGS) $(1) $(2)
491 endef
492 else
493 define cp_cmd
494 ln -sf $(LNHEADER_FLAGS) $(1) $(2)
495 endef
496 endif
497
498 # TODO: Check modification date of changed header files. If it is
499 # newer that in source dir, show a warning.
500
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 ; )
511 endef
512
513 # Local Variables:
514 # mode:makefile
515 # End:
516                                                              #OMK@sysless
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
522
523 OMK_SERIALIZE_INCLUDED = y
524 endif
525
526 ifeq ($(wildcard $(MAKERULES_DIR)/config.target),$(MAKERULES_DIR)/config.target)
527 $(error Please, rename $(MAKERULES_DIR)/config.target to config.omk!)
528 endif
529
530
531
532 # -------------------------------------
533 # Rules for compilation for target
534 ifdef TARGET_RULE_TEMPLATES
535
536 LDFLAGS += -nostartfiles
537
538  # FIXME: These are not used. What they are good for?
539 LIB_CPPFLAGS += $(CPPFLAGS)
540 LIB_CFLAGS   += $(CFLAGS)
541
542 SOLIB_PICFLAGS += -shared -fpic
543
544 CFLAGS += -DOMK_FOR_TARGET
545
546 INCLUDE_DIR := $(USER_INCLUDE_DIR)
547 LIB_DIR     := $(USER_LIB_DIR)
548 OBJS_DIR    := $(USER_OBJS_DIR)
549
550 $(eval $(COMPILER_DEFS_template))
551
552 # Special rules for CMETRIC generated headers
553
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)))))
560
561 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
562
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))))
568
569 # User-space static libraries and applications object files
570 SOURCES := $(sort $(SOURCES))
571 #$(warning SOURCES = $(SOURCES))
572
573 # User-space shared libraries object files
574 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
575 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
576
577
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),)))
584
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))))
589
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))
593
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))))
596
597
598 endif # TARGET_RULE_TEMPLATES
599
600
601 # -------------------------------------
602 # Rules for compilation utilities for host (user space)
603 ifdef HOST_RULE_TEMPLATES
604
605 CROSS_COMPILE =
606 TARGET_ARCH =
607
608 SOLIB_PICFLAGS += -shared -fpic
609
610 # For host compilation, we don't use a specfic ld script
611 LD_SCRIPT =
612
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)
618
619 $(eval $(COMPILER_DEFS_template))
620
621  #User-space static libraries and applications object files
622 #SOURCES := $(sort $(SOURCES))
623 #$(warning SOURCES = $(SOURCES))
624
625 # Generate rules for compilation of utility programs
626 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),)))
627
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),)))
634
635 utils-pass-local: $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
636
637 endif # HOST_RULE_TEMPLATES
638
639
640 #=====================================================================
641 # Automatic loading of compiled program by issuing "make load"
642
643 ifneq ($(OUTPUT_FORMATS),)
644 # Select a file extension (e.g. .bin) for "make load" command to load.
645 LOAD_EXTENSION = .$(firstword $(OUTPUT_FORMATS))
646 endif
647
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:%=-%)) $$<
656 endef
657
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)
662
663 load-$(1): load-$(firstword $(bin_PROGRAMS))-$(1)
664
665 run-$(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
668         $(Q) $(RUN_CMD-$(1))
669
670 endef
671
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))))
674
675 .PHONY: load run
676 load: $(addprefix load-,$(firstword $(bin_PROGRAMS))-$(firstword $(link_VARIANTS)))
677
678 run: run-$(firstword $(link_VARIANTS))
679
680
681
682 #=====================================================================
683 # Generate pass rules from generic templates
684 OTHER_PASSES = dep clean install
685
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)"))
690
691
692 $(eval $(call omk_pass_template,$(OTHER_PASSES),$(LOCAL_BUILD_DIR)))
693
694
695 dep-local:
696
697 install-local:
698
699 include-pass-local:
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 ; )
703
704
705 .PHONY: clean-custom
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)/%)
712
713 check-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))
718
719 # Create directories only for the first time
720 ifndef CHECK_DIR_DONE
721 check-dir = check-dir
722 CHECK_DIR_DONE = y
723 endif
724 export CHECK_DIR_DONE
725
726 # Which passes to pass
727 default: $(check-dir) include-pass library-pass binary-pass utils-pass
728
729 ETAGS = etags -R -f $(MAKERULES_DIR)/TAGS
730 .PHONY: TAGS
731 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
737
738 # Local Variables:
739 # mode:makefile
740 # End:
741                                                              #OMK@localeval
742 ifneq ($(local_EVALUATE),)
743 #$(warning $(local_EVALUATE))
744 $(eval $(local_EVALUATE))
745 endif
746
747 # Local Variables:
748 # mode:makefile
749 # End: