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