]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/sdcc.omk
Apply allow sources in subdirectories to rtems and sdcc rules.
[omk.git] / snippets / sdcc.omk
1 # bin_PROGRAMS     .. list of the require binary programs
2 # test_PROGRAMS    .. list of the test programs
3 # include_HEADERS  .. list of the user-space public header files
4 # lib_LIBRARIES    .. list of the user-space libraries
5 # lib_LDSCRIPTS    .. list of LD scripts that should be copied to the lib direcotry
6 # lib_obj_SOURCES  .. list of source files which should be compiled and
7 #                     the produced object file placed to the lib directory (e.g. crt0.S)
8 # shared_LIBRARIES .. list of the user-space shared libraries
9 # nobase_include_HEADERS .. public headers copied even with directory part
10 # renamed_include_HEADERS .. public headers copied to the different target name (xxx.h->yyy.h)
11 # asm_build_HEADERS .. list of files copied to the build object directory
12 # utils_PROGRAMS   .. list of the development utility programs (compiled for host computer, this might change in future)
13 # xxx_SOURCES      .. list of specific target sources
14 # xxx_LIBS         .. list of specific target libraries
15 # INCLUDES         .. additional include directories and defines for user-space
16 # lib_LOADLIBES    .. list of libraries linked to each executable
17 # link_VARIANTS    .. list of ld script suffixes (after hypen `-') that
18 #                     should be used for linking (e.g. ram flash). If this is not
19 #                     specified, then the value of DEFAULT_LD_SCRIPT_VARIANT from config.target is used.
20 # PREFIX_DIR       .. Prefix to  directories in _compiled and _build. Used in config.omk.
21
22 BUILD_DIR_NAME = _build
23 COMPILED_DIR_NAME = _compiled
24 GROUP_DIR_NAME =
25
26 BUILD_DIR_NAME = _build$(addprefix /,$(PREFIX_DIR))
27 COMPILED_DIR_NAME = _compiled$(addprefix /,$(PREFIX_DIR))
28
29 LOCAL_BUILD_DIR=$(MAKERULES_DIR)/$(BUILD_DIR_NAME)/$(RELATIVE_DIR)
30 #$(warning LOCAL_BUILD_DIR = $(LOCAL_BUILD_DIR))
31
32 #=====================================================================
33 # Common utility rules
34
35 link_VARIANTS ?= $(DEFAULT_LD_SCRIPT_VARIANT)
36
37 #=====================================================================
38 # Include correct rules for just running pass
39
40 USER_COMPILED_DIR_NAME=$(MAKERULES_DIR)/$(COMPILED_DIR_NAME)
41
42 USER_INCLUDE_DIR = $(USER_COMPILED_DIR_NAME)/include
43 USER_LIB_DIR     = $(USER_COMPILED_DIR_NAME)/lib
44 USER_UTILS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-utils
45 USER_TESTS_DIR   = $(USER_COMPILED_DIR_NAME)/bin-tests
46 USER_BIN_DIR     = $(USER_COMPILED_DIR_NAME)/bin
47 USER_OBJS_DIR    = $(LOCAL_BUILD_DIR)
48
49 .PHONY: check-dir
50
51 # Some support to serialize some targets for parallel make
52 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
53 include-pass: check-dir
54 library-pass: include-pass
55 binary-pass utils-pass: library-pass
56
57 override OMK_SERIALIZE_INCLUDED = y
58 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
59 endif
60
61 CPPFLAGS  += -I $(USER_INCLUDE_DIR)
62
63 CPPFLAGS  += $(CONFIG_OMK_DEFINES)
64
65 CFLAGS  += $(TARGET_ARCH) 
66 LDFLAGS += $(TARGET_ARCH) 
67
68 LOADLIBES += -L$(USER_LIB_DIR) 
69
70 LOADLIBES += $(lib_LOADLIBES:%=-l%)
71
72 INCLUDE_DIR := $(USER_INCLUDE_DIR)
73 LIB_DIR     := $(USER_LIB_DIR)
74 OBJS_DIR    := $(USER_OBJS_DIR)
75
76 #=====================================================================
77 # User-space rules and templates to compile programs, libraries etc.
78
79 ifdef USER_RULE_TEMPLATES
80
81 USER_SOURCES2OBJS = $(OBJ_EXT)/.c $(OBJ_EXT)/.cc $(OBJ_EXT)/.cxx $(OBJ_EXT)/.S $(OBJ_EXT)/.asm $(OBJ_EXT)/.o
82
83 #%.o: %.c
84 #       $(CC) -o $@ $(LCFLAGS) -c $<
85
86 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(OMK_CPPFLAGS) \
87         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(INCLUDES) -DOMK_FOR_USER
88
89 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(OMK_CPPFLAGS) \
90         $(CPPFLAGS) $(OMK_CXXFLAGS) $(CFLAGS) $(INCLUDES) -DOMK_FOR_USER
91
92 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
93         $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) $(ASFLAGS)
94
95 # Check C compiler version for user build
96 ifndef CC_MAJOR_VERSION
97 CC_MAJOR_VERSION := sdcc
98 endif
99
100
101 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
102 define COMPILE_c_o_template
103 $(2): $(1) $$(GEN_HEADERS)
104         @$(QUIET_CMD_ECHO) "  CC      $$@"
105         $(Q) if $$(c_o_COMPILE) $(3) -o "$$@" -c $$< ; \
106         then $$(c_o_COMPILE) $(3) -M $$< > "$$@.d" ; \
107         else exit 1; \
108         fi
109 endef
110
111
112 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
113 define COMPILE_cc_o_template
114 $(2): $(1) $(LOCAL_CONFIG_H)
115         @$(QUIET_CMD_ECHO) "  CXX     $$@"
116         $(Q) echo "C++ compilation not suported for this compiler"
117 endef
118
119
120 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
121 define COMPILE_S_o_template
122 $(2): $(1) $$(GEN_HEADERS)
123         @$(QUIET_CMD_ECHO) "  CC      $$@"
124         $(Q) if $$(S_o_COMPILE) $(3) -o "$$@" -c $$< ; \
125         then $$(c_o_COMPILE) $(3) -M $$< > "$$@.d" ; \
126         else exit 1; \
127         fi
128 endef
129
130 # Syntax: $(call COMPILE_asm_o_template,<source>,<target>,<additional c-flags>)
131 define COMPILE_asm_o_template
132 $(2): $(1) $(LOCAL_CONFIG_H)
133         @$(QUIET_CMD_ECHO) "  ASM      $$@"
134         $(Q) $$(A51TOASX) $(1) $(2:%$(OBJ_EXT)=%.s)
135         $(Q) $$(AS) -l -o $(2:%$(OBJ_EXT)=%.s)
136 endef
137
138
139 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<executable-suffix>,<linker-sript>)
140 define PROGRAM_template
141
142 $(foreach x, $(USER_SOURCES2OBJS),
143 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
144                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
145 )
146 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
147
148 USER_OBJS  += $$($(1)_OBJS)
149 USER_SOURCES += $$($(1)_SOURCES)
150
151 -include $(USER_LIB_DIR)/$(LD_SCRIPT).ld$$(addprefix -,$(4))
152
153 $(2)/$(1)$(addprefix -,$(4))$(3): $(USER_LIB_DIR)/$(LD_SCRIPT).ld$$(addprefix -,$(4))
154
155 $(2)/$(1)$(addprefix -,$(4))$(3): $(USER_LIB_DIR)/timestamp
156
157 $(2)/$(1)$(addprefix -,$(4))$(3): $$($(1)_OBJS)
158         @$(QUIET_CMD_ECHO) "  LINK    $$@"
159         $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
160           then echo $$(CC)  $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CFLAGS) $$(CFLAGS) ; \
161           else echo $$(CXX) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CXXFLAGS) $$(CXXFLAGS) ; fi) \
162           $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
163           -o $(2)/$(1)$(addprefix -,$(4))$(3)
164         $(HEX2BIN) $(2)/$(1)$(addprefix -,$(4))$(3)
165 endef
166
167 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
168 %.bin: %$(EXE_SUFFIX)
169         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
170         $(Q) $(OBJCOPY) --input-target=ihex --output-target=binary $< $@
171
172 %.hex: %$(EXE_SUFFIX)
173         @$(QUIET_CMD_ECHO) "  GENIHEX $@"
174         $(Q) cd $(dirname $<) && $(HC) $< >$@
175
176 # Syntax: $(call LIBRARY_template,<library-name>)
177 define LIBRARY_template
178 $(foreach x, $(USER_SOURCES2OBJS),
179 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
180                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
181 )
182 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
183
184 USER_OBJS  += $$($(1)_OBJS)
185 USER_SOURCES += $$($(1)_SOURCES)
186
187 $(USER_LIB_DIR)/$(LIB_PREF)$(1)$(LIB_EXT): $$($(1)_OBJS)
188         @$(QUIET_CMD_ECHO) "  AR      $$@"
189         $(Q) for i in $$^ ; do $(AR) $(ARFLAGS) $$@ $$$$i ; done
190         @touch $(USER_LIB_DIR)/timestamp
191 endef
192
193 # -------------------------------------
194 # Rules for compilation for target
195
196 # Generate rules for compilation of programs and libraries
197 ifneq ($(link_VARIANTS),)
198 $(foreach prog,$(bin_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX),$(link)))))
199 $(foreach prog,$(utils_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX),$(link)))))
200 $(foreach prog,$(test_PROGRAMS),$(foreach link,$(link_VARIANTS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX),$(link)))))
201 else
202 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX))))
203 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
204 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX))))
205 endif
206
207 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
208 $(foreach src,$(lib_obj_SOURCES),$(eval $(call LIBOBJ_template,$(addsuffix $(OBJ_EXT),$(basename $(src))))))
209
210 # lib_obj_SOURCES handling
211 lib_OBJS = $(addsuffix $(OBJ_EXT),$(basename $(lib_obj_SOURCES)))
212 #$(warning lib_OBJS = $(lib_OBJS))
213 SOURCES += $(filter-out %$(OBJ_EXT),$(lib_obj_SOURCES))
214
215 $(LIB_DIR)/%$(OBJ_EXT): %$(OBJ_EXT)
216         @echo "  CP      $(^:$(MAKERULES_DIR)/%=%) -> $(@:$(MAKERULES_DIR)/%=%)"
217         $(Q)cp $(CP_FLAGS) $< $@
218
219
220 # User-space static libraries and applications object files
221 USER_SOURCES := $(sort $(USER_SOURCES))
222 #$(warning USER_SOURCES = $(USER_SOURCES))
223
224 # Create _build directories for sources in subdirectories i.e. *_SOURCES=dir/file.c
225 _dirs_to_create=$(filter-out ./,$(sort $(dir $(USER_SOURCES))))
226 ifneq ($(_dirs_to_create),)
227 $(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create)))
228 endif
229
230 # The above generated rules produced $(USER_SOURCES) and $(SOLIB_SOURCES)
231 # variables. Now generate rules for compilation of theese USER_SOURCES
232 $(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%$(OBJ_EXT)),)))
233 $(foreach src,$(filter %.cc,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%$(OBJ_EXT)),)))
234 $(foreach src,$(filter %.cxx,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%$(OBJ_EXT)),)))
235 $(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%$(OBJ_EXT)),)))
236 $(foreach src,$(filter %.asm,$(USER_SOURCES)), $(eval $(call COMPILE_asm_o_template,$(SOURCES_DIR)/$(src),$(src:%.asm=%$(OBJ_EXT)),)))
237
238
239 library-pass-local: $(lib_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%$(LIB_EXT)) $(shared_LIBRARIES:%=$(LIB_DIR)/$(LIB_PREF)%.so) \
240                     $(addprefix $(LIB_DIR)/,$(lib_OBJS))
241
242 ifneq ($(link_VARIANTS),)
243 binary-pass-local:  $(foreach link,$(link_VARIANTS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link)$(EXE_SUFFIX)) \
244                     $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%-$(link).$(of))))
245 binary-pass-local:  $(foreach link,$(link_VARIANTS),$(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link)$(EXE_SUFFIX)) \
246                     $(foreach of,$(OUTPUT_FORMATS),$(test_PROGRAMS:%=$(USER_TESTS_DIR)/%-$(link).$(of))))
247 else
248 binary-pass-local:  $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%$(EXE_SUFFIX)) \
249                     $(foreach of,$(OUTPUT_FORMATS),$(bin_PROGRAMS:%=$(USER_BIN_DIR)/%.$(of)))
250 binary-pass-local:  $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%$(EXE_SUFFIX)) \
251                     $(foreach of,$(OUTPUT_FORMATS),$(test_PROGRAMS:%=$(USER_TESTS_DIR)/%.$(of)))
252 endif
253
254
255 library-pass-local: $(lib_LIBRARIES:%=$(USER_LIB_DIR)/$(LIB_PREF)%$(LIB_EXT)) $(shared_LIBRARIES:%=$(USER_LIB_DIR)/$(LIB_PREF)%.so)
256
257 utils-pass-local: $(foreach link,$(link_VARIANTS),$(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%-$(link)) $(foreach of,$(OUTPUT_FORMATS),$(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%-$(link).$(of))))
258
259 -include $(USER_OBJS_DIR)/*.d
260
261 endif
262
263 #=====================================================================
264 # Automatic loading of compiled program by issuing "make load"
265
266 ifneq ($(OUTPUT_FORMATS),)
267 # Select a file extension (e.g. .bin) for "make load" command to load.
268 LOAD_EXTENSION = .$(firstword $(OUTPUT_FORMATS))
269 endif
270
271 # Syntax: $(call LOAD_PROGRAM_template,<executable-name>,<dir>,<link-variant>)
272 # Used to load program to the target hardware
273 define LOAD_PROGRAM_template
274 .PHONY: load-$(1)$(addprefix -,$(3))
275 -include $(USER_LIB_DIR)/$(LD_SCRIPT).ld$$(addprefix -,$(3))
276 load-$(1)$(addprefix -,$(3)): $(2)/$(1)$(addprefix -,$(3))$(LOAD_EXTENSION)
277         @$(QUIET_CMD_ECHO) "  LOAD    $$<"
278         @if [ -z "$$(LOAD_CMD$(addprefix -,$(3)))" ]; then echo "No command for loading applications to '$(3)' is specified."; exit 1; fi
279         $(Q) $$(LOAD_CMD$(addprefix -,$(3))) $$<
280 endef
281
282 # Syntax: $(call LOAD__RUN_VARIANT_template,<link-variant>)
283 # Used to load and/or run non-default variant of the default program
284 define LOAD_RUN_VARIANT_template
285 .PHONY: load-$(1) run-$(1)
286 -include $(USER_LIB_DIR)/$(LD_SCRIPT).ld$$(addprefix -,$(3))
287 load-$(1): load-$(firstword $(bin_PROGRAMS))-$(1)
288 run-$(1):
289         @$(QUIET_CMD_ECHO) "  RUN     $(1)"
290         @if [ -z "$(RUN_CMD-$(1))" ]; then echo "No command for running '$(1)' variant is specified."; exit 1; fi
291         $(Q) $(RUN_CMD-$(1))
292 endef
293
294 $(foreach link,$(link_VARIANTS),$(foreach prog,$(bin_PROGRAMS) $(test_PROGRAMS),$(eval $(call LOAD_PROGRAM_template,$(prog),$(USER_BIN_DIR),$(link)))))
295 $(foreach link,$(link_VARIANTS),$(eval $(call LOAD_RUN_VARIANT_template,$(link))))
296
297 .PHONY: load run
298 load: $(addprefix load-,$(firstword $(bin_PROGRAMS) $(test_PROGRAMS))-$(firstword $(link_VARIANTS)))
299
300 run: run-$(firstword $(link_VARIANTS))
301
302 #=====================================================================
303 # Generate pass rules from generic templates
304
305 $(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)))
306 $(eval $(call omk_pass_template, library-pass, $(LOCAL_BUILD_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(lib_obj_SOURCES)))
307 $(eval $(call omk_pass_template, binary-pass,  $(LOCAL_BUILD_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(test_PROGRAMS)))
308 $(eval $(call omk_pass_template, utils-pass,   $(LOCAL_BUILD_DIR),USER_RULE_TEMPLATES=y,$(utils_PROGRAMS)))
309 $(eval $(call omk_pass_template, dep,    $(LOCAL_BUILD_DIR),,always))
310 $(eval $(call omk_pass_template, clean,  $(LOCAL_BUILD_DIR),,always))
311 $(eval $(call omk_pass_template, install,$(LOCAL_BUILD_DIR),,always))
312
313
314 dep-local:
315
316 install-local:
317
318 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
319
320 include-pass-local:
321         @$(foreach f, $(lib_LDSCRIPTS), cmp --quiet $(SOURCES_DIR)/$(f) $(USER_LIB_DIR)/$(notdir $(f)) \
322            || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(USER_LIB_DIR)/$(notdir $(f))) || exit 1 ; )
323         @$(foreach f, $(asm_build_HEADERS), \
324            srcfname=`echo '$(f)' | $(SED4OMK) -e 's/^\(.*\)->.*$$/\1/'` ; destfname=`echo '$(f)' | $(SED4OMK) -e 's/^.*->\(.*\)$$/\1/'` ; \
325            cmp --quiet $(SOURCES_DIR)/$${srcfname} $(USER_OBJS_DIR)/$${destfname} \
326            || ( mkdir -p `dirname $(USER_OBJS_DIR)/$${destfname}` && \
327            $(A51TOASX) $(SOURCES_DIR)/$${srcfname} $(USER_OBJS_DIR)/$${destfname} ) || exit 1 ; )
328
329
330 clean-local::
331         @echo Cleaning in $(USER_OBJS_DIR)
332         $(Q)rm -f $(USER_OBJS_DIR)/*$(OBJ_EXT) $(USER_OBJS_DIR)/*.lo \
333                $(USER_OBJS_DIR)/*.d \
334                $(USER_OBJS_DIR)/*.map \
335                $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%) \
336                $(tar_EMBEDFILES:%=$(USER_OBJS_DIR)/%_tarfile)
337         $(Q)$(foreach confh,$(addprefix $(USER_INCLUDE_DIR)/,$(config_include_HEADERS)),\
338             if [ -e $(confh) ] ; then touch -t 200001010101 $(confh) ; fi ; \
339         )
340
341 check-dir::
342         @$(call mkdir_def,$(USER_OBJS_DIR))
343         @$(call mkdir_def,$(USER_INCLUDE_DIR))
344         @$(call mkdir_def,$(USER_LIB_DIR))
345         @$(call mkdir_def,$(USER_BIN_DIR))
346         @$(call mkdir_def,$(USER_UTILS_DIR))
347         @$(call mkdir_def,$(USER_TESTS_DIR))
348
349 include-pass-submakes: extra-rules-subdirs
350 # Which passes to pass
351 default: include-pass library-pass binary-pass utils-pass