Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 1 | # Input |
| 2 | # |
| 3 | # The output from mk/sub.mk |
| 4 | # base-prefix |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 5 | # conf-file [optional] if set, all objects will depend on $(conf-file) |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 6 | # |
| 7 | # Output |
| 8 | # |
| 9 | # set objs |
| 10 | # update cleanfiles |
| 11 | # |
| 12 | # Generates explicit rules for all objs |
| 13 | |
| 14 | objs := |
| 15 | |
| 16 | # Disable all builtin rules |
| 17 | .SUFFIXES: |
| 18 | |
| 19 | comp-cflags$(sm) = -std=gnu99 |
| 20 | comp-aflags$(sm) = |
| 21 | comp-cppflags$(sm) = |
| 22 | |
| 23 | ifndef NOWERROR |
| 24 | comp-cflags$(sm) += -Werror |
| 25 | endif |
| 26 | comp-cflags$(sm) += -fdiagnostics-show-option |
| 27 | |
| 28 | comp-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 |
| 38 | comp-cflags-warns-medium = \ |
| 39 | -Waggregate-return -Wredundant-decls |
| 40 | comp-cflags-warns-low = \ |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 41 | -Wold-style-definition -Wstrict-aliasing=2 \ |
| 42 | -Wundef -pedantic \ |
| 43 | -Wdeclaration-after-statement |
| 44 | |
| 45 | comp-cflags-warns-1:= $(comp-cflags-warns-high) |
| 46 | comp-cflags-warns-2:= $(comp-cflags-warns-1) $(comp-cflags-warns-medium) |
| 47 | comp-cflags-warns-3:= $(comp-cflags-warns-2) $(comp-cflags-warns-low) |
| 48 | |
| 49 | WARNS ?= 3 |
| 50 | |
| 51 | comp-cflags$(sm) += $(comp-cflags-warns-$(WARNS)) |
| 52 | |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 53 | CHECK ?= sparse |
| 54 | |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 55 | .PHONY: FORCE |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 56 | .PHONY: FORCE-GENSRC |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 57 | FORCE: |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 58 | FORCE-GENSRC: |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 59 | |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 60 | |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 61 | define process_srcs |
| 62 | objs += $2 |
| 63 | comp-dep-$2 := $$(dir $2).$$(notdir $2).d |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 64 | comp-cmd-file-$2:= $$(dir $2).$$(notdir $2).cmd |
| 65 | comp-sm-$2 := $(sm) |
Jerome Forissier | 73dc728 | 2014-09-25 16:04:03 +0200 | [diff] [blame] | 66 | comp-lib-$2 := $(libname) |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 67 | |
| 68 | cleanfiles := $$(cleanfiles) $$(comp-dep-$2) $$(comp-cmd-file-$2) $2 |
| 69 | |
| 70 | ifeq ($$(filter %.c,$1),$1) |
Jerome Forissier | 76f5987 | 2014-11-03 17:03:28 +0100 | [diff] [blame] | 71 | comp-q-$2 := CC |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 72 | comp-flags-$2 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \ |
| 73 | $$(cflags-remove-$2), \ |
| 74 | $$(CFLAGS) $$(CFLAGS_WARNS) \ |
| 75 | $$(comp-cflags$$(comp-sm-$2)) $$(cflags$$(comp-sm-$2)) \ |
Jerome Forissier | 3d34e12 | 2014-10-30 17:37:42 +0100 | [diff] [blame] | 76 | $$(cflags-lib$$(comp-lib-$2)) $$(cflags-$2)) |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 77 | ifeq ($C,1) |
| 78 | check-cmd-$2 = $(CHECK) $$(comp-cppflags-$2) $$< |
| 79 | echo-check-$2 := $(cmd-echo-silent) |
| 80 | echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2)) |
| 81 | endif |
| 82 | |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 83 | else ifeq ($$(filter %.S,$1),$1) |
Jerome Forissier | 76f5987 | 2014-11-03 17:03:28 +0100 | [diff] [blame] | 84 | comp-q-$2 := AS |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 85 | comp-flags-$2 = -DASM=1 $$(filter-out $$(AFLAGS_REMOVE) $$(aflags-remove) \ |
| 86 | $$(aflags-remove-$2), \ |
| 87 | $$(AFLAGS) $$(comp-aflags$$(comp-sm-$2)) \ |
| 88 | $$(aflags$$(comp-sm-$2)) $$(aflags-$2)) |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 89 | |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 90 | else |
| 91 | $$(error "Don't know what to do with $1") |
| 92 | endif |
| 93 | |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 94 | comp-cppflags-$2 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \ |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 95 | $$(cppflags-remove-$2), \ |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 96 | $$(nostdinc$$(comp-sm-$2)) $$(CPPFLAGS) \ |
| 97 | $$(addprefix -I,$$(incdirs$$(comp-sm-$2))) \ |
| 98 | $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$2))) \ |
| 99 | $$(addprefix -I,$$(incdirs-$2)) \ |
| 100 | $$(cppflags$$(comp-sm-$2)) \ |
| 101 | $$(cppflags-lib$$(comp-lib-$2)) $$(cppflags-$2)) |
| 102 | |
| 103 | comp-flags-$2 += -MD -MF $$(comp-dep-$2) -MT $$@ |
| 104 | comp-flags-$2 += $$(comp-cppflags-$2) |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 105 | |
Jens Wiklander | 1b4eb4f | 2015-02-02 09:19:18 +0100 | [diff] [blame] | 106 | comp-cmd-$2 = $$(CC$(sm)) $$(comp-flags-$2) -c $$< -o $$@ |
| 107 | comp-objcpy-cmd-$2 = $$(OBJCOPY$(sm)) \ |
Jens Wiklander | 6d6ea54 | 2014-12-02 11:06:01 +0100 | [diff] [blame] | 108 | --rename-section .rodata=.rodata.$1 \ |
| 109 | --rename-section .rodata.str1.1=.rodata.str1.1.$1 \ |
| 110 | $2 |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 111 | |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 112 | # Assign defaults if unassigned |
| 113 | echo-check-$2 ?= true |
| 114 | echo-check-cmd-$2 ?= true |
| 115 | check-cmd-$2 ?= true |
| 116 | |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 117 | -include $$(comp-cmd-file-$2) |
| 118 | -include $$(comp-dep-$2) |
| 119 | |
| 120 | |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 121 | $2: $1 FORCE-GENSRC |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 122 | # Check if any prerequisites are newer than the target and |
| 123 | # check if command line has changed |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 124 | $$(if $$(strip $$(filter-out FORCE-GENSRC, $$?) \ |
Jerome Forissier | 313ead4 | 2015-01-22 18:17:54 +0100 | [diff] [blame] | 125 | $$(filter-out $$(comp-cmd-$2), $$(old-cmd-$2)) \ |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 126 | $$(filter-out $$(old-cmd-$2), $$(comp-cmd-$2))), \ |
| 127 | @set -e ;\ |
| 128 | mkdir -p $$(dir $2) ;\ |
Jens Wiklander | fbecf4e | 2015-04-16 17:00:22 +0200 | [diff] [blame] | 129 | $$(echo-check-$2) ' CHECK $$<' ;\ |
| 130 | $$(echo-check-cmd-$2) ;\ |
| 131 | $$(check-cmd-$2) ;\ |
Jens Wiklander | 6242863 | 2015-04-29 15:05:19 +0200 | [diff] [blame] | 132 | $(cmd-echo-silent) ' $$(comp-q-$2) $$@' ;\ |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 133 | $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$2)) ;\ |
| 134 | $$(comp-cmd-$2) ;\ |
Jens Wiklander | 6d6ea54 | 2014-12-02 11:06:01 +0100 | [diff] [blame] | 135 | $(cmd-echo) $$(comp-objcpy-cmd-$2) ;\ |
| 136 | $$(comp-objcpy-cmd-$2) ;\ |
Pascal Brand | b010477 | 2014-06-12 15:56:20 +0200 | [diff] [blame] | 137 | echo "old-cmd-$2 := $$(subst \",\\\",$$(comp-cmd-$2))" > \ |
| 138 | $$(comp-cmd-file-$2) ;\ |
| 139 | ) |
| 140 | |
| 141 | endef |
| 142 | |
| 143 | $(foreach f, $(srcs), $(eval $(call \ |
Jerome Forissier | 4334e8d | 2014-09-08 10:53:42 +0200 | [diff] [blame] | 144 | process_srcs,$(f),$(out-dir)/$(base-prefix)$$(basename $f).o))) |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 145 | |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 146 | # Handle generated source files, that is, files that are compiled from out-dir |
| 147 | $(foreach f, $(gen-srcs), $(eval $(call \ |
| 148 | process_srcs,$(out-dir)/$(f),$(out-dir)/$(base-prefix)$$(basename $f).o))) |
| 149 | |
Jerome Forissier | fe52b1f | 2014-11-06 17:54:51 +0100 | [diff] [blame] | 150 | $(objs): $(conf-file) |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 151 | |
| 152 | define _gen-asm-defines-file |
| 153 | # c-filename in $1 |
| 154 | # h-filename in $2 |
| 155 | # s-filename in $3 |
| 156 | |
| 157 | FORCE-GENSRC: $(2) |
| 158 | |
| 159 | comp-dep-$3 := $$(dir $3)$$(notdir $3).d |
| 160 | comp-cmd-file-$3:= $$(dir $3)$$(notdir $3).cmd |
| 161 | comp-sm-$3 := $(sm) |
| 162 | |
| 163 | cleanfiles := $$(cleanfiles) $$(comp-dep-$3) $$(comp-cmd-file-$3) $3 $2 |
| 164 | |
| 165 | comp-flags-$3 = $$(filter-out $$(CFLAGS_REMOVE) $$(cflags-remove) \ |
| 166 | $$(cflags-remove-$3), \ |
| 167 | $$(CFLAGS) $$(CFLAGS_WARNS) \ |
| 168 | $$(comp-cflags$$(comp-sm-$3)) $$(cflags$$(comp-sm-$3)) \ |
| 169 | $$(cflags-lib$$(comp-lib-$3)) $$(cflags-$3)) |
| 170 | |
| 171 | comp-cppflags-$3 = $$(filter-out $$(CPPFLAGS_REMOVE) $$(cppflags-remove) \ |
| 172 | $$(cppflags-remove-$3), \ |
| 173 | $$(nostdinc$$(comp-sm-$3)) $$(CPPFLAGS) \ |
| 174 | $$(addprefix -I,$$(incdirs$$(comp-sm-$3))) \ |
| 175 | $$(addprefix -I,$$(incdirs-lib$$(comp-lib-$3))) \ |
| 176 | $$(addprefix -I,$$(incdirs-$3)) \ |
| 177 | $$(cppflags$$(comp-sm-$3)) \ |
| 178 | $$(cppflags-lib$$(comp-lib-$3)) $$(cppflags-$3)) |
| 179 | |
| 180 | comp-flags-$3 += -MD -MF $$(comp-dep-$3) -MT $$@ |
| 181 | comp-flags-$3 += $$(comp-cppflags-$3) |
| 182 | |
| 183 | comp-cmd-$3 = $$(CC$(sm)) $$(comp-flags-$3) -fverbose-asm -S $$< -o $$@ |
| 184 | |
| 185 | |
| 186 | -include $$(comp-cmd-file-$3) |
| 187 | -include $$(comp-dep-$3) |
| 188 | |
| 189 | $3: $1 $(conf-file) FORCE |
| 190 | # Check if any prerequisites are newer than the target and |
| 191 | # check if command line has changed |
| 192 | $$(if $$(strip $$(filter-out FORCE, $$?) \ |
| 193 | $$(filter-out $$(comp-cmd-$3), $$(old-cmd-$3)) \ |
| 194 | $$(filter-out $$(old-cmd-$3), $$(comp-cmd-$3))), \ |
| 195 | @set -e ;\ |
Jens Wiklander | c02f9fb | 2016-01-12 10:14:50 +0100 | [diff] [blame^] | 196 | mkdir -p $$(dir $2) $$(dir $3) ;\ |
Jens Wiklander | d5a887c | 2016-01-12 15:19:16 +0100 | [diff] [blame] | 197 | $(cmd-echo) $$(subst \",\\\",$$(comp-cmd-$3)) ;\ |
| 198 | $$(comp-cmd-$3) ;\ |
| 199 | echo "old-cmd-$3 := $$(subst \",\\\",$$(comp-cmd-$3))" > \ |
| 200 | $$(comp-cmd-file-$3) ;\ |
| 201 | ) |
| 202 | |
| 203 | guard-$2 := $$(subst -,_,$$(subst .,_,$$(subst /,_,$2))) |
| 204 | |
| 205 | $(2): $(3) |
| 206 | $(q)set -e; \ |
| 207 | $(cmd-echo-silent) ' CHK $$@'; \ |
| 208 | mkdir -p $$(dir $$@); \ |
| 209 | echo "#ifndef $$(guard-$2)" >$$@.tmp; \ |
| 210 | echo "#define $$(guard-$2)" >>$$@.tmp; \ |
| 211 | sed -ne 's|^==>\([^ ]*\) [\$$$$#]*\([-0-9]*\) \([^@/]*\).*|#define \1\t\2\t/* \3*/|p' \ |
| 212 | < $$< >>$$@.tmp; \ |
| 213 | echo "#endif" >>$$@.tmp; \ |
| 214 | $$(call mv-if-changed,$$@.tmp,$$@) |
| 215 | |
| 216 | endef |
| 217 | |
| 218 | define gen-asm-defines-file |
| 219 | $(call _gen-asm-defines-file,$1,$2,$(dir $2).$(notdir $(2:.h=.s))) |
| 220 | endef |
| 221 | |
| 222 | ifneq ($(asm-defines-file),) |
| 223 | h-file-$(asm-defines-file) := $(out-dir)/$(sm)/include/generated/$(basename $(notdir $(asm-defines-file))).h |
| 224 | $(eval $(call gen-asm-defines-file,$(asm-defines-file),$(h-file-$(asm-defines-file)))) |
| 225 | asm-defines-file := |
| 226 | endif |