blob: d085c6efa969e18abbd2fd958ffbfe2a9ad3acf1 [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
56FORCE:
57
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020058
Pascal Brandb0104772014-06-12 15:56:20 +020059define process_srcs
60objs += $2
61comp-dep-$2 := $$(dir $2).$$(notdir $2).d
Pascal Brandb0104772014-06-12 15:56:20 +020062comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd
63comp-sm-$2 := $(sm)
Jerome Forissier73dc7282014-09-25 16:04:03 +020064comp-lib-$2 := $(libname)
Pascal Brandb0104772014-06-12 15:56:20 +020065
66cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2
67
68ifeq ($$(filter %.c,$1),$1)
Jerome Forissier76f59872014-11-03 17:03:28 +010069comp-q-$2 := CC
Pascal Brandb0104772014-06-12 15:56:20 +020070comp-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)) \
Jerome Forissier3d34e122014-10-30 17:37:42 +010074 $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020075ifeq ($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
Pascal Brandb0104772014-06-12 15:56:20 +020081else ifeq ($$(filter %.S,$1),$1)
Jerome Forissier76f59872014-11-03 17:03:28 +010082comp-q-$2 := AS
Pascal Brandb0104772014-06-12 15:56:20 +020083comp-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))
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020087
Pascal Brandb0104772014-06-12 15:56:20 +020088else
89$$(error "Don't know what to do with $1")
90endif
91
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020092comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \
Pascal Brandb0104772014-06-12 15:56:20 +020093 $$(cppflags-remove-$2), \
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +020094 $$(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)
Pascal Brandb0104772014-06-12 15:56:20 +0200103
Jens Wiklander1b4eb4f2015-02-02 09:19:18 +0100104comp-cmd-$2 = $$(CC$(sm)) $$(comp-flags-$2) -c $$< -o $$@
105comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100106 --rename-section .rodata=.rodata.$1 \
107 --rename-section .rodata.str1.1=.rodata.str1.1.$1 \
108 $2
Pascal Brandb0104772014-06-12 15:56:20 +0200109
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200110# Assign defaults if unassigned
111echo-check-$2 ?= true
112echo-check-cmd-$2 ?= true
113check-cmd-$2 ?= true
114
Pascal Brandb0104772014-06-12 15:56:20 +0200115-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
Jerome Forissier313ead42015-01-22 18:17:54 +0100122 $$(if $$(strip $$(filter-out FORCE, $$?) \
123 $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \
Pascal Brandb0104772014-06-12 15:56:20 +0200124 $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \
125 @set -e ;\
126 mkdir -p $$(dir $2) ;\
Jens Wiklanderfbecf4e2015-04-16 17:00:22 +0200127 $$(echo-check-$2) ' CHECK $$<' ;\
128 $$(echo-check-cmd-$2) ;\
129 $$(check-cmd-$2) ;\
Jens Wiklander62428632015-04-29 15:05:19 +0200130 $(cmd-echo-silent) ' $$(comp-q-$2) $$@' ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200131 $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\
132 $$(comp-cmd-$2) ;\
Jens Wiklander6d6ea542014-12-02 11:06:01 +0100133 $(cmd-echo) $$(comp-objcpy-cmd-$2) ;\
134 $$(comp-objcpy-cmd-$2) ;\
Pascal Brandb0104772014-06-12 15:56:20 +0200135 echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \
136 $$(comp-cmd-file-$2) ;\
137 )
138
139endef
140
141$(foreach f, $(srcs), $(eval $(call \
Jerome Forissier4334e8d2014-09-08 10:53:42 +0200142 process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
Jerome Forissierfe52b1f2014-11-06 17:54:51 +0100143
Jens Wiklanderbc420742015-05-05 14:59:15 +0200144# Handle generated source files, that is, files that are compiled from out-dir
145$(foreach f, $(gen-srcs), $(eval $(call \
146 process_srcs,$(out-dir)/$(f),$(out-dir)/$(base-prefix)$$(basename $f).o)))
147
Jerome Forissierfe52b1f2014-11-06 17:54:51 +0100148$(objs): $(conf-file)