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