196ac6d43SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 28ec4b4ffSSam Ravnborg#### 38ec4b4ffSSam Ravnborg# kbuild: Generic definitions 48ec4b4ffSSam Ravnborg 5beda9f3aSRoman Zippel# Convenient variables 68ec4b4ffSSam Ravnborgcomma := , 713338935SMasahiro Yamadaquote := " 8d51bfb78SSam Ravnborgsquote := ' 98ec4b4ffSSam Ravnborgempty := 108ec4b4ffSSam Ravnborgspace := $(empty) $(empty) 119c8fa9bcSMasahiro Yamadaspace_escape := _-_SPACE_-_ 129564a8cfSRasmus Villemoespound := \# 138ec4b4ffSSam Ravnborg 148ec4b4ffSSam Ravnborg### 1548f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 1648f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@) 1748f1f058SSam Ravnborg 1848f1f058SSam Ravnborg### 1930a77297SMasahiro Yamada# The temporary file to save gcc -MMD generated dependencies must not 208ec4b4ffSSam Ravnborg# contain a comma 2148f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d) 228ec4b4ffSSam Ravnborg 238ec4b4ffSSam Ravnborg### 245e8d780dSSam Ravnborg# filename of target with directory and extension stripped 255e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@)) 265e8d780dSSam Ravnborg 275e8d780dSSam Ravnborg### 28afa974b7SMasahiro Yamada# real prerequisites without phony targets 29afa974b7SMasahiro Yamadareal-prereqs = $(filter-out $(PHONY), $^) 30afa974b7SMasahiro Yamada 31afa974b7SMasahiro Yamada### 32d51bfb78SSam Ravnborg# Escape single quote for use in echo statements 33d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1) 34d51bfb78SSam Ravnborg 35d51bfb78SSam Ravnborg### 367e826c44SMasahiro Yamada# Quote a string to pass it to C files. foo => '"foo"' 377e826c44SMasahiro Yamadastringify = $(squote)$(quote)$1$(quote)$(squote) 387e826c44SMasahiro Yamada 397e826c44SMasahiro Yamada### 405410ecc0SMike Frysinger# Easy method for doing a status message 415410ecc0SMike Frysinger kecho := : 425410ecc0SMike Frysinger quiet_kecho := echo 435410ecc0SMike Frysingersilent_kecho := : 445410ecc0SMike Frysingerkecho := $($(quiet)kecho) 455410ecc0SMike Frysinger 465410ecc0SMike Frysinger### 478ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated. 488ec4b4ffSSam Ravnborg# Sample usage: 49ba97df45SMasahiro Yamada# 50ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE) 51ba97df45SMasahiro Yamada# version.h: FORCE 528ec4b4ffSSam Ravnborg# $(call filechk,sample) 53ba97df45SMasahiro Yamada# 548ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file. 558ec4b4ffSSam Ravnborg# The existing file will be compared with the new one. 568ec4b4ffSSam Ravnborg# - If no file exist it is created 578ec4b4ffSSam Ravnborg# - If the content differ the new file is used 588ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update 598ec4b4ffSSam Ravnborgdefine filechk 608ec4b4ffSSam Ravnborg $(Q)set -e; \ 618ec4b4ffSSam Ravnborg mkdir -p $(dir $@); \ 6288fe89a4SMasahiro Yamada trap "rm -f $(dot-target).tmp" EXIT; \ 6388fe89a4SMasahiro Yamada { $(filechk_$(1)); } > $(dot-target).tmp; \ 6488fe89a4SMasahiro Yamada if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \ 65fd54f502SMike Frysinger $(kecho) ' UPD $@'; \ 6688fe89a4SMasahiro Yamada mv -f $(dot-target).tmp $@; \ 678ec4b4ffSSam Ravnborg fi 688ec4b4ffSSam Ravnborgendef 698ec4b4ffSSam Ravnborg 70beda9f3aSRoman Zippel### 718ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 728ec4b4ffSSam Ravnborg# Usage: 738ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 745b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj 758ec4b4ffSSam Ravnborg 76bc081dd6SMichal Marek### 779fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= 789fb5e537SRobert Richter# Usage: 799fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir 80487c7c77SMasahiro Yamadadtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj 819fb5e537SRobert Richter 82d08372caSLinus Torvalds### 83371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj= 84371fdc77SMasahiro Yamada# Usage: 85371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir 86371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj 87371fdc77SMasahiro Yamada 885de043f4SOleg Verych# echo command. 895de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 905de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\ 915de043f4SOleg Verych echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 925de043f4SOleg Verych 93174a1dccSMasahiro Yamada# sink stdout for 'make -s' 94174a1dccSMasahiro Yamada redirect := 95174a1dccSMasahiro Yamada quiet_redirect := 96174a1dccSMasahiro Yamadasilent_redirect := exec >/dev/null; 97174a1dccSMasahiro Yamada 985de043f4SOleg Verych# printing commands 99174a1dccSMasahiro Yamadacmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(cmd_$(1)) 1008ec4b4ffSSam Ravnborg 1018ec4b4ffSSam Ravnborg### 1028ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 1038ec4b4ffSSam Ravnborg# target, or command line has changed 1048ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1058ec4b4ffSSam Ravnborg# including used config symbols 1068ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 107cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info 1088ec4b4ffSSam Ravnborg 1098ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 11050bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty 1119c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1 11250bcca6aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \ 1139c8fa9bcSMasahiro Yamada $(subst $(space),$(space_escape),$(strip $(cmd_$1)))) 114c4d5ee13SMichal Marekelse 11550bcca6aSMasahiro Yamadacmd-check = $(if $(strip $(cmd_$@)),,1) 1168ec4b4ffSSam Ravnborgendif 1178ec4b4ffSSam Ravnborg 118164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file 119164f0d2eSMichal Marek# (needed for make) 1209564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 121164f0d2eSMichal Marek# (needed for make) 122164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...' 123164f0d2eSMichal Marek# (needed for the shell) 1249564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 1256176aa9aSJan Beulich 1262d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist. 1272d3b1b8fSMasahiro Yamada# (This is not true for now; $? should contain any non-existent prerequisites, 1282d3b1b8fSMasahiro Yamada# but it does not work as expected when .SECONDARY is present. This seems a bug 1292d3b1b8fSMasahiro Yamada# of GNU Make.) 13048f1f058SSam Ravnborg# PHONY targets skipped in both cases. 131eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?) 13248f1f058SSam Ravnborg 133*6796e804SMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check) 134*6796e804SMasahiro Yamada 1355de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 136*6796e804SMasahiro Yamadaif_changed = $(if $(if-changed-cond), \ 13767126965SMasahiro Yamada $(cmd); \ 1382aedcd09SMasahiro Yamada printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:) 1398ec4b4ffSSam Ravnborg 1405de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 141*6796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:) 142c1a95fdaSNicolas Pitre 143e4aca459SNicolas Pitrecmd_and_fixdep = \ 1443a2429e1SMasahiro Yamada $(cmd); \ 145392885eeSMasahiro Yamada scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ 146e5d28910SMasahiro Yamada rm -f $(depfile) 147c1a95fdaSNicolas Pitre 1488ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 149beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed, 150beda9f3aSRoman Zippel# and if so will execute $(rule_foo). 151*6796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:) 15248f1f058SSam Ravnborg 15345d506bdSSam Ravnborg### 154312a3d09SCao jin# why - tell why a target got built 15545d506bdSSam Ravnborg# enabled by make V=2 15645d506bdSSam Ravnborg# Output (listed in the order they are checked): 15745d506bdSSam Ravnborg# (1) - due to target is PHONY 15845d506bdSSam Ravnborg# (2) - due to target missing 15945d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 16045d506bdSSam Ravnborg# (4) - due to command line change 16145d506bdSSam Ravnborg# (5) - due to missing .cmd file 16245d506bdSSam Ravnborg# (6) - due to target not in $(targets) 16345d506bdSSam Ravnborg# (1) PHONY targets are always build 16445d506bdSSam Ravnborg# (2) No target, so we better build it 16545d506bdSSam Ravnborg# (3) Prerequisite is newer than target 16645d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 16745d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 16845d506bdSSam Ravnborg# options changes 16945d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 17045d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 17145d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 17245d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2) 17345d506bdSSam Ravnborgwhy = \ 17445d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 17545d506bdSSam Ravnborg $(if $(wildcard $@), \ 176eba19032SMasahiro Yamada $(if $(newer-prereqs),- due to: $(newer-prereqs), \ 17750bcca6aSMasahiro Yamada $(if $(cmd-check), \ 17845d506bdSSam Ravnborg $(if $(cmd_$@),- due to command line change, \ 17945d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 18045d506bdSSam Ravnborg - due to missing .cmd file, \ 18145d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 18245d506bdSSam Ravnborg ) \ 18345d506bdSSam Ravnborg ) \ 18445d506bdSSam Ravnborg ) \ 18545d506bdSSam Ravnborg ), \ 18645d506bdSSam Ravnborg - due to target missing \ 18745d506bdSSam Ravnborg ) \ 18845d506bdSSam Ravnborg ) 18945d506bdSSam Ravnborg 19045d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why))) 19145d506bdSSam Ravnborgendif 1923ee550f1SDavid Woodhouse 1933ee550f1SDavid Woodhouse############################################################################### 1943ee550f1SDavid Woodhouse# 1953ee550f1SDavid Woodhouse# When a Kconfig string contains a filename, it is suitable for 1963ee550f1SDavid Woodhouse# passing to shell commands. It is surrounded by double-quotes, and 1973ee550f1SDavid Woodhouse# any double-quotes or backslashes within it are escaped by 1983ee550f1SDavid Woodhouse# backslashes. 1993ee550f1SDavid Woodhouse# 2003ee550f1SDavid Woodhouse# This is no use for dependencies or $(wildcard). We need to strip the 2013ee550f1SDavid Woodhouse# surrounding quotes and the escaping from quotes and backslashes, and 2023ee550f1SDavid Woodhouse# we *do* need to escape any spaces in the string. So, for example: 2033ee550f1SDavid Woodhouse# 2043ee550f1SDavid Woodhouse# Usage: $(eval $(call config_filename,FOO)) 2053ee550f1SDavid Woodhouse# 2063ee550f1SDavid Woodhouse# Defines FOO_FILENAME based on the contents of the CONFIG_FOO option, 2073ee550f1SDavid Woodhouse# transformed as described above to be suitable for use within the 2083ee550f1SDavid Woodhouse# makefile. 2093ee550f1SDavid Woodhouse# 2103ee550f1SDavid Woodhouse# Also, if the filename is a relative filename and exists in the source 2113ee550f1SDavid Woodhouse# tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to 2123ee550f1SDavid Woodhouse# be prefixed to *both* command invocation and dependencies. 2133ee550f1SDavid Woodhouse# 2143ee550f1SDavid Woodhouse# Note: We also print the filenames in the quiet_cmd_foo text, and 2153ee550f1SDavid Woodhouse# perhaps ought to have a version specially escaped for that purpose. 2163ee550f1SDavid Woodhouse# But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good 2173ee550f1SDavid Woodhouse# enough. It'll strip the quotes in the common case where there's no 2183ee550f1SDavid Woodhouse# space and it's a simple filename, and it'll retain the quotes when 2193ee550f1SDavid Woodhouse# there's a space. There are some esoteric cases in which it'll print 2203ee550f1SDavid Woodhouse# the wrong thing, but we don't really care. The actual dependencies 2213ee550f1SDavid Woodhouse# and commands *do* get it right, with various combinations of single 2223ee550f1SDavid Woodhouse# and double quotes, backslashes and spaces in the filenames. 2233ee550f1SDavid Woodhouse# 2243ee550f1SDavid Woodhouse############################################################################### 2253ee550f1SDavid Woodhouse# 2263ee550f1SDavid Woodhousedefine config_filename 2273ee550f1SDavid Woodhouseifneq ($$(CONFIG_$(1)),"") 2283ee550f1SDavid Woodhouse$(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1))))))) 2293ee550f1SDavid Woodhouseifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME))) 2303ee550f1SDavid Woodhouseelse 2313ee550f1SDavid Woodhouseifeq ($$(wildcard $$($(1)_FILENAME)),) 2323ee550f1SDavid Woodhouseifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),) 2333ee550f1SDavid Woodhouse$(1)_SRCPREFIX := $(srctree)/ 2343ee550f1SDavid Woodhouseendif 2353ee550f1SDavid Woodhouseendif 2363ee550f1SDavid Woodhouseendif 2373ee550f1SDavid Woodhouseendif 2383ee550f1SDavid Woodhouseendef 2393ee550f1SDavid Woodhouse# 2403ee550f1SDavid Woodhouse############################################################################### 2419c2af1c7SMasahiro Yamada 2429c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error 2439c2af1c7SMasahiro Yamada.DELETE_ON_ERROR: 2448e9b61b2SMasahiro Yamada 2458e9b61b2SMasahiro Yamada# do not delete intermediate files automatically 2468e9b61b2SMasahiro Yamada.SECONDARY: 247