mirror of
https://github.com/torvalds/linux.git
synced 2024-11-22 04:02:20 +00:00
875ef1a57f
In Kbuild, some files are generated by chains of pattern/implicit rules. For example, *.dtb.o files in drivers/of/unittest-data/Makefile are generated by the chain of 3 pattern rules, like this: %.dts -> %.dtb -> %.dtb.S -> %.dtb.o Here, %.dts is the real source, %.dtb.o is the final target. %.dtb and %.dtb.S are called "intermediate files". As GNU Make manual [1] says, intermediate files are treated differently in two ways: (a) The first difference is what happens if the intermediate file does not exist. If an ordinary file 'b' does not exist, and make considers a target that depends on 'b', it invariably creates 'b' and then updates the target from 'b'. But if 'b' is an intermediate file, then make can leave well enough alone: it won't create 'b' unless one of its prerequisites is out of date. This means the target depending on 'b' won't be rebuilt either, unless there is some other reason to update that target: for example the target doesn't exist or a different prerequisite is newer than the target. (b) The second difference is that if make does create 'b' in order to update something else, it deletes 'b' later on after it is no longer needed. Therefore, an intermediate file which did not exist before make also does not exist after make. make reports the deletion to you by printing a 'rm' command showing which file it is deleting. The combination of these is problematic for Kbuild because most of the build rules depend on FORCE and the if_changed* macros really determine if the target should be updated. So, all missing files, whether they are intermediate or not, are always rebuilt. To see the problem, delete ".SECONDARY:" from scripts/Kbuild.include, and repeat this command: $ make allmodconfig drivers/of/unittest-data/ The intermediate files will be deleted, which results in rebuilding intermediate and final objects in the next run of make. In the old days, people suppressed (b) in inconsistent ways. As commit54a702f705
("kbuild: mark $(targets) as .SECONDARY and remove .PRECIOUS markers") noted, you should not use .PRECIOUS because .PRECIOUS has the following behavior (c), which is not likely what you want. (c) If make is killed or interrupted during the execution of their recipes, the target is not deleted. Also, the target is not deleted on error even if .DELETE_ON_ERROR is specified. .SECONDARY is a much better way to disable (b), but a small problem is that .SECONDARY enables (a), which gives a side-effect to $?; prerequisites marked as .SECONDARY do not appear in $?. This is a drawback for Kbuild. I thought it was a bug and opened a bug report. As Paul, the GNU Make maintainer, concluded in [2], this is not a bug. A good news is that, GNU Make 4.4 added the perfect solution, .NOTINTERMEDIATE, which cancels both (a) and (b). For clarificaton, my understanding of .INTERMEDIATE, .SECONDARY, .PRECIOUS and .NOTINTERMEDIATE are as follows: (a) (b) (c) .INTERMEDIATE enable enable disable .SECONDARY enable disable disable .PRECIOUS disable disable enable .NOTINTERMEDIATE disable disable disable However, GNU Make 4.4 has a bug for the global .NOTINTERMEDIATE. [3] It was fixed by commit 6164608900ad ("[SV 63417] Ensure global .NOTINTERMEDIATE disables all intermediates"), and will be available in the next release of GNU Make. The following is the gain for .NOTINTERMEDIATE: [Current Make] $ make allnoconfig vmlinux [ full build ] $ rm include/linux/device.h $ make vmlinux CALL scripts/checksyscalls.sh Make does not notice the removal of <linux/device.h>. [Future Make] $ make-latest allnoconfig vmlinux [ full build ] $ rm include/linux/device.h $ make-latest vmlinux CC arch/x86/kernel/asm-offsets.s In file included from ./include/linux/writeback.h:13, from ./include/linux/memcontrol.h:22, from ./include/linux/swap.h:9, from ./include/linux/suspend.h:5, from arch/x86/kernel/asm-offsets.c:13: ./include/linux/blk_types.h:11:10: fatal error: linux/device.h: No such file or directory 11 | #include <linux/device.h> | ^~~~~~~~~~~~~~~~ compilation terminated. make-latest[1]: *** [scripts/Makefile.build:114: arch/x86/kernel/asm-offsets.s] Error 1 make-latest: *** [Makefile:1282: prepare0] Error 2 Make notices the removal of <linux/device.h>, and rebuilds objects that depended on <linux/device.h>. There exists a source file that includes <linux/device.h>, and it raises an error. To see detailed background information, refer to commit2d3b1b8f0d
("kbuild: drop $(wildcard $^) check in if_changed* for faster rebuild"). [1]: https://www.gnu.org/software/make/manual/make.html#Chained-Rules [2]: https://savannah.gnu.org/bugs/?55532 [3]: https://savannah.gnu.org/bugs/?63417 Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
274 lines
9.6 KiB
Plaintext
274 lines
9.6 KiB
Plaintext
# SPDX-License-Identifier: GPL-2.0
|
|
####
|
|
# kbuild: Generic definitions
|
|
|
|
# Convenient variables
|
|
comma := ,
|
|
quote := "
|
|
squote := '
|
|
empty :=
|
|
space := $(empty) $(empty)
|
|
space_escape := _-_SPACE_-_
|
|
pound := \#
|
|
define newline
|
|
|
|
|
|
endef
|
|
|
|
###
|
|
# Comparison macros.
|
|
# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
|
|
#
|
|
# Use $(intcmp ...) if supported. (Make >= 4.4)
|
|
# Otherwise, fall back to the 'test' shell command.
|
|
ifeq ($(intcmp 1,0,,,y),y)
|
|
test-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
|
|
test-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
|
|
else
|
|
test-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
|
|
test-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
|
|
endif
|
|
test-le = $(call test-ge, $2, $1)
|
|
test-lt = $(call test-gt, $2, $1)
|
|
|
|
###
|
|
# 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)
|
|
|
|
###
|
|
# The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
|
|
kbuild-dir = $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
|
|
kbuild-file = $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
|
|
|
|
###
|
|
# Read a file, replacing newlines with spaces
|
|
#
|
|
# Make 4.2 or later can read a file by using its builtin function.
|
|
ifneq ($(filter-out 3.% 4.0 4.1, $(MAKE_VERSION)),)
|
|
read-file = $(subst $(newline),$(space),$(file < $1))
|
|
else
|
|
read-file = $(shell cat $1 2>/dev/null)
|
|
endif
|
|
|
|
###
|
|
# 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.
|
|
# 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
|
|
#
|
|
# .NOTINTERMEDIATE is more correct, but only available on newer Make versions.
|
|
# Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the
|
|
# global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4.
|
|
# Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect
|
|
# deleted files.
|
|
ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),)
|
|
.NOTINTERMEDIATE:
|
|
else
|
|
.SECONDARY:
|
|
endif
|