blob: 66605037c4713e3d0536ce200315555a2d6f938d [file] [log] [blame]
Pascal Brandb0104772014-06-12 15:56:20 +02001# Input
2#
3# The output from mk/sub.mk
4# base-prefix
Jerome Forissierfe52b1f2014-11-06 17:54:51 +01005# conf-file [optional] if set, all objects will depend on $(conf-file)
Jerome Forissiercf903a62018-08-10 14:24:33 +02006# additional-compile-deps [optional] additional dependencies
Pascal Brandb0104772014-06-12 15:56:20 +02007#
8# Output
9#
10# set objs
11# update cleanfiles
12#
13# Generates explicit rules for all objs
14
15objs :=
16
17# Disable all builtin rules
18.SUFFIXES:
19
20comp-cflags$(sm) = -std=gnu99
21comp-aflags$(sm) =
22comp-cppflags$(sm) =
23
Jerome Forissierbeb065d2018-06-26 10:04:49 +020024ifeq ($(CFG_WERROR),y)
Pascal Brandb0104772014-06-12 15:56:20 +020025comp-cflags$(sm) += -Werror
26endif
27comp-cflags$(sm) += -fdiagnostics-show-option
28
29comp-cflags-warns-high = \
30 -Wall -Wcast-align \
31 -Werror-implicit-function-declaration -Wextra -Wfloat-equal \
32 -Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self \
33 -Wmissing-declarations -Wmissing-format-attribute \
34 -Wmissing-include-dirs -Wmissing-noreturn \
35 -Wmissing-prototypes -Wnested-externs -Wpointer-arith \
36 -Wshadow -Wstrict-prototypes -Wswitch-default \
37 -Wwrite-strings \
38 -Wno-missing-field-initializers -Wno-format-zero-length
39comp-cflags-warns-medium = \
40 -Waggregate-return -Wredundant-decls
41comp-cflags-warns-low = \
Pascal Brandb0104772014-06-12 15:56:20 +020042 -Wold-style-definition -Wstrict-aliasing=2 \
Etienne Carriere33035002019-04-04 09:11:41 +020043 -Wundef
Pascal Brandb0104772014-06-12 15:56:20 +020044
45comp-cflags-warns-1:= $(comp-cflags-warns-high)
46comp-cflags-warns-2:= $(comp-cflags-warns-1) $(comp-cflags-warns-medium)
47comp-cflags-warns-3:= $(comp-cflags-warns-2) $(comp-cflags-warns-low)
48
49WARNS ?= 3
50
Jerome Forissier98d863a2019-07-05 14:34:22 +020051comp-cflags$(sm) += $(comp-cflags-warns-$(WARNS)) \
52 $(comp-cflags-warns-$(COMPILER_$(sm)))
Pascal Brandb0104772014-06-12 15:56:20 +020053
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020054CHECK ?= sparse
55
Pascal Brandb0104772014-06-12 15:56:20 +020056.PHONY: FORCE
Jens Wiklander94cd76c2018-07-17 15:47:46 +020057.PHONY: FORCE-GENSRC$(sm)
Pascal Brandb0104772014-06-12 15:56:20 +020058FORCE:
Jens Wiklander94cd76c2018-07-17 15:47:46 +020059FORCE-GENSRC$(sm):
Pascal Brandb0104772014-06-12 15:56:20 +020060
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020061
Pascal Brandb0104772014-06-12 15:56:20 +020062define process_srcs
63objs += $2
64comp-dep-$2 := $$(dir $2).$$(notdir $2).d
Pascal Brandb0104772014-06-12 15:56:20 +020065comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd
66comp-sm-$2 := $(sm)
Jerome Forissier606f3782016-10-13 14:26:54 +020067comp-lib-$2 := $(libname)-$(sm)
Pascal Brandb0104772014-06-12 15:56:20 +020068
69cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2
70
71ifeq ($$(filter %.c,$1),$1)
Jerome Forissierbe3bc462020-07-21 10:55:15 +020072comp-q-$2 := CC # one trailing space
73comp-compiler-$2 := $$(CC$(sm))
Pascal Brandb0104772014-06-12 15:56:20 +020074comp-flags-$2 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +010075 $$(cflags-remove-$$(comp-sm-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +020076 $$(cflags-remove-$2), \
Jerome Forissierb09cddc2016-02-24 13:01:45 +010077 $$(CFLAGS$$(arch-bits-$$(comp-sm-$2))) $$(CFLAGS_WARNS) \
Pascal Brandb0104772014-06-12 15:56:20 +020078 $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \
Jerome Forissier3d34e122014-10-30 17:37:42 +010079 $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020080ifeq ($C,1)
81check-cmd-$2 = $(CHECK) $$(comp-cppflags-$2) $$<
82echo-check-$2 := $(cmd-echo-silent)
83echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2))
84endif
85
Pascal Brandb0104772014-06-12 15:56:20 +020086else ifeq ($$(filter %.S,$1),$1)
Jerome Forissierbe3bc462020-07-21 10:55:15 +020087comp-q-$2 := AS # one trailing space
88comp-compiler-$2 := $$(CC$(sm))
Jens Wiklanderdf7712b2019-08-21 11:28:37 +020089comp-flags-$2 = $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \
90 $$(aflags-remove-$$(comp-sm-$2)) \
91 $$(aflags-remove-$2), \
92 $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \
93 $$(aflags$$(comp-sm-$2)) $$(aflags-$2))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020094
Jerome Forissierbe3bc462020-07-21 10:55:15 +020095else ifeq ($$(filter %.cpp,$1),$1)
96comp-q-$2 := CXX
97comp-compiler-$2 := $$(CXX$(sm))
98comp-flags-$2 = $$(filter-out $$(CXXFLAGS_REMOVE) $$(cxxflags-remove) \
99 $$(cxxflags-remove-$$(comp-sm-$2)) \
100 $$(cxxflags-remove-$2), \
101 $$(CXXFLAGS) $$(comp-cxxflags$$(comp-sm-$2)) \
102 $$(cxxflags$$(comp-sm-$2)) $$(cxxflags-$2))
103
Pascal Brandb0104772014-06-12 15:56:20 +0200104else
105$$(error "Don't know what to do with $1")
106endif
107
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200108comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +0100109 $$(cppflags-remove-$$(comp-sm-$2)) \
110 $$(cppflags-remove-$2), \
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200111 $$(nostdinc$$(comp-sm-$2)) $$(CPPFLAGS) \
112 $$(addprefix -I,$$(incdirs$$(comp-sm-$2))) \
113 $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$2))) \
114 $$(addprefix -I,$$(incdirs-$2)) \
115 $$(cppflags$$(comp-sm-$2)) \
Jens Wiklanderd3353b42018-11-22 22:09:14 +0100116 $$(cppflags-lib$$(comp-lib-$2)) $$(cppflags-$2)) \
117 -D__FILE_ID__=$$(subst -,_,$$(subst /,_,$$(subst .,_,$1)))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200118
119comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@
120comp-flags-$2 += $$(comp-cppflags-$2)
Pascal Brandb0104772014-06-12 15:56:20 +0200121
Jerome Forissierbe3bc462020-07-21 10:55:15 +0200122comp-cmd-$2 = $$(comp-compiler-$2) $$(comp-flags-$2) -c $$< -o $$@
Jens Wiklander1b4eb4f2015-02-02 09:19:18 +0100123comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100124 --rename-section .rodata=.rodata.$1 \
125 --rename-section .rodata.str1.1=.rodata.str1.1.$1 \
126 $2
Pascal Brandb0104772014-06-12 15:56:20 +0200127
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200128# Assign defaults if unassigned
129echo-check-$2 ?= true
130echo-check-cmd-$2 ?= true
131check-cmd-$2 ?= true
132
Pascal Brandb0104772014-06-12 15:56:20 +0200133-include $$(comp-cmd-file-$2)
134-include $$(comp-dep-$2)
135
136
Jens Wiklander94cd76c2018-07-17 15:47:46 +0200137$2: $1 FORCE-GENSRC$(sm)
Pascal Brandb0104772014-06-12 15:56:20 +0200138# Check if any prerequisites are newer than the target and
139# check if command line has changed
Jens Wiklander94cd76c2018-07-17 15:47:46 +0200140 $$(if $$(strip $$(filter-out FORCE-GENSRC$(sm), $$?) \
Jerome Forissier313ead42015-01-22 18:17:54 +0100141 $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +0200142 $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
143 @set -e ;\
144 mkdir -p $$(dir $2) ;\
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200145 $$(echo-check-$2) ' CHECK $$<' ;\
146 $$(echo-check-cmd-$2) ;\
147 $$(check-cmd-$2) ;\
Jerome Forissierbe3bc462020-07-21 10:55:15 +0200148 $(cmd-echo-silent) ' $$(comp-q-$2) $$@' ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200149 $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
150 $$(comp-cmd-$2) ;\
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100151 $(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
152 $$(comp-objcpy-cmd-$2) ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200153 echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
154 $$(comp-cmd-file-$2) ;\
155 )
156
157endef
158
159$(foreach f, $(srcs), $(eval $(call \
Jerome Forissier4334e8d2014-09-08 10:53:42 +0200160 process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +0100161
Jens Wiklanderbc420742015-05-05 14:59:15 +0200162# Handle generated source files, that is, files that are compiled from out-dir
Jens Wiklanderc95ce412016-05-30 16:06:35 +0200163$(foreach f, $(gen-srcs), $(eval $(call process_srcs,$(f),$$(basename $f).o)))
Jens Wiklanderbc420742015-05-05 14:59:15 +0200164
Jens Wiklandercfa34d92018-07-17 15:47:46 +0200165# Handle specified source files, that is, files that have a specified path
166# but where the object file should go into a specified out directory
167$(foreach f, $(spec-srcs), $(eval $(call \
168 process_srcs,$(f),$(spec-out-dir)/$$(notdir $$(basename $f)).o)))
169
Jerome Forissiercf903a62018-08-10 14:24:33 +0200170$(objs): $(conf-file) $(additional-compile-deps)
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100171
172define _gen-asm-defines-file
173# c-filename in $1
174# h-filename in $2
175# s-filename in $3
176
Jens Wiklander94cd76c2018-07-17 15:47:46 +0200177FORCE-GENSRC$(sm): $(2)
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100178
179comp-dep-$3 := $$(dir $3)$$(notdir $3).d
180comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd
181comp-sm-$3 := $(sm)
182
183cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2
184
185comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +0100186 $$(cflags-remove-$$(comp-sm-$3)) \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100187 $$(cflags-remove-$3), \
188 $$(CFLAGS) $$(CFLAGS_WARNS) \
189 $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \
190 $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3))
191
192comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +0100193 $$(cppflags-remove-$$(comp-sm-$3)) \
194 $$(cppflags-remove-$3), \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100195 $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \
196 $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \
197 $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \
198 $$(addprefix -I,$$(incdirs-$3)) \
199 $$(cppflags$$(comp-sm-$3)) \
200 $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3))
201
202comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@
203comp-flags-$3 += $$(comp-cppflags-$3)
204
205comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@
206
207
208-include $$(comp-cmd-file-$3)
209-include $$(comp-dep-$3)
210
211$3: $1 $(conf-file) FORCE
212# Check if any prerequisites are newer than the target and
213# check if command line has changed
214 $$(if $$(strip $$(filter-out FORCE, $$?) \
215 $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \
216 $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \
217 @set -e ;\
Jens Wiklanderc02f9fb2016-01-12 10:14:50 +0100218 mkdir -p $$(dir $2) $$(dir $3) ;\
Jerome Forissier3ee44162019-03-22 14:25:26 +0100219 $(cmd-echo-silent) ' CC $$@'; \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100220 $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\
221 $$(comp-cmd-$3) ;\
222 echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \
223 $$(comp-cmd-file-$3) ;\
224 )
225
226guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$2)))
227
228$(2): $(3)
229 $(q)set -e; \
230 $(cmd-echo-silent) ' CHK $$@'; \
231 mkdir -p $$(dir $$@); \
232 echo "#ifndef $$(guard-$2)" >$$@.tmp; \
233 echo "#define $$(guard-$2)" >>$$@.tmp; \
Jerome Forissierec295ea2019-03-22 17:06:08 +0100234 sed -ne 's|^.*==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100235 < $$< >>$$@.tmp; \
236 echo "#endif" >>$$@.tmp; \
237 $$(call mv-if-changed,$$@.tmp,$$@)
238
239endef
240
241define gen-asm-defines-file
242$(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s)))
243endef
244
Jerome Forissier070168e2018-03-12 14:45:16 +0100245$(foreach f,$(asm-defines-files),$(eval $(call gen-asm-defines-file,$(f),$(out-dir)/$(sm)/include/generated/$(basename $(notdir $(f))).h)))
Jerome Forissiercf903a62018-08-10 14:24:33 +0200246
Etienne Carrierec828ece2018-11-23 18:03:52 +0100247# Device tree source file compilation
248DTC := dtc
249DTC_FLAGS += -I dts -O dtb
250DTC_FLAGS += -Wno-unit_address_vs_reg
251
252define gen-dtb-file
253# dts file path/name in $1
254# dtb file path/name in $2
255
256dtb-basename-$2 := $$(basename $$(notdir $2))
257dtb-predts-$2 := $$(dir $2)$$(dtb-basename-$2).pre.dts
258dtb-predep-$2 := $$(dir $2).$$(dtb-basename-$2).pre.dts.d
259dtb-dep-$2 := $$(dir $2).$$(notdir $2).d
260dtb-cmd-file-$2 := $$(dir $2).$$(notdir $2).cmd
261
262cleanfiles := $$(cleanfiles) $2 \
263 $$(dtb-predts-$2) $$(dtb-predep-$2) \
264 $$(dtb-dep-$2) $$(dtb-cmd-file-$2)
265
266dtb-cppflags-$2 := -Icore/include/ -x assembler-with-cpp \
267 -E -ffreestanding $$(CPPFLAGS) \
268 -MD -MF $$(dtb-predep-$2) -MT $2
269
270dtb-dtcflags-$2 := $$(DTC_FLAGS) -d $$(dtb-dep-$2)
271
272-include $$(dtb-dep-$2)
273-include $$(dtb-predep-$2)
274-include $$(dtb-cmd-file-$2)
275
276dtb-precmd-$2 = $$(CPP$(sm)) $$(dtb-cppflags-$2) -o $$(dtb-predts-$2) $$<
277dtb-cmd-$2 = $$(DTC) $$(dtb-dtcflags-$2) -o $$@ $$(dtb-predts-$2)
278
279$2: $1 FORCE
280# Check if any prerequisites are newer than the target and
281# check if command line has changed
282 $$(if $$(strip $$(filter-out FORCE, $$?) \
283 $$(filter-out $$(dtb-precmd-$2), $$(dtb-old-precmd-$2)) \
284 $$(filter-out $$(dtb-old-precmd-$2), $$(dtb-precmd-$2)) \
285 $$(filter-out $$(dtb-cmd-$2), $$(dtb-old-cmd-$2)) \
286 $$(filter-out $$(dtb-old-cmd-$2), $$(dtb-cmd-$2))), \
287 @set -e; \
288 mkdir -p $$(dir $2); \
289 $(cmd-echo-silent) ' CPP $$(dtb-predts-$2)'; \
290 $$(dtb-precmd-$2); \
291 $(cmd-echo-silent) ' DTC $$@'; \
292 $$(dtb-cmd-$2); \
293 echo "dtb-old-precmd-$2 := $$(subst \",\\\",$$(dtb-precmd-$2))" > \
294 $$(dtb-cmd-file-$2) ;\
295 echo "dtb-old-cmd-$2 := $$(subst \",\\\",$$(dtb-cmd-$2))" >> \
296 $$(dtb-cmd-file-$2) ;\
297 )
298
299endef
300
Jerome Forissiercf903a62018-08-10 14:24:33 +0200301additional-compile-deps :=