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