xref: /optee_os/mk/compile.mk (revision bc12b0e95e3c63f46850c1e69c79cd6879c68543)
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_WARNS) \
84		   $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \
85		   $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
86ifeq ($C,1)
87check-cmd-$2 = $(CHECK) $$(comp-cppflags-$2) $$<
88echo-check-$2 := $(cmd-echo-silent)
89echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2))
90endif
91
92else ifeq ($$(filter %.S,$1),$1)
93comp-q-$2 := AS # one trailing space
94comp-compiler-$2 := $$(CC$(sm))
95comp-flags-$2 = $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \
96			      $$(aflags-remove-$$(comp-sm-$2)) \
97			      $$(aflags-remove-$2), \
98		   $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \
99		   $$(aflags$$(comp-sm-$2)) $$(aflags-$2))
100
101else ifeq ($$(filter %.cpp,$1),$1)
102comp-q-$2 := CXX
103comp-compiler-$2 := $$(CXX$(sm))
104comp-flags-$2 = $$(filter-out $$(CXXFLAGS_REMOVE) $$(cxxflags-remove) \
105			      $$(cxxflags-remove-$$(comp-sm-$2)) \
106			      $$(cxxflags-remove-$2), \
107		   $$(CXXFLAGS) $$(comp-cxxflags$$(comp-sm-$2)) \
108		   $$(cxxflags$$(comp-sm-$2)) $$(cxxflags-$2))
109
110else
111$$(error "Don't know what to do with $1")
112endif
113
114comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
115				 $$(cppflags-remove-$$(comp-sm-$2)) \
116				 $$(cppflags-remove-$2), \
117		      $$(nostdinc$$(comp-sm-$2)) $$(CPPFLAGS) \
118		      $$(addprefix -I,$$(incdirs$$(comp-sm-$2))) \
119		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$2))) \
120		      $$(addprefix -I,$$(incdirs-$2)) \
121		      $$(cppflags$$(comp-sm-$2)) \
122		      $$(cppflags-lib$$(comp-lib-$2)) $$(cppflags-$2)) \
123		      -D__FILE_ID__=$$(subst -,_,$$(subst /,_,$$(subst .,_,$1)))
124
125comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@
126comp-flags-$2 += $$(comp-cppflags-$2)
127
128comp-cmd-$2 = $$(comp-compiler-$2) $$(comp-flags-$2) -c $$< -o $$@
129comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
130	--rename-section .rodata=.rodata.$1 \
131	--rename-section .rodata.str1.1=.rodata.str1.1.$1 \
132	$2
133
134# Assign defaults if unassigned
135echo-check-$2 ?= true
136echo-check-cmd-$2 ?= true
137check-cmd-$2 ?= true
138
139-include $$(comp-cmd-file-$2)
140-include $$(comp-dep-$2)
141
142
143$2: $1 FORCE-GENSRC$(sm)
144# Check if any prerequisites are newer than the target and
145# check if command line has changed
146	$$(if $$(strip $$(filter-out FORCE-GENSRC$(sm), $$?) \
147	    $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
148	    $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
149		@set -e ;\
150		mkdir -p $$(dir $2) ;\
151		$$(echo-check-$2) '  CHECK   $$<' ;\
152		$$(echo-check-cmd-$2) ;\
153		$$(check-cmd-$2) ;\
154		$(cmd-echo-silent) '  $$(comp-q-$2)     $$@' ;\
155		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
156		$$(comp-cmd-$2) ;\
157		$(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
158		$$(comp-objcpy-cmd-$2) ;\
159		echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
160			$$(comp-cmd-file-$2) ;\
161	)
162
163endef
164
165$(foreach f, $(srcs), $(eval $(call \
166	process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
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
256DTC_FLAGS += -I dts -O dtb
257DTC_FLAGS += -Wno-unit_address_vs_reg
258
259define gen-dtb-file
260# dts file path/name in $1
261# dtb file path/name in $2
262
263dtb-basename-$2	:= $$(basename $$(notdir $2))
264dtb-predts-$2   := $$(dir $2)$$(dtb-basename-$2).pre.dts
265dtb-predep-$2	:= $$(dir $2).$$(dtb-basename-$2).pre.dts.d
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 $2
276
277dtb-dtcflags-$2	:= $$(DTC_FLAGS) -d $$(dtb-dep-$2)
278
279-include $$(dtb-dep-$2)
280-include $$(dtb-predep-$2)
281-include $$(dtb-cmd-file-$2)
282
283dtb-precmd-$2 = $$(CPP$(sm)) $$(dtb-cppflags-$2) -o $$(dtb-predts-$2) $$<
284dtb-cmd-$2 = $$(DTC) $$(dtb-dtcflags-$2) -o $$@ $$(dtb-predts-$2)
285
286$2: $1 FORCE
287# Check if any prerequisites are newer than the target and
288# check if command line has changed
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	    $$(filter-out $$(dtb-cmd-$2), $$(dtb-old-cmd-$2)) 		\
293	    $$(filter-out $$(dtb-old-cmd-$2), $$(dtb-cmd-$2))),		\
294		@set -e; 						\
295		mkdir -p $$(dir $2); 					\
296		$(cmd-echo-silent) '  CPP     $$(dtb-predts-$2)'; 	\
297		$$(dtb-precmd-$2);					\
298		$(cmd-echo-silent) '  DTC     $$@'; 			\
299		$$(dtb-cmd-$2);						\
300		echo "dtb-old-precmd-$2 := $$(subst \",\\\",$$(dtb-precmd-$2))" > \
301			$$(dtb-cmd-file-$2) ;\
302		echo "dtb-old-cmd-$2 := $$(subst \",\\\",$$(dtb-cmd-$2))" >> \
303			$$(dtb-cmd-file-$2) ;\
304	)
305
306endef
307
308additional-compile-deps :=
309