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### 19c25e1c55SMasahiro Yamada# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o 20c25e1c55SMasahiro Yamadatmp-target = $(dir $@).tmp_$(notdir $@) 21c25e1c55SMasahiro Yamada 22c25e1c55SMasahiro Yamada### 2330a77297SMasahiro Yamada# The temporary file to save gcc -MMD generated dependencies must not 248ec4b4ffSSam Ravnborg# contain a comma 2548f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d) 268ec4b4ffSSam Ravnborg 278ec4b4ffSSam Ravnborg### 285e8d780dSSam Ravnborg# filename of target with directory and extension stripped 295e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@)) 305e8d780dSSam Ravnborg 315e8d780dSSam Ravnborg### 32afa974b7SMasahiro Yamada# real prerequisites without phony targets 33afa974b7SMasahiro Yamadareal-prereqs = $(filter-out $(PHONY), $^) 34afa974b7SMasahiro Yamada 35afa974b7SMasahiro Yamada### 36d51bfb78SSam Ravnborg# Escape single quote for use in echo statements 37d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1) 38d51bfb78SSam Ravnborg 39d51bfb78SSam Ravnborg### 407e826c44SMasahiro Yamada# Quote a string to pass it to C files. foo => '"foo"' 417e826c44SMasahiro Yamadastringify = $(squote)$(quote)$1$(quote)$(squote) 427e826c44SMasahiro Yamada 437e826c44SMasahiro Yamada### 44*a2430b25SMasahiro Yamada# The path to Kbuild or Makefile. Kbuild has precedence over Makefile. 45*a2430b25SMasahiro Yamadakbuild-dir = $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) 46*a2430b25SMasahiro Yamadakbuild-file = $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile) 47*a2430b25SMasahiro Yamada 48*a2430b25SMasahiro Yamada### 495410ecc0SMike Frysinger# Easy method for doing a status message 505410ecc0SMike Frysinger kecho := : 515410ecc0SMike Frysinger quiet_kecho := echo 525410ecc0SMike Frysingersilent_kecho := : 535410ecc0SMike Frysingerkecho := $($(quiet)kecho) 545410ecc0SMike Frysinger 555410ecc0SMike Frysinger### 568ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated. 578ec4b4ffSSam Ravnborg# Sample usage: 58ba97df45SMasahiro Yamada# 59ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE) 60ba97df45SMasahiro Yamada# version.h: FORCE 618ec4b4ffSSam Ravnborg# $(call filechk,sample) 62ba97df45SMasahiro Yamada# 638ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file. 648ec4b4ffSSam Ravnborg# The existing file will be compared with the new one. 658ec4b4ffSSam Ravnborg# - If no file exist it is created 668ec4b4ffSSam Ravnborg# - If the content differ the new file is used 678ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update 688ec4b4ffSSam Ravnborgdefine filechk 69e1f86d7bSMasahiro Yamada $(check-FORCE) 708ec4b4ffSSam Ravnborg $(Q)set -e; \ 718ec4b4ffSSam Ravnborg mkdir -p $(dir $@); \ 7288fe89a4SMasahiro Yamada trap "rm -f $(dot-target).tmp" EXIT; \ 7388fe89a4SMasahiro Yamada { $(filechk_$(1)); } > $(dot-target).tmp; \ 7488fe89a4SMasahiro Yamada if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \ 75fd54f502SMike Frysinger $(kecho) ' UPD $@'; \ 7688fe89a4SMasahiro Yamada mv -f $(dot-target).tmp $@; \ 778ec4b4ffSSam Ravnborg fi 788ec4b4ffSSam Ravnborgendef 798ec4b4ffSSam Ravnborg 80beda9f3aSRoman Zippel### 818ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= 828ec4b4ffSSam Ravnborg# Usage: 838ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir 845b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj 858ec4b4ffSSam Ravnborg 86bc081dd6SMichal Marek### 879fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= 889fb5e537SRobert Richter# Usage: 899fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir 90487c7c77SMasahiro Yamadadtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj 919fb5e537SRobert Richter 92d08372caSLinus Torvalds### 93371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj= 94371fdc77SMasahiro Yamada# Usage: 95371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir 96371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj 97371fdc77SMasahiro Yamada 985de043f4SOleg Verych# echo command. 995de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 1005de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\ 1015de043f4SOleg Verych echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 1025de043f4SOleg Verych 103174a1dccSMasahiro Yamada# sink stdout for 'make -s' 104174a1dccSMasahiro Yamada redirect := 105174a1dccSMasahiro Yamada quiet_redirect := 106174a1dccSMasahiro Yamadasilent_redirect := exec >/dev/null; 107174a1dccSMasahiro Yamada 108a7f3257dSMasahiro Yamada# Delete the target on interruption 109a7f3257dSMasahiro Yamada# 110a7f3257dSMasahiro Yamada# GNU Make automatically deletes the target if it has already been changed by 111a7f3257dSMasahiro Yamada# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make 112a7f3257dSMasahiro Yamada# will delete incomplete targets), and resume it later. 113a7f3257dSMasahiro Yamada# 114a7f3257dSMasahiro Yamada# However, this does not work when the stderr is piped to another program, like 115a7f3257dSMasahiro Yamada# $ make >&2 | tee log 116a7f3257dSMasahiro Yamada# Make dies with SIGPIPE before cleaning the targets. 117a7f3257dSMasahiro Yamada# 118a7f3257dSMasahiro Yamada# To address it, we clean the target in signal traps. 119a7f3257dSMasahiro Yamada# 120a7f3257dSMasahiro Yamada# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM. 121a7f3257dSMasahiro Yamada# So, we cover them, and also SIGPIPE just in case. 122a7f3257dSMasahiro Yamada# 123a7f3257dSMasahiro Yamada# Of course, this is unneeded for phony targets. 124a7f3257dSMasahiro Yamadadelete-on-interrupt = \ 125a7f3257dSMasahiro Yamada $(if $(filter-out $(PHONY), $@), \ 126a7f3257dSMasahiro Yamada $(foreach sig, HUP INT QUIT TERM PIPE, \ 127a7f3257dSMasahiro Yamada trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);)) 128a7f3257dSMasahiro Yamada 1295de043f4SOleg Verych# printing commands 130a7f3257dSMasahiro Yamadacmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1)) 1318ec4b4ffSSam Ravnborg 1328ec4b4ffSSam Ravnborg### 1338ec4b4ffSSam Ravnborg# if_changed - execute command if any prerequisite is newer than 1348ec4b4ffSSam Ravnborg# target, or command line has changed 1358ec4b4ffSSam Ravnborg# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies 1368ec4b4ffSSam Ravnborg# including used config symbols 1378ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead 138cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info 1398ec4b4ffSSam Ravnborg 1408ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1) 14150bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty 1429c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1 14350bcca6aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \ 1449c8fa9bcSMasahiro Yamada $(subst $(space),$(space_escape),$(strip $(cmd_$1)))) 145c4d5ee13SMichal Marekelse 14650bcca6aSMasahiro Yamadacmd-check = $(if $(strip $(cmd_$@)),,1) 1478ec4b4ffSSam Ravnborgendif 1488ec4b4ffSSam Ravnborg 149164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file 150164f0d2eSMichal Marek# (needed for make) 1519564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 152164f0d2eSMichal Marek# (needed for make) 153164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...' 154164f0d2eSMichal Marek# (needed for the shell) 1559564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 1566176aa9aSJan Beulich 1572d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist. 1582d3b1b8fSMasahiro Yamada# (This is not true for now; $? should contain any non-existent prerequisites, 1592d3b1b8fSMasahiro Yamada# but it does not work as expected when .SECONDARY is present. This seems a bug 1602d3b1b8fSMasahiro Yamada# of GNU Make.) 16148f1f058SSam Ravnborg# PHONY targets skipped in both cases. 162eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?) 16348f1f058SSam Ravnborg 164e1f86d7bSMasahiro Yamada# It is a typical mistake to forget the FORCE prerequisite. Check it here so 165e1f86d7bSMasahiro Yamada# no more breakage will slip in. 166e1f86d7bSMasahiro Yamadacheck-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing)) 167e1f86d7bSMasahiro Yamada 168e1f86d7bSMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE) 1696796e804SMasahiro Yamada 1705de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated. 171ebd191b3SMasahiro Yamadaif_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:) 172ebd191b3SMasahiro Yamada 173ebd191b3SMasahiro Yamadacmd_and_savecmd = \ 17467126965SMasahiro Yamada $(cmd); \ 175ebd191b3SMasahiro Yamada printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd 1768ec4b4ffSSam Ravnborg 1775de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file. 1786796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:) 179c1a95fdaSNicolas Pitre 180e4aca459SNicolas Pitrecmd_and_fixdep = \ 1813a2429e1SMasahiro Yamada $(cmd); \ 182392885eeSMasahiro Yamada scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ 183e5d28910SMasahiro Yamada rm -f $(depfile) 184c1a95fdaSNicolas Pitre 1858ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo) 186beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed, 187beda9f3aSRoman Zippel# and if so will execute $(rule_foo). 1886796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:) 18948f1f058SSam Ravnborg 19045d506bdSSam Ravnborg### 191312a3d09SCao jin# why - tell why a target got built 19245d506bdSSam Ravnborg# enabled by make V=2 19345d506bdSSam Ravnborg# Output (listed in the order they are checked): 19445d506bdSSam Ravnborg# (1) - due to target is PHONY 19545d506bdSSam Ravnborg# (2) - due to target missing 19645d506bdSSam Ravnborg# (3) - due to: file1.h file2.h 19745d506bdSSam Ravnborg# (4) - due to command line change 19845d506bdSSam Ravnborg# (5) - due to missing .cmd file 19945d506bdSSam Ravnborg# (6) - due to target not in $(targets) 20045d506bdSSam Ravnborg# (1) PHONY targets are always build 20145d506bdSSam Ravnborg# (2) No target, so we better build it 20245d506bdSSam Ravnborg# (3) Prerequisite is newer than target 20345d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd 20445d506bdSSam Ravnborg# differed from actual command line. This happens when compiler 20545d506bdSSam Ravnborg# options changes 20645d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line) 20745d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets) 20845d506bdSSam Ravnborg# This is a good hint that there is a bug in the kbuild file 20945d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2) 21045d506bdSSam Ravnborgwhy = \ 21145d506bdSSam Ravnborg $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 21245d506bdSSam Ravnborg $(if $(wildcard $@), \ 213eba19032SMasahiro Yamada $(if $(newer-prereqs),- due to: $(newer-prereqs), \ 21450bcca6aSMasahiro Yamada $(if $(cmd-check), \ 21545d506bdSSam Ravnborg $(if $(cmd_$@),- due to command line change, \ 21645d506bdSSam Ravnborg $(if $(filter $@, $(targets)), \ 21745d506bdSSam Ravnborg - due to missing .cmd file, \ 21845d506bdSSam Ravnborg - due to $(notdir $@) not in $$(targets) \ 21945d506bdSSam Ravnborg ) \ 22045d506bdSSam Ravnborg ) \ 22145d506bdSSam Ravnborg ) \ 22245d506bdSSam Ravnborg ), \ 22345d506bdSSam Ravnborg - due to target missing \ 22445d506bdSSam Ravnborg ) \ 22545d506bdSSam Ravnborg ) 22645d506bdSSam Ravnborg 22745d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why))) 22845d506bdSSam Ravnborgendif 2293ee550f1SDavid Woodhouse 2303ee550f1SDavid Woodhouse############################################################################### 2319c2af1c7SMasahiro Yamada 2329c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error 2339c2af1c7SMasahiro Yamada.DELETE_ON_ERROR: 2348e9b61b2SMasahiro Yamada 2358e9b61b2SMasahiro Yamada# do not delete intermediate files automatically 2368e9b61b2SMasahiro Yamada.SECONDARY: 237