2 # Rules for NuttX binaries compilation of C, C++ and assembler
3 # sources using GNU toolchain.
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
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
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
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
39 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
40 # templates that create rules for compilation of sources
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.
46 define COMPILER_DEFS_template
52 CC = $(CROSS_COMPILE)gcc
53 CXX = $(CROSS_COMPILE)g++
54 LINK = $(CROSS_COMPILE)ld
55 LD = $(CROSS_COMPILE)ld
56 AR = $(CROSS_COMPILE)ar
57 OBJCOPY = $(CROSS_COMPILE)objcopy
58 NM = $(CROSS_COMPILE)nm
60 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
62 CFLAGS += -I$(SOURCES_DIR)
63 CFLAGS += -I$(INCLUDE_DIR)
65 ELF_FILE_LDSCRIPT?=$(wildcard $(NUTTX_EXPORT)/scripts/gnu-elf.ld)
67 LOADLIBES += -L$(LIB_DIR)
68 LOADLIBES += $(lib_LOADLIBES:%=-l%)
71 -include $(OBJS_DIR)/*.d
73 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.s .o/.o
75 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.s .lo/.lo
78 # $(CC) -o $@ $(LCFLAGS) -c $<
80 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
81 $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
83 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
84 $(CPPFLAGS) $(CXXFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
86 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
87 $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) $(ASFLAGS)
90 # Check GCC version for user build
91 ifndef CC_MAJOR_VERSION
92 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
94 # Prepare suitable define for dependency building
95 ifeq ($$(CC_MAJOR_VERSION),2)
96 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
98 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
101 endef # COMPILER_DEFS_template
104 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
105 define COMPILE_c_o_template
106 $(2): $(1) $$(GEN_HEADERS)
107 @$(QUIET_CMD_ECHO) " CC $$@"
108 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
109 then mv -f "$$@.d.tmp" "$$@.d" ; \
110 else rm -f "$$@.d.tmp" ; exit 1; \
115 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
116 define COMPILE_cc_o_template
117 $(2): $(1) $$(GEN_HEADERS)
118 @$(QUIET_CMD_ECHO) " CXX $$@"
119 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
120 then mv -f "$$@.d.tmp" "$$@.d" ; \
121 else rm -f "$$@.d.tmp" ; exit 1; \
125 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
126 define COMPILE_S_o_template
127 $(2): $(1) $$(GEN_HEADERS)
128 @$(QUIET_CMD_ECHO) " AS $$@"
129 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
130 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
131 else rm -f "$$@.d.tmp" ; exit 1; \
135 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
136 define PROGRAM_template
138 GEN_SOURCES += $$($(1)_GEN_SOURCES)
140 $(foreach x, $(SOURCES2OBJS),
141 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
142 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
144 $(1)_OBJS += $$($(1)_EMBEDROMFS:%=%_img.o)
145 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
147 SOURCES += $$($(1)_SOURCES)
148 EMBEDROMFS += $$($(1)_EMBEDROMFS)
150 ifneq ($$($(1)_PROGBUILTIN),)
151 ifneq ($$($(1)_PROGBUILTIN),all)
152 $(1)_PROGBUILTIN_TMP = $$($(1)_PROGBUILTIN)
154 $(1)_PROGBUILTIN_TMP1 = $$(wildcard $$(USER_REGISTRY_DIR)/*.pbi)
155 $(1)_PROGBUILTIN_TMP = $$($(1)_PROGBUILTIN_TMP1:$$(USER_REGISTRY_DIR)/%.pbi=%)
157 $(1)_PROGBUILTIN_LIST = $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE),$$($(1)_PROGBUILTIN_TMP))
159 $(1)_PROGBUILTIN_OTHER=$$(filter-out $(1),$$($(1)_PROGBUILTIN_LIST))
160 $(1)_PROGBUILTIN_PBI=$$($(1)_PROGBUILTIN_OTHER:%=$$(USER_REGISTRY_DIR)/%.pbi)
161 $(1)_PROGBUILTIN_LDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.ldat)
162 $(1)_PROGBUILTIN_PDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.pdat) \
163 $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE:%=$(NUTTXREGISTRY)/%.pdat),\
164 $$(wildcard $(NUTTXREGISTRY)/*.pdat))
165 $(1)_PROGBUILTIN_BDAT=$$($(1)_PROGBUILTIN_LIST:%=$$(USER_REGISTRY_DIR)/%.bdat) \
166 $$(filter-out $$($(1)_PROGBUILTIN_EXCLUDE:%=$(NUTTXREGISTRY)/%.bdat),\
167 $$(wildcard $(NUTTXREGISTRY)/*.bdat))
169 ifneq ($$($(1)_KMODBUILTIN),all)
170 $(1)_KMODBUILTIN_LIBS = $$($(1)_KMODBUILTIN:%=$$(KERN_MODULES_DIR)/lib%.ka)
172 $(1)_KMODBUILTIN_LIBS = $$(wildcard $$(KERN_MODULES_DIR)/lib*.ka)
176 ifneq ($$($(1)_PROGBUILTIN_PDAT),)
177 $(1)_BUILTINTABLE = $(OBJS_DIR)/$(1)-builtintab.c
179 $$($(1)_BUILTINTABLE): $$($(1)_PROGBUILTIN_PDAT) $$($(1)_PROGBUILTIN_BDAT)
180 $(Q) cat $$($(1)_PROGBUILTIN_PDAT) >$$@
181 $(Q) echo "#include <nuttx/config.h>" >>$$@
182 $(Q) echo "#include <nuttx/lib/builtin.h>" >>$$@
183 $(Q) echo "const struct builtin_s g_builtins[] = {" >>$$@
184 $(Q) cat $$($(1)_PROGBUILTIN_BDAT) >>$$@
185 $(Q) echo "{ NULL, 0, 0, 0 }};" >>$$@
186 $(Q) echo "const int g_builtin_count = sizeof(g_builtins) / sizeof(g_builtins[0]);" >>$$@
188 GEN_SOURCES += $$($(1)_BUILTINTABLE)
190 $(1)_BUILTINWRAPMAIN = $(OBJS_DIR)/$(1)-builtinwrapmain.c
192 $$($(1)_BUILTINWRAPMAIN):
193 $(Q) echo "#include <nuttx/config.h>" >$$@
194 $(Q) echo "#include <nuttx/compiler.h>" >>$$@
195 $(Q) echo "int main(int argc, FAR char *argv[]);" >>$$@
196 $(Q) echo "int weak_function $(1)_main(int argc, FAR char *argv[]) {return main(argc, argv);}" >>$$@
198 GEN_SOURCES += $$($(1)_BUILTINWRAPMAIN)
203 ifneq ($$(LD_SCRIPT$(3:%=-%)),)
204 $(1)$(3:%=-%)_LD_SCRIPT_FN = $$(LD_SCRIPT$(3:%=-%))
206 $(1)$(3:%=-%)_LD_SCRIPT_FN = $$(LD_SCRIPT)
209 ifeq ($$(dir $$($(1)$(3:%=-%)_LD_SCRIPT_FN)),)
210 $(1)$(3:%=-%)_LD_SCRIPT = $$(LIB_DIR)/$$($(1)$(3:%=-%)_LD_SCRIPT_FN)
212 $(1)$(3:%=-%)_LD_SCRIPT = $$($(1)$(3:%=-%)_LD_SCRIPT_FN)
215 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$$($(1)$(3:%=-%)_LD_SCRIPT)
217 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS) $$($(1)_BUILTINTABLE:%.c=%.o) $$($(1)_BUILTINWRAPMAIN:%.c=%.o) $$($(1)_PROGBUILTIN_LDAT) $$($(1)$(3:%=-%)_LD_SCRIPT) $$($(1)_KMODBUILTIN_LIBS)
218 @$(QUIET_CMD_ECHO) " LINK $$@"
219 $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES)) , \
220 $$(CXX) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CXXFLAGS) $$(CXXFLAGS) , \
221 $$(CC) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CFLAGS) $$(CFLAGS) ) \
222 $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
223 $$($(1)_OBJS) $$($(1)_MOREOBJS) $$($(1)_BUILTINTABLE:%.c=%.o) $$($(1)_BUILTINWRAPMAIN:%.c=%.o) \
224 $$($(1)_PROGBUILTIN_PBI) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) $$($(1)_KMODBUILTIN_LIBS) \
225 $$(shell for i in $$($(1)_PROGBUILTIN_LDAT); do cat $$$$i ; done) $$(NUTTXLIBES) \
227 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
228 @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134' >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
229 @echo >>$(OBJS_DIR)/$(1).exe.d
232 # Syntax: $(call ELF_template,<executable-name>,<dir>)
235 $(2)/$(1).elf: $$($(1)_OBJS)
236 @$(QUIET_CMD_ECHO) " ELF $$@"
238 -r -e main -T $(ELF_FILE_LDSCRIPT) \
240 $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
242 @echo "$(2)/$(1).elf: \\" >$(OBJS_DIR)/$(1).elf.d
243 @echo " $(ELF_FILE_LDSCRIPT) \\" >>$(OBJS_DIR)/$(1).elf.d
244 @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1).elf.map|tr '&' '\134' >>$(OBJS_DIR)/$(1).elf.d
245 @echo >>$(OBJS_DIR)/$(1).elf.d
248 # Syntax: $(call PROGBUILTIN_template,<executable-name>,<dir>,<registry-dir>)
249 define PROGBUILTIN_template
251 $(1)_PRIORITY ?= SCHED_PRIORITY_DEFAULT
252 $(1)_STACKSIZE ?= 2048
254 $(2)/$(1).pbi: $$($(1)_OBJS)
255 @$(QUIET_CMD_ECHO) " PROGMOD $$@"
257 -r -x --defsym=$(1)_main=main \
259 $$($(1)_OBJS) $$($(1)_MOREOBJS) \
260 -o $(OBJS_DIR)/$(1).pbo
261 $(Q) $(OBJCOPY) --keep-global-symbol=$(1)_main $(OBJS_DIR)/$(1).pbo $$@
262 @echo "$(2)/$(1).pbi: \\" >$(OBJS_DIR)/$(1).pbi.d
263 @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1).pbi.map|tr '&' '\134' >>$(OBJS_DIR)/$(1).pbi.d
264 @echo >>$(OBJS_DIR)/$(1).pbi.d
265 @echo "{ \"$1\", $$($(1)_PRIORITY), $$($(1)_STACKSIZE), $(1)_main }," >$(3)/$(1).bdat
266 @echo "int $(1)_main(int argc, char *argv[]);" >$(3)/$(1).pdat
267 @echo "$$(LOADLIBES) $$($(1)_LIBS:%=-l%)" >$(3)/$(1).ldat
271 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
273 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
274 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
277 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
278 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
281 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
282 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
284 # Syntax: $(call LIBRARY_template,<library-name>)
285 define LIBRARY_template
287 GEN_SOURCES += $$($(1)_GEN_SOURCES)
289 $(foreach x, $(SOURCES2OBJS),
290 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
291 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
293 $(1)_OBJS += $$($(1)_EMBEDROMFS:%=%_img.o)
294 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
296 SOURCES += $$($(1)_SOURCES)
297 EMBEDROMFS += $$($(1)_EMBEDROMFS)
299 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
300 @$(QUIET_CMD_ECHO) " AR $$@"
301 $(Q) $(AR) rcs $$@ $$^
305 # Syntax: $(call SOLIB_template,<library-name>)
306 define SOLIB_template
308 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
310 $(foreach x, $(SOURCES2OBJSLO),
311 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
312 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
314 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
316 SOLIB_OBJS += $$($(1)_OBJSLO)
317 SOLIB_SOURCES += $$($(1)_SOURCES)
319 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
320 @$(QUIET_CMD_ECHO) " LINK $$@"
321 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
324 ifneq ($(bin_PROGRAMS),)
325 library-pass_HOOKS += force_builtin_library_hook
327 .PHONY: force_builtin_library_hook
328 force_builtin_library_hook:
332 # Syntax: $(call ROMFS_template,<filename>,<filename2>,<directory>) FIXME: Is this correct?
333 define ROMFS_template
338 $(Q) genromfs -f $(2).tmp -V $(1) -d $(3)
339 $(Q) if ( [ ! -e $(2) ] || ! cmp -s $(2).tmp $(2) ) ; then cp $(2).tmp $(2) ; fi
342 @$(QUIET_CMD_ECHO) " ROMFS $$@"
343 $(Q) $(LD) -r --accept-unknown-input-arch -b binary \
344 --defsym=$(2)_size=_binary_$(2)_size \
345 --defsym=$(2)_start=_binary_$(2)_start \
346 --defsym=$(2)_end=_binary_$(2)_end \
348 $(Q) $(OBJCOPY) --rename-section .data=.rodata,alloc,load,readonly,data,contents \