1# SPDX-License-Identifier: GPL-2.0 2#### 3# kbuild: Generic definitions 4 5# Convenient variables 6comma := , 7quote := " 8squote := ' 9empty := 10space := $(empty) $(empty) 11space_escape := _-_SPACE_-_ 12pound := \# 13 14### 15# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 16dot-target = $(dir $@).$(notdir $@) 17 18### 19# The temporary file to save gcc -MMD generated dependencies must not 20# contain a comma 21depfile = $(subst $(comma),_,$(dot-target).d) 22 23### 24# filename of target with directory and extension stripped 25basetarget = $(basename $(notdir $@)) 26 27### 28# real prerequisites without phony targets 29real-prereqs = $(filter-out $(PHONY), $^) 30 31### 32# Escape single quote for use in echo statements 33escsq = $(subst $(squote),'\$(squote)',$1) 34 35### 36# Quote a string to pass it to C files. foo => '"foo"' 37stringify = $(squote)$(quote)$1$(quote)$(squote) 38 39### 40# Easy method for doing a status message 41 kecho := : 42 quiet_kecho := echo 43silent_kecho := : 44kecho := $($(quiet)kecho) 45 46### 47# filechk is used to check if the content of a generated file is updated. 48# Sample usage: 49# 50# filechk_sample = echo $(KERNELRELEASE) 51# version.h: FORCE 52# $(call filechk,sample) 53# 54# The rule defined shall write to stdout the content of the new file. 55# The existing file will be compared with the new one. 56# - If no file exist it is created 57# - If the content differ the new file is used 58# - If they are equal no change, and no timestamp update 59define filechk 60 $(Q)set -e; \ 61 mkdir -p $(dir $@); \ 62 trap "rm -f $(dot-target).tmp" EXIT; \ 63 { $(filechk_$(1)); } > $(dot-target).tmp; \ 64 if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \ 65 $(kecho) ' UPD $@'; \ 66 mv -f $(dot-target).tmp $@; \ 67 fi 68endef 69 70### 71# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 72# Usage: 73# $(Q)$(MAKE) $(build)=dir 74build := -f $(srctree)/scripts/Makefile.build obj 75 76### 77# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= 78# Usage: 79# $(Q)$(MAKE) $(dtbinst)=dir 80dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj 81 82### 83# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj= 84# Usage: 85# $(Q)$(MAKE) $(clean)=dir 86clean := -f $(srctree)/scripts/Makefile.clean obj 87 88# echo command. 89# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 90echo-cmd = $(if $($(quiet)cmd_$(1)),\ 91 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 92 93# sink stdout for 'make -s' 94 redirect := 95 quiet_redirect := 96silent_redirect := exec >/dev/null; 97 98# Delete the target on interruption 99# 100# GNU Make automatically deletes the target if it has already been changed by 101# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make 102# will delete incomplete targets), and resume it later. 103# 104# However, this does not work when the stderr is piped to another program, like 105# $ make >&2 | tee log 106# Make dies with SIGPIPE before cleaning the targets. 107# 108# To address it, we clean the target in signal traps. 109# 110# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM. 111# So, we cover them, and also SIGPIPE just in case. 112# 113# Of course, this is unneeded for phony targets. 114delete-on-interrupt = \ 115 $(if $(filter-out $(PHONY), $@), \ 116 $(foreach sig, HUP INT QUIT TERM PIPE, \ 117 trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);)) 118 119# printing commands 120cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1)) 121 122### 123# if_changed - execute command if any prerequisite is newer than 124# target, or command line has changed 125# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 126# including used config symbols 127# if_changed_rule - as if_changed but execute rule instead 128# See Documentation/kbuild/makefiles.rst for more info 129 130ifneq ($(KBUILD_NOCMDDEP),1) 131# Check if both commands are the same including their order. Result is empty 132# string if equal. User may override this check using make KBUILD_NOCMDDEP=1 133cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \ 134 $(subst $(space),$(space_escape),$(strip $(cmd_$1)))) 135else 136cmd-check = $(if $(strip $(cmd_$@)),,1) 137endif 138 139# Replace >$< with >$$< to preserve $ when reloading the .cmd file 140# (needed for make) 141# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 142# (needed for make) 143# Replace >'< with >'\''< to be able to enclose the whole string in '...' 144# (needed for the shell) 145make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 146 147# Find any prerequisites that are newer than target or that do not exist. 148# (This is not true for now; $? should contain any non-existent prerequisites, 149# but it does not work as expected when .SECONDARY is present. This seems a bug 150# of GNU Make.) 151# PHONY targets skipped in both cases. 152newer-prereqs = $(filter-out $(PHONY),$?) 153 154# Execute command if command has changed or prerequisite(s) are updated. 155if_changed = $(if $(newer-prereqs)$(cmd-check), \ 156 $(cmd); \ 157 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:) 158 159# Execute the command and also postprocess generated .d dependencies file. 160if_changed_dep = $(if $(newer-prereqs)$(cmd-check),$(cmd_and_fixdep),@:) 161 162cmd_and_fixdep = \ 163 $(cmd); \ 164 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ 165 rm -f $(depfile) 166 167# Usage: $(call if_changed_rule,foo) 168# Will check if $(cmd_foo) or any of the prerequisites changed, 169# and if so will execute $(rule_foo). 170if_changed_rule = $(if $(newer-prereqs)$(cmd-check),$(rule_$(1)),@:) 171 172### 173# why - tell why a target got built 174# enabled by make V=2 175# Output (listed in the order they are checked): 176# (1) - due to target is PHONY 177# (2) - due to target missing 178# (3) - due to: file1.h file2.h 179# (4) - due to command line change 180# (5) - due to missing .cmd file 181# (6) - due to target not in $(targets) 182# (1) PHONY targets are always build 183# (2) No target, so we better build it 184# (3) Prerequisite is newer than target 185# (4) The command line stored in the file named dir/.target.cmd 186# differed from actual command line. This happens when compiler 187# options changes 188# (5) No dir/.target.cmd file (used to store command line) 189# (6) No dir/.target.cmd file and target not listed in $(targets) 190# This is a good hint that there is a bug in the kbuild file 191ifeq ($(KBUILD_VERBOSE),2) 192why = \ 193 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 194 $(if $(wildcard $@), \ 195 $(if $(newer-prereqs),- due to: $(newer-prereqs), \ 196 $(if $(cmd-check), \ 197 $(if $(cmd_$@),- due to command line change, \ 198 $(if $(filter $@, $(targets)), \ 199 - due to missing .cmd file, \ 200 - due to $(notdir $@) not in $$(targets) \ 201 ) \ 202 ) \ 203 ) \ 204 ), \ 205 - due to target missing \ 206 ) \ 207 ) 208 209echo-why = $(call escsq, $(strip $(why))) 210endif 211 212############################################################################### 213# 214# When a Kconfig string contains a filename, it is suitable for 215# passing to shell commands. It is surrounded by double-quotes, and 216# any double-quotes or backslashes within it are escaped by 217# backslashes. 218# 219# This is no use for dependencies or $(wildcard). We need to strip the 220# surrounding quotes and the escaping from quotes and backslashes, and 221# we *do* need to escape any spaces in the string. So, for example: 222# 223# Usage: $(eval $(call config_filename,FOO)) 224# 225# Defines FOO_FILENAME based on the contents of the CONFIG_FOO option, 226# transformed as described above to be suitable for use within the 227# makefile. 228# 229# Also, if the filename is a relative filename and exists in the source 230# tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to 231# be prefixed to *both* command invocation and dependencies. 232# 233# Note: We also print the filenames in the quiet_cmd_foo text, and 234# perhaps ought to have a version specially escaped for that purpose. 235# But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good 236# enough. It'll strip the quotes in the common case where there's no 237# space and it's a simple filename, and it'll retain the quotes when 238# there's a space. There are some esoteric cases in which it'll print 239# the wrong thing, but we don't really care. The actual dependencies 240# and commands *do* get it right, with various combinations of single 241# and double quotes, backslashes and spaces in the filenames. 242# 243############################################################################### 244# 245define config_filename 246ifneq ($$(CONFIG_$(1)),"") 247$(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1))))))) 248ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME))) 249else 250ifeq ($$(wildcard $$($(1)_FILENAME)),) 251ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),) 252$(1)_SRCPREFIX := $(srctree)/ 253endif 254endif 255endif 256endif 257endef 258# 259############################################################################### 260 261# delete partially updated (i.e. corrupted) files on error 262.DELETE_ON_ERROR: 263 264# do not delete intermediate files automatically 265.SECONDARY: 266