xref: /linux/scripts/Kbuild.include (revision a7f3257da8a86b96fb9bf1bba40ae0bbd7f1885a)
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###
1548f1f058SSam Ravnborg# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
1648f1f058SSam Ravnborgdot-target = $(dir $@).$(notdir $@)
1748f1f058SSam Ravnborg
1848f1f058SSam Ravnborg###
19c25e1c55SMasahiro Yamada# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
20c25e1c55SMasahiro Yamadatmp-target = $(dir $@).tmp_$(notdir $@)
21c25e1c55SMasahiro Yamada
22c25e1c55SMasahiro Yamada###
2330a77297SMasahiro Yamada# The temporary file to save gcc -MMD generated dependencies must not
248ec4b4ffSSam Ravnborg# contain a comma
2548f1f058SSam Ravnborgdepfile = $(subst $(comma),_,$(dot-target).d)
268ec4b4ffSSam Ravnborg
278ec4b4ffSSam Ravnborg###
285e8d780dSSam Ravnborg# filename of target with directory and extension stripped
295e8d780dSSam Ravnborgbasetarget = $(basename $(notdir $@))
305e8d780dSSam Ravnborg
315e8d780dSSam Ravnborg###
32afa974b7SMasahiro Yamada# real prerequisites without phony targets
33afa974b7SMasahiro Yamadareal-prereqs = $(filter-out $(PHONY), $^)
34afa974b7SMasahiro Yamada
35afa974b7SMasahiro Yamada###
36d51bfb78SSam Ravnborg# Escape single quote for use in echo statements
37d51bfb78SSam Ravnborgescsq = $(subst $(squote),'\$(squote)',$1)
38d51bfb78SSam Ravnborg
39d51bfb78SSam Ravnborg###
407e826c44SMasahiro Yamada# Quote a string to pass it to C files. foo => '"foo"'
417e826c44SMasahiro Yamadastringify = $(squote)$(quote)$1$(quote)$(squote)
427e826c44SMasahiro Yamada
437e826c44SMasahiro Yamada###
445410ecc0SMike Frysinger# Easy method for doing a status message
455410ecc0SMike Frysinger       kecho := :
465410ecc0SMike Frysinger quiet_kecho := echo
475410ecc0SMike Frysingersilent_kecho := :
485410ecc0SMike Frysingerkecho := $($(quiet)kecho)
495410ecc0SMike Frysinger
505410ecc0SMike Frysinger###
518ec4b4ffSSam Ravnborg# filechk is used to check if the content of a generated file is updated.
528ec4b4ffSSam Ravnborg# Sample usage:
53ba97df45SMasahiro Yamada#
54ba97df45SMasahiro Yamada# filechk_sample = echo $(KERNELRELEASE)
55ba97df45SMasahiro Yamada# version.h: FORCE
568ec4b4ffSSam Ravnborg#	$(call filechk,sample)
57ba97df45SMasahiro Yamada#
588ec4b4ffSSam Ravnborg# The rule defined shall write to stdout the content of the new file.
598ec4b4ffSSam Ravnborg# The existing file will be compared with the new one.
608ec4b4ffSSam Ravnborg# - If no file exist it is created
618ec4b4ffSSam Ravnborg# - If the content differ the new file is used
628ec4b4ffSSam Ravnborg# - If they are equal no change, and no timestamp update
638ec4b4ffSSam Ravnborgdefine filechk
64e1f86d7bSMasahiro Yamada	$(check-FORCE)
658ec4b4ffSSam Ravnborg	$(Q)set -e;						\
668ec4b4ffSSam Ravnborg	mkdir -p $(dir $@);					\
6788fe89a4SMasahiro Yamada	trap "rm -f $(dot-target).tmp" EXIT;			\
6888fe89a4SMasahiro Yamada	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
6988fe89a4SMasahiro Yamada	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
70fd54f502SMike Frysinger		$(kecho) '  UPD     $@';			\
7188fe89a4SMasahiro Yamada		mv -f $(dot-target).tmp $@;			\
728ec4b4ffSSam Ravnborg	fi
738ec4b4ffSSam Ravnborgendef
748ec4b4ffSSam Ravnborg
75beda9f3aSRoman Zippel###
768ec4b4ffSSam Ravnborg# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
778ec4b4ffSSam Ravnborg# Usage:
788ec4b4ffSSam Ravnborg# $(Q)$(MAKE) $(build)=dir
795b2389b4SMasahiro Yamadabuild := -f $(srctree)/scripts/Makefile.build obj
808ec4b4ffSSam Ravnborg
81bc081dd6SMichal Marek###
829fb5e537SRobert Richter# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
839fb5e537SRobert Richter# Usage:
849fb5e537SRobert Richter# $(Q)$(MAKE) $(dtbinst)=dir
85487c7c77SMasahiro Yamadadtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
869fb5e537SRobert Richter
87d08372caSLinus Torvalds###
88371fdc77SMasahiro Yamada# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
89371fdc77SMasahiro Yamada# Usage:
90371fdc77SMasahiro Yamada# $(Q)$(MAKE) $(clean)=dir
91371fdc77SMasahiro Yamadaclean := -f $(srctree)/scripts/Makefile.clean obj
92371fdc77SMasahiro Yamada
935de043f4SOleg Verych# echo command.
945de043f4SOleg Verych# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
955de043f4SOleg Verychecho-cmd = $(if $($(quiet)cmd_$(1)),\
965de043f4SOleg Verych	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
975de043f4SOleg Verych
98174a1dccSMasahiro Yamada# sink stdout for 'make -s'
99174a1dccSMasahiro Yamada       redirect :=
100174a1dccSMasahiro Yamada quiet_redirect :=
101174a1dccSMasahiro Yamadasilent_redirect := exec >/dev/null;
102174a1dccSMasahiro Yamada
103*a7f3257dSMasahiro Yamada# Delete the target on interruption
104*a7f3257dSMasahiro Yamada#
105*a7f3257dSMasahiro Yamada# GNU Make automatically deletes the target if it has already been changed by
106*a7f3257dSMasahiro Yamada# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
107*a7f3257dSMasahiro Yamada# will delete incomplete targets), and resume it later.
108*a7f3257dSMasahiro Yamada#
109*a7f3257dSMasahiro Yamada# However, this does not work when the stderr is piped to another program, like
110*a7f3257dSMasahiro Yamada#  $ make >&2 | tee log
111*a7f3257dSMasahiro Yamada# Make dies with SIGPIPE before cleaning the targets.
112*a7f3257dSMasahiro Yamada#
113*a7f3257dSMasahiro Yamada# To address it, we clean the target in signal traps.
114*a7f3257dSMasahiro Yamada#
115*a7f3257dSMasahiro Yamada# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
116*a7f3257dSMasahiro Yamada# So, we cover them, and also SIGPIPE just in case.
117*a7f3257dSMasahiro Yamada#
118*a7f3257dSMasahiro Yamada# Of course, this is unneeded for phony targets.
119*a7f3257dSMasahiro Yamadadelete-on-interrupt = \
120*a7f3257dSMasahiro Yamada	$(if $(filter-out $(PHONY), $@), \
121*a7f3257dSMasahiro Yamada		$(foreach sig, HUP INT QUIT TERM PIPE, \
122*a7f3257dSMasahiro Yamada			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
123*a7f3257dSMasahiro Yamada
1245de043f4SOleg Verych# printing commands
125*a7f3257dSMasahiro Yamadacmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1))
1268ec4b4ffSSam Ravnborg
1278ec4b4ffSSam Ravnborg###
1288ec4b4ffSSam Ravnborg# if_changed      - execute command if any prerequisite is newer than
1298ec4b4ffSSam Ravnborg#                   target, or command line has changed
1308ec4b4ffSSam Ravnborg# if_changed_dep  - as if_changed, but uses fixdep to reveal dependencies
1318ec4b4ffSSam Ravnborg#                   including used config symbols
1328ec4b4ffSSam Ravnborg# if_changed_rule - as if_changed but execute rule instead
133cd238effSMauro Carvalho Chehab# See Documentation/kbuild/makefiles.rst for more info
1348ec4b4ffSSam Ravnborg
1358ec4b4ffSSam Ravnborgifneq ($(KBUILD_NOCMDDEP),1)
13650bcca6aSMasahiro Yamada# Check if both commands are the same including their order. Result is empty
1379c8fa9bcSMasahiro Yamada# string if equal. User may override this check using make KBUILD_NOCMDDEP=1
13850bcca6aSMasahiro Yamadacmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
1399c8fa9bcSMasahiro Yamada                         $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
140c4d5ee13SMichal Marekelse
14150bcca6aSMasahiro Yamadacmd-check = $(if $(strip $(cmd_$@)),,1)
1428ec4b4ffSSam Ravnborgendif
1438ec4b4ffSSam Ravnborg
144164f0d2eSMichal Marek# Replace >$< with >$$< to preserve $ when reloading the .cmd file
145164f0d2eSMichal Marek# (needed for make)
1469564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
147164f0d2eSMichal Marek# (needed for make)
148164f0d2eSMichal Marek# Replace >'< with >'\''< to be able to enclose the whole string in '...'
149164f0d2eSMichal Marek# (needed for the shell)
1509564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
1516176aa9aSJan Beulich
1522d3b1b8fSMasahiro Yamada# Find any prerequisites that are newer than target or that do not exist.
1532d3b1b8fSMasahiro Yamada# (This is not true for now; $? should contain any non-existent prerequisites,
1542d3b1b8fSMasahiro Yamada# but it does not work as expected when .SECONDARY is present. This seems a bug
1552d3b1b8fSMasahiro Yamada# of GNU Make.)
15648f1f058SSam Ravnborg# PHONY targets skipped in both cases.
157eba19032SMasahiro Yamadanewer-prereqs = $(filter-out $(PHONY),$?)
15848f1f058SSam Ravnborg
159e1f86d7bSMasahiro Yamada# It is a typical mistake to forget the FORCE prerequisite. Check it here so
160e1f86d7bSMasahiro Yamada# no more breakage will slip in.
161e1f86d7bSMasahiro Yamadacheck-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
162e1f86d7bSMasahiro Yamada
163e1f86d7bSMasahiro Yamadaif-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
1646796e804SMasahiro Yamada
1655de043f4SOleg Verych# Execute command if command has changed or prerequisite(s) are updated.
166ebd191b3SMasahiro Yamadaif_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
167ebd191b3SMasahiro Yamada
168ebd191b3SMasahiro Yamadacmd_and_savecmd =                                                            \
16967126965SMasahiro Yamada	$(cmd);                                                              \
170ebd191b3SMasahiro Yamada	printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd
1718ec4b4ffSSam Ravnborg
1725de043f4SOleg Verych# Execute the command and also postprocess generated .d dependencies file.
1736796e804SMasahiro Yamadaif_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
174c1a95fdaSNicolas Pitre
175e4aca459SNicolas Pitrecmd_and_fixdep =                                                             \
1763a2429e1SMasahiro Yamada	$(cmd);                                                              \
177392885eeSMasahiro Yamada	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
178e5d28910SMasahiro Yamada	rm -f $(depfile)
179c1a95fdaSNicolas Pitre
1808ec4b4ffSSam Ravnborg# Usage: $(call if_changed_rule,foo)
181beda9f3aSRoman Zippel# Will check if $(cmd_foo) or any of the prerequisites changed,
182beda9f3aSRoman Zippel# and if so will execute $(rule_foo).
1836796e804SMasahiro Yamadaif_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
18448f1f058SSam Ravnborg
18545d506bdSSam Ravnborg###
186312a3d09SCao jin# why - tell why a target got built
18745d506bdSSam Ravnborg#       enabled by make V=2
18845d506bdSSam Ravnborg#       Output (listed in the order they are checked):
18945d506bdSSam Ravnborg#          (1) - due to target is PHONY
19045d506bdSSam Ravnborg#          (2) - due to target missing
19145d506bdSSam Ravnborg#          (3) - due to: file1.h file2.h
19245d506bdSSam Ravnborg#          (4) - due to command line change
19345d506bdSSam Ravnborg#          (5) - due to missing .cmd file
19445d506bdSSam Ravnborg#          (6) - due to target not in $(targets)
19545d506bdSSam Ravnborg# (1) PHONY targets are always build
19645d506bdSSam Ravnborg# (2) No target, so we better build it
19745d506bdSSam Ravnborg# (3) Prerequisite is newer than target
19845d506bdSSam Ravnborg# (4) The command line stored in the file named dir/.target.cmd
19945d506bdSSam Ravnborg#     differed from actual command line. This happens when compiler
20045d506bdSSam Ravnborg#     options changes
20145d506bdSSam Ravnborg# (5) No dir/.target.cmd file (used to store command line)
20245d506bdSSam Ravnborg# (6) No dir/.target.cmd file and target not listed in $(targets)
20345d506bdSSam Ravnborg#     This is a good hint that there is a bug in the kbuild file
20445d506bdSSam Ravnborgifeq ($(KBUILD_VERBOSE),2)
20545d506bdSSam Ravnborgwhy =                                                                        \
20645d506bdSSam Ravnborg    $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
20745d506bdSSam Ravnborg        $(if $(wildcard $@),                                                 \
208eba19032SMasahiro Yamada            $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
20950bcca6aSMasahiro Yamada                $(if $(cmd-check),                                           \
21045d506bdSSam Ravnborg                    $(if $(cmd_$@),- due to command line change,             \
21145d506bdSSam Ravnborg                        $(if $(filter $@, $(targets)),                       \
21245d506bdSSam Ravnborg                            - due to missing .cmd file,                      \
21345d506bdSSam Ravnborg                            - due to $(notdir $@) not in $$(targets)         \
21445d506bdSSam Ravnborg                         )                                                   \
21545d506bdSSam Ravnborg                     )                                                       \
21645d506bdSSam Ravnborg                 )                                                           \
21745d506bdSSam Ravnborg             ),                                                              \
21845d506bdSSam Ravnborg             - due to target missing                                         \
21945d506bdSSam Ravnborg         )                                                                   \
22045d506bdSSam Ravnborg     )
22145d506bdSSam Ravnborg
22245d506bdSSam Ravnborgecho-why = $(call escsq, $(strip $(why)))
22345d506bdSSam Ravnborgendif
2243ee550f1SDavid Woodhouse
2253ee550f1SDavid Woodhouse###############################################################################
2269c2af1c7SMasahiro Yamada
2279c2af1c7SMasahiro Yamada# delete partially updated (i.e. corrupted) files on error
2289c2af1c7SMasahiro Yamada.DELETE_ON_ERROR:
2298e9b61b2SMasahiro Yamada
2308e9b61b2SMasahiro Yamada# do not delete intermediate files automatically
2318e9b61b2SMasahiro Yamada.SECONDARY:
232