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