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