xref: /optee_os/mk/compile.mk (revision 8e81e2f5366a971afdd2ac47fb8529d1def5feb0)
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#
7# Output
8#
9# set	  objs
10# update  cleanfiles
11#
12# Generates explicit rules for all objs
13
14objs		:=
15
16# Disable all builtin rules
17.SUFFIXES:
18
19comp-cflags$(sm) = -std=gnu99
20comp-aflags$(sm) =
21comp-cppflags$(sm) =
22
23ifndef NOWERROR
24comp-cflags$(sm)	+= -Werror
25endif
26comp-cflags$(sm)  	+= -fdiagnostics-show-option
27
28comp-cflags-warns-high = \
29	-Wall -Wcast-align  \
30	-Werror-implicit-function-declaration -Wextra -Wfloat-equal \
31	-Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self \
32	-Wmissing-declarations -Wmissing-format-attribute \
33	-Wmissing-include-dirs -Wmissing-noreturn \
34	-Wmissing-prototypes -Wnested-externs -Wpointer-arith \
35	-Wshadow -Wstrict-prototypes -Wswitch-default \
36	-Wwrite-strings \
37	-Wno-missing-field-initializers -Wno-format-zero-length
38comp-cflags-warns-medium = \
39	-Waggregate-return -Wredundant-decls
40comp-cflags-warns-low = \
41	-Wold-style-definition -Wstrict-aliasing=2 \
42	-Wundef -pedantic \
43	-Wdeclaration-after-statement
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
57FORCE:
58FORCE-GENSRC:
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 = -DASM=1 $$(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
106comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@
107comp-flags-$2 += $$(comp-cppflags-$2)
108
109comp-cmd-$2 = $$(CC$(sm)) $$(comp-flags-$2) -c $$< -o $$@
110comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
111	--rename-section .rodata=.rodata.$1 \
112	--rename-section .rodata.str1.1=.rodata.str1.1.$1 \
113	$2
114
115# Assign defaults if unassigned
116echo-check-$2 ?= true
117echo-check-cmd-$2 ?= true
118check-cmd-$2 ?= true
119
120-include $$(comp-cmd-file-$2)
121-include $$(comp-dep-$2)
122
123
124$2: $1 FORCE-GENSRC
125# Check if any prerequisites are newer than the target and
126# check if command line has changed
127	$$(if $$(strip $$(filter-out FORCE-GENSRC, $$?) \
128	    $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
129	    $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
130		@set -e ;\
131		mkdir -p $$(dir $2) ;\
132		$$(echo-check-$2) '  CHECK   $$<' ;\
133		$$(echo-check-cmd-$2) ;\
134		$$(check-cmd-$2) ;\
135		$(cmd-echo-silent) '  $$(comp-q-$2)      $$@' ;\
136		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
137		$$(comp-cmd-$2) ;\
138		$(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
139		$$(comp-objcpy-cmd-$2) ;\
140		echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
141			$$(comp-cmd-file-$2) ;\
142	)
143
144endef
145
146$(foreach f, $(srcs), $(eval $(call \
147	process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
148
149# Handle generated source files, that is, files that are compiled from out-dir
150$(foreach f, $(gen-srcs), $(eval $(call process_srcs,$(f),$$(basename $f).o)))
151
152$(objs): $(conf-file)
153
154define _gen-asm-defines-file
155# c-filename in $1
156# h-filename in $2
157# s-filename in $3
158
159FORCE-GENSRC: $(2)
160
161comp-dep-$3	:= $$(dir $3)$$(notdir $3).d
162comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd
163comp-sm-$3	:= $(sm)
164
165cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2
166
167comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
168			      $$(cflags-remove-$$(comp-sm-$3)) \
169			      $$(cflags-remove-$3), \
170		   $$(CFLAGS) $$(CFLAGS_WARNS) \
171		   $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \
172		   $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3))
173
174comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
175				 $$(cppflags-remove-$$(comp-sm-$3)) \
176				 $$(cppflags-remove-$3), \
177		      $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \
178		      $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \
179		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \
180		      $$(addprefix -I,$$(incdirs-$3)) \
181		      $$(cppflags$$(comp-sm-$3)) \
182		      $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3))
183
184comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@
185comp-flags-$3 += $$(comp-cppflags-$3)
186
187comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@
188
189
190-include $$(comp-cmd-file-$3)
191-include $$(comp-dep-$3)
192
193$3: $1 $(conf-file) FORCE
194# Check if any prerequisites are newer than the target and
195# check if command line has changed
196	$$(if $$(strip $$(filter-out FORCE, $$?) \
197	    $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \
198	    $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \
199		@set -e ;\
200		mkdir -p $$(dir $2) $$(dir $3) ;\
201		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\
202		$$(comp-cmd-$3) ;\
203		echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \
204			$$(comp-cmd-file-$3) ;\
205	)
206
207guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$2)))
208
209$(2): $(3)
210	$(q)set -e;							\
211	$(cmd-echo-silent) '  CHK     $$@';			\
212	mkdir -p $$(dir $$@);					\
213	echo "#ifndef $$(guard-$2)" >$$@.tmp;			\
214	echo "#define $$(guard-$2)" >>$$@.tmp;			\
215	sed -ne 's|^==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \
216	< $$< >>$$@.tmp;					\
217	echo "#endif" >>$$@.tmp;				\
218	$$(call mv-if-changed,$$@.tmp,$$@)
219
220endef
221
222define gen-asm-defines-file
223$(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s)))
224endef
225
226ifneq ($(asm-defines-file),)
227h-file-$(asm-defines-file) := $(out-dir)/$(sm)/include/generated/$(basename $(notdir $(asm-defines-file))).h
228$(eval $(call gen-asm-defines-file,$(asm-defines-file),$(h-file-$(asm-defines-file))))
229asm-defines-file :=
230endif
231