1# Input
2#
3# The output from mk/sub.mk
4# base-prefix
5# conf-file [optional] if set, all objects will depend on $(conf-file)
6# additional-compile-deps [optional] additional dependencies
7#
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=gnu11
21comp-aflags$(sm) =
22comp-cppflags$(sm) =
23
24ifeq ($(CFG_WERROR),y)
25comp-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 \
39	-Wno-c2x-extensions
40comp-cflags-warns-high += $(call cc-option,-Wpacked-not-aligned)
41comp-cflags-warns-high += $(call cc-option,-Waddress-of-packed-member)
42ifeq ($(CFG_WARN_DECL_AFTER_STATEMENT),y)
43comp-cflags-warns-high += $(call cc-option,-Wdeclaration-after-statement)
44endif
45comp-cflags-warns-medium = \
46	-Wredundant-decls
47comp-cflags-warns-low = \
48	-Wold-style-definition -Wstrict-aliasing=2 \
49	-Wundef
50
51comp-cflags-warns-1:= $(comp-cflags-warns-high)
52comp-cflags-warns-2:= $(comp-cflags-warns-1) $(comp-cflags-warns-medium)
53comp-cflags-warns-3:= $(comp-cflags-warns-2) $(comp-cflags-warns-low)
54
55WARNS		?= 3
56
57comp-cflags$(sm) += $(comp-cflags-warns-$(WARNS)) \
58			$(comp-cflags-warns-$(COMPILER_$(sm)))
59
60CHECK ?= sparse
61
62.PHONY: FORCE
63.PHONY: FORCE-GENSRC$(sm)
64FORCE:
65FORCE-GENSRC$(sm):
66
67
68define process_srcs
69objs		+= $2
70comp-dep-$2	:= $$(dir $2).$$(notdir $2).d
71comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd
72comp-sm-$2	:= $(sm)
73comp-lib-$2	:= $(libname)-$(sm)
74
75cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2
76
77ifeq ($$(filter %.c,$1),$1)
78comp-q-$2 := CC # one trailing space
79comp-compiler-$2 := $$(CC$(sm))
80comp-flags-$2 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
81			      $$(cflags-remove-$$(comp-sm-$2)) \
82			      $$(cflags-remove-$2), \
83		   $$(CFLAGS$$(arch-bits-$$(comp-sm-$2))) $$(CFLAGS) \
84		   $$(CFLAGS_WARNS) \
85		   $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \
86		   $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
87ifeq ($C,1)
88check-cmd-$2 = $(CHECK) $$(comp-cppflags-$2) $$<
89echo-check-$2 := $(cmd-echo-silent)
90echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2))
91endif
92
93else ifeq ($$(filter %.S,$1),$1)
94comp-q-$2 := AS # one trailing space
95comp-compiler-$2 := $$(CC$(sm))
96comp-flags-$2 = $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \
97			      $$(aflags-remove-$$(comp-sm-$2)) \
98			      $$(aflags-remove-$2), \
99		   $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \
100		   $$(aflags$$(comp-sm-$2)) $$(aflags-$2))
101
102else ifeq ($$(filter %.cpp,$1),$1)
103comp-q-$2 := CXX
104comp-compiler-$2 := $$(CXX$(sm))
105comp-flags-$2 = $$(filter-out $$(CXXFLAGS_REMOVE) $$(cxxflags-remove) \
106			      $$(cxxflags-remove-$$(comp-sm-$2)) \
107			      $$(cxxflags-remove-$2), \
108		   $$(CXXFLAGS) $$(comp-cxxflags$$(comp-sm-$2)) \
109		   $$(cxxflags$$(comp-sm-$2)) $$(cxxflags-$2))
110
111else
112$$(error "Don't know what to do with $1")
113endif
114
115comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
116				 $$(cppflags-remove-$$(comp-sm-$2)) \
117				 $$(cppflags-remove-$2), \
118		      $$(nostdinc$$(comp-sm-$2)) $$(CPPFLAGS) \
119		      $$(addprefix -I,$$(incdirs$$(comp-sm-$2))) \
120		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$2))) \
121		      $$(addprefix -I,$$(incdirs-$2)) \
122		      $$(cppflags$$(comp-sm-$2)) \
123		      $$(cppflags-lib$$(comp-lib-$2)) $$(cppflags-$2)) \
124		      -D__FILE_ID__=$$(subst -,_,$$(subst /,_,$$(subst .,_,$$(patsubst $$(out-dir)/%,%,$1))))
125
126comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@
127comp-flags-$2 += $$(comp-cppflags-$2)
128
129comp-cmd-$2 = $$(comp-compiler-$2) $$(comp-flags-$2) -c $$< -o $$@
130comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
131	--rename-section .rodata=.rodata.$1 \
132	--rename-section .rodata.str1.1=.rodata.str1.1.$1 \
133	$2
134
135# Assign defaults if unassigned
136echo-check-$2 ?= true
137echo-check-cmd-$2 ?= true
138check-cmd-$2 ?= true
139
140-include $$(comp-cmd-file-$2)
141-include $$(comp-dep-$2)
142
143
144$2: $1 FORCE-GENSRC$(sm)
145# Check if any prerequisites are newer than the target and
146# check if command line has changed
147	$$(if $$(strip $$(filter-out FORCE-GENSRC$(sm), $$?) \
148	    $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
149	    $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
150		@set -e ;\
151		mkdir -p $$(dir $2) ;\
152		$$(echo-check-$2) '  CHECK   $$<' ;\
153		$$(echo-check-cmd-$2) ;\
154		$$(check-cmd-$2) ;\
155		$(cmd-echo-silent) '  $$(comp-q-$2)     $$@' ;\
156		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
157		$$(comp-cmd-$2) ;\
158		$(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
159		$$(comp-objcpy-cmd-$2) ;\
160		echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
161			$$(comp-cmd-file-$2) ;\
162	)
163
164endef
165
166$(foreach f, $(srcs), $(eval $(call process_srcs,$(f),$$(oname-$(sm)-$(f)))))
167
168# Handle generated source files, that is, files that are compiled from out-dir
169$(foreach f, $(gen-srcs), $(eval $(call process_srcs,$(f),$$(basename $f).o)))
170
171# Handle specified source files, that is, files that have a specified path
172# but where the object file should go into a specified out directory
173$(foreach f, $(spec-srcs), $(eval $(call \
174	process_srcs,$(f),$(spec-out-dir)/$$(notdir $$(basename $f)).o)))
175
176$(objs): $(conf-file) $(additional-compile-deps)
177
178define _gen-asm-defines-file
179# c-filename in $1
180# h-filename in $2
181# s-filename in $3
182# Dependencies in $4
183
184FORCE-GENSRC$(sm): $(2)
185
186comp-dep-$3	:= $$(dir $3)$$(notdir $3).d
187comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd
188comp-sm-$3	:= $(sm)
189
190cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2
191
192comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
193			      $$(cflags-remove-$$(comp-sm-$3)) \
194			      $$(cflags-remove-$3), \
195		   $$(CFLAGS) $$(CFLAGS_WARNS) \
196		   $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \
197		   $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3))
198
199comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
200				 $$(cppflags-remove-$$(comp-sm-$3)) \
201				 $$(cppflags-remove-$3), \
202		      $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \
203		      $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \
204		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \
205		      $$(addprefix -I,$$(incdirs-$3)) \
206		      $$(cppflags$$(comp-sm-$3)) \
207		      $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3))
208
209comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@
210comp-flags-$3 += $$(comp-cppflags-$3)
211
212comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@
213
214
215-include $$(comp-cmd-file-$3)
216-include $$(comp-dep-$3)
217
218$3: $1 $(conf-file) $(4) FORCE
219# Check if any prerequisites are newer than the target and
220# check if command line has changed
221	$$(if $$(strip $$(filter-out FORCE, $$?) \
222	    $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \
223	    $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \
224		@set -e ;\
225		mkdir -p $$(dir $2) $$(dir $3) ;\
226		$(cmd-echo-silent) '  CC      $$@'; 			\
227		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\
228		$$(comp-cmd-$3) ;\
229		echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \
230			$$(comp-cmd-file-$3) ;\
231	)
232
233guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$$(subst +,_,$2))))
234
235$(2): $(3)
236	$(q)set -e;							\
237	$(cmd-echo-silent) '  CHK     $$@';			\
238	mkdir -p $$(dir $$@);					\
239	echo "#ifndef $$(guard-$2)" >$$@.tmp;			\
240	echo "#define $$(guard-$2)" >>$$@.tmp;			\
241	sed -ne 's|^.*==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \
242	< $$< >>$$@.tmp;					\
243	echo "#endif" >>$$@.tmp;				\
244	$$(call mv-if-changed,$$@.tmp,$$@)
245
246endef
247
248define gen-asm-defines-file
249$(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s)),$(asm-defines-$(notdir $(1))-deps))
250endef
251
252$(foreach f,$(asm-defines-files),$(eval $(call gen-asm-defines-file,$(f),$(out-dir)/$(sm)/include/generated/$(basename $(notdir $(f))).h)))
253
254# Device tree source file compilation
255DTC := dtc
256
257define gen-dtb-file
258# dts file path/name in $1
259# dtb file path/name in $2
260
261dtb-basename-$2	:= $$(basename $$(notdir $2))
262dtb-predts-$2   := $$(dir $2)$$(dtb-basename-$2).pre.dts
263dtb-predep-$2	:= $$(dir $2).$$(dtb-basename-$2).pre.dts.d
264dtb-precmd-file-$2 := $$(dir $2).$$(dtb-basename-$2).pre.dts.cmd
265dtb-dep-$2	:= $$(dir $2).$$(notdir $2).d
266dtb-cmd-file-$2	:= $$(dir $2).$$(notdir $2).cmd
267
268cleanfiles := $$(cleanfiles) $2 \
269		$$(dtb-predts-$2) $$(dtb-predep-$2) \
270		$$(dtb-dep-$2) $$(dtb-cmd-file-$2)
271
272dtb-cppflags-$2 := -Icore/include/ -x assembler-with-cpp -Ulinux -Uunix \
273		   -E -ffreestanding $$(CPPFLAGS) \
274		   -MD -MF $$(dtb-predep-$2) -MT $$(dtb-predts-$2)
275
276dtb-dtcflags-$2	:= $$(DTC_FLAGS) -I dts -O dtb -Wno-unit_address_vs_reg \
277		   -d $$(dtb-dep-$2)
278
279-include $$(dtb-dep-$2)
280-include $$(dtb-predep-$2)
281-include $$(dtb-cmd-file-$2)
282-include $$(dtb-precmd-file-$2)
283
284dtb-precmd-$2 = $$(CPP$(sm)) $$(dtb-cppflags-$2) -o $$(dtb-predts-$2) $$<
285dtb-cmd-$2 = $$(DTC) $$(dtb-dtcflags-$2) -o $$@ $$(dtb-predts-$2)
286
287$$(dtb-predts-$2): $1 FORCE
288	$$(if $$(strip $$(filter-out FORCE, $$?) \
289	    $$(filter-out $$(dtb-precmd-$2), $$(dtb-old-precmd-$2)) 	\
290	    $$(filter-out $$(dtb-old-precmd-$2), $$(dtb-precmd-$2))), 	\
291		$(q)set -e; 						\
292		mkdir -p $$(dir $2); 					\
293		$(cmd-echo-silent) '  CPP     $$(dtb-predts-$2)'; 	\
294		$$(dtb-precmd-$2);					\
295		echo "dtb-old-precmd-$2 := $$(subst \",\\\",$$(dtb-precmd-$2))" > \
296			$$(dtb-precmd-file-$2) ;\
297	)
298
299$2: $$(dtb-predts-$2) FORCE
300	$$(if $$(strip $$(filter-out FORCE, $$?) \
301	    $$(filter-out $$(dtb-cmd-$2), $$(dtb-old-cmd-$2)) 		\
302	    $$(filter-out $$(dtb-old-cmd-$2), $$(dtb-cmd-$2))),		\
303		$(q)set -e; 						\
304		mkdir -p $$(dir $2); 					\
305		$(cmd-echo-silent) '  DTC     $$@'; 			\
306		$$(dtb-cmd-$2);						\
307		echo "dtb-old-cmd-$2 := $$(subst \",\\\",$$(dtb-cmd-$2))" > \
308			$$(dtb-cmd-file-$2) ;\
309	)
310
311endef
312
313additional-compile-deps :=
314