]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/nuttx-compile.omk
NuttX: allow to specify xxx_PROGBUILTIN_EXCLUDE list of user and or NuttX programs...
[omk.git] / snippets / nuttx-compile.omk
1
2 # Rules for NuttX binaries compilation of C, C++ and assembler
3 # sources using GNU toolchain.
4
5 # Interface to other rules:
6 # kernel_HEADERS   .. list of the kernel-space public header files
7 # kernel_MODULES   .. list of the kernel side modules/applications
8
9 # Input variables:
10 # NUTTX_EXPORT - export directory of NuttX build
11 #                it should contain subdirectories:
12 #                arch  build  include  libs  registry  startup)
13 # LIB_DIR - directory where compiled libraries are stored
14 # OBJS_DIR - directory where intermediate files (.o, .map, ...) are stored
15 # INCLUDE_DIR - where includes can be found
16 # from config.omk or Makefile.omk
17 # CROSS_COMPILE - target architecture tools prefix
18 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
19 # from base: SOURCES_DIR
20 # from Makefile.omk: lib_LOADLIBES
21 # xxx_PROGBUILTIN - list of builtin programs linked into final system image
22 #                   when "all" is used then all programs provided by build are linked
23 # xxx_PROGBUILTIN_EXCLUDE - list of builtin user or NuttX provided programs which are excluded
24 # xxx_KMODBUILTIN - list of builtin programs linked into final system image
25
26 # Output variables:
27 # SOURCES - all the source files that needs to be compiled (except for shared library sources)
28 # SOLIB_SOURCES - all the source files that needs to be compiled for a shared library
29 # OBJ_EXT - extension of object files
30 # LIB_EXT - extension of library files
31 # LIB_PREF - prefix for library files
32 # ASM_EXT - extension of assembler sources
33
34 # Templates:
35 # COMPILER_DEFS_template - definitions that should be defined before
36 # the following templates can be used. The input variables needs to be
37 # defined before evaluating this template
38
39 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
40 # templates that create rules for compilation of sources
41
42 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
43 # create rules for compilation of a program, library and shared
44 # library. The rules can use rules produced by COMPILE_xxx_template.
45
46 define COMPILER_DEFS_template
47 OBJ_EXT = .o
48 LIB_EXT = .a
49 LIB_PREF = lib
50 ASM_EXT = .S
51
52 CC = $(CROSS_COMPILE)gcc
53 CXX = $(CROSS_COMPILE)g++
54 LINK = $(CROSS_COMPILE)ld
55 AR = $(CROSS_COMPILE)ar
56 OBJCOPY = $(CROSS_COMPILE)objcopy
57 NM = $(CROSS_COMPILE)nm
58
59 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
60 CFLAGS  += -Wall
61 CFLAGS += -I$(SOURCES_DIR)
62 CFLAGS += -I$(INCLUDE_DIR)
63
64 ELF_FILE_LDSCRIPT?=$(wildcard $(NUTTX_EXPORT)/build/gnu-elf.ld)
65
66 LOADLIBES += -L$(LIB_DIR)
67 LOADLIBES += $(lib_LOADLIBES:%=-l%)
68
69
70 -include $(OBJS_DIR)/*.d
71
72 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.s .o/.o
73
74 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.s .lo/.lo
75
76 #%.lo: %.c
77 #       $(CC) -o $@ $(LCFLAGS) -c $<
78
79 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
80         $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
81
82 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
83         $(CPPFLAGS) $(CXXFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
84
85 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
86         $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) $(ASFLAGS)
87
88
89 # Check GCC version for user build
90 ifndef CC_MAJOR_VERSION
91 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
92 endif
93 # Prepare suitable define for dependency building
94 ifeq ($$(CC_MAJOR_VERSION),2)
95 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
96 else
97 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
98 endif
99
100 endef # COMPILER_DEFS_template
101
102
103 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
104 define COMPILE_c_o_template
105 $(2): $(1) $$(GEN_HEADERS)
106         @$(QUIET_CMD_ECHO) "  CC      $$@"
107         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
108         then mv -f "$$@.d.tmp" "$$@.d" ; \
109         else rm -f "$$@.d.tmp" ; exit 1; \
110         fi
111 endef
112
113
114 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
115 define COMPILE_cc_o_template
116 $(2): $(1) $$(GEN_HEADERS)
117         @$(QUIET_CMD_ECHO) "  CXX     $$@"
118         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
119         then mv -f "$$@.d.tmp" "$$@.d" ; \
120         else rm -f "$$@.d.tmp" ; exit 1; \
121         fi
122 endef
123
124 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
125 define COMPILE_S_o_template
126 $(2): $(1) $$(GEN_HEADERS)
127         @$(QUIET_CMD_ECHO) "  AS      $$@"
128         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
129         then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
130         else rm -f "$$@.d.tmp" ; exit 1; \
131         fi
132 endef
133
134 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
135 define PROGRAM_template
136
137 GEN_SOURCES += $$($(1)_GEN_SOURCES)
138
139 $(foreach x, $(SOURCES2OBJS),
140 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
141                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
142 )
143 $(1)_OBJS += $$($(1)_EMBEDROMFS:%=%_img.o)
144 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
145
146 SOURCES += $$($(1)_SOURCES)
147 EMBEDROMFS += $$($(1)_EMBEDROMFS)
148
149 ifneq ($$($(1)_PROGBUILTIN),)
150 ifneq ($$($(1)_PROGBUILTIN),all)
151 $(1)_PROGBUILTIN_TMP = $$($(1)_PROGBUILTIN)
152 else
153 $(1)_PROGBUILTIN_TMP1 = $$(wildcard $$(USER_REGISTRY_DIR)/*.pbi)
154 $(1)_PROGBUILTIN_TMP = $$($(1)_PROGBUILTIN_TMP1:$$(USER_REGISTRY_DIR)/%.pbi=%)
155 endif
156 $(1)_PROGBUILTIN_LIST = $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE),$$($(1)_PROGBUILTIN_TMP))
157
158 $(1)_PROGBUILTIN_OTHER=$$(filter-out $(1),$$($(1)_PROGBUILTIN_LIST))
159 $(1)_PROGBUILTIN_PBI=$$($(1)_PROGBUILTIN_OTHER:%=$$(USER_REGISTRY_DIR)/%.pbi)
160 $(1)_PROGBUILTIN_LDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.ldat)
161 $(1)_PROGBUILTIN_PDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.pdat) \
162         $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE:%=$(NUTTXREGISTRY)/%.pdat),\
163         $$(wildcard $(NUTTXREGISTRY)/*.pdat))
164 $(1)_PROGBUILTIN_BDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.bdat) \
165         $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE:%=$(NUTTXREGISTRY)/%.bdat),\
166         $$(wildcard $(NUTTXREGISTRY)/*.bdat))
167
168 ifneq ($$($(1)_KMODBUILTIN),all)
169 $(1)_KMODBUILTIN_LIBS = $$($(1)_KMODBUILTIN:%=$$(KERN_MODULES_DIR)/lib%.ka)
170 else
171 $(1)_KMODBUILTIN_LIBS = $$(wildcard $$(KERN_MODULES_DIR)/lib*.ka)
172 endif
173
174
175 ifneq ($$($(1)_PROGBUILTIN_PDAT),)
176 $(1)_BUILTINTABLE = $(OBJS_DIR)/$(1)-builtintab.c
177
178 $$($(1)_BUILTINTABLE): $$($(1)_PROGBUILTIN_PDAT) $$($(1)_PROGBUILTIN_BDAT)
179         $(Q) cat $$($(1)_PROGBUILTIN_PDAT) >$$@
180         $(Q) echo "#include <nuttx/config.h>" >>$$@
181         $(Q) echo "#include <nuttx/lib/builtin.h>" >>$$@
182         $(Q) echo "const struct builtin_s g_builtins[] = {" >>$$@
183         $(Q) cat $$($(1)_PROGBUILTIN_BDAT) >>$$@
184         $(Q) echo "{ NULL, 0, 0, 0 }};" >>$$@
185         $(Q) echo "const int g_builtin_count = sizeof(g_builtins) / sizeof(g_builtins[0]);" >>$$@
186
187 GEN_SOURCES += $$($(1)_BUILTINTABLE)
188
189 $(1)_BUILTINWRAPMAIN = $(OBJS_DIR)/$(1)-builtinwrapmain.c
190
191 $$($(1)_BUILTINWRAPMAIN):
192         $(Q) echo "#include <nuttx/config.h>" >$$@
193         $(Q) echo "#include <nuttx/compiler.h>" >>$$@
194         $(Q) echo "int main(int argc, FAR char *argv[]);" >>$$@
195         $(Q) echo "int weak_function $(1)_main(int argc, FAR char *argv[]) {return main(argc, argv);}" >>$$@
196
197 GEN_SOURCES += $$($(1)_BUILTINWRAPMAIN)
198
199 endif
200 endif
201
202 ifneq ($$(LD_SCRIPT$(3:%=-%)),)
203 $(1)$(3:%=-%)_LD_SCRIPT_FN = $$(LD_SCRIPT$(3:%=-%))
204 else
205 $(1)$(3:%=-%)_LD_SCRIPT_FN = $$(LD_SCRIPT)
206 endif
207
208 ifeq ($$(dir $$($(1)$(3:%=-%)_LD_SCRIPT_FN)),)
209 $(1)$(3:%=-%)_LD_SCRIPT = $$(LIB_DIR)/$$($(1)$(3:%=-%)_LD_SCRIPT_FN)
210 else
211 $(1)$(3:%=-%)_LD_SCRIPT = $$($(1)$(3:%=-%)_LD_SCRIPT_FN)
212 endif
213
214 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$$($(1)$(3:%=-%)_LD_SCRIPT)
215
216 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS) $$($(1)_BUILTINTABLE:%.c=%.o) $$($(1)_BUILTINWRAPMAIN:%.c=%.o) $$($(1)_PROGBUILTIN_LDAT) $$($(1)$(3:%=-%)_LD_SCRIPT) $$($(1)_KMODBUILTIN_LIBS)
217         @$(QUIET_CMD_ECHO) "  LINK    $$@"
218         $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES)) , \
219           $$(CXX) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CXXFLAGS) $$(CXXFLAGS) , \
220           $$(CC)  $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CFLAGS)   $$(CFLAGS) ) \
221           $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
222           $$($(1)_OBJS) $$($(1)_MOREOBJS) $$($(1)_BUILTINTABLE:%.c=%.o) $$($(1)_BUILTINWRAPMAIN:%.c=%.o) \
223           $$($(1)_PROGBUILTIN_PBI) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) $$($(1)_KMODBUILTIN_LIBS) \
224           $$(shell for i in $$($(1)_PROGBUILTIN_LDAT); do cat $$$$i ; done) $$(NUTTXLIBES) \
225           -o $$@
226         @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
227         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134'  >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
228         @echo >>$(OBJS_DIR)/$(1).exe.d
229 endef
230
231 # Syntax: $(call ELF_template,<executable-name>,<dir>)
232 define ELF_template
233
234 $(2)/$(1).elf: $$($(1)_OBJS)
235         @$(QUIET_CMD_ECHO) "  ELF     $$@"
236         $(Q) $(LINK) \
237           -r -e main -T $(ELF_FILE_LDSCRIPT) \
238           -Map $(1).elf.map \
239           $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
240           -o $$@
241         @echo "$(2)/$(1).elf: \\" >$(OBJS_DIR)/$(1).elf.d
242         @echo "  $(ELF_FILE_LDSCRIPT) \\" >>$(OBJS_DIR)/$(1).elf.d
243         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1).elf.map|tr '&' '\134'  >>$(OBJS_DIR)/$(1).elf.d
244         @echo >>$(OBJS_DIR)/$(1).elf.d
245 endef
246
247 # Syntax: $(call PROGBUILTIN_template,<executable-name>,<dir>,<registry-dir>)
248 define PROGBUILTIN_template
249
250 $(1)_PRIORITY ?= SCHED_PRIORITY_DEFAULT
251 $(1)_STACKSIZE ?= 2048
252
253 $(2)/$(1).pbi: $$($(1)_OBJS)
254         @$(QUIET_CMD_ECHO) "  PROGMOD $$@"
255         $(Q) $(LINK) \
256           -r -x --defsym=$(1)_main=main \
257           -Map $(1).pbi.map \
258           $$($(1)_OBJS) $$($(1)_MOREOBJS) \
259           -o $(OBJS_DIR)/$(1).pbo
260         $(Q) $(OBJCOPY) --keep-global-symbol=$(1)_main $(OBJS_DIR)/$(1).pbo $$@
261         @echo "$(2)/$(1).pbi: \\" >$(OBJS_DIR)/$(1).pbi.d
262         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1).pbi.map|tr '&' '\134'  >>$(OBJS_DIR)/$(1).pbi.d
263         @echo >>$(OBJS_DIR)/$(1).pbi.d
264         @echo "{ \"$1\", $$($(1)_PRIORITY), $$($(1)_STACKSIZE), $(1)_main }," >$(3)/$(1).bdat
265         @echo "int $(1)_main(int argc, char *argv[]);" >$(3)/$(1).pdat
266         @echo "$$(LOADLIBES) $$($(1)_LIBS:%=-l%)" >$(3)/$(1).ldat
267 endef
268
269
270 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
271 %.bin: %
272         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
273         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
274
275 %.hex: %
276         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
277         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
278
279 %.srec: %
280         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
281         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
282
283 # Syntax: $(call LIBRARY_template,<library-name>)
284 define LIBRARY_template
285
286 GEN_SOURCES += $$($(1)_GEN_SOURCES)
287
288 $(foreach x, $(SOURCES2OBJS),
289 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
290                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
291 )
292 $(1)_OBJS += $$($(1)_EMBEDROMFS:%=%_img.o)
293 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
294
295 SOURCES += $$($(1)_SOURCES)
296 EMBEDROMFS += $$($(1)_EMBEDROMFS)
297
298 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
299         @$(QUIET_CMD_ECHO) "  AR      $$@"
300         $(Q) $(AR) rcs $$@ $$^
301 endef
302
303
304 # Syntax: $(call SOLIB_template,<library-name>)
305 define SOLIB_template
306
307 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
308
309 $(foreach x, $(SOURCES2OBJSLO),
310 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
311                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
312 )
313 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
314
315 SOLIB_OBJS  += $$($(1)_OBJSLO)
316 SOLIB_SOURCES += $$($(1)_SOURCES)
317
318 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
319         @$(QUIET_CMD_ECHO) "  LINK    $$@"
320         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
321 endef
322
323 ifneq ($(bin_PROGRAMS),)
324 library-pass_HOOKS += force_builtin_library_hook
325
326 .PHONY: force_builtin_library_hook
327 force_builtin_library_hook:
328
329 endif
330
331 # Syntax: $(call ROMFS_template,<filename>,<filename2>,<directory>) FIXME: Is this correct?
332 define ROMFS_template
333
334 .PHONY: $(2)
335
336 $(2):
337         $(Q) genromfs -f $(2).tmp -V $(1) -d $(3)
338         $(Q) if ( [ ! -e $(2) ] || ! cmp -s $(2).tmp $(2) ) ; then cp $(2).tmp $(2) ; fi
339
340 $(2).o: $(2)
341         @$(QUIET_CMD_ECHO) "  ROMFS   $$@"
342         $(Q) $(LD) -r --accept-unknown-input-arch -b binary \
343                 --defsym=$(2)_size=_binary_$(2)_size \
344                 --defsym=$(2)_start=_binary_$(2)_start \
345                 --defsym=$(2)_end=_binary_$(2)_end \
346                 -o $$@.tmp $$^
347         $(Q) $(OBJCOPY) --rename-section .data=.rodata,alloc,load,readonly,data,contents \
348                 $$@.tmp $$@
349
350 endef