1 # Version for system-less builds.
4 # bin_PROGRAMS .. list of the require binary programs
5 # test_PROGRAMS .. list of the test programs
6 # include_HEADERS .. list of the user-space public header files
7 # lib_LIBRARIES .. list of the user-space libraries
8 # lib_LDSCRIPTS .. list of LD scripts that should be copied to the lib direcotry
9 # lib_obj_SOURCES .. list of source files which should be compiled and
10 # the produced object file placed to the lib directory (e.g. crt0.S)
11 # shared_LIBRARIES .. list of the user-space shared libraries
12 # nobase_include_HEADERS .. public headers copied even with directory part
13 # renamed_include_HEADERS .. public headers copied to the different target name (xxx.h->yyy.h)
14 # utils_PROGRAMS .. list of the development utility programs (compiled for host computer, this might change in future)
15 # xxx_SOURCES .. list of specific target sources
16 # xxx_LIBS .. list of specific target libraries
17 # INCLUDES .. additional include directories and defines for user-space
18 # lib_LOADLIBES .. list of libraries linked to each executable
19 # link_VARIANTS .. list of ld script suffixes (after hypen `-') that
20 # should be used for linking (e.g. ram flash). If this is not
21 # specified, then the value of DEFAULT_LD_SCRIPT_VARIANT from config.target is used.
22 # PREFIX_DIR .. Prefix to directories in _compiled and _build. Used in config.omk.
24 BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
25 COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
27 LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
28 #$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
30 #=====================================================================
31 # Common utility rules
33 link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
36 #=====================================================================
37 # Include correct rules for just running pass
39 USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
41 USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
42 USER_LIB_DIR = $(USER_COMPILED_DIR_NAME)/lib
43 USER_UTILS_DIR = $(USER_COMPILED_DIR_NAME)/bin-utils
44 USER_TESTS_DIR = $(USER_COMPILED_DIR_NAME)/bin-tests
45 USER_BIN_DIR = $(USER_COMPILED_DIR_NAME)/bin
46 USER_OBJS_DIR = $(LOCAL_BUILD_DIR)
50 # Some support to serialize some targets for parallel make
51 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
52 include-pass: check-dir
53 library-pass: include-pass
54 binary-pass utils-pass: library-pass
56 override OMK_SERIALIZE_INCLUDED = y
57 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
60 # -------------------------------------
61 # Rules for compilation for target
62 ifdef TARGET_RULE_TEMPLATES
64 LDFLAGS += -nostartfiles
66 # FIXME: These are not used. What they are good for?
67 LIB_CPPFLAGS += $(CPPFLAGS)
68 LIB_CFLAGS += $(CFLAGS)
70 SOLIB_PICFLAGS += -shared -fpic
72 CFLAGS += -DOMK_FOR_TARGET
74 INCLUDE_DIR := $(USER_INCLUDE_DIR)
75 LIB_DIR := $(USER_LIB_DIR)
76 OBJS_DIR := $(USER_OBJS_DIR)
78 $(eval $(COMPILER_DEFS_template))
80 # Special rules for CMETRIC generated headers
82 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\
83 $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\
84 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),)))
85 $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
86 $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\
87 $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh)))))
89 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
91 # Generate rules for compilation of programs and libraries
92 ifneq ($(link_VARIANTS),)
93 $(foreach prog,$(bin_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
94 $(foreach prog,$(test_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
96 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR))))
97 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR))))
100 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
101 $(foreach src,$(lib_obj_SOURCES),$(eval $(call LIBOBJ_template,$(addsuffix $(OBJ_EXT),$(basename $(src))))))
102 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
105 # lib_obj_SOURCES handling
106 lib_OBJS = $(addsuffix $(OBJ_EXT),$(basename $(lib_obj_SOURCES)))
107 #$(warning lib_OBJS = $(lib_OBJS))
108 SOURCES += $(filter-out %$(OBJ_EXT),$(lib_obj_SOURCES))
110 $(LIB_DIR)/%$(OBJ_EXT): %$(OBJ_EXT)
111 @echo " CP $(^:$(MAKERULES_DIR)/%=%) -> $(@:$(MAKERULES_DIR)/%=%)"
112 $(Q)cp $(CP_FLAGS) $< $@
115 # User-space static libraries and applications object files
116 SOURCES := $(sort $(SOURCES))
117 #$(warning SOURCES = $(SOURCES))
118 GEN_SOURCES := $(sort $(GEN_SOURCES))
120 # User-space shared libraries object files
121 SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
122 #$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
123 SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES))
125 # Create _build directories for sources in subdirectories i.e. *_SOURCES=dir/file.c
126 _dirs_to_create=$(filter-out ./,$(sort $(dir $(SOURCES) $(SOLIB_SOURCES))))
127 ifneq ($(_dirs_to_create),)
128 $(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create)))
131 # The above generated rules produced $(SOURCES) and $(SOLIB_SOURCES)
132 # variables. Now generate rules for compilation of theese sources
133 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%$(OBJ_EXT)),)))
134 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
135 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
136 $(foreach src,$(filter %$(ASM_EXT),$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%$(OBJ_EXT)),)))
137 $(foreach src,$(filter %.c,$(GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),)))
139 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
140 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
141 $(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS))))
142 $(foreach src,$(filter %$(ASM_EXT),$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%.lo),$(SOLIB_PICFLAGS))))
143 $(foreach src,$(filter %.c,$(SOLIB_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
145 library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \
146 $(lib_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%$(LIB_EXT)) $(shared_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%.so) \
147 $(addprefix $(LIB_DIR)/,$(lib_OBJS))
149 ifneq ($(link_VARIANTS),)
150 binary-pass-local: $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link)) \
151 $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link).$(of))))
153 binary-pass-local: $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%) \
154 $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%.$(of)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%.$(of)))
157 endif # TARGET_RULE_TEMPLATES
160 # -------------------------------------
161 # Rules for compilation utilities for host (user space)
162 ifdef HOST_RULE_TEMPLATES
167 SOLIB_PICFLAGS += -shared -fpic
169 # For host compilation, we don't use a specfic ld script
172 # TODO: It is probably better to use different directories for host
173 # includes, libraries and objects
174 INCLUDE_DIR := $(USER_INCLUDE_DIR)
175 LIB_DIR := $(USER_LIB_DIR)
176 OBJS_DIR := $(USER_OBJS_DIR)
178 $(eval $(COMPILER_DEFS_template))
180 #User-space static libraries and applications object files
181 #SOURCES := $(sort $(SOURCES))
182 #$(warning SOURCES = $(SOURCES))
184 # Generate rules for compilation of utility programs
185 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),)))
187 # The above generated rule produced $(SOURCES) variable. Now generate
188 # rules for compilation of theese sources
189 $(foreach src,$(filter %.c,$(SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%$(OBJ_EXT)),)))
190 $(foreach src,$(filter %.cc,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
191 $(foreach src,$(filter %.cxx,$(SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
192 $(foreach src,$(filter %$(ASM_EXT),$(SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%$(ASM_EXT)=%$(OBJ_EXT)),)))
194 utils-pass-local: $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%)
196 endif # HOST_RULE_TEMPLATES
198 #=====================================================================
199 # Automatic loading of compiled program by issuing "make load"
201 ifneq ($(OUTPUT_FORMATS),)
202 # Select a file extension (e.g. .bin) for "make load" command to load.
203 LOAD_EXTENSION = .$(firstword $(OUTPUT_FORMATS))
206 # Syntax: $(call LOAD_PROGRAM_template,<executable-name>,<dir>,<link-variant>)
207 # Used to load program to the target hardware
208 define LOAD_PROGRAM_template
209 .PHONY: load-$(1)$(3:%=-%)
210 load-$(1)$(3:%=-%): $(2)/$(1)$(3:%=-%)$(if $(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION-$(3)),$(LOAD_EXTENSION))
211 @$(QUIET_CMD_ECHO) " LOAD $$<"
212 @if [ -z '$$(LOAD_CMD$(3:%=-%))' ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
213 $(Q) $$(LOAD_CMD$(3:%=-%)) $$<
216 # Syntax: $(call LOAD__RUN_VARIANT_template,<link-variant>)
217 # Used to load and/or run non-default variant of the default program
218 define LOAD_RUN_VARIANT_template
219 .PHONY: load-$(1) run-$(1)
221 load-$(1): load-$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(1)
224 @$(QUIET_CMD_ECHO) " RUN $(1)"
225 @if [ -z "$(RUN_CMD-$(1))" ]; then echo "No command for running '$(1)' variant is specified."; exit 1; fi
230 $(foreach link,$(link_VARIANTS),$(foreach prog,$(bin_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
231 $(foreach link,$(link_VARIANTS),$(foreach prog,$(test_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(link)))))
232 $(foreach link,$(link_VARIANTS),$(eval $(call LOAD_RUN_VARIANT_template,$(link))))
235 load: $(addprefix load-,$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(firstword $(link_VARIANTS)))
237 run: run-$(firstword $(link_VARIANTS))
241 #=====================================================================
242 # Generate pass rules from generic templates
244 $(eval $(call omk_pass_template, include-pass, $(LOCAL_BUILD_DIR),,$(include_HEADERS)$(nobase_include_HEADERS)$(renamed_include_HEADERS)$(lib_LDSCRIPTS)$(config_include_HEADERS)$(LOCAL_CONFIG_H)))
245 $(eval $(call omk_pass_template, library-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(lib_obj_SOURCES)))
246 $(eval $(call omk_pass_template, binary-pass, $(LOCAL_BUILD_DIR),TARGET_RULE_TEMPLATES=y,$(bin_PROGRAMS) $(test_PROGRAMS)))
247 $(eval $(call omk_pass_template, utils-pass, $(LOCAL_BUILD_DIR),HOST_RULE_TEMPLATES=y,$(utils_PROGRAMS)))
248 $(eval $(call omk_pass_template, dep, $(LOCAL_BUILD_DIR),,always))
249 $(eval $(call omk_pass_template, clean, $(LOCAL_BUILD_DIR),,always))
250 $(eval $(call omk_pass_template, install,$(LOCAL_BUILD_DIR),,always))
257 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
260 @$(foreach f, $(lib_LDSCRIPTS), cmp --quiet $(SOURCES_DIR)/$(f) $(USER_LIB_DIR)/$(notdir $(f)) \
261 || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(USER_LIB_DIR)/$(notdir $(f))) || exit 1 ; )
265 clean-local: clean-custom
266 $(Q)rm -f $(USER_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.lo \
267 $(USER_OBJS_DIR)/*.d \
268 $(USER_OBJS_DIR)/*.map \
269 $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
272 @$(call mkdir_def,$(USER_INCLUDE_DIR))
273 @$(call mkdir_def,$(USER_LIB_DIR))
274 @$(call mkdir_def,$(USER_BIN_DIR))
275 @$(call mkdir_def,$(USER_UTILS_DIR))
276 @$(call mkdir_def,$(USER_TESTS_DIR))
278 include-pass-submakes: extra-rules-subdirs
279 # Which passes to pass
280 default: include-pass library-pass binary-pass utils-pass