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 := \# 136768fa4bSMasahiro Yamadadefine newline 146768fa4bSMasahiro Yamada 156768fa4bSMasahiro Yamada 166768fa4bSMasahiro Yamadaendef 178ec4b4ffSSam Ravnborg 188ec4b4ffSSam Ravnborg### 19fccb3d3eSMasahiro Yamada# Comparison macros. 20fccb3d3eSMasahiro Yamada# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000) 21fccb3d3eSMasahiro Yamada# 22fccb3d3eSMasahiro Yamada# Use $(intcmp ...) if supported. (Make >= 4.4) 23fccb3d3eSMasahiro Yamada# Otherwise, fall back to the 'test' shell command. 24fccb3d3eSMasahiro Yamadaifeq ($(intcmp 1,0,,,y),y) 25fccb3d3eSMasahiro Yamadatest-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y) 26fccb3d3eSMasahiro Yamadatest-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y) 27fccb3d3eSMasahiro Yamadaelse 28fccb3d3eSMasahiro Yamadatest-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y) 29fccb3d3eSMasahiro Yamadatest-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y) 30fccb3d3eSMasahiro Yamadaendif 31fccb3d3eSMasahiro Yamadatest-le = $(call test-ge, $2, $1) 32fccb3d3eSMasahiro Yamadatest-lt = $(call test-gt, $2, $1) 33fccb3d3eSMasahiro Yamada 34fccb3d3eSMasahiro Yamada### 3548f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 3648f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@) 3748f1f058SSam Ravnborg 3848f1f058SSam Ravnborg### 39c25e1c55SMasahiro Yamada# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o 40c25e1c55SMasahiro Yamadatmp-target = $(dir $@).tmp_$(notdir $@) 41c25e1c55SMasahiro Yamada 42c25e1c55SMasahiro Yamada### 4330a77297SMasahiro Yamada# The temporary file to save gcc -MMD generated dependencies must not 448ec4b4ffSSam Ravnborg# contain a comma 4548f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d) 468ec4b4ffSSam Ravnborg 478ec4b4ffSSam Ravnborg### 485e8d780dSSam Ravnborg# filename of target with directory and extension stripped 495e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@)) 505e8d780dSSam Ravnborg 515e8d780dSSam Ravnborg### 52afa974b7SMasahiro Yamada# real prerequisites without phony targets 53afa974b7SMasahiro Yamadareal-prereqs = $(filter-out $(PHONY), $^) 54afa974b7SMasahiro Yamada 55afa974b7SMasahiro Yamada### 56d51bfb78SSam Ravnborg# Escape single quote for use in echo statements 57d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1) 58d51bfb78SSam Ravnborg 59d51bfb78SSam Ravnborg### 607e826c44SMasahiro Yamada# Quote a string to pass it to C files. foo => '"foo"' 617e826c44SMasahiro Yamadastringify = $(squote)$(quote)$1$(quote)$(squote) 627e826c44SMasahiro Yamada 637e826c44SMasahiro Yamada### 64a2430b25SMasahiro Yamada# The path to Kbuild or Makefile. Kbuild has precedence over Makefile. 65b1992c37SMasahiro Yamadakbuild-file = $(or $(wildcard $(src)/Kbuild),$(src)/Makefile) 66a2430b25SMasahiro Yamada 67a2430b25SMasahiro Yamada### 686768fa4bSMasahiro Yamada# Read a file, replacing newlines with spaces 696768fa4bSMasahiro Yamada# 706768fa4bSMasahiro Yamada# Make 4.2 or later can read a file by using its builtin function. 715f99665eSMasahiro Yamadaifneq ($(filter-out 4.0 4.1, $(MAKE_VERSION)),) 726768fa4bSMasahiro Yamadaread-file = $(subst $(newline),$(space),$(file < $1)) 736768fa4bSMasahiro Yamadaelse 746768fa4bSMasahiro Yamadaread-file = $(shell cat $1 2>/dev/null) 756768fa4bSMasahiro Yamadaendif 766768fa4bSMasahiro Yamada 776768fa4bSMasahiro Yamada### 785410ecc0SMike Frysinger# Easy method for doing a status message 795410ecc0SMike Frysinger kecho := : 805410ecc0SMike Frysinger quiet_kecho := echo 815410ecc0SMike Frysingersilent_kecho := : 825410ecc0SMike Frysingerkecho := $($(quiet)kecho) 835410ecc0SMike Frysinger 845410ecc0SMike Frysinger### 858ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated. 868ec4b4ffSSam Ravnborg# Sample usage: 87ba97df45SMasahiro Yamada# 88ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE) 89ba97df45SMasahiro Yamada# version.h: FORCE 908ec4b4ffSSam Ravnborg# $(call filechk,sample) 91ba97df45SMasahiro Yamada# 928ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file. 938ec4b4ffSSam Ravnborg# The existing file will be compared with the new one. 948ec4b4ffSSam Ravnborg# - If no file exist it is created 958ec4b4ffSSam Ravnborg# - If the content differ the new file is used 968ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update 978ec4b4ffSSam Ravnborgdefine filechk 98e1f86d7bSMasahiro Yamada $(check-FORCE) 998ec4b4ffSSam Ravnborg $(Q)set -e; \ 1008ec4b4ffSSam Ravnborg mkdir -p $(dir $@); \ 10112fec3d6SMasahiro Yamada trap "rm -f $(tmp-target)" EXIT; \ 10212fec3d6SMasahiro Yamada { $(filechk_$(1)); } > $(tmp-target); \ 10312fec3d6SMasahiro Yamada if [ ! -r $@ ] || ! cmp -s $@ $(tmp-target); then \ 104fd54f502SMike Frysinger $(kecho) ' UPD $@'; \ 10512fec3d6SMasahiro Yamada mv -f $(tmp-target) $@; \ 1068ec4b4ffSSam Ravnborg fi 1078ec4b4ffSSam Ravnborgendef 1088ec4b4ffSSam Ravnborg 109beda9f3aSRoman Zippel### 1108ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 1118ec4b4ffSSam Ravnborg# Usage: 1128ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 1135b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj 1148ec4b4ffSSam Ravnborg 115bc081dd6SMichal Marek### 116371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj= 117371fdc77SMasahiro Yamada# Usage: 118371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir 119371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj 120371fdc77SMasahiro Yamada 1218962b6b4SMasahiro Yamada# pring log 1228962b6b4SMasahiro Yamada# 1238962b6b4SMasahiro Yamada# If quiet is "silent_", print nothing and sink stdout 1248962b6b4SMasahiro Yamada# If quiet is "quiet_", print short log 1258962b6b4SMasahiro Yamada# If quiet is empty, print short log and whole command 1268962b6b4SMasahiro Yamadasilent_log_print = exec >/dev/null; 1278962b6b4SMasahiro Yamada quiet_log_print = $(if $(quiet_cmd_$1), echo ' $(call escsq,$(quiet_cmd_$1)$(why))';) 1286ae4b986SMasahiro Yamada log_print = echo '$(pound) $(call escsq,$(or $(quiet_cmd_$1),cmd_$1 $@)$(why))'; \ 1298962b6b4SMasahiro Yamada echo ' $(call escsq,$(cmd_$1))'; 130174a1dccSMasahiro Yamada 131a7f3257dSMasahiro Yamada# Delete the target on interruption 132a7f3257dSMasahiro Yamada# 133a7f3257dSMasahiro Yamada# GNU Make automatically deletes the target if it has already been changed by 134a7f3257dSMasahiro Yamada# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make 135a7f3257dSMasahiro Yamada# will delete incomplete targets), and resume it later. 136a7f3257dSMasahiro Yamada# 137a7f3257dSMasahiro Yamada# However, this does not work when the stderr is piped to another program, like 138a7f3257dSMasahiro Yamada# $ make >&2 | tee log 139a7f3257dSMasahiro Yamada# Make dies with SIGPIPE before cleaning the targets. 140a7f3257dSMasahiro Yamada# 141a7f3257dSMasahiro Yamada# To address it, we clean the target in signal traps. 142a7f3257dSMasahiro Yamada# 143a7f3257dSMasahiro Yamada# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM. 144a7f3257dSMasahiro Yamada# So, we cover them, and also SIGPIPE just in case. 145a7f3257dSMasahiro Yamada# 146a7f3257dSMasahiro Yamada# Of course, this is unneeded for phony targets. 147a7f3257dSMasahiro Yamadadelete-on-interrupt = \ 148a7f3257dSMasahiro Yamada $(if $(filter-out $(PHONY), $@), \ 149a7f3257dSMasahiro Yamada $(foreach sig, HUP INT QUIT TERM PIPE, \ 150a7f3257dSMasahiro Yamada trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);)) 151a7f3257dSMasahiro Yamada 1528962b6b4SMasahiro Yamada# print and execute commands 1538962b6b4SMasahiro Yamadacmd = @$(if $(cmd_$(1)),set -e; $($(quiet)log_print) $(delete-on-interrupt) $(cmd_$(1)),:) 1548ec4b4ffSSam Ravnborg 1558ec4b4ffSSam Ravnborg### 1568ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 1578ec4b4ffSSam Ravnborg# target, or command line has changed 1588ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1598ec4b4ffSSam Ravnborg# including used config symbols 1608ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 161cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info 1628ec4b4ffSSam Ravnborg 1638ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 16450bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty 1659c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1 166ee2162bdSMasahiro Yamada# If the target does not exist, the *.cmd file should not be included so 167ee2162bdSMasahiro Yamada# $(savedcmd_$@) gets empty. Then, target will be built even if $(newer-prereqs) 168ee2162bdSMasahiro Yamada# happens to become empty. 16992215e7aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(savedcmd_$@))), \ 1709c8fa9bcSMasahiro Yamada $(subst $(space),$(space_escape),$(strip $(cmd_$1)))) 171c4d5ee13SMichal Marekelse 172ee2162bdSMasahiro Yamada# We still need to detect missing targets. 17392215e7aSMasahiro Yamadacmd-check = $(if $(strip $(savedcmd_$@)),,1) 1748ec4b4ffSSam Ravnborgendif 1758ec4b4ffSSam Ravnborg 176164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file 177164f0d2eSMichal Marek# (needed for make) 1789564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 179164f0d2eSMichal Marek# (needed for make) 180164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...' 181164f0d2eSMichal Marek# (needed for the shell) 1829564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 1836176aa9aSJan Beulich 1842d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist. 18548f1f058SSam Ravnborg# PHONY targets skipped in both cases. 186ee2162bdSMasahiro Yamada# If there is no prerequisite other than phony targets, $(newer-prereqs) becomes 187ee2162bdSMasahiro Yamada# empty even if the target does not exist. cmd-check saves this corner case. 188eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?) 18948f1f058SSam Ravnborg 190e1f86d7bSMasahiro Yamada# It is a typical mistake to forget the FORCE prerequisite. Check it here so 191e1f86d7bSMasahiro Yamada# no more breakage will slip in. 192e1f86d7bSMasahiro Yamadacheck-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing)) 193e1f86d7bSMasahiro Yamada 194e1f86d7bSMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE) 1956796e804SMasahiro Yamada 1965de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 197ebd191b3SMasahiro Yamadaif_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:) 198ebd191b3SMasahiro Yamada 199ebd191b3SMasahiro Yamadacmd_and_savecmd = \ 20067126965SMasahiro Yamada $(cmd); \ 20192215e7aSMasahiro Yamada printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd 2028ec4b4ffSSam Ravnborg 2035de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 2046796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:) 205c1a95fdaSNicolas Pitre 206e4aca459SNicolas Pitrecmd_and_fixdep = \ 2073a2429e1SMasahiro Yamada $(cmd); \ 208*214c0eeaSMasahiro Yamada $(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ 209e5d28910SMasahiro Yamada rm -f $(depfile) 210c1a95fdaSNicolas Pitre 2118ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 212beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed, 213beda9f3aSRoman Zippel# and if so will execute $(rule_foo). 2146796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:) 21548f1f058SSam Ravnborg 21645d506bdSSam Ravnborg### 217312a3d09SCao jin# why - tell why a target got built 21845d506bdSSam Ravnborg# enabled by make V=2 21945d506bdSSam Ravnborg# Output (listed in the order they are checked): 22045d506bdSSam Ravnborg# (1) - due to target is PHONY 22145d506bdSSam Ravnborg# (2) - due to target missing 22245d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 22345d506bdSSam Ravnborg# (4) - due to command line change 22445d506bdSSam Ravnborg# (5) - due to missing .cmd file 22545d506bdSSam Ravnborg# (6) - due to target not in $(targets) 22645d506bdSSam Ravnborg# (1) PHONY targets are always build 22745d506bdSSam Ravnborg# (2) No target, so we better build it 22845d506bdSSam Ravnborg# (3) Prerequisite is newer than target 22945d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 23045d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 23145d506bdSSam Ravnborg# options changes 23245d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 23345d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 23445d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 2356ae4b986SMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_VERBOSE)),) 2368962b6b4SMasahiro Yamada_why = \ 23745d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 23845d506bdSSam Ravnborg $(if $(wildcard $@), \ 239eba19032SMasahiro Yamada $(if $(newer-prereqs),- due to: $(newer-prereqs), \ 24050bcca6aSMasahiro Yamada $(if $(cmd-check), \ 24192215e7aSMasahiro Yamada $(if $(savedcmd_$@),- due to command line change, \ 24245d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 24345d506bdSSam Ravnborg - due to missing .cmd file, \ 24445d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 24545d506bdSSam Ravnborg ) \ 24645d506bdSSam Ravnborg ) \ 24745d506bdSSam Ravnborg ) \ 24845d506bdSSam Ravnborg ), \ 24945d506bdSSam Ravnborg - due to target missing \ 25045d506bdSSam Ravnborg ) \ 25145d506bdSSam Ravnborg ) 25245d506bdSSam Ravnborg 2538962b6b4SMasahiro Yamadawhy = $(space)$(strip $(_why)) 25445d506bdSSam Ravnborgendif 2553ee550f1SDavid Woodhouse 2563ee550f1SDavid Woodhouse############################################################################### 2579c2af1c7SMasahiro Yamada 2589c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error 2599c2af1c7SMasahiro Yamada.DELETE_ON_ERROR: 2608e9b61b2SMasahiro Yamada 2618e9b61b2SMasahiro Yamada# do not delete intermediate files automatically 262875ef1a5SMasahiro Yamada# 263875ef1a5SMasahiro Yamada# .NOTINTERMEDIATE is more correct, but only available on newer Make versions. 264875ef1a5SMasahiro Yamada# Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the 265875ef1a5SMasahiro Yamada# global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4. 266875ef1a5SMasahiro Yamada# Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect 267875ef1a5SMasahiro Yamada# deleted files. 268875ef1a5SMasahiro Yamadaifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),) 269875ef1a5SMasahiro Yamada.NOTINTERMEDIATE: 270875ef1a5SMasahiro Yamadaelse 2718e9b61b2SMasahiro Yamada.SECONDARY: 272875ef1a5SMasahiro Yamadaendif 273