forked from Minki/linux
a7f3257da8
When receiving some signal, GNU Make automatically deletes the target if it has already been changed by the interrupted recipe. If the target is possibly incomplete due to interruption, it must be deleted so that it will be remade from scratch on the next run of make. Otherwise, the target would remain corrupted permanently because its timestamp had already been updated. Thanks to this behavior of Make, you can stop the build any time by pressing Ctrl-C, and just run 'make' to resume it. Kbuild also relies on this feature, but it is equivalently important for any build systems that make decisions based on timestamps (if you want to support Ctrl-C reliably). However, this does not always work as claimed; Make immediately dies with Ctrl-C if its stderr goes into a pipe. [Test Makefile] foo: echo hello > $@ sleep 3 echo world >> $@ [Test Result] $ make # hit Ctrl-C echo hello > foo sleep 3 ^Cmake: *** Deleting file 'foo' make: *** [Makefile:3: foo] Interrupt $ make 2>&1 | cat # hit Ctrl-C echo hello > foo sleep 3 ^C$ # 'foo' is often left-over The reason is because SIGINT is sent to the entire process group. In this example, SIGINT kills 'cat', and 'make' writes the message to the closed pipe, then dies with SIGPIPE before cleaning the target. A typical bad scenario (as reported by [1], [2]) is to save build log by using the 'tee' command: $ make 2>&1 | tee log This can be problematic for any build systems based on Make, so I hope it will be fixed in GNU Make. The maintainer of GNU Make stated this is a long-standing issue and difficult to fix [3]. It has not been fixed yet as of writing. So, we cannot rely on Make cleaning the target. We can do it by ourselves, in signal traps. As far as I understand, Make takes care of SIGHUP, SIGINT, SIGQUIT, and SITERM for the target removal. I added the traps for them, and also for SIGPIPE just in case cmd_* rule prints something to stdout or stderr (but I did not observe an actual case where SIGPIPE was triggered). [Note 1] The trap handler might be worth explaining. rm -f $@; trap - $(sig); kill -s $(sig) $$ This lets the shell kill itself by the signal it caught, so the parent process can tell the child has exited on the signal. Generally, this is a proper manner for handling signals, in case the calling program (like Bash) may monitor WIFSIGNALED() and WTERMSIG() for WCE although this may not be a big deal here because GNU Make handles SIGHUP, SIGINT, SIGQUIT in WUE and SIGTERM in IUE. IUE - Immediate Unconditional Exit WUE - Wait and Unconditional Exit WCE - Wait and Cooperative Exit For details, see "Proper handling of SIGINT/SIGQUIT" [4]. [Note 2] Reverting392885ee82
("kbuild: let fixdep directly write to .*.cmd files") would directly address [1], but it only saves if_changed_dep. As reported in [2], all commands that use redirection can potentially leave an empty (i.e. broken) target. [Note 3] Another (even safer) approach might be to always write to a temporary file, and rename it to $@ at the end of the recipe. <command> > $(tmp-target) mv $(tmp-target) $@ It would require a lot of Makefile changes, and result in ugly code, so I did not take it. [Note 4] A little more thoughts about a pattern rule with multiple targets (or a grouped target). %.x %.y: %.z <recipe> When interrupted, GNU Make deletes both %.x and %.y, while this solution only deletes $@. Probably, this is not a big deal. The next run of make will execute the rule again to create $@ along with the other files. [1]: https://lore.kernel.org/all/YLeot94yAaM4xbMY@gmail.com/ [2]: https://lore.kernel.org/all/20220510221333.2770571-1-robh@kernel.org/ [3]: https://lists.gnu.org/archive/html/help-make/2021-06/msg00001.html [4]: https://www.cons.org/cracauer/sigint.html Fixes:392885ee82
("kbuild: let fixdep directly write to .*.cmd files") Reported-by: Ingo Molnar <mingo@kernel.org> Reported-by: Rob Herring <robh@kernel.org> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Tested-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
232 lines
8.3 KiB
Plaintext
232 lines
8.3 KiB
Plaintext
# SPDX-License-Identifier: GPL-2.0
|
|
####
|
|
# kbuild: Generic definitions
|
|
|
|
# Convenient variables
|
|
comma := ,
|
|
quote := "
|
|
squote := '
|
|
empty :=
|
|
space := $(empty) $(empty)
|
|
space_escape := _-_SPACE_-_
|
|
pound := \#
|
|
|
|
###
|
|
# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
|
|
dot-target = $(dir $@).$(notdir $@)
|
|
|
|
###
|
|
# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
|
|
tmp-target = $(dir $@).tmp_$(notdir $@)
|
|
|
|
###
|
|
# The temporary file to save gcc -MMD generated dependencies must not
|
|
# contain a comma
|
|
depfile = $(subst $(comma),_,$(dot-target).d)
|
|
|
|
###
|
|
# filename of target with directory and extension stripped
|
|
basetarget = $(basename $(notdir $@))
|
|
|
|
###
|
|
# real prerequisites without phony targets
|
|
real-prereqs = $(filter-out $(PHONY), $^)
|
|
|
|
###
|
|
# Escape single quote for use in echo statements
|
|
escsq = $(subst $(squote),'\$(squote)',$1)
|
|
|
|
###
|
|
# Quote a string to pass it to C files. foo => '"foo"'
|
|
stringify = $(squote)$(quote)$1$(quote)$(squote)
|
|
|
|
###
|
|
# Easy method for doing a status message
|
|
kecho := :
|
|
quiet_kecho := echo
|
|
silent_kecho := :
|
|
kecho := $($(quiet)kecho)
|
|
|
|
###
|
|
# filechk is used to check if the content of a generated file is updated.
|
|
# Sample usage:
|
|
#
|
|
# filechk_sample = echo $(KERNELRELEASE)
|
|
# version.h: FORCE
|
|
# $(call filechk,sample)
|
|
#
|
|
# The rule defined shall write to stdout the content of the new file.
|
|
# The existing file will be compared with the new one.
|
|
# - If no file exist it is created
|
|
# - If the content differ the new file is used
|
|
# - If they are equal no change, and no timestamp update
|
|
define filechk
|
|
$(check-FORCE)
|
|
$(Q)set -e; \
|
|
mkdir -p $(dir $@); \
|
|
trap "rm -f $(dot-target).tmp" EXIT; \
|
|
{ $(filechk_$(1)); } > $(dot-target).tmp; \
|
|
if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \
|
|
$(kecho) ' UPD $@'; \
|
|
mv -f $(dot-target).tmp $@; \
|
|
fi
|
|
endef
|
|
|
|
###
|
|
# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
|
|
# Usage:
|
|
# $(Q)$(MAKE) $(build)=dir
|
|
build := -f $(srctree)/scripts/Makefile.build obj
|
|
|
|
###
|
|
# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
|
|
# Usage:
|
|
# $(Q)$(MAKE) $(dtbinst)=dir
|
|
dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
|
|
|
|
###
|
|
# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
|
|
# Usage:
|
|
# $(Q)$(MAKE) $(clean)=dir
|
|
clean := -f $(srctree)/scripts/Makefile.clean obj
|
|
|
|
# echo command.
|
|
# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
|
|
echo-cmd = $(if $($(quiet)cmd_$(1)),\
|
|
echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
|
|
|
|
# sink stdout for 'make -s'
|
|
redirect :=
|
|
quiet_redirect :=
|
|
silent_redirect := exec >/dev/null;
|
|
|
|
# Delete the target on interruption
|
|
#
|
|
# GNU Make automatically deletes the target if it has already been changed by
|
|
# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
|
|
# will delete incomplete targets), and resume it later.
|
|
#
|
|
# However, this does not work when the stderr is piped to another program, like
|
|
# $ make >&2 | tee log
|
|
# Make dies with SIGPIPE before cleaning the targets.
|
|
#
|
|
# To address it, we clean the target in signal traps.
|
|
#
|
|
# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
|
|
# So, we cover them, and also SIGPIPE just in case.
|
|
#
|
|
# Of course, this is unneeded for phony targets.
|
|
delete-on-interrupt = \
|
|
$(if $(filter-out $(PHONY), $@), \
|
|
$(foreach sig, HUP INT QUIT TERM PIPE, \
|
|
trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
|
|
|
|
# printing commands
|
|
cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1))
|
|
|
|
###
|
|
# if_changed - execute command if any prerequisite is newer than
|
|
# target, or command line has changed
|
|
# if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
|
|
# including used config symbols
|
|
# if_changed_rule - as if_changed but execute rule instead
|
|
# See Documentation/kbuild/makefiles.rst for more info
|
|
|
|
ifneq ($(KBUILD_NOCMDDEP),1)
|
|
# Check if both commands are the same including their order. Result is empty
|
|
# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
|
|
cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
|
|
$(subst $(space),$(space_escape),$(strip $(cmd_$1))))
|
|
else
|
|
cmd-check = $(if $(strip $(cmd_$@)),,1)
|
|
endif
|
|
|
|
# Replace >$< with >$$< to preserve $ when reloading the .cmd file
|
|
# (needed for make)
|
|
# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
|
|
# (needed for make)
|
|
# Replace >'< with >'\''< to be able to enclose the whole string in '...'
|
|
# (needed for the shell)
|
|
make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
|
|
|
|
# Find any prerequisites that are newer than target or that do not exist.
|
|
# (This is not true for now; $? should contain any non-existent prerequisites,
|
|
# but it does not work as expected when .SECONDARY is present. This seems a bug
|
|
# of GNU Make.)
|
|
# PHONY targets skipped in both cases.
|
|
newer-prereqs = $(filter-out $(PHONY),$?)
|
|
|
|
# It is a typical mistake to forget the FORCE prerequisite. Check it here so
|
|
# no more breakage will slip in.
|
|
check-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
|
|
|
|
if-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
|
|
|
|
# Execute command if command has changed or prerequisite(s) are updated.
|
|
if_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
|
|
|
|
cmd_and_savecmd = \
|
|
$(cmd); \
|
|
printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd
|
|
|
|
# Execute the command and also postprocess generated .d dependencies file.
|
|
if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
|
|
|
|
cmd_and_fixdep = \
|
|
$(cmd); \
|
|
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
|
|
rm -f $(depfile)
|
|
|
|
# Usage: $(call if_changed_rule,foo)
|
|
# Will check if $(cmd_foo) or any of the prerequisites changed,
|
|
# and if so will execute $(rule_foo).
|
|
if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
|
|
|
|
###
|
|
# why - tell why a target got built
|
|
# enabled by make V=2
|
|
# Output (listed in the order they are checked):
|
|
# (1) - due to target is PHONY
|
|
# (2) - due to target missing
|
|
# (3) - due to: file1.h file2.h
|
|
# (4) - due to command line change
|
|
# (5) - due to missing .cmd file
|
|
# (6) - due to target not in $(targets)
|
|
# (1) PHONY targets are always build
|
|
# (2) No target, so we better build it
|
|
# (3) Prerequisite is newer than target
|
|
# (4) The command line stored in the file named dir/.target.cmd
|
|
# differed from actual command line. This happens when compiler
|
|
# options changes
|
|
# (5) No dir/.target.cmd file (used to store command line)
|
|
# (6) No dir/.target.cmd file and target not listed in $(targets)
|
|
# This is a good hint that there is a bug in the kbuild file
|
|
ifeq ($(KBUILD_VERBOSE),2)
|
|
why = \
|
|
$(if $(filter $@, $(PHONY)),- due to target is PHONY, \
|
|
$(if $(wildcard $@), \
|
|
$(if $(newer-prereqs),- due to: $(newer-prereqs), \
|
|
$(if $(cmd-check), \
|
|
$(if $(cmd_$@),- due to command line change, \
|
|
$(if $(filter $@, $(targets)), \
|
|
- due to missing .cmd file, \
|
|
- due to $(notdir $@) not in $$(targets) \
|
|
) \
|
|
) \
|
|
) \
|
|
), \
|
|
- due to target missing \
|
|
) \
|
|
)
|
|
|
|
echo-why = $(call escsq, $(strip $(why)))
|
|
endif
|
|
|
|
###############################################################################
|
|
|
|
# delete partially updated (i.e. corrupted) files on error
|
|
.DELETE_ON_ERROR:
|
|
|
|
# do not delete intermediate files automatically
|
|
.SECONDARY:
|