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