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