xref: /linux/scripts/Kbuild.include (revision fccb3d3eda8d19b893e1fd18e8c70b78784b2a72)
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###
15*fccb3d3eSMasahiro Yamada# Comparison macros.
16*fccb3d3eSMasahiro Yamada# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
17*fccb3d3eSMasahiro Yamada#
18*fccb3d3eSMasahiro Yamada# Use $(intcmp ...) if supported. (Make >= 4.4)
19*fccb3d3eSMasahiro Yamada# Otherwise, fall back to the 'test' shell command.
20*fccb3d3eSMasahiro Yamadaifeq ($(intcmp 1,0,,,y),y)
21*fccb3d3eSMasahiro Yamadatest-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
22*fccb3d3eSMasahiro Yamadatest-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
23*fccb3d3eSMasahiro Yamadaelse
24*fccb3d3eSMasahiro Yamadatest-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
25*fccb3d3eSMasahiro Yamadatest-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
26*fccb3d3eSMasahiro Yamadaendif
27*fccb3d3eSMasahiro Yamadatest-le = $(call test-ge, $2, $1)
28*fccb3d3eSMasahiro Yamadatest-lt = $(call test-gt, $2, $1)
29*fccb3d3eSMasahiro Yamada
30*fccb3d3eSMasahiro Yamada###
3148f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
3248f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@)
3348f1f058SSam Ravnborg
3448f1f058SSam Ravnborg###
35c25e1c55SMasahiro Yamada# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
36c25e1c55SMasahiro Yamadatmp-target = $(dir $@).tmp_$(notdir $@)
37c25e1c55SMasahiro Yamada
38c25e1c55SMasahiro Yamada###
3930a77297SMasahiro Yamada# The temporary file to save gcc -MMD generated dependencies must not
408ec4b4ffSSam Ravnborg# contain a comma
4148f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d)
428ec4b4ffSSam Ravnborg
438ec4b4ffSSam Ravnborg###
445e8d780dSSam Ravnborg# filename of target with directory and extension stripped
455e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@))
465e8d780dSSam Ravnborg
475e8d780dSSam Ravnborg###
48afa974b7SMasahiro Yamada# real prerequisites without phony targets
49afa974b7SMasahiro Yamadareal-prereqs = $(filter-out $(PHONY), $^)
50afa974b7SMasahiro Yamada
51afa974b7SMasahiro Yamada###
52d51bfb78SSam Ravnborg# Escape single quote for use in echo statements
53d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1)
54d51bfb78SSam Ravnborg
55d51bfb78SSam Ravnborg###
567e826c44SMasahiro Yamada# Quote a string to pass it to C files. foo => '"foo"'
577e826c44SMasahiro Yamadastringify = $(squote)$(quote)$1$(quote)$(squote)
587e826c44SMasahiro Yamada
597e826c44SMasahiro Yamada###
60a2430b25SMasahiro Yamada# The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
61a2430b25SMasahiro Yamadakbuild-dir = $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
62a2430b25SMasahiro Yamadakbuild-file = $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
63a2430b25SMasahiro Yamada
64a2430b25SMasahiro Yamada###
655410ecc0SMike Frysinger# Easy method for doing a status message
665410ecc0SMike Frysinger       kecho := :
675410ecc0SMike Frysinger quiet_kecho := echo
685410ecc0SMike Frysingersilent_kecho := :
695410ecc0SMike Frysingerkecho := $($(quiet)kecho)
705410ecc0SMike Frysinger
715410ecc0SMike Frysinger###
728ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated.
738ec4b4ffSSam Ravnborg# Sample usage:
74ba97df45SMasahiro Yamada#
75ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE)
76ba97df45SMasahiro Yamada# version.h: FORCE
778ec4b4ffSSam Ravnborg#	$(call filechk,sample)
78ba97df45SMasahiro Yamada#
798ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file.
808ec4b4ffSSam Ravnborg# The existing file will be compared with the new one.
818ec4b4ffSSam Ravnborg# - If no file exist it is created
828ec4b4ffSSam Ravnborg# - If the content differ the new file is used
838ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update
848ec4b4ffSSam Ravnborgdefine filechk
85e1f86d7bSMasahiro Yamada	$(check-FORCE)
868ec4b4ffSSam Ravnborg	$(Q)set -e;						\
878ec4b4ffSSam Ravnborg	mkdir -p $(dir $@);					\
8888fe89a4SMasahiro Yamada	trap "rm -f $(dot-target).tmp" EXIT;			\
8988fe89a4SMasahiro Yamada	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
9088fe89a4SMasahiro Yamada	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
91fd54f502SMike Frysinger		$(kecho) '  UPD     $@';			\
9288fe89a4SMasahiro Yamada		mv -f $(dot-target).tmp $@;			\
938ec4b4ffSSam Ravnborg	fi
948ec4b4ffSSam Ravnborgendef
958ec4b4ffSSam Ravnborg
96beda9f3aSRoman Zippel###
978ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
988ec4b4ffSSam Ravnborg# Usage:
998ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir
1005b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj
1018ec4b4ffSSam Ravnborg
102bc081dd6SMichal Marek###
1039fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
1049fb5e537SRobert Richter# Usage:
1059fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir
106487c7c77SMasahiro Yamadadtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
1079fb5e537SRobert Richter
108d08372caSLinus Torvalds###
109371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
110371fdc77SMasahiro Yamada# Usage:
111371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir
112371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj
113371fdc77SMasahiro Yamada
1145de043f4SOleg Verych# echo command.
1155de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
1165de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\
1175de043f4SOleg Verych	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
1185de043f4SOleg Verych
119174a1dccSMasahiro Yamada# sink stdout for 'make -s'
120174a1dccSMasahiro Yamada       redirect :=
121174a1dccSMasahiro Yamada quiet_redirect :=
122174a1dccSMasahiro Yamadasilent_redirect := exec >/dev/null;
123174a1dccSMasahiro Yamada
124a7f3257dSMasahiro Yamada# Delete the target on interruption
125a7f3257dSMasahiro Yamada#
126a7f3257dSMasahiro Yamada# GNU Make automatically deletes the target if it has already been changed by
127a7f3257dSMasahiro Yamada# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
128a7f3257dSMasahiro Yamada# will delete incomplete targets), and resume it later.
129a7f3257dSMasahiro Yamada#
130a7f3257dSMasahiro Yamada# However, this does not work when the stderr is piped to another program, like
131a7f3257dSMasahiro Yamada#  $ make >&2 | tee log
132a7f3257dSMasahiro Yamada# Make dies with SIGPIPE before cleaning the targets.
133a7f3257dSMasahiro Yamada#
134a7f3257dSMasahiro Yamada# To address it, we clean the target in signal traps.
135a7f3257dSMasahiro Yamada#
136a7f3257dSMasahiro Yamada# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
137a7f3257dSMasahiro Yamada# So, we cover them, and also SIGPIPE just in case.
138a7f3257dSMasahiro Yamada#
139a7f3257dSMasahiro Yamada# Of course, this is unneeded for phony targets.
140a7f3257dSMasahiro Yamadadelete-on-interrupt = \
141a7f3257dSMasahiro Yamada	$(if $(filter-out $(PHONY), $@), \
142a7f3257dSMasahiro Yamada		$(foreach sig, HUP INT QUIT TERM PIPE, \
143a7f3257dSMasahiro Yamada			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
144a7f3257dSMasahiro Yamada
1455de043f4SOleg Verych# printing commands
146a7f3257dSMasahiro Yamadacmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1))
1478ec4b4ffSSam Ravnborg
1488ec4b4ffSSam Ravnborg###
1498ec4b4ffSSam Ravnborg# if_changed      - execute command if any prerequisite is newer than
1508ec4b4ffSSam Ravnborg#                   target, or command line has changed
1518ec4b4ffSSam Ravnborg# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
1528ec4b4ffSSam Ravnborg#                   including used config symbols
1538ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead
154cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info
1558ec4b4ffSSam Ravnborg
1568ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1)
15750bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty
1589c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
15950bcca6aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
1609c8fa9bcSMasahiro Yamada                         $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
161c4d5ee13SMichal Marekelse
16250bcca6aSMasahiro Yamadacmd-check = $(if $(strip $(cmd_$@)),,1)
1638ec4b4ffSSam Ravnborgendif
1648ec4b4ffSSam Ravnborg
165164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file
166164f0d2eSMichal Marek# (needed for make)
1679564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
168164f0d2eSMichal Marek# (needed for make)
169164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...'
170164f0d2eSMichal Marek# (needed for the shell)
1719564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
1726176aa9aSJan Beulich
1732d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist.
1742d3b1b8fSMasahiro Yamada# (This is not true for now; $? should contain any non-existent prerequisites,
1752d3b1b8fSMasahiro Yamada# but it does not work as expected when .SECONDARY is present. This seems a bug
1762d3b1b8fSMasahiro Yamada# of GNU Make.)
17748f1f058SSam Ravnborg# PHONY targets skipped in both cases.
178eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?)
17948f1f058SSam Ravnborg
180e1f86d7bSMasahiro Yamada# It is a typical mistake to forget the FORCE prerequisite. Check it here so
181e1f86d7bSMasahiro Yamada# no more breakage will slip in.
182e1f86d7bSMasahiro Yamadacheck-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
183e1f86d7bSMasahiro Yamada
184e1f86d7bSMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
1856796e804SMasahiro Yamada
1865de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated.
187ebd191b3SMasahiro Yamadaif_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
188ebd191b3SMasahiro Yamada
189ebd191b3SMasahiro Yamadacmd_and_savecmd =                                                            \
19067126965SMasahiro Yamada	$(cmd);                                                              \
191ebd191b3SMasahiro Yamada	printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd
1928ec4b4ffSSam Ravnborg
1935de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file.
1946796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
195c1a95fdaSNicolas Pitre
196e4aca459SNicolas Pitrecmd_and_fixdep =                                                             \
1973a2429e1SMasahiro Yamada	$(cmd);                                                              \
198392885eeSMasahiro Yamada	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
199e5d28910SMasahiro Yamada	rm -f $(depfile)
200c1a95fdaSNicolas Pitre
2018ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo)
202beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed,
203beda9f3aSRoman Zippel# and if so will execute $(rule_foo).
2046796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
20548f1f058SSam Ravnborg
20645d506bdSSam Ravnborg###
207312a3d09SCao jin# why - tell why a target got built
20845d506bdSSam Ravnborg#       enabled by make V=2
20945d506bdSSam Ravnborg#       Output (listed in the order they are checked):
21045d506bdSSam Ravnborg#          (1) - due to target is PHONY
21145d506bdSSam Ravnborg#          (2) - due to target missing
21245d506bdSSam Ravnborg#          (3) - due to: file1.h file2.h
21345d506bdSSam Ravnborg#          (4) - due to command line change
21445d506bdSSam Ravnborg#          (5) - due to missing .cmd file
21545d506bdSSam Ravnborg#          (6) - due to target not in $(targets)
21645d506bdSSam Ravnborg# (1) PHONY targets are always build
21745d506bdSSam Ravnborg# (2) No target, so we better build it
21845d506bdSSam Ravnborg# (3) Prerequisite is newer than target
21945d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd
22045d506bdSSam Ravnborg#     differed from actual command line. This happens when compiler
22145d506bdSSam Ravnborg#     options changes
22245d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line)
22345d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets)
22445d506bdSSam Ravnborg#     This is a good hint that there is a bug in the kbuild file
22545d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2)
22645d506bdSSam Ravnborgwhy =                                                                        \
22745d506bdSSam Ravnborg    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
22845d506bdSSam Ravnborg        $(if $(wildcard $@),                                                 \
229eba19032SMasahiro Yamada            $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
23050bcca6aSMasahiro Yamada                $(if $(cmd-check),                                           \
23145d506bdSSam Ravnborg                    $(if $(cmd_$@),- due to command line change,             \
23245d506bdSSam Ravnborg                        $(if $(filter $@, $(targets)),                       \
23345d506bdSSam Ravnborg                            - due to missing .cmd file,                      \
23445d506bdSSam Ravnborg                            - due to $(notdir $@) not in $$(targets)         \
23545d506bdSSam Ravnborg                         )                                                   \
23645d506bdSSam Ravnborg                     )                                                       \
23745d506bdSSam Ravnborg                 )                                                           \
23845d506bdSSam Ravnborg             ),                                                              \
23945d506bdSSam Ravnborg             - due to target missing                                         \
24045d506bdSSam Ravnborg         )                                                                   \
24145d506bdSSam Ravnborg     )
24245d506bdSSam Ravnborg
24345d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why)))
24445d506bdSSam Ravnborgendif
2453ee550f1SDavid Woodhouse
2463ee550f1SDavid Woodhouse###############################################################################
2479c2af1c7SMasahiro Yamada
2489c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error
2499c2af1c7SMasahiro Yamada.DELETE_ON_ERROR:
2508e9b61b2SMasahiro Yamada
2518e9b61b2SMasahiro Yamada# do not delete intermediate files automatically
2528e9b61b2SMasahiro Yamada.SECONDARY:
253