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