xref: /openbmc/linux/scripts/Kbuild.include (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
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.
65a2430b25SMasahiro Yamadakbuild-dir = $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
66a2430b25SMasahiro Yamadakbuild-file = $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
67a2430b25SMasahiro Yamada
68a2430b25SMasahiro Yamada###
696768fa4bSMasahiro Yamada# Read a file, replacing newlines with spaces
706768fa4bSMasahiro Yamada#
716768fa4bSMasahiro Yamada# Make 4.2 or later can read a file by using its builtin function.
726768fa4bSMasahiro Yamadaifneq ($(filter-out 3.% 4.0 4.1, $(MAKE_VERSION)),)
736768fa4bSMasahiro Yamadaread-file = $(subst $(newline),$(space),$(file < $1))
746768fa4bSMasahiro Yamadaelse
756768fa4bSMasahiro Yamadaread-file = $(shell cat $1 2>/dev/null)
766768fa4bSMasahiro Yamadaendif
776768fa4bSMasahiro Yamada
786768fa4bSMasahiro Yamada###
795410ecc0SMike Frysinger# Easy method for doing a status message
805410ecc0SMike Frysinger       kecho := :
815410ecc0SMike Frysinger quiet_kecho := echo
825410ecc0SMike Frysingersilent_kecho := :
835410ecc0SMike Frysingerkecho := $($(quiet)kecho)
845410ecc0SMike Frysinger
855410ecc0SMike Frysinger###
868ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated.
878ec4b4ffSSam Ravnborg# Sample usage:
88ba97df45SMasahiro Yamada#
89ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE)
90ba97df45SMasahiro Yamada# version.h: FORCE
918ec4b4ffSSam Ravnborg#	$(call filechk,sample)
92ba97df45SMasahiro Yamada#
938ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file.
948ec4b4ffSSam Ravnborg# The existing file will be compared with the new one.
958ec4b4ffSSam Ravnborg# - If no file exist it is created
968ec4b4ffSSam Ravnborg# - If the content differ the new file is used
978ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update
988ec4b4ffSSam Ravnborgdefine filechk
99e1f86d7bSMasahiro Yamada	$(check-FORCE)
1008ec4b4ffSSam Ravnborg	$(Q)set -e;						\
1018ec4b4ffSSam Ravnborg	mkdir -p $(dir $@);					\
102*12fec3d6SMasahiro Yamada	trap "rm -f $(tmp-target)" EXIT;			\
103*12fec3d6SMasahiro Yamada	{ $(filechk_$(1)); } > $(tmp-target);			\
104*12fec3d6SMasahiro Yamada	if [ ! -r $@ ] || ! cmp -s $@ $(tmp-target); then	\
105fd54f502SMike Frysinger		$(kecho) '  UPD     $@';			\
106*12fec3d6SMasahiro Yamada		mv -f $(tmp-target) $@;				\
1078ec4b4ffSSam Ravnborg	fi
1088ec4b4ffSSam Ravnborgendef
1098ec4b4ffSSam Ravnborg
110beda9f3aSRoman Zippel###
1118ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
1128ec4b4ffSSam Ravnborg# Usage:
1138ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir
1145b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj
1158ec4b4ffSSam Ravnborg
116bc081dd6SMichal Marek###
1179fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
1189fb5e537SRobert Richter# Usage:
1199fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir
120487c7c77SMasahiro Yamadadtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
1219fb5e537SRobert Richter
122d08372caSLinus Torvalds###
123371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
124371fdc77SMasahiro Yamada# Usage:
125371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir
126371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj
127371fdc77SMasahiro Yamada
1288962b6b4SMasahiro Yamada# pring log
1298962b6b4SMasahiro Yamada#
1308962b6b4SMasahiro Yamada# If quiet is "silent_", print nothing and sink stdout
1318962b6b4SMasahiro Yamada# If quiet is "quiet_", print short log
1328962b6b4SMasahiro Yamada# If quiet is empty, print short log and whole command
1338962b6b4SMasahiro Yamadasilent_log_print = exec >/dev/null;
1348962b6b4SMasahiro Yamada quiet_log_print = $(if $(quiet_cmd_$1), echo '  $(call escsq,$(quiet_cmd_$1)$(why))';)
1356ae4b986SMasahiro Yamada       log_print = echo '$(pound) $(call escsq,$(or $(quiet_cmd_$1),cmd_$1 $@)$(why))'; \
1368962b6b4SMasahiro Yamada                   echo '  $(call escsq,$(cmd_$1))';
137174a1dccSMasahiro Yamada
138a7f3257dSMasahiro Yamada# Delete the target on interruption
139a7f3257dSMasahiro Yamada#
140a7f3257dSMasahiro Yamada# GNU Make automatically deletes the target if it has already been changed by
141a7f3257dSMasahiro Yamada# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
142a7f3257dSMasahiro Yamada# will delete incomplete targets), and resume it later.
143a7f3257dSMasahiro Yamada#
144a7f3257dSMasahiro Yamada# However, this does not work when the stderr is piped to another program, like
145a7f3257dSMasahiro Yamada#  $ make >&2 | tee log
146a7f3257dSMasahiro Yamada# Make dies with SIGPIPE before cleaning the targets.
147a7f3257dSMasahiro Yamada#
148a7f3257dSMasahiro Yamada# To address it, we clean the target in signal traps.
149a7f3257dSMasahiro Yamada#
150a7f3257dSMasahiro Yamada# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
151a7f3257dSMasahiro Yamada# So, we cover them, and also SIGPIPE just in case.
152a7f3257dSMasahiro Yamada#
153a7f3257dSMasahiro Yamada# Of course, this is unneeded for phony targets.
154a7f3257dSMasahiro Yamadadelete-on-interrupt = \
155a7f3257dSMasahiro Yamada	$(if $(filter-out $(PHONY), $@), \
156a7f3257dSMasahiro Yamada		$(foreach sig, HUP INT QUIT TERM PIPE, \
157a7f3257dSMasahiro Yamada			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
158a7f3257dSMasahiro Yamada
1598962b6b4SMasahiro Yamada# print and execute commands
1608962b6b4SMasahiro Yamadacmd = @$(if $(cmd_$(1)),set -e; $($(quiet)log_print) $(delete-on-interrupt) $(cmd_$(1)),:)
1618ec4b4ffSSam Ravnborg
1628ec4b4ffSSam Ravnborg###
1638ec4b4ffSSam Ravnborg# if_changed      - execute command if any prerequisite is newer than
1648ec4b4ffSSam Ravnborg#                   target, or command line has changed
1658ec4b4ffSSam Ravnborg# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
1668ec4b4ffSSam Ravnborg#                   including used config symbols
1678ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead
168cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info
1698ec4b4ffSSam Ravnborg
1708ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1)
17150bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty
1729c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
173ee2162bdSMasahiro Yamada# If the target does not exist, the *.cmd file should not be included so
174ee2162bdSMasahiro Yamada# $(savedcmd_$@) gets empty. Then, target will be built even if $(newer-prereqs)
175ee2162bdSMasahiro Yamada# happens to become empty.
17692215e7aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(savedcmd_$@))), \
1779c8fa9bcSMasahiro Yamada                         $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
178c4d5ee13SMichal Marekelse
179ee2162bdSMasahiro Yamada# We still need to detect missing targets.
18092215e7aSMasahiro Yamadacmd-check = $(if $(strip $(savedcmd_$@)),,1)
1818ec4b4ffSSam Ravnborgendif
1828ec4b4ffSSam Ravnborg
183164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file
184164f0d2eSMichal Marek# (needed for make)
1859564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
186164f0d2eSMichal Marek# (needed for make)
187164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...'
188164f0d2eSMichal Marek# (needed for the shell)
1899564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
1906176aa9aSJan Beulich
1912d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist.
19248f1f058SSam Ravnborg# PHONY targets skipped in both cases.
193ee2162bdSMasahiro Yamada# If there is no prerequisite other than phony targets, $(newer-prereqs) becomes
194ee2162bdSMasahiro Yamada# empty even if the target does not exist. cmd-check saves this corner case.
195eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?)
19648f1f058SSam Ravnborg
197e1f86d7bSMasahiro Yamada# It is a typical mistake to forget the FORCE prerequisite. Check it here so
198e1f86d7bSMasahiro Yamada# no more breakage will slip in.
199e1f86d7bSMasahiro Yamadacheck-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
200e1f86d7bSMasahiro Yamada
201e1f86d7bSMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
2026796e804SMasahiro Yamada
2035de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated.
204ebd191b3SMasahiro Yamadaif_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
205ebd191b3SMasahiro Yamada
206ebd191b3SMasahiro Yamadacmd_and_savecmd =                                                            \
20767126965SMasahiro Yamada	$(cmd);                                                              \
20892215e7aSMasahiro Yamada	printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd
2098ec4b4ffSSam Ravnborg
2105de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file.
2116796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
212c1a95fdaSNicolas Pitre
213e4aca459SNicolas Pitrecmd_and_fixdep =                                                             \
2143a2429e1SMasahiro Yamada	$(cmd);                                                              \
215392885eeSMasahiro Yamada	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
216e5d28910SMasahiro Yamada	rm -f $(depfile)
217c1a95fdaSNicolas Pitre
2188ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo)
219beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed,
220beda9f3aSRoman Zippel# and if so will execute $(rule_foo).
2216796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
22248f1f058SSam Ravnborg
22345d506bdSSam Ravnborg###
224312a3d09SCao jin# why - tell why a target got built
22545d506bdSSam Ravnborg#       enabled by make V=2
22645d506bdSSam Ravnborg#       Output (listed in the order they are checked):
22745d506bdSSam Ravnborg#          (1) - due to target is PHONY
22845d506bdSSam Ravnborg#          (2) - due to target missing
22945d506bdSSam Ravnborg#          (3) - due to: file1.h file2.h
23045d506bdSSam Ravnborg#          (4) - due to command line change
23145d506bdSSam Ravnborg#          (5) - due to missing .cmd file
23245d506bdSSam Ravnborg#          (6) - due to target not in $(targets)
23345d506bdSSam Ravnborg# (1) PHONY targets are always build
23445d506bdSSam Ravnborg# (2) No target, so we better build it
23545d506bdSSam Ravnborg# (3) Prerequisite is newer than target
23645d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd
23745d506bdSSam Ravnborg#     differed from actual command line. This happens when compiler
23845d506bdSSam Ravnborg#     options changes
23945d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line)
24045d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets)
24145d506bdSSam Ravnborg#     This is a good hint that there is a bug in the kbuild file
2426ae4b986SMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_VERBOSE)),)
2438962b6b4SMasahiro Yamada_why =                                                                        \
24445d506bdSSam Ravnborg    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
24545d506bdSSam Ravnborg        $(if $(wildcard $@),                                                 \
246eba19032SMasahiro Yamada            $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
24750bcca6aSMasahiro Yamada                $(if $(cmd-check),                                           \
24892215e7aSMasahiro Yamada                    $(if $(savedcmd_$@),- due to command line change,        \
24945d506bdSSam Ravnborg                        $(if $(filter $@, $(targets)),                       \
25045d506bdSSam Ravnborg                            - due to missing .cmd file,                      \
25145d506bdSSam Ravnborg                            - due to $(notdir $@) not in $$(targets)         \
25245d506bdSSam Ravnborg                         )                                                   \
25345d506bdSSam Ravnborg                     )                                                       \
25445d506bdSSam Ravnborg                 )                                                           \
25545d506bdSSam Ravnborg             ),                                                              \
25645d506bdSSam Ravnborg             - due to target missing                                         \
25745d506bdSSam Ravnborg         )                                                                   \
25845d506bdSSam Ravnborg     )
25945d506bdSSam Ravnborg
2608962b6b4SMasahiro Yamadawhy = $(space)$(strip $(_why))
26145d506bdSSam Ravnborgendif
2623ee550f1SDavid Woodhouse
2633ee550f1SDavid Woodhouse###############################################################################
2649c2af1c7SMasahiro Yamada
2659c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error
2669c2af1c7SMasahiro Yamada.DELETE_ON_ERROR:
2678e9b61b2SMasahiro Yamada
2688e9b61b2SMasahiro Yamada# do not delete intermediate files automatically
269875ef1a5SMasahiro Yamada#
270875ef1a5SMasahiro Yamada# .NOTINTERMEDIATE is more correct, but only available on newer Make versions.
271875ef1a5SMasahiro Yamada# Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the
272875ef1a5SMasahiro Yamada# global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4.
273875ef1a5SMasahiro Yamada# Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect
274875ef1a5SMasahiro Yamada# deleted files.
275875ef1a5SMasahiro Yamadaifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),)
276875ef1a5SMasahiro Yamada.NOTINTERMEDIATE:
277875ef1a5SMasahiro Yamadaelse
2788e9b61b2SMasahiro Yamada.SECONDARY:
279875ef1a5SMasahiro Yamadaendif
280