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