xref: /optee_os/mk/compile.mk (revision 316a94e710afc8dcb5b6ac991741ac6370af65fc)
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
56FORCE:
57
58
59define process_srcs
60objs		+= $2
61comp-dep-$2	:= $$(dir $2).$$(notdir $2).d
62comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd
63comp-sm-$2	:= $(sm)
64comp-lib-$2	:= $(libname)
65
66cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2
67
68ifeq ($$(filter %.c,$1),$1)
69comp-q-$2 := CC
70comp-flags-$2 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
71			      $$(cflags-remove-$2), \
72		   $$(CFLAGS) $$(CFLAGS_WARNS) \
73		   $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \
74		   $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
75ifeq ($C,1)
76check-cmd-$2 = $(CHECK) $$(comp-cppflags-$2) $$<
77echo-check-$2 := $(cmd-echo-silent)
78echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2))
79endif
80
81else ifeq ($$(filter %.S,$1),$1)
82comp-q-$2 := AS
83comp-flags-$2 = -DASM=1 $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \
84				      $$(aflags-remove-$2), \
85			   $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \
86			   $$(aflags$$(comp-sm-$2)) $$(aflags-$2))
87
88else
89$$(error "Don't know what to do with $1")
90endif
91
92comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
93			 $$(cppflags-remove-$2), \
94		      $$(nostdinc$$(comp-sm-$2)) $$(CPPFLAGS) \
95		      $$(addprefix -I,$$(incdirs$$(comp-sm-$2))) \
96		      $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$2))) \
97		      $$(addprefix -I,$$(incdirs-$2)) \
98		      $$(cppflags$$(comp-sm-$2)) \
99		      $$(cppflags-lib$$(comp-lib-$2)) $$(cppflags-$2))
100
101comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@
102comp-flags-$2 += $$(comp-cppflags-$2)
103
104comp-cmd-$2 = $$(CC$(sm)) $$(comp-flags-$2) -c $$< -o $$@
105comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
106	--rename-section .rodata=.rodata.$1 \
107	--rename-section .rodata.str1.1=.rodata.str1.1.$1 \
108	$2
109
110# Assign defaults if unassigned
111echo-check-$2 ?= true
112echo-check-cmd-$2 ?= true
113check-cmd-$2 ?= true
114
115-include $$(comp-cmd-file-$2)
116-include $$(comp-dep-$2)
117
118
119$2: $1 FORCE
120# Check if any prerequisites are newer than the target and
121# check if command line has changed
122	$$(if $$(strip $$(filter-out FORCE, $$?) \
123	    $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
124	    $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
125		@set -e ;\
126		mkdir -p $$(dir $2) ;\
127		$$(echo-check-$2) '  CHECK   $$<' ;\
128		$$(echo-check-cmd-$2) ;\
129		$$(check-cmd-$2) ;\
130		$(cmd-echo-silent) '  $$(comp-q-$2)      $$@' ;\
131		$(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
132		$$(comp-cmd-$2) ;\
133		$(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
134		$$(comp-objcpy-cmd-$2) ;\
135		echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
136			$$(comp-cmd-file-$2) ;\
137	)
138
139endef
140
141$(foreach f, $(srcs), $(eval $(call \
142	process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
143
144$(objs): $(conf-file)
145