blob: 3d3819600bda26f3100429b318a1d2a258ba1b7a [file] [log] [blame]
Pascal Brandb0104772014-06-12 15:56:20 +02001# Input
2#
3# The output from mk/sub.mk
4# base-prefix
Jerome Forissierfe52b1f2014-11-06 17:54:51 +01005# conf-file [optional] if set, all objects will depend on $(conf-file)
Pascal Brandb0104772014-06-12 15:56:20 +02006#
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 = \
Pascal Brandb0104772014-06-12 15:56:20 +020041 -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
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020053CHECK ?= sparse
54
Pascal Brandb0104772014-06-12 15:56:20 +020055.PHONY: FORCE
Jens Wiklanderd5a887c2016-01-12 15:19:16 +010056.PHONY: FORCE-GENSRC
Pascal Brandb0104772014-06-12 15:56:20 +020057FORCE:
Jens Wiklanderd5a887c2016-01-12 15:19:16 +010058FORCE-GENSRC:
Pascal Brandb0104772014-06-12 15:56:20 +020059
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020060
Pascal Brandb0104772014-06-12 15:56:20 +020061define process_srcs
62objs += $2
63comp-dep-$2 := $$(dir $2).$$(notdir $2).d
Pascal Brandb0104772014-06-12 15:56:20 +020064comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd
65comp-sm-$2 := $(sm)
Jerome Forissier73dc7282014-09-25 16:04:03 +020066comp-lib-$2 := $(libname)
Pascal Brandb0104772014-06-12 15:56:20 +020067
68cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2
69
70ifeq ($$(filter %.c,$1),$1)
Jerome Forissier76f59872014-11-03 17:03:28 +010071comp-q-$2 := CC
Pascal Brandb0104772014-06-12 15:56:20 +020072comp-flags-$2 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +010073 $$(cflags-remove-$$(comp-sm-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +020074 $$(cflags-remove-$2), \
Jerome Forissierb09cddc2016-02-24 13:01:45 +010075 $$(CFLAGS$$(arch-bits-$$(comp-sm-$2))) $$(CFLAGS_WARNS) \
Pascal Brandb0104772014-06-12 15:56:20 +020076 $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \
Jerome Forissier3d34e122014-10-30 17:37:42 +010077 $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020078ifeq ($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
Pascal Brandb0104772014-06-12 15:56:20 +020084else ifeq ($$(filter %.S,$1),$1)
Jerome Forissier76f59872014-11-03 17:03:28 +010085comp-q-$2 := AS
Pascal Brandb0104772014-06-12 15:56:20 +020086comp-flags-$2 = -DASM=1 $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +010087 $$(aflags-remove-$$(comp-sm-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +020088 $$(aflags-remove-$2), \
89 $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \
90 $$(aflags$$(comp-sm-$2)) $$(aflags-$2))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020091
Pascal Brandb0104772014-06-12 15:56:20 +020092else
93$$(error "Don't know what to do with $1")
94endif
95
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020096comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +010097 $$(cppflags-remove-$$(comp-sm-$2)) \
98 $$(cppflags-remove-$2), \
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020099 $$(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)
Pascal Brandb0104772014-06-12 15:56:20 +0200108
Jens Wiklander1b4eb4f2015-02-02 09:19:18 +0100109comp-cmd-$2 = $$(CC$(sm)) $$(comp-flags-$2) -c $$< -o $$@
110comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100111 --rename-section .rodata=.rodata.$1 \
112 --rename-section .rodata.str1.1=.rodata.str1.1.$1 \
113 $2
Pascal Brandb0104772014-06-12 15:56:20 +0200114
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200115# Assign defaults if unassigned
116echo-check-$2 ?= true
117echo-check-cmd-$2 ?= true
118check-cmd-$2 ?= true
119
Pascal Brandb0104772014-06-12 15:56:20 +0200120-include $$(comp-cmd-file-$2)
121-include $$(comp-dep-$2)
122
123
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100124$2: $1 FORCE-GENSRC
Pascal Brandb0104772014-06-12 15:56:20 +0200125# Check if any prerequisites are newer than the target and
126# check if command line has changed
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100127 $$(if $$(strip $$(filter-out FORCE-GENSRC, $$?) \
Jerome Forissier313ead42015-01-22 18:17:54 +0100128 $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +0200129 $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
130 @set -e ;\
131 mkdir -p $$(dir $2) ;\
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200132 $$(echo-check-$2) ' CHECK $$<' ;\
133 $$(echo-check-cmd-$2) ;\
134 $$(check-cmd-$2) ;\
Jens Wiklander62428632015-04-29 15:05:19 +0200135 $(cmd-echo-silent) ' $$(comp-q-$2) $$@' ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200136 $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
137 $$(comp-cmd-$2) ;\
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100138 $(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
139 $$(comp-objcpy-cmd-$2) ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200140 echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
141 $$(comp-cmd-file-$2) ;\
142 )
143
144endef
145
146$(foreach f, $(srcs), $(eval $(call \
Jerome Forissier4334e8d2014-09-08 10:53:42 +0200147 process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +0100148
Jens Wiklanderbc420742015-05-05 14:59:15 +0200149# Handle generated source files, that is, files that are compiled from out-dir
150$(foreach f, $(gen-srcs), $(eval $(call \
151 process_srcs,$(out-dir)/$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
152
Jerome Forissierfe52b1f2014-11-06 17:54:51 +0100153$(objs): $(conf-file)
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100154
155define _gen-asm-defines-file
156# c-filename in $1
157# h-filename in $2
158# s-filename in $3
159
160FORCE-GENSRC: $(2)
161
162comp-dep-$3 := $$(dir $3)$$(notdir $3).d
163comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd
164comp-sm-$3 := $(sm)
165
166cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2
167
168comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +0100169 $$(cflags-remove-$$(comp-sm-$3)) \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100170 $$(cflags-remove-$3), \
171 $$(CFLAGS) $$(CFLAGS_WARNS) \
172 $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \
173 $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3))
174
175comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
Jerome Forissier02598912016-02-26 09:59:55 +0100176 $$(cppflags-remove-$$(comp-sm-$3)) \
177 $$(cppflags-remove-$3), \
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100178 $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \
179 $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \
180 $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \
181 $$(addprefix -I,$$(incdirs-$3)) \
182 $$(cppflags$$(comp-sm-$3)) \
183 $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3))
184
185comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@
186comp-flags-$3 += $$(comp-cppflags-$3)
187
188comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@
189
190
191-include $$(comp-cmd-file-$3)
192-include $$(comp-dep-$3)
193
194$3: $1 $(conf-file) FORCE
195# Check if any prerequisites are newer than the target and
196# check if command line has changed
197 $$(if $$(strip $$(filter-out FORCE, $$?) \
198 $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \
199 $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \
200 @set -e ;\
Jens Wiklanderc02f9fb2016-01-12 10:14:50 +0100201 mkdir -p $$(dir $2) $$(dir $3) ;\
Jens Wiklanderd5a887c2016-01-12 15:19:16 +0100202 $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\
203 $$(comp-cmd-$3) ;\
204 echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \
205 $$(comp-cmd-file-$3) ;\
206 )
207
208guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$2)))
209
210$(2): $(3)
211 $(q)set -e; \
212 $(cmd-echo-silent) ' CHK $$@'; \
213 mkdir -p $$(dir $$@); \
214 echo "#ifndef $$(guard-$2)" >$$@.tmp; \
215 echo "#define $$(guard-$2)" >>$$@.tmp; \
216 sed -ne 's|^==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \
217 < $$< >>$$@.tmp; \
218 echo "#endif" >>$$@.tmp; \
219 $$(call mv-if-changed,$$@.tmp,$$@)
220
221endef
222
223define gen-asm-defines-file
224$(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s)))
225endef
226
227ifneq ($(asm-defines-file),)
228h-file-$(asm-defines-file) := $(out-dir)/$(sm)/include/generated/$(basename $(notdir $(asm-defines-file))).h
229$(eval $(call gen-asm-defines-file,$(asm-defines-file),$(h-file-$(asm-defines-file))))
230asm-defines-file :=
231endif