xref: /linux-6.15/scripts/Kbuild.include (revision fccb3d3e)
1# SPDX-License-Identifier: GPL-2.0
2####
3# kbuild: Generic definitions
4
5# Convenient variables
6comma   := ,
7quote   := "
8squote  := '
9empty   :=
10space   := $(empty) $(empty)
11space_escape := _-_SPACE_-_
12pound := \#
13
14###
15# Comparison macros.
16# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
17#
18# Use $(intcmp ...) if supported. (Make >= 4.4)
19# Otherwise, fall back to the 'test' shell command.
20ifeq ($(intcmp 1,0,,,y),y)
21test-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
22test-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
23else
24test-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
25test-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
26endif
27test-le = $(call test-ge, $2, $1)
28test-lt = $(call test-gt, $2, $1)
29
30###
31# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
32dot-target = $(dir $@).$(notdir $@)
33
34###
35# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
36tmp-target = $(dir $@).tmp_$(notdir $@)
37
38###
39# The temporary file to save gcc -MMD generated dependencies must not
40# contain a comma
41depfile = $(subst $(comma),_,$(dot-target).d)
42
43###
44# filename of target with directory and extension stripped
45basetarget = $(basename $(notdir $@))
46
47###
48# real prerequisites without phony targets
49real-prereqs = $(filter-out $(PHONY), $^)
50
51###
52# Escape single quote for use in echo statements
53escsq = $(subst $(squote),'\$(squote)',$1)
54
55###
56# Quote a string to pass it to C files. foo => '"foo"'
57stringify = $(squote)$(quote)$1$(quote)$(squote)
58
59###
60# The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
61kbuild-dir = $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
62kbuild-file = $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
63
64###
65# Easy method for doing a status message
66       kecho := :
67 quiet_kecho := echo
68silent_kecho := :
69kecho := $($(quiet)kecho)
70
71###
72# filechk is used to check if the content of a generated file is updated.
73# Sample usage:
74#
75# filechk_sample = echo $(KERNELRELEASE)
76# version.h: FORCE
77#	$(call filechk,sample)
78#
79# The rule defined shall write to stdout the content of the new file.
80# The existing file will be compared with the new one.
81# - If no file exist it is created
82# - If the content differ the new file is used
83# - If they are equal no change, and no timestamp update
84define filechk
85	$(check-FORCE)
86	$(Q)set -e;						\
87	mkdir -p $(dir $@);					\
88	trap "rm -f $(dot-target).tmp" EXIT;			\
89	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
90	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
91		$(kecho) '  UPD     $@';			\
92		mv -f $(dot-target).tmp $@;			\
93	fi
94endef
95
96###
97# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
98# Usage:
99# $(Q)$(MAKE) $(build)=dir
100build := -f $(srctree)/scripts/Makefile.build obj
101
102###
103# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
104# Usage:
105# $(Q)$(MAKE) $(dtbinst)=dir
106dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
107
108###
109# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
110# Usage:
111# $(Q)$(MAKE) $(clean)=dir
112clean := -f $(srctree)/scripts/Makefile.clean obj
113
114# echo command.
115# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
116echo-cmd = $(if $($(quiet)cmd_$(1)),\
117	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
118
119# sink stdout for 'make -s'
120       redirect :=
121 quiet_redirect :=
122silent_redirect := exec >/dev/null;
123
124# Delete the target on interruption
125#
126# GNU Make automatically deletes the target if it has already been changed by
127# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
128# will delete incomplete targets), and resume it later.
129#
130# However, this does not work when the stderr is piped to another program, like
131#  $ make >&2 | tee log
132# Make dies with SIGPIPE before cleaning the targets.
133#
134# To address it, we clean the target in signal traps.
135#
136# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
137# So, we cover them, and also SIGPIPE just in case.
138#
139# Of course, this is unneeded for phony targets.
140delete-on-interrupt = \
141	$(if $(filter-out $(PHONY), $@), \
142		$(foreach sig, HUP INT QUIT TERM PIPE, \
143			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
144
145# printing commands
146cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1))
147
148###
149# if_changed      - execute command if any prerequisite is newer than
150#                   target, or command line has changed
151# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
152#                   including used config symbols
153# if_changed_rule - as if_changed but execute rule instead
154# See Documentation/kbuild/makefiles.rst for more info
155
156ifneq ($(KBUILD_NOCMDDEP),1)
157# Check if both commands are the same including their order. Result is empty
158# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
159cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
160                         $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
161else
162cmd-check = $(if $(strip $(cmd_$@)),,1)
163endif
164
165# Replace >$< with >$$< to preserve $ when reloading the .cmd file
166# (needed for make)
167# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
168# (needed for make)
169# Replace >'< with >'\''< to be able to enclose the whole string in '...'
170# (needed for the shell)
171make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
172
173# Find any prerequisites that are newer than target or that do not exist.
174# (This is not true for now; $? should contain any non-existent prerequisites,
175# but it does not work as expected when .SECONDARY is present. This seems a bug
176# of GNU Make.)
177# PHONY targets skipped in both cases.
178newer-prereqs = $(filter-out $(PHONY),$?)
179
180# It is a typical mistake to forget the FORCE prerequisite. Check it here so
181# no more breakage will slip in.
182check-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
183
184if-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
185
186# Execute command if command has changed or prerequisite(s) are updated.
187if_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
188
189cmd_and_savecmd =                                                            \
190	$(cmd);                                                              \
191	printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd
192
193# Execute the command and also postprocess generated .d dependencies file.
194if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
195
196cmd_and_fixdep =                                                             \
197	$(cmd);                                                              \
198	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
199	rm -f $(depfile)
200
201# Usage: $(call if_changed_rule,foo)
202# Will check if $(cmd_foo) or any of the prerequisites changed,
203# and if so will execute $(rule_foo).
204if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
205
206###
207# why - tell why a target got built
208#       enabled by make V=2
209#       Output (listed in the order they are checked):
210#          (1) - due to target is PHONY
211#          (2) - due to target missing
212#          (3) - due to: file1.h file2.h
213#          (4) - due to command line change
214#          (5) - due to missing .cmd file
215#          (6) - due to target not in $(targets)
216# (1) PHONY targets are always build
217# (2) No target, so we better build it
218# (3) Prerequisite is newer than target
219# (4) The command line stored in the file named dir/.target.cmd
220#     differed from actual command line. This happens when compiler
221#     options changes
222# (5) No dir/.target.cmd file (used to store command line)
223# (6) No dir/.target.cmd file and target not listed in $(targets)
224#     This is a good hint that there is a bug in the kbuild file
225ifeq ($(KBUILD_VERBOSE),2)
226why =                                                                        \
227    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
228        $(if $(wildcard $@),                                                 \
229            $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
230                $(if $(cmd-check),                                           \
231                    $(if $(cmd_$@),- due to command line change,             \
232                        $(if $(filter $@, $(targets)),                       \
233                            - due to missing .cmd file,                      \
234                            - due to $(notdir $@) not in $$(targets)         \
235                         )                                                   \
236                     )                                                       \
237                 )                                                           \
238             ),                                                              \
239             - due to target missing                                         \
240         )                                                                   \
241     )
242
243echo-why = $(call escsq, $(strip $(why)))
244endif
245
246###############################################################################
247
248# delete partially updated (i.e. corrupted) files on error
249.DELETE_ON_ERROR:
250
251# do not delete intermediate files automatically
252.SECONDARY:
253