xref: /optee_os/mk/compile.mk (revision 1868eb206733e931b6c6c2d85d55e646bc8a2496)
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 \
167	process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
168
169# Handle generated source files, that is, files that are compiled from out-dir
170$(foreach f, $(gen-srcs), $(eval $(call process_srcs,$(f),$$(basename $f).o)))
171
172# Handle specified source files, that is, files that have a specified path
173# but where the object file should go into a specified out directory
174$(foreach f, $(spec-srcs), $(eval $(call \
175	process_srcs,$(f),$(spec-out-dir)/$$(notdir $$(basename $f)).o)))
176
177$(objs): $(conf-file) $(additional-compile-deps)
178
179define _gen-asm-defines-file
180# c-filename in $1
181# h-filename in $2
182# s-filename in $3
183# Dependencies in $4
184
185FORCE-GENSRC$(sm): $(2)
186
187comp-dep-$3	:= $$(dir $3)$$(notdir $3).d
188comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd
189comp-sm-$3	:= $(sm)
190
191cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2
192
193comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
194			      $$(cflags-remove-$$(comp-sm-$3)) \
195			      $$(cflags-remove-$3), \
196		   $$(CFLAGS) $$(CFLAGS_WARNS) \
197		   $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \
198		   $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3))
199
200comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
201				 $$(cppflags-remove-$$(comp-sm-$3)) \
202				 $$(cppflags-remove-$3), \
203		      $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \
204		      $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \
205		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \
206		      $$(addprefix -I,$$(incdirs-$3)) \
207		      $$(cppflags$$(comp-sm-$3)) \
208		      $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3))
209
210comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@
211comp-flags-$3 += $$(comp-cppflags-$3)
212
213comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@
214
215
216-include $$(comp-cmd-file-$3)
217-include $$(comp-dep-$3)
218
219$3: $1 $(conf-file) $(4) FORCE
220# Check if any prerequisites are newer than the target and
221# check if command line has changed
222	$$(if $$(strip $$(filter-out FORCE, $$?) \
223	    $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \
224	    $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \
225		@set -e ;\
226		mkdir -p $$(dir $2) $$(dir $3) ;\
227		$(cmd-echo-silent) '  CC      $$@'; 			\
228		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\
229		$$(comp-cmd-$3) ;\
230		echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \
231			$$(comp-cmd-file-$3) ;\
232	)
233
234guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$$(subst +,_,$2))))
235
236$(2): $(3)
237	$(q)set -e;							\
238	$(cmd-echo-silent) '  CHK     $$@';			\
239	mkdir -p $$(dir $$@);					\
240	echo "#ifndef $$(guard-$2)" >$$@.tmp;			\
241	echo "#define $$(guard-$2)" >>$$@.tmp;			\
242	sed -ne 's|^.*==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \
243	< $$< >>$$@.tmp;					\
244	echo "#endif" >>$$@.tmp;				\
245	$$(call mv-if-changed,$$@.tmp,$$@)
246
247endef
248
249define gen-asm-defines-file
250$(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s)),$(asm-defines-$(notdir $(1))-deps))
251endef
252
253$(foreach f,$(asm-defines-files),$(eval $(call gen-asm-defines-file,$(f),$(out-dir)/$(sm)/include/generated/$(basename $(notdir $(f))).h)))
254
255# Device tree source file compilation
256DTC := dtc
257
258define gen-dtb-file
259# dts file path/name in $1
260# dtb file path/name in $2
261
262dtb-basename-$2	:= $$(basename $$(notdir $2))
263dtb-predts-$2   := $$(dir $2)$$(dtb-basename-$2).pre.dts
264dtb-predep-$2	:= $$(dir $2).$$(dtb-basename-$2).pre.dts.d
265dtb-precmd-file-$2 := $$(dir $2).$$(dtb-basename-$2).pre.dts.cmd
266dtb-dep-$2	:= $$(dir $2).$$(notdir $2).d
267dtb-cmd-file-$2	:= $$(dir $2).$$(notdir $2).cmd
268
269cleanfiles := $$(cleanfiles) $2 \
270		$$(dtb-predts-$2) $$(dtb-predep-$2) \
271		$$(dtb-dep-$2) $$(dtb-cmd-file-$2)
272
273dtb-cppflags-$2 := -Icore/include/ -x assembler-with-cpp -Ulinux -Uunix \
274		   -E -ffreestanding $$(CPPFLAGS) \
275		   -MD -MF $$(dtb-predep-$2) -MT $$(dtb-predts-$2)
276
277dtb-dtcflags-$2	:= $$(DTC_FLAGS) -I dts -O dtb -Wno-unit_address_vs_reg \
278		   -d $$(dtb-dep-$2)
279
280-include $$(dtb-dep-$2)
281-include $$(dtb-predep-$2)
282-include $$(dtb-cmd-file-$2)
283-include $$(dtb-precmd-file-$2)
284
285dtb-precmd-$2 = $$(CPP$(sm)) $$(dtb-cppflags-$2) -o $$(dtb-predts-$2) $$<
286dtb-cmd-$2 = $$(DTC) $$(dtb-dtcflags-$2) -o $$@ $$(dtb-predts-$2)
287
288$$(dtb-predts-$2): $1 FORCE
289	$$(if $$(strip $$(filter-out FORCE, $$?) \
290	    $$(filter-out $$(dtb-precmd-$2), $$(dtb-old-precmd-$2)) 	\
291	    $$(filter-out $$(dtb-old-precmd-$2), $$(dtb-precmd-$2))), 	\
292		$(q)set -e; 						\
293		mkdir -p $$(dir $2); 					\
294		$(cmd-echo-silent) '  CPP     $$(dtb-predts-$2)'; 	\
295		$$(dtb-precmd-$2);					\
296		echo "dtb-old-precmd-$2 := $$(subst \",\\\",$$(dtb-precmd-$2))" > \
297			$$(dtb-precmd-file-$2) ;\
298	)
299
300$2: $$(dtb-predts-$2) FORCE
301	$$(if $$(strip $$(filter-out FORCE, $$?) \
302	    $$(filter-out $$(dtb-cmd-$2), $$(dtb-old-cmd-$2)) 		\
303	    $$(filter-out $$(dtb-old-cmd-$2), $$(dtb-cmd-$2))),		\
304		$(q)set -e; 						\
305		mkdir -p $$(dir $2); 					\
306		$(cmd-echo-silent) '  DTC     $$@'; 			\
307		$$(dtb-cmd-$2);						\
308		echo "dtb-old-cmd-$2 := $$(subst \",\\\",$$(dtb-cmd-$2))" > \
309			$$(dtb-cmd-file-$2) ;\
310	)
311
312endef
313
314additional-compile-deps :=
315