xref: /linux/Makefile (revision a90ee4305c4a5df72c11b31dacfdc76e00fcf78a)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
26de23f81SLinus TorvaldsVERSION = 7
3cee9395aSLinus TorvaldsPATCHLEVEL = 3
455922c9dSLinus TorvaldsSUBLEVEL = 0
5*a90ee430SLinus TorvaldsEXTRAVERSION = -rc6
61613e604SLinus TorvaldsNAME = Baby Opossum Posse
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds# *DOCUMENTATION*
91da177e4SLinus Torvalds# To see a list of typical targets execute "make help"
101da177e4SLinus Torvalds# More info can be located in ./README
111da177e4SLinus Torvalds# Comments in this file are targeted only to the developer, do not
121da177e4SLinus Torvalds# expect to learn how to build the kernel reading this file.
131da177e4SLinus Torvalds
145f99665eSMasahiro Yamadaifeq ($(filter output-sync,$(.FEATURES)),)
155f99665eSMasahiro Yamada$(error GNU Make >= 4.0 is required. Your Make version is $(MAKE_VERSION))
1687d599fcSMasahiro Yamadaendif
1787d599fcSMasahiro Yamada
18121c2a13SMasahiro Yamada$(if $(filter __%, $(MAKECMDGOALS)), \
19121c2a13SMasahiro Yamada	$(error targets prefixed with '__' are only for internal use))
20121c2a13SMasahiro Yamada
21ba634eceSMasahiro Yamada# That's our default target when none is given on the command line
22121c2a13SMasahiro YamadaPHONY := __all
23121c2a13SMasahiro Yamada__all:
24ba634eceSMasahiro Yamada
251da177e4SLinus Torvalds# We are using a recursive build, so we need to do a little thinking
261da177e4SLinus Torvalds# to get the ordering right.
271da177e4SLinus Torvalds#
281da177e4SLinus Torvalds# Most importantly: sub-Makefiles should only ever modify files in
291da177e4SLinus Torvalds# their own directory. If in some directory we have a dependency on
301da177e4SLinus Torvalds# a file in another dir (which doesn't happen often, but it's often
31f49821eeSNicholas Piggin# unavoidable when linking the built-in.a targets which finally
321da177e4SLinus Torvalds# turn into vmlinux), we will call a sub make in that other dir, and
331da177e4SLinus Torvalds# after that we are sure that everything which is in that other dir
341da177e4SLinus Torvalds# is now up to date.
351da177e4SLinus Torvalds#
361da177e4SLinus Torvalds# The only cases where we need to modify files which have global
371da177e4SLinus Torvalds# effects are thus separated out and done before the recursive
381da177e4SLinus Torvalds# descending is started. They are now explicitly listed as the
391da177e4SLinus Torvalds# prepare rule.
401da177e4SLinus Torvalds
415fa94cebSMasahiro Yamadathis-makefile := $(lastword $(MAKEFILE_LIST))
42e2bad142SMasahiro Yamadaabs_srctree := $(realpath $(dir $(this-makefile)))
435ea17216SMasahiro Yamadaabs_output := $(CURDIR)
445fa94cebSMasahiro Yamada
45221cc2d2SMasahiro Yamadaifneq ($(sub_make_done),1)
463812b8c5SMasahiro Yamada
473812b8c5SMasahiro Yamada# Do not use make's built-in rules and variables
483812b8c5SMasahiro Yamada# (this increases performance and avoids hard-to-debug behaviour)
493812b8c5SMasahiro YamadaMAKEFLAGS += -rR
503812b8c5SMasahiro Yamada
513812b8c5SMasahiro Yamada# Avoid funny character set dependencies
523812b8c5SMasahiro Yamadaunexport LC_ALL
533812b8c5SMasahiro YamadaLC_COLLATE=C
543812b8c5SMasahiro YamadaLC_NUMERIC=C
553812b8c5SMasahiro Yamadaexport LC_COLLATE LC_NUMERIC
563812b8c5SMasahiro Yamada
573812b8c5SMasahiro Yamada# Avoid interference with shell env settings
583812b8c5SMasahiro Yamadaunexport GREP_OPTIONS
593812b8c5SMasahiro Yamada
60066b7ed9SMichal Marek# Beautify output
61066b7ed9SMichal Marek# ---------------------------------------------------------------------------
62066b7ed9SMichal Marek#
638962b6b4SMasahiro Yamada# Most of build commands in Kbuild start with "cmd_". You can optionally define
648962b6b4SMasahiro Yamada# "quiet_cmd_*". If defined, the short log is printed. Otherwise, no log from
658962b6b4SMasahiro Yamada# that command is printed by default.
66066b7ed9SMichal Marek#
678962b6b4SMasahiro Yamada# e.g.)
688962b6b4SMasahiro Yamada#    quiet_cmd_depmod = DEPMOD  $(MODLIB)
698962b6b4SMasahiro Yamada#          cmd_depmod = $(srctree)/scripts/depmod.sh $(DEPMOD) $(KERNELRELEASE)
70066b7ed9SMichal Marek#
71066b7ed9SMichal Marek# A simple variant is to prefix commands with $(Q) - that's useful
72066b7ed9SMichal Marek# for commands that shall be hidden in non-verbose mode.
73066b7ed9SMichal Marek#
748962b6b4SMasahiro Yamada#    $(Q)$(MAKE) $(build)=scripts/basic
75066b7ed9SMichal Marek#
766ae4b986SMasahiro Yamada# If KBUILD_VERBOSE contains 1, the whole command is echoed.
776ae4b986SMasahiro Yamada# If KBUILD_VERBOSE contains 2, the reason for rebuilding is printed.
78066b7ed9SMichal Marek#
791da177e4SLinus Torvalds# To put more focus on warnings, be less verbose as default
801da177e4SLinus Torvalds# Use 'make V=1' to see the full commands
811da177e4SLinus Torvalds
821da177e4SLinus Torvaldsifeq ("$(origin V)", "command line")
831da177e4SLinus Torvalds  KBUILD_VERBOSE = $(V)
841da177e4SLinus Torvaldsendif
851da177e4SLinus Torvalds
86066b7ed9SMichal Marekquiet = quiet_
87066b7ed9SMichal MarekQ = @
886ae4b986SMasahiro Yamada
896ae4b986SMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_VERBOSE)),)
906ae4b986SMasahiro Yamada  quiet =
916ae4b986SMasahiro Yamada  Q =
92066b7ed9SMichal Marekendif
93066b7ed9SMichal Marek
94066b7ed9SMichal Marek# If the user is running make -s (silent mode), suppress echoing of
95066b7ed9SMichal Marek# commands
965f99665eSMasahiro Yamadaifneq ($(findstring s,$(firstword -$(MAKEFLAGS))),)
97066b7ed9SMichal Marekquiet=silent_
9883d98d73SMasahiro Yamadaoverride KBUILD_VERBOSE :=
99066b7ed9SMichal Marekendif
100066b7ed9SMichal Marek
101066b7ed9SMichal Marekexport quiet Q KBUILD_VERBOSE
102066b7ed9SMichal Marek
103bcf637f5SMasahiro Yamada# Call a source code checker (by default, "sparse") as part of the
104bcf637f5SMasahiro Yamada# C compilation.
105bcf637f5SMasahiro Yamada#
106bcf637f5SMasahiro Yamada# Use 'make C=1' to enable checking of only re-compiled files.
107bcf637f5SMasahiro Yamada# Use 'make C=2' to enable checking of *all* source files, regardless
108bcf637f5SMasahiro Yamada# of whether they are re-compiled or not.
109bcf637f5SMasahiro Yamada#
110bcf637f5SMasahiro Yamada# See the file "Documentation/dev-tools/sparse.rst" for more details,
111bcf637f5SMasahiro Yamada# including where to get the "sparse" utility.
112bcf637f5SMasahiro Yamada
113bcf637f5SMasahiro Yamadaifeq ("$(origin C)", "command line")
114bcf637f5SMasahiro Yamada  KBUILD_CHECKSRC = $(C)
115bcf637f5SMasahiro Yamadaendif
116bcf637f5SMasahiro Yamadaifndef KBUILD_CHECKSRC
117bcf637f5SMasahiro Yamada  KBUILD_CHECKSRC = 0
118bcf637f5SMasahiro Yamadaendif
119bcf637f5SMasahiro Yamada
120bcf637f5SMasahiro Yamadaexport KBUILD_CHECKSRC
121bcf637f5SMasahiro Yamada
1222f7ab126SMiguel Ojeda# Enable "clippy" (a linter) as part of the Rust compilation.
1232f7ab126SMiguel Ojeda#
1242f7ab126SMiguel Ojeda# Use 'make CLIPPY=1' to enable it.
1252f7ab126SMiguel Ojedaifeq ("$(origin CLIPPY)", "command line")
1262f7ab126SMiguel Ojeda  KBUILD_CLIPPY := $(CLIPPY)
1272f7ab126SMiguel Ojedaendif
1282f7ab126SMiguel Ojeda
1292f7ab126SMiguel Ojedaexport KBUILD_CLIPPY
1302f7ab126SMiguel Ojeda
131bcf637f5SMasahiro Yamada# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the
132bcf637f5SMasahiro Yamada# directory of external module to build. Setting M= takes precedence.
133bcf637f5SMasahiro Yamadaifeq ("$(origin M)", "command line")
134bcf637f5SMasahiro Yamada  KBUILD_EXTMOD := $(M)
135bcf637f5SMasahiro Yamadaendif
136bcf637f5SMasahiro Yamada
13711b3d517SMasahiro Yamadaifeq ("$(origin MO)", "command line")
13811b3d517SMasahiro Yamada  KBUILD_EXTMOD_OUTPUT := $(MO)
13911b3d517SMasahiro Yamadaendif
14011b3d517SMasahiro Yamada
141bcf637f5SMasahiro Yamada$(if $(word 2, $(KBUILD_EXTMOD)), \
142bcf637f5SMasahiro Yamada	$(error building multiple external modules is not supported))
143bcf637f5SMasahiro Yamada
1449a68fd7fSMasahiro Yamada$(foreach x, % :, $(if $(findstring $x, $(KBUILD_EXTMOD)), \
1459a68fd7fSMasahiro Yamada	$(error module directory path cannot contain '$x')))
1469a68fd7fSMasahiro Yamada
14774ee585bSMasahiro Yamada# Remove trailing slashes
14874ee585bSMasahiro Yamadaifneq ($(filter %/, $(KBUILD_EXTMOD)),)
14974ee585bSMasahiro YamadaKBUILD_EXTMOD := $(shell dirname $(KBUILD_EXTMOD).)
15074ee585bSMasahiro Yamadaendif
15174ee585bSMasahiro Yamada
152bcf637f5SMasahiro Yamadaexport KBUILD_EXTMOD
153bcf637f5SMasahiro Yamada
15492ef432fSMasahiro Yamadaifeq ("$(origin W)", "command line")
15592ef432fSMasahiro Yamada  KBUILD_EXTRA_WARN := $(W)
15692ef432fSMasahiro Yamadaendif
15792ef432fSMasahiro Yamada
15892ef432fSMasahiro Yamadaexport KBUILD_EXTRA_WARN
15992ef432fSMasahiro Yamada
16025b146c5SMasahiro Yamada# Kbuild will save output files in the current working directory.
16125b146c5SMasahiro Yamada# This does not need to match to the root of the kernel source tree.
16225b146c5SMasahiro Yamada#
16325b146c5SMasahiro Yamada# For example, you can do this:
16425b146c5SMasahiro Yamada#
16525b146c5SMasahiro Yamada#  cd /dir/to/store/output/files; make -f /dir/to/kernel/source/Makefile
16625b146c5SMasahiro Yamada#
16725b146c5SMasahiro Yamada# If you want to save output files in a different location, there are
16825b146c5SMasahiro Yamada# two syntaxes to specify it.
16925b146c5SMasahiro Yamada#
1701da177e4SLinus Torvalds# 1) O=
1711da177e4SLinus Torvalds# Use "make O=dir/to/store/output/files/"
1721da177e4SLinus Torvalds#
1731da177e4SLinus Torvalds# 2) Set KBUILD_OUTPUT
17425b146c5SMasahiro Yamada# Set the environment variable KBUILD_OUTPUT to point to the output directory.
17525b146c5SMasahiro Yamada# export KBUILD_OUTPUT=dir/to/store/output/files/; make
1761da177e4SLinus Torvalds#
1771da177e4SLinus Torvalds# The O= assignment takes precedence over the KBUILD_OUTPUT environment
1781da177e4SLinus Torvalds# variable.
1791da177e4SLinus Torvalds
1801da177e4SLinus Torvaldsifeq ("$(origin O)", "command line")
1811da177e4SLinus Torvalds  KBUILD_OUTPUT := $(O)
1821da177e4SLinus Torvaldsendif
1831da177e4SLinus Torvalds
18413b25489SMasahiro Yamadaifdef KBUILD_EXTMOD
18513b25489SMasahiro Yamada    ifdef KBUILD_OUTPUT
18613b25489SMasahiro Yamada        objtree := $(realpath $(KBUILD_OUTPUT))
18713b25489SMasahiro Yamada        $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist))
18813b25489SMasahiro Yamada    else
1898cd07cc6SMasahiro Yamada        objtree := $(abs_srctree)
19013b25489SMasahiro Yamada    endif
1918cd07cc6SMasahiro Yamada    # If Make is invoked from the kernel directory (either kernel
1928cd07cc6SMasahiro Yamada    # source directory or kernel build directory), external modules
1938cd07cc6SMasahiro Yamada    # are built in $(KBUILD_EXTMOD) for backward compatibility,
1948cd07cc6SMasahiro Yamada    # otherwise, built in the current directory.
1958cd07cc6SMasahiro Yamada    output := $(or $(KBUILD_EXTMOD_OUTPUT),$(if $(filter $(CURDIR),$(objtree) $(abs_srctree)),$(KBUILD_EXTMOD)))
19613b25489SMasahiro Yamada    # KBUILD_EXTMOD might be a relative path. Remember its absolute path before
19713b25489SMasahiro Yamada    # Make changes the working directory.
19813b25489SMasahiro Yamada    srcroot := $(realpath $(KBUILD_EXTMOD))
19913b25489SMasahiro Yamada    $(if $(srcroot),,$(error specified external module directory "$(KBUILD_EXTMOD)" does not exist))
20013b25489SMasahiro Yamadaelse
20113b25489SMasahiro Yamada    objtree := .
202d1711360SMasahiro Yamada    output := $(KBUILD_OUTPUT)
20313b25489SMasahiro Yamadaendif
20413b25489SMasahiro Yamada
20513b25489SMasahiro Yamadaexport objtree srcroot
206d1711360SMasahiro Yamada
207d1711360SMasahiro Yamada# Do we want to change the working directory?
208d1711360SMasahiro Yamadaifneq ($(output),)
2097beba04eSMasahiro Yamada# $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first.
210d1711360SMasahiro Yamada$(shell mkdir -p "$(output)")
21125b146c5SMasahiro Yamada# $(realpath ...) resolves symlinks
212d1711360SMasahiro Yamadaabs_output := $(realpath $(output))
213d1711360SMasahiro Yamada$(if $(abs_output),,$(error failed to create output directory "$(output)"))
214d1711360SMasahiro Yamadaendif
21525b146c5SMasahiro Yamada
21625b146c5SMasahiro Yamadaifneq ($(words $(subst :, ,$(abs_srctree))), 1)
21725b146c5SMasahiro Yamada$(error source directory cannot contain spaces or colons)
21825b146c5SMasahiro Yamadaendif
21925b146c5SMasahiro Yamada
220221cc2d2SMasahiro Yamadaexport sub_make_done := 1
221221cc2d2SMasahiro Yamada
2225fc10e76SMasahiro Yamadaendif # sub_make_done
2235fc10e76SMasahiro Yamada
2245ea17216SMasahiro Yamadaifeq ($(abs_output),$(CURDIR))
2255fc10e76SMasahiro Yamada# Suppress "Entering directory ..." if we are at the final work directory.
2265fc10e76SMasahiro Yamadano-print-directory := --no-print-directory
2275fc10e76SMasahiro Yamadaelse
2285fc10e76SMasahiro Yamada# Recursion to show "Entering directory ..."
2295fc10e76SMasahiro Yamadaneed-sub-make := 1
2305fc10e76SMasahiro Yamadaendif
2315fc10e76SMasahiro Yamada
2325fc10e76SMasahiro Yamadaifeq ($(filter --no-print-directory, $(MAKEFLAGS)),)
2335fc10e76SMasahiro Yamada# If --no-print-directory is unset, recurse once again to set it.
2345fc10e76SMasahiro Yamada# You may end up recursing into __sub-make twice. This is needed due to the
2355fc10e76SMasahiro Yamada# behavior change in GNU Make 4.4.1.
2365fc10e76SMasahiro Yamadaneed-sub-make := 1
2375fc10e76SMasahiro Yamadaendif
2385fc10e76SMasahiro Yamada
239688931a5SMasahiro Yamadaifeq ($(need-sub-make),1)
240688931a5SMasahiro Yamada
241121c2a13SMasahiro YamadaPHONY += $(MAKECMDGOALS) __sub-make
2420b35786dSMilton Miller
243121c2a13SMasahiro Yamada$(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
24416f89098SCharles Keepax	@:
2450b35786dSMilton Miller
246c4e6fff1SCao jin# Invoke a second make in the output directory, passing relevant variables
247121c2a13SMasahiro Yamada__sub-make:
2485ea17216SMasahiro Yamada	$(Q)$(MAKE) $(no-print-directory) -C $(abs_output) \
2495fc10e76SMasahiro Yamada	-f $(abs_srctree)/Makefile $(MAKECMDGOALS)
2501da177e4SLinus Torvalds
2515fc10e76SMasahiro Yamadaelse # need-sub-make
252688931a5SMasahiro Yamada
2531da177e4SLinus Torvalds# We process the rest of the Makefile if this is the final invocation of make
2547ff52571SMasahiro Yamada
25513b25489SMasahiro Yamadaifndef KBUILD_EXTMOD
25613b25489SMasahiro Yamadasrcroot := $(abs_srctree)
25713b25489SMasahiro Yamadaendif
25813b25489SMasahiro Yamada
25913b25489SMasahiro Yamadaifeq ($(srcroot),$(CURDIR))
260051f278eSMasahiro Yamadabuilding_out_of_srctree :=
2619da0763bSMichal Marekelse
26213b25489SMasahiro Yamadaexport building_out_of_srctree := 1
2639da0763bSMichal Marekendif
2642c1f4f12SMasahiro Yamada
26513b25489SMasahiro Yamadaifdef KBUILD_ABS_SRCTREE
26613b25489SMasahiro Yamada    # Do nothing. Use the absolute path.
26713b25489SMasahiro Yamadaelse ifeq ($(srcroot),$(CURDIR))
26813b25489SMasahiro Yamada    # Building in the source.
26913b25489SMasahiro Yamada    srcroot := .
27013b25489SMasahiro Yamadaelse ifeq ($(srcroot)/,$(dir $(CURDIR)))
27113b25489SMasahiro Yamada    # Building in a subdirectory of the source.
27213b25489SMasahiro Yamada    srcroot := ..
27395fd3f87SMasahiro Yamadaendif
2742c1f4f12SMasahiro Yamada
27513b25489SMasahiro Yamadaexport srctree := $(if $(KBUILD_EXTMOD),$(abs_srctree),$(srcroot))
276b1992c37SMasahiro Yamada
277b1992c37SMasahiro Yamadaifdef building_out_of_srctree
27813b25489SMasahiro Yamadaexport VPATH := $(srcroot)
27913b25489SMasahiro Yamadaelse
28013b25489SMasahiro YamadaVPATH :=
281b1992c37SMasahiro Yamadaendif
2821da177e4SLinus Torvalds
2832c1f4f12SMasahiro Yamada# To make sure we do not include .config for any of the *config targets
2842c1f4f12SMasahiro Yamada# catch them early, and hand them over to scripts/kconfig/Makefile
2852c1f4f12SMasahiro Yamada# It is allowed to specify more targets when calling make, including
2862c1f4f12SMasahiro Yamada# mixing *config targets and build targets.
2872c1f4f12SMasahiro Yamada# For example 'make oldconfig all'.
2882c1f4f12SMasahiro Yamada# Detect when mixed targets is specified, and make a second invocation
2892c1f4f12SMasahiro Yamada# of make so .config is not included in this case either (for *config).
2902c1f4f12SMasahiro Yamada
2912c1f4f12SMasahiro Yamadaversion_h := include/generated/uapi/linux/version.h
2922c1f4f12SMasahiro Yamada
29322340a06SMasahiro Yamadaclean-targets := %clean mrproper cleandocs
29422340a06SMasahiro Yamadano-dot-config-targets := $(clean-targets) \
2952c1f4f12SMasahiro Yamada			 cscope gtags TAGS tags help% %docs check% coccicheck \
296f58316a4SAndrew Jones			 kconfig-sym-check \
29759b2bd05SMasahiro Yamada			 $(version_h) headers headers_% archheaders archscripts \
2984623980dSDavid Engraf			 %asm-generic kernelversion %src-pkg dt_binding_check \
299efb6347dSDaniel Golle			 dt_style_selftest \
300d8ad80a8SThomas Weißschuh			 outputmakefile rustavailable rustfmt rustfmtcheck \
301d8ad80a8SThomas Weißschuh			 run-command
302eb931e12SMasahiro Yamadano-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \
303993bdde9SMasahiro Yamada			  image_name
30491ca8be3SMasahiro Yamadasingle-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %/
3052c1f4f12SMasahiro Yamada
3062042b548SMasahiro Yamadaconfig-build	:=
3072042b548SMasahiro Yamadamixed-build	:=
3082042b548SMasahiro Yamadaneed-config	:= 1
309d7942413SMasahiro Yamadamay-sync-config	:= 1
310394053f4SMasahiro Yamadasingle-build	:=
3112c1f4f12SMasahiro Yamada
3122c1f4f12SMasahiro Yamadaifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
3132c1f4f12SMasahiro Yamada    ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
3142042b548SMasahiro Yamada        need-config :=
3152c1f4f12SMasahiro Yamada    endif
3162c1f4f12SMasahiro Yamadaendif
3172c1f4f12SMasahiro Yamada
318d7942413SMasahiro Yamadaifneq ($(filter $(no-sync-config-targets), $(MAKECMDGOALS)),)
319d7942413SMasahiro Yamada    ifeq ($(filter-out $(no-sync-config-targets), $(MAKECMDGOALS)),)
3202042b548SMasahiro Yamada        may-sync-config :=
321d7942413SMasahiro Yamada    endif
322d7942413SMasahiro Yamadaendif
323d7942413SMasahiro Yamada
3249d361173SMasahiro Yamadaneed-compiler := $(may-sync-config)
3259d361173SMasahiro Yamada
326d7942413SMasahiro Yamadaifneq ($(KBUILD_EXTMOD),)
3272042b548SMasahiro Yamada    may-sync-config :=
328d7942413SMasahiro Yamadaendif
329d7942413SMasahiro Yamada
3302c1f4f12SMasahiro Yamadaifeq ($(KBUILD_EXTMOD),)
331efbf9726SMasahiro Yamada    ifneq ($(filter %config,$(MAKECMDGOALS)),)
3322042b548SMasahiro Yamada        config-build := 1
3332c1f4f12SMasahiro Yamada        ifneq ($(words $(MAKECMDGOALS)),1)
3342042b548SMasahiro Yamada            mixed-build := 1
3352c1f4f12SMasahiro Yamada        endif
3362c1f4f12SMasahiro Yamada    endif
3372c1f4f12SMasahiro Yamadaendif
33822340a06SMasahiro Yamada
339394053f4SMasahiro Yamada# We cannot build single targets and the others at the same time
340394053f4SMasahiro Yamadaifneq ($(filter $(single-targets), $(MAKECMDGOALS)),)
341394053f4SMasahiro Yamada    single-build := 1
342394053f4SMasahiro Yamada    ifneq ($(filter-out $(single-targets), $(MAKECMDGOALS)),)
343394053f4SMasahiro Yamada        mixed-build := 1
344394053f4SMasahiro Yamada    endif
345394053f4SMasahiro Yamadaendif
346394053f4SMasahiro Yamada
34722340a06SMasahiro Yamada# For "make -j clean all", "make -j mrproper defconfig all", etc.
34822340a06SMasahiro Yamadaifneq ($(filter $(clean-targets),$(MAKECMDGOALS)),)
34922340a06SMasahiro Yamada    ifneq ($(filter-out $(clean-targets),$(MAKECMDGOALS)),)
3502042b548SMasahiro Yamada        mixed-build := 1
35122340a06SMasahiro Yamada    endif
35222340a06SMasahiro Yamadaendif
35322340a06SMasahiro Yamada
3542c1f4f12SMasahiro Yamada# install and modules_install need also be processed one by one
3552c1f4f12SMasahiro Yamadaifneq ($(filter install,$(MAKECMDGOALS)),)
3562c1f4f12SMasahiro Yamada    ifneq ($(filter modules_install,$(MAKECMDGOALS)),)
3572042b548SMasahiro Yamada        mixed-build := 1
3582c1f4f12SMasahiro Yamada    endif
3592c1f4f12SMasahiro Yamadaendif
3602c1f4f12SMasahiro Yamada
3612042b548SMasahiro Yamadaifdef mixed-build
3622c1f4f12SMasahiro Yamada# ===========================================================================
3632c1f4f12SMasahiro Yamada# We're called with mixed targets (*config and build targets).
3642c1f4f12SMasahiro Yamada# Handle them one by one.
3652c1f4f12SMasahiro Yamada
3662c1f4f12SMasahiro YamadaPHONY += $(MAKECMDGOALS) __build_one_by_one
3672c1f4f12SMasahiro Yamada
368121c2a13SMasahiro Yamada$(MAKECMDGOALS): __build_one_by_one
3692c1f4f12SMasahiro Yamada	@:
3702c1f4f12SMasahiro Yamada
3712c1f4f12SMasahiro Yamada__build_one_by_one:
3722c1f4f12SMasahiro Yamada	$(Q)set -e; \
3732c1f4f12SMasahiro Yamada	for i in $(MAKECMDGOALS); do \
3742c1f4f12SMasahiro Yamada		$(MAKE) -f $(srctree)/Makefile $$i; \
3752c1f4f12SMasahiro Yamada	done
3762c1f4f12SMasahiro Yamada
3772042b548SMasahiro Yamadaelse # !mixed-build
3782c1f4f12SMasahiro Yamada
3793204a7fbSMasahiro Yamadainclude $(srctree)/scripts/Kbuild.include
3802c1f4f12SMasahiro Yamada
3812c1f4f12SMasahiro Yamada# Read KERNELRELEASE from include/config/kernel.release (if it exists)
382214c0eeaSMasahiro YamadaKERNELRELEASE = $(call read-file, $(objtree)/include/config/kernel.release)
3832c1f4f12SMasahiro YamadaKERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
3842c1f4f12SMasahiro Yamadaexport VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
3852c1f4f12SMasahiro Yamada
3863204a7fbSMasahiro Yamadainclude $(srctree)/scripts/subarch.include
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvalds# Cross compiling and selecting different set of gcc/bin-utils
3891da177e4SLinus Torvalds# ---------------------------------------------------------------------------
3901da177e4SLinus Torvalds#
3911da177e4SLinus Torvalds# When performing cross compilation for other architectures ARCH shall be set
3921da177e4SLinus Torvalds# to the target architecture. (See arch/* for the possibilities).
3931da177e4SLinus Torvalds# ARCH can be set during invocation of make:
39494483490SArd Biesheuvel# make ARCH=arm64
3951da177e4SLinus Torvalds# Another way is to have ARCH set in the environment.
3961da177e4SLinus Torvalds# The default ARCH is the host where make is executed.
3971da177e4SLinus Torvalds
3981da177e4SLinus Torvalds# CROSS_COMPILE specify the prefix used for all executables used
3991da177e4SLinus Torvalds# during compilation. Only gcc and related bin-utils executables
4001da177e4SLinus Torvalds# are prefixed with $(CROSS_COMPILE).
4011da177e4SLinus Torvalds# CROSS_COMPILE can be set on the command line
40294483490SArd Biesheuvel# make CROSS_COMPILE=aarch64-linux-gnu-
4031da177e4SLinus Torvalds# Alternatively CROSS_COMPILE can be set in the environment.
4041da177e4SLinus Torvalds# Default value for CROSS_COMPILE is not to prefix executables
4051da177e4SLinus Torvalds# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
4062331d1a6SSam RavnborgARCH		?= $(SUBARCH)
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds# Architecture as present in compile.h
4091da177e4SLinus TorvaldsUTS_MACHINE 	:= $(ARCH)
4106752ed90SThomas GleixnerSRCARCH 	:= $(ARCH)
4111da177e4SLinus Torvalds
412d746d647SSam Ravnborg# Additional ARCH settings for x86
413d746d647SSam Ravnborgifeq ($(ARCH),i386)
414d746d647SSam Ravnborg        SRCARCH := x86
415d746d647SSam Ravnborgendif
416d746d647SSam Ravnborgifeq ($(ARCH),x86_64)
417d746d647SSam Ravnborg        SRCARCH := x86
418d746d647SSam Ravnborgendif
41974b469f2SSam Ravnborg
4205e538790SSam Ravnborg# Additional ARCH settings for sparc
421e69f58c0SNamhyung Kimifeq ($(ARCH),sparc32)
422e69f58c0SNamhyung Kim       SRCARCH := sparc
423e69f58c0SNamhyung Kimendif
424a439fe51SSam Ravnborgifeq ($(ARCH),sparc64)
4255e538790SSam Ravnborg       SRCARCH := sparc
426a439fe51SSam Ravnborgendif
4272fb9b1bdSSam Ravnborg
4285f6e0fe0SMasahiro Yamada# Additional ARCH settings for parisc
4295f6e0fe0SMasahiro Yamadaifeq ($(ARCH),parisc64)
4305f6e0fe0SMasahiro Yamada       SRCARCH := parisc
4315f6e0fe0SMasahiro Yamadaendif
4325f6e0fe0SMasahiro Yamada
433f02aa48dSMasahiro Yamadaexport cross_compiling :=
434f02aa48dSMasahiro Yamadaifneq ($(SRCARCH),$(SUBARCH))
435f02aa48dSMasahiro Yamadacross_compiling := 1
436f02aa48dSMasahiro Yamadaendif
437f02aa48dSMasahiro Yamada
43814cdd3c4SRoman ZippelKCONFIG_CONFIG	?= .config
43941263fc6SBen Gardinerexport KCONFIG_CONFIG
44014cdd3c4SRoman Zippel
4411da177e4SLinus Torvalds# SHELL used by kbuild
442858805b3SMasahiro YamadaCONFIG_SHELL := sh
4431da177e4SLinus Torvalds
4446d79a7b4SMasahiro YamadaHOST_LFS_CFLAGS := $(shell getconf LFS_CFLAGS 2>/dev/null)
4456d79a7b4SMasahiro YamadaHOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
4466d79a7b4SMasahiro YamadaHOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
447d7f14c66SUwe Kleine-König
448a0d1c951SMasahiro Yamadaifneq ($(LLVM),)
449e9c28192SNathan Chancellorifneq ($(filter %/,$(LLVM)),)
450e9c28192SNathan ChancellorLLVM_PREFIX := $(LLVM)
451e9c28192SNathan Chancellorelse ifneq ($(filter -%,$(LLVM)),)
452e9c28192SNathan ChancellorLLVM_SUFFIX := $(LLVM)
453502678b8SThomas Weißschuhelse ifneq ($(LLVM),1)
454502678b8SThomas Weißschuh$(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst)
455e9c28192SNathan Chancellorendif
456e9c28192SNathan Chancellor
457e9c28192SNathan ChancellorHOSTCC	= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
458e9c28192SNathan ChancellorHOSTCXX	= $(LLVM_PREFIX)clang++$(LLVM_SUFFIX)
459a0d1c951SMasahiro Yamadaelse
4601da177e4SLinus TorvaldsHOSTCC	= gcc
4611da177e4SLinus TorvaldsHOSTCXX	= g++
462a0d1c951SMasahiro Yamadaendif
4632f7ab126SMiguel OjedaHOSTRUSTC = rustc
464d5ea4fecSChun-Tse ShaoHOSTPKG_CONFIG	= pkg-config
4657f3a59dbSMasahiro Yamada
466110214e4SMauro Carvalho Chehab# the KERNELDOC macro needs to be exported, as scripts/Makefile.build
467110214e4SMauro Carvalho Chehab# has a logic to call it
468eba6ffd1SJonathan CorbetKERNELDOC       = $(srctree)/tools/docs/kernel-doc
469110214e4SMauro Carvalho Chehabexport KERNELDOC
470110214e4SMauro Carvalho Chehab
471f67695c9SElliot BermanKBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
472b5ec6fd2SPeter Zijlstra			 -O2 -fomit-frame-pointer -std=gnu11
473f67695c9SElliot BermanKBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
474f67695c9SElliot BermanKBUILD_USERLDFLAGS := $(USERLDFLAGS)
4757f3a59dbSMasahiro Yamada
4762f7ab126SMiguel Ojeda# These flags apply to all Rust code in the tree, including the kernel and
4772f7ab126SMiguel Ojeda# host programs.
4784688cf88SAlexandre Courbot#
4794688cf88SAlexandre Courbot# `-Aclippy::unwrap_or_default`: the lint is buggy [1] and ignores our
4804688cf88SAlexandre Courbot# MSRV. It can trigger depending on the optimization level.
4814688cf88SAlexandre Courbot# [1] https://github.com/rust-lang/rust-clippy/issues/17379
4822f7ab126SMiguel Ojedaexport rust_common_flags := --edition=2021 \
4832f7ab126SMiguel Ojeda			    -Zbinary_dep_depinfo=y \
4848e95e53cSMiguel Ojeda			    -Astable_features \
485592c61f3SMiguel Ojeda			    -Aunused_features \
486f8f88aa2SMiguel Ojeda			    -Dnon_ascii_idents \
487a135aa3dSMiguel Ojeda			    -Dunsafe_op_in_unsafe_fn \
488a135aa3dSMiguel Ojeda			    -Wmissing_docs \
489f8f88aa2SMiguel Ojeda			    -Wrust_2018_idioms \
490f8f88aa2SMiguel Ojeda			    -Wunreachable_pub \
491bb421b51SMiguel Ojeda			    -Wclippy::all \
49223773bd8STamir Duberstein			    -Wclippy::as_ptr_cast_mut \
4935e305505STamir Duberstein			    -Wclippy::as_underscore \
494b7c8d7a8STamir Duberstein			    -Wclippy::cast_lossless \
4952adc8664SMiguel Ojeda			    -Aclippy::collapsible_if \
496838d852dSMiguel Ojeda			    -Aclippy::collapsible_match \
4973fcc2339SMiguel Ojeda			    -Wclippy::ignored_unit_patterns \
4987ed18860SMiguel Ojeda			    -Aclippy::incompatible_msrv \
499bb421b51SMiguel Ojeda			    -Wclippy::mut_mut \
500f8f88aa2SMiguel Ojeda			    -Wclippy::needless_bitwise_bool \
50160fc1e67SMiguel Ojeda			    -Aclippy::needless_lifetimes \
502f8f88aa2SMiguel Ojeda			    -Wclippy::no_mangle_with_rust_abi \
503fcad9bbfSTamir Duberstein			    -Wclippy::ptr_as_ptr \
504d8c9e735STamir Duberstein			    -Wclippy::ptr_cast_constness \
505dc35ddcfSTamir Duberstein			    -Wclippy::ref_as_ptr \
506db4f72c9SMiguel Ojeda			    -Wclippy::undocumented_unsafe_blocks \
50710eea3c1SMiguel Ojeda			    -Aclippy::uninlined_format_args \
508c28bfe76SMiguel Ojeda			    -Wclippy::unnecessary_safety_comment \
50923f42dc0SMiguel Ojeda			    -Wclippy::unnecessary_safety_doc \
5104688cf88SAlexandre Courbot			    -Aclippy::unwrap_or_default \
511bef83245SMiguel Ojeda			    -Wrustdoc::missing_crate_level_docs \
512bef83245SMiguel Ojeda			    -Wrustdoc::unescaped_backticks
5132f7ab126SMiguel Ojeda
514fbaf242cSMasahiro YamadaKBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \
515fbaf242cSMasahiro Yamada		       $(HOSTCFLAGS) -I $(srctree)/scripts/include
516fbaf242cSMasahiro YamadaKBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS) \
517fbaf242cSMasahiro Yamada		       -I $(srctree)/scripts/include
5182f7ab126SMiguel OjedaKBUILD_HOSTRUSTFLAGS := $(rust_common_flags) -O -Cstrip=debuginfo \
519b2aa1535SMiguel Ojeda			-Zallow-features=
520f92d19e0SLaura AbbottKBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
521f92d19e0SLaura AbbottKBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
522ceff0757SHONG YifanKBUILD_PROCMACROLDFLAGS := $(or $(PROCMACROLDFLAGS),$(KBUILD_HOSTLDFLAGS))
5231da177e4SLinus Torvalds
5241da177e4SLinus Torvalds# Make variables (CC, etc...)
5251da177e4SLinus TorvaldsCPP		= $(CC) -E
526a0d1c951SMasahiro Yamadaifneq ($(LLVM),)
527e9c28192SNathan ChancellorCC		= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
528e9c28192SNathan ChancellorLD		= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
529e9c28192SNathan ChancellorAR		= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
5303a2486ccSGary GuoLLVM_LINK	= $(LLVM_PREFIX)llvm-link$(LLVM_SUFFIX)
531e9c28192SNathan ChancellorNM		= $(LLVM_PREFIX)llvm-nm$(LLVM_SUFFIX)
532e9c28192SNathan ChancellorOBJCOPY		= $(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)
533e9c28192SNathan ChancellorOBJDUMP		= $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX)
534e9c28192SNathan ChancellorREADELF		= $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
535e9c28192SNathan ChancellorSTRIP		= $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
5367158c5b6SNathan Chancellorifeq ($(filter -fuse-ld=% --ld-path=%,$(KBUILD_HOSTLDFLAGS)),)
5377158c5b6SNathan ChancellorKBUILD_HOSTLDFLAGS += -fuse-ld=lld
5387158c5b6SNathan Chancellorendif
539a0d1c951SMasahiro Yamadaelse
540a0d1c951SMasahiro YamadaCC		= $(CROSS_COMPILE)gcc
541a0d1c951SMasahiro YamadaLD		= $(CROSS_COMPILE)ld
5421da177e4SLinus TorvaldsAR		= $(CROSS_COMPILE)ar
5431da177e4SLinus TorvaldsNM		= $(CROSS_COMPILE)nm
5441da177e4SLinus TorvaldsOBJCOPY		= $(CROSS_COMPILE)objcopy
5451da177e4SLinus TorvaldsOBJDUMP		= $(CROSS_COMPILE)objdump
546eefb8c12SDmitry GolovinREADELF		= $(CROSS_COMPILE)readelf
547a0d1c951SMasahiro YamadaSTRIP		= $(CROSS_COMPILE)strip
548a0d1c951SMasahiro Yamadaendif
5492f7ab126SMiguel OjedaRUSTC		= rustc
5502f7ab126SMiguel OjedaRUSTDOC		= rustdoc
5512f7ab126SMiguel OjedaRUSTFMT		= rustfmt
5522f7ab126SMiguel OjedaCLIPPY_DRIVER	= clippy-driver
5532f7ab126SMiguel OjedaBINDGEN		= bindgen
554e83b9f55SAndrii NakryikoPAHOLE		= pahole
555c9a0f3b8SJiri OlsaRESOLVE_BTFIDS	= $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
55673a4f6dbSMasahiro YamadaLEX		= flex
55773a4f6dbSMasahiro YamadaYACC		= bison
5581da177e4SLinus TorvaldsAWK		= awk
559caa27b66SSam RavnborgINSTALLKERNEL  := installkernel
5601da177e4SLinus TorvaldsPERL		= perl
561e9781b52SMasahiro YamadaPYTHON3		= python3
5621da177e4SLinus TorvaldsCHECK		= sparse
563858805b3SMasahiro YamadaBASH		= bash
564e4a42c82SDenis EfremovKGZIP		= gzip
565e4a42c82SDenis EfremovKBZIP2		= bzip2
566e4a42c82SDenis EfremovKLZOP		= lzop
5678dfb61dcSDenis EfremovLZMA		= lzma
568e397a603SParth PancholiLZ4		= lz4
5698dfb61dcSDenis EfremovXZ		= xz
57048f7ddf7SNick TerrellZSTD		= zstd
57173d210e9SMichał GórnyTAR		= tar
5728dfb61dcSDenis Efremov
57380a7d1d9SHannes EderCHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
5746c49f359SLuc Van Oostenryck		  -Wbitwise -Wno-return-void -Wno-unknown-attribute $(CF)
5750c22be07SDouglas AndersonNOSTDINC_FLAGS :=
5766588169dSSam RavnborgCFLAGS_MODULE   =
5772f7ab126SMiguel OjedaRUSTFLAGS_MODULE =
5786588169dSSam RavnborgAFLAGS_MODULE   =
5796588169dSSam RavnborgLDFLAGS_MODULE  =
5801da177e4SLinus TorvaldsCFLAGS_KERNEL	=
5812f7ab126SMiguel OjedaRUSTFLAGS_KERNEL =
5821da177e4SLinus TorvaldsAFLAGS_KERNEL	=
5838debed3eSMasahiro YamadaLDFLAGS_vmlinux =
5841da177e4SLinus Torvalds
585abbf1590SDavid Howells# Use USERINCLUDE when you must reference the UAPI directories only.
586abbf1590SDavid HowellsUSERINCLUDE    := \
5879d022c54SMasahiro Yamada		-I$(srctree)/arch/$(SRCARCH)/include/uapi \
5889d022c54SMasahiro Yamada		-I$(objtree)/arch/$(SRCARCH)/include/generated/uapi \
589abbf1590SDavid Howells		-I$(srctree)/include/uapi \
5903308b285SArnd Bergmann		-I$(objtree)/include/generated/uapi \
591ce6ed1c4SMasahiro Yamada                -include $(srctree)/include/linux/compiler-version.h \
592abbf1590SDavid Howells                -include $(srctree)/include/linux/kconfig.h
593abbf1590SDavid Howells
5941da177e4SLinus Torvalds# Use LINUXINCLUDE when you must reference the include/ directory.
5951da177e4SLinus Torvalds# Needed to be compatible with the O= option
596abbf1590SDavid HowellsLINUXINCLUDE    := \
5979d022c54SMasahiro Yamada		-I$(srctree)/arch/$(SRCARCH)/include \
5989d022c54SMasahiro Yamada		-I$(objtree)/arch/$(SRCARCH)/include/generated \
59913b25489SMasahiro Yamada		-I$(srctree)/include \
600f8224f7fSMasahiro Yamada		-I$(objtree)/include \
601f8224f7fSMasahiro Yamada		$(USERINCLUDE)
6021da177e4SLinus Torvalds
60342a92bccSMasahiro YamadaKBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-PIE
6040817d259SAlexey Dobriyan
6050817d259SAlexey DobriyanKBUILD_CFLAGS :=
6060817d259SAlexey DobriyanKBUILD_CFLAGS += -fshort-wchar
6070817d259SAlexey DobriyanKBUILD_CFLAGS += -funsigned-char
6080817d259SAlexey DobriyanKBUILD_CFLAGS += -fno-common
6090817d259SAlexey DobriyanKBUILD_CFLAGS += -fno-PIE
6100817d259SAlexey DobriyanKBUILD_CFLAGS += -fno-strict-aliasing
6110817d259SAlexey Dobriyan
612433dc2ebSMasahiro YamadaKBUILD_CPPFLAGS := -D__KERNEL__
6132f7ab126SMiguel OjedaKBUILD_RUSTFLAGS := $(rust_common_flags) \
6142f7ab126SMiguel Ojeda		    -Cpanic=abort -Cembed-bitcode=n -Clto=n \
6152f7ab126SMiguel Ojeda		    -Cforce-unwind-tables=n -Ccodegen-units=1 \
6162f7ab126SMiguel Ojeda		    -Csymbol-mangling-version=v0 \
6172f7ab126SMiguel Ojeda		    -Crelocation-model=static \
6182f7ab126SMiguel Ojeda		    -Zfunction-sections=n \
619f8f88aa2SMiguel Ojeda		    -Wclippy::float_arithmetic
6204a44b174SAlice RyhlKBUILD_RUSTFLAGS_OPTION_CHKS :=
6212f7ab126SMiguel Ojeda
62280c00ba9SSam RavnborgKBUILD_AFLAGS_KERNEL :=
62380c00ba9SSam RavnborgKBUILD_CFLAGS_KERNEL :=
6242f7ab126SMiguel OjedaKBUILD_RUSTFLAGS_KERNEL :=
6256588169dSSam RavnborgKBUILD_AFLAGS_MODULE  := -DMODULE
6266588169dSSam RavnborgKBUILD_CFLAGS_MODULE  := -DMODULE
6272f7ab126SMiguel OjedaKBUILD_RUSTFLAGS_MODULE := --cfg MODULE
62810df0638SMasahiro YamadaKBUILD_LDFLAGS_MODULE :=
629d503ac53SMasahiro YamadaKBUILD_LDFLAGS :=
6305241ab4cSMasahiro YamadaCLANG_FLAGS :=
6311da177e4SLinus Torvalds
6322f7ab126SMiguel Ojedaifeq ($(KBUILD_CLIPPY),1)
6332f7ab126SMiguel Ojeda	RUSTC_OR_CLIPPY_QUIET := CLIPPY
6342f7ab126SMiguel Ojeda	RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER)
6352f7ab126SMiguel Ojedaelse
6362f7ab126SMiguel Ojeda	RUSTC_OR_CLIPPY_QUIET := RUSTC
6372f7ab126SMiguel Ojeda	RUSTC_OR_CLIPPY = $(RUSTC)
6382f7ab126SMiguel Ojedaendif
6392f7ab126SMiguel Ojeda
6402f7ab126SMiguel Ojeda# Allows the usage of unstable features in stable compilers.
6412f7ab126SMiguel Ojedaexport RUSTC_BOOTSTRAP := 1
6422f7ab126SMiguel Ojeda
6437d56786eSMiguel Ojeda# Allows finding `.clippy.toml` in out-of-srctree builds.
6447d56786eSMiguel Ojedaexport CLIPPY_CONF_DIR := $(srctree)
6457d56786eSMiguel Ojeda
646d5ea4fecSChun-Tse Shaoexport ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC HOSTPKG_CONFIG
6473a2486ccSGary Guoexport RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK
6482f7ab126SMiguel Ojedaexport HOSTRUSTC KBUILD_HOSTRUSTFLAGS
649d9b5665fSVasily Gorbikexport CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
650d8d2d382SMasahiro Yamadaexport PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
65173d210e9SMichał Górnyexport KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD TAR
652ceff0757SHONG Yifanexport KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS KBUILD_PROCMACROLDFLAGS LDFLAGS_MODULE
653f67695c9SElliot Bermanexport KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
6541da177e4SLinus Torvalds
655d503ac53SMasahiro Yamadaexport KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
6560e410e15SAndrey Konovalovexport KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
6574a44b174SAlice Ryhlexport KBUILD_RUSTFLAGS RUSTFLAGS_KERNEL RUSTFLAGS_MODULE KBUILD_RUSTFLAGS_OPTION_CHKS
658222d394dSSam Ravnborgexport KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
6592f7ab126SMiguel Ojedaexport KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_RUSTFLAGS_MODULE KBUILD_LDFLAGS_MODULE
6602f7ab126SMiguel Ojedaexport KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL KBUILD_RUSTFLAGS_KERNEL
6611da177e4SLinus Torvalds
6621da177e4SLinus Torvalds# Files to ignore in find ... statements
6631da177e4SLinus Torvalds
664ae63b2d7SPrarit Bhargavaexport RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o    \
665ae63b2d7SPrarit Bhargava			  -name CVS -o -name .pc -o -name .hg -o -name .git \) \
666ae63b2d7SPrarit Bhargava			  -prune -o
6671da177e4SLinus Torvalds
6681da177e4SLinus Torvalds# ===========================================================================
6691da177e4SLinus Torvalds# Rules shared between *config targets and build targets
6701da177e4SLinus Torvalds
671312a3d09SCao jin# Basic helpers built in scripts/basic/
6724f193362SPaul SmithPHONY += scripts_basic
67375f7c47cSThomas Weißschuhscripts_basic: KBUILD_HOSTCFLAGS := $(KBUILD_HOSTCFLAGS)
67475f7c47cSThomas Weißschuhscripts_basic: KBUILD_HOSTLDFLAGS := $(KBUILD_HOSTLDFLAGS)
6751da177e4SLinus Torvaldsscripts_basic:
6761da177e4SLinus Torvalds	$(Q)$(MAKE) $(build)=scripts/basic
6771da177e4SLinus Torvalds
6784f193362SPaul SmithPHONY += outputmakefile
6792728fcfaSMasahiro Yamadaifdef building_out_of_srctree
680e8e83a23SMasahiro Yamada# Before starting out-of-tree build, make sure the source tree is clean.
681fd5f0cd6SJan Beulich# outputmakefile generates a Makefile in the output directory, if using a
682fd5f0cd6SJan Beulich# separate output directory. This allows convenient use of make in the
683fd5f0cd6SJan Beulich# output directory.
6843a51ff34SVladimir Kondratiev# At the same time when output Makefile generated, generate .gitignore to
6853a51ff34SVladimir Kondratiev# ignore whole output directory
6862728fcfaSMasahiro Yamada
687a2a45ebeSMasahiro Yamadaifdef KBUILD_EXTMOD
688a2a45ebeSMasahiro Yamadaprint_env_for_makefile = \
689a2a45ebeSMasahiro Yamada	echo "export KBUILD_OUTPUT = $(objtree)"; \
690a2a45ebeSMasahiro Yamada	echo "export KBUILD_EXTMOD = $(realpath $(srcroot))" ; \
691a2a45ebeSMasahiro Yamada	echo "export KBUILD_EXTMOD_OUTPUT = $(CURDIR)"
692a2a45ebeSMasahiro Yamadaelse
693a2a45ebeSMasahiro Yamadaprint_env_for_makefile = \
694a2a45ebeSMasahiro Yamada	echo "export KBUILD_OUTPUT = $(CURDIR)"
695a2a45ebeSMasahiro Yamadaendif
696a2a45ebeSMasahiro Yamada
697c9bb03acSThomas Weißschuhfilechk_makefile = { \
698822b11a7SMasahiro Yamada	echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \
699a2a45ebeSMasahiro Yamada	$(print_env_for_makefile); \
700822b11a7SMasahiro Yamada	echo "include $(abs_srctree)/Makefile"; \
701c9bb03acSThomas Weißschuh	}
7022728fcfaSMasahiro Yamada
70339d6e68fSNicolas SchierPHONY += $(CURDIR)/Makefile
70439d6e68fSNicolas Schier$(CURDIR)/Makefile: FORCE
705c9bb03acSThomas Weißschuh	$(call filechk,makefile)
706c9bb03acSThomas Weißschuh
70739d6e68fSNicolas Schieroutputmakefile: $(CURDIR)/Makefile
70811b3d517SMasahiro Yamadaifeq ($(KBUILD_EXTMOD),)
7094b098659SMasahiro Yamada	@if [ -f $(srctree)/.config -o \
710e8e83a23SMasahiro Yamada		 -d $(srctree)/include/config -o \
711e8e83a23SMasahiro Yamada		 -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \
712e8e83a23SMasahiro Yamada		echo >&2 "***"; \
713e8e83a23SMasahiro Yamada		echo >&2 "*** The source tree is not clean, please run 'make$(if $(findstring command line, $(origin ARCH)), ARCH=$(ARCH)) mrproper'"; \
714e8e83a23SMasahiro Yamada		echo >&2 "*** in $(abs_srctree)";\
715e8e83a23SMasahiro Yamada		echo >&2 "***"; \
716e8e83a23SMasahiro Yamada		false; \
717e8e83a23SMasahiro Yamada	fi
71811b3d517SMasahiro Yamadaelse
71911b3d517SMasahiro Yamada	@if [ -f $(srcroot)/modules.order ]; then \
72011b3d517SMasahiro Yamada		echo >&2 "***"; \
72111b3d517SMasahiro Yamada		echo >&2 "*** The external module source tree is not clean."; \
72211b3d517SMasahiro Yamada		echo >&2 "*** Please run 'make -C $(abs_srctree) M=$(realpath $(srcroot)) clean'"; \
72311b3d517SMasahiro Yamada		echo >&2 "***"; \
72411b3d517SMasahiro Yamada		false; \
72511b3d517SMasahiro Yamada	fi
72611b3d517SMasahiro Yamadaendif
72711b3d517SMasahiro Yamada	$(Q)ln -fsn $(srcroot) source
728156e7cbbSMasahiro Yamada	$(Q)test -e .gitignore || \
729156e7cbbSMasahiro Yamada	{ echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
730fd5f0cd6SJan Beulichendif
7311da177e4SLinus Torvalds
732db07562aSNick Desaulniers# The expansion should be delayed until arch/$(SRCARCH)/Makefile is included.
733db07562aSNick Desaulniers# Some architectures define CROSS_COMPILE in arch/$(SRCARCH)/Makefile.
73490e5b38aSIhor Solodrai# CC_VERSION_TEXT, RUSTC_VERSION_TEXT and PAHOLE_VERSION are referenced from
73590e5b38aSIhor Solodrai# Kconfig (so they need export), and from include/config/auto.conf.cmd to
73690e5b38aSIhor Solodrai# detect the version changes between builds.
7376072b2c4SMasahiro YamadaCC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
7385134a335SMiguel OjedaRUSTC_VERSION_TEXT = $(subst $(pound),,$(shell $(RUSTC) --version 2>/dev/null))
73990e5b38aSIhor SolodraiPAHOLE_VERSION = $(shell $(srctree)/scripts/pahole-version.sh $(PAHOLE))
740db07562aSNick Desaulniers
741db07562aSNick Desaulniersifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
7426f5b41a2SNick Desaulniersinclude $(srctree)/scripts/Makefile.clang
743ae6b289aSChris Friesendif
744ae6b289aSChris Fries
74557fd251cSMasahiro Yamada# Include this also for config targets because some architectures need
74657fd251cSMasahiro Yamada# cc-cross-prefix to determine CROSS_COMPILE.
747805b2e1dSMasahiro Yamadaifdef need-compiler
74857fd251cSMasahiro Yamadainclude $(srctree)/scripts/Makefile.compiler
749805b2e1dSMasahiro Yamadaendif
75057fd251cSMasahiro Yamada
7512042b548SMasahiro Yamadaifdef config-build
7521da177e4SLinus Torvalds# ===========================================================================
7531da177e4SLinus Torvalds# *config targets only - make sure prerequisites are updated, and descend
7541da177e4SLinus Torvalds# in scripts/kconfig to make the *config target
7551da177e4SLinus Torvalds
756766b7007SSimon Glass# Read arch-specific Makefile to set KBUILD_DEFCONFIG as needed.
7571da177e4SLinus Torvalds# KBUILD_DEFCONFIG may point out an alternative default configuration
7581da177e4SLinus Torvalds# used for 'make defconfig'
7593204a7fbSMasahiro Yamadainclude $(srctree)/arch/$(SRCARCH)/Makefile
76090e5b38aSIhor Solodraiexport KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT RUSTC_VERSION_TEXT PAHOLE_VERSION
7611da177e4SLinus Torvalds
76236de077bSMasahiro Yamadaconfig: outputmakefile scripts_basic FORCE
76331110ebbSSam Ravnborg	$(Q)$(MAKE) $(build)=scripts/kconfig $@
76431110ebbSSam Ravnborg
76536de077bSMasahiro Yamada%config: outputmakefile scripts_basic FORCE
7661da177e4SLinus Torvalds	$(Q)$(MAKE) $(build)=scripts/kconfig $@
7671da177e4SLinus Torvalds
7682042b548SMasahiro Yamadaelse #!config-build
7691da177e4SLinus Torvalds# ===========================================================================
770766b7007SSimon Glass# Build targets only - this includes vmlinux, arch-specific targets, clean
7711da177e4SLinus Torvalds# targets and others. In general all targets except *config targets.
7721da177e4SLinus Torvalds
7732c1f4f12SMasahiro Yamada# If building an external module we do not care about the all: rule
774121c2a13SMasahiro Yamada# but instead __all depend on modules
7752c1f4f12SMasahiro YamadaPHONY += all
7762c1f4f12SMasahiro Yamadaifeq ($(KBUILD_EXTMOD),)
777121c2a13SMasahiro Yamada__all: all
7782c1f4f12SMasahiro Yamadaelse
779121c2a13SMasahiro Yamada__all: modules
7802c1f4f12SMasahiro Yamadaendif
7812c1f4f12SMasahiro Yamada
78232164845SMasahiro Yamadatargets :=
78332164845SMasahiro Yamada
7842c1f4f12SMasahiro Yamada# Decide whether to build built-in, modular, or both.
7852c1f4f12SMasahiro Yamada# Normally, just do built-in.
7862c1f4f12SMasahiro Yamada
7872c1f4f12SMasahiro YamadaKBUILD_MODULES :=
7881bf807b8SMasahiro YamadaKBUILD_BUILTIN := y
7892c1f4f12SMasahiro Yamada
7902c1f4f12SMasahiro Yamada# If we have only "make modules", don't compile built-in objects.
7912c1f4f12SMasahiro Yamadaifeq ($(MAKECMDGOALS),modules)
7924b50c8c4SMasahiro Yamada  KBUILD_BUILTIN :=
7932c1f4f12SMasahiro Yamadaendif
7942c1f4f12SMasahiro Yamada
7952c1f4f12SMasahiro Yamada# If we have "make <whatever> modules", compile modules
7962c1f4f12SMasahiro Yamada# in addition to whatever we do anyway.
7972c1f4f12SMasahiro Yamada# Just "make" or "make all" shall build modules as well
7982c1f4f12SMasahiro Yamada
799e72b635cSLuis Augensteinifneq ($(filter all modules nsdeps compile_commands.json clang-% sbom,$(MAKECMDGOALS)),)
8001bf807b8SMasahiro Yamada  KBUILD_MODULES := y
8012c1f4f12SMasahiro Yamadaendif
8022c1f4f12SMasahiro Yamada
8032c1f4f12SMasahiro Yamadaifeq ($(MAKECMDGOALS),)
8041bf807b8SMasahiro Yamada  KBUILD_MODULES := y
8052c1f4f12SMasahiro Yamadaendif
8062c1f4f12SMasahiro Yamada
8072c1f4f12SMasahiro Yamadaexport KBUILD_MODULES KBUILD_BUILTIN
8082c1f4f12SMasahiro Yamada
8092042b548SMasahiro Yamadaifdef need-config
810214c0eeaSMasahiro Yamadainclude $(objtree)/include/config/auto.conf
811d93a18f2SMasahiro Yamadaendif
812d93a18f2SMasahiro Yamada
813ec4c2827SNathan ChancellorCC_FLAGS_DIALECT := -std=gnu11
814ec4c2827SNathan Chancellor# Allow including a tagged struct or union anonymously in another struct/union.
8150d3fccf6SNathan ChancellorCC_FLAGS_DIALECT += $(CONFIG_CC_MS_EXTENSIONS)
816ec4c2827SNathan Chancellor# Clang enables warnings about GNU and Microsoft extensions by default, disable
817ec4c2827SNathan Chancellor# them because this is expected with the above options.
818ec4c2827SNathan Chancellorifdef CONFIG_CC_IS_CLANG
819ec4c2827SNathan ChancellorCC_FLAGS_DIALECT += -Wno-gnu
820ec4c2827SNathan ChancellorCC_FLAGS_DIALECT += -Wno-microsoft-anon-tag
821ec4c2827SNathan Chancellorendif
822ec4c2827SNathan Chancellorexport CC_FLAGS_DIALECT
823ec4c2827SNathan ChancellorKBUILD_CFLAGS += $(CC_FLAGS_DIALECT)
824ec4c2827SNathan Chancellor
8251da177e4SLinus Torvaldsifeq ($(KBUILD_EXTMOD),)
8261da177e4SLinus Torvalds# Objects we will link into vmlinux / subdirs we need to visit
8275750121aSMasahiro Yamadacore-y		:=
8285750121aSMasahiro Yamadadrivers-y	:=
8291da177e4SLinus Torvaldslibs-y		:= lib/
8301da177e4SLinus Torvaldsendif # KBUILD_EXTMOD
8311da177e4SLinus Torvalds
832315bab4eSMasahiro Yamada# The all: target is the default when no target is given on the
833315bab4eSMasahiro Yamada# command line.
834315bab4eSMasahiro Yamada# This allow a user to issue only 'make' to build a kernel including modules
835315bab4eSMasahiro Yamada# Defaults to vmlinux, but the arch makefile usually adds further targets
836315bab4eSMasahiro Yamadaall: vmlinux
837315bab4eSMasahiro Yamada
838b1f4ff74SPaulo Zanoni# The arch Makefiles can override CC_FLAGS_FTRACE. We may also append it later.
839b1f4ff74SPaulo Zanoniifdef CONFIG_FUNCTION_TRACER
840b1f4ff74SPaulo Zanoni  CC_FLAGS_FTRACE := -pg
841b1f4ff74SPaulo Zanoniendif
842b1f4ff74SPaulo Zanoni
843e30f8e61SSteven Rostedtifdef CONFIG_TRACEPOINTS
844e30f8e61SSteven Rostedt# To check for unused tracepoints (tracepoints that are defined but never
845e30f8e61SSteven Rostedt# called), run with:
846e30f8e61SSteven Rostedt#
847e30f8e61SSteven Rostedt# make UT=1
848e30f8e61SSteven Rostedt#
849e30f8e61SSteven Rostedt# Each unused tracepoints can take up to 5KB of memory in the running kernel.
850e30f8e61SSteven Rostedt# It is best to remove any that are not used.
851e30f8e61SSteven Rostedt#
852e30f8e61SSteven Rostedt# This command line option will be removed when all current unused
853e30f8e61SSteven Rostedt# tracepoints are removed.
854e30f8e61SSteven Rostedt
855e30f8e61SSteven Rostedtifeq ("$(origin UT)", "command line")
856e30f8e61SSteven Rostedt  WARN_ON_UNUSED_TRACEPOINTS := $(UT)
857e30f8e61SSteven Rostedtendif
858e30f8e61SSteven Rostedtendif # CONFIG_TRACEPOINTS
859e30f8e61SSteven Rostedt
860e30f8e61SSteven Rostedtexport WARN_ON_UNUSED_TRACEPOINTS
861e30f8e61SSteven Rostedt
862b2aa1535SMiguel Ojeda# Per-version Rust flags. These are like `rust_common_flags`, but may
863b2aa1535SMiguel Ojeda# depend on the Rust compiler version (e.g. using `rustc-min-version`).
8642e2f8b5aSMiguel Ojeda#
8652e2f8b5aSMiguel Ojeda# `-Aclippy::precedence`: the lint was extended in Rust 1.85.0 to
8662e2f8b5aSMiguel Ojeda# include bitmasking and shift operations. However, because it generated
8672e2f8b5aSMiguel Ojeda# many hits, in Rust 1.86.0 it was split into a new `precedence_bits`
8682e2f8b5aSMiguel Ojeda# lint which is not enabled by default.
8692e2f8b5aSMiguel Ojedarust_common_flags_per_version := \
8702e2f8b5aSMiguel Ojeda    $(if $(call rustc-min-version,108600),,-Aclippy::precedence)
871b2aa1535SMiguel Ojeda
872b2aa1535SMiguel Ojedarust_common_flags += $(rust_common_flags_per_version)
873b2aa1535SMiguel OjedaKBUILD_HOSTRUSTFLAGS += $(rust_common_flags_per_version) $(HOSTRUSTFLAGS)
874b2aa1535SMiguel OjedaKBUILD_RUSTFLAGS += $(rust_common_flags_per_version)
875b2aa1535SMiguel Ojeda
8763204a7fbSMasahiro Yamadainclude $(srctree)/arch/$(SRCARCH)/Makefile
877315bab4eSMasahiro Yamada
8782042b548SMasahiro Yamadaifdef need-config
8792042b548SMasahiro Yamadaifdef may-sync-config
880315bab4eSMasahiro Yamada# Read in dependencies to all Kconfig* files, make sure to run syncconfig if
881315bab4eSMasahiro Yamada# changes are detected. This should be included after arch/$(SRCARCH)/Makefile
882315bab4eSMasahiro Yamada# because some architectures define CROSS_COMPILE there.
883d2f8ae0eSMasahiro Yamadainclude include/config/auto.conf.cmd
8841da177e4SLinus Torvalds
88505850719SMasahiro Yamada$(KCONFIG_CONFIG):
88605850719SMasahiro Yamada	@echo >&2 '***'
88705850719SMasahiro Yamada	@echo >&2 '*** Configuration file "$@" not found!'
88805850719SMasahiro Yamada	@echo >&2 '***'
88905850719SMasahiro Yamada	@echo >&2 '*** Please run some configurator (e.g. "make oldconfig" or'
89005850719SMasahiro Yamada	@echo >&2 '*** "make menuconfig" or "make xconfig").'
89105850719SMasahiro Yamada	@echo >&2 '***'
89205850719SMasahiro Yamada	@/bin/false
8931da177e4SLinus Torvalds
89461277981SUlf Magnusson# The actual configuration files used during the build are stored in
89561277981SUlf Magnusson# include/generated/ and include/config/. Update them if .config is newer than
89661277981SUlf Magnusson# include/config/auto.conf (which mirrors .config).
8979390dff6SMasahiro Yamada#
8989390dff6SMasahiro Yamada# This exploits the 'multi-target pattern rule' trick.
8999390dff6SMasahiro Yamada# The syncconfig should be executed only once to make all the targets.
900f463c351SMasahiro Yamada# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
901d952cfafSMasahiro Yamada#
902d952cfafSMasahiro Yamada# Do not use $(call cmd,...) here. That would suppress prompts from syncconfig,
903d952cfafSMasahiro Yamada# so you cannot notice that Kconfig is waiting for the user input.
9042f7ab126SMiguel Ojeda%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg: $(KCONFIG_CONFIG)
905d952cfafSMasahiro Yamada	$(Q)$(kecho) "  SYNC    $@"
906d952cfafSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
9072042b548SMasahiro Yamadaelse # !may-sync-config
908d7942413SMasahiro Yamada# External modules and some install targets need include/generated/autoconf.h
909d7942413SMasahiro Yamada# and include/config/auto.conf but do not care if they are up-to-date.
910ec873a4cSMasahiro Yamada# Use auto.conf to show the error message
9119ee4e336SSam Ravnborg
912214c0eeaSMasahiro Yamadachecked-configs := $(addprefix $(objtree)/, include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf)
913ec873a4cSMasahiro Yamadamissing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs))
914ec873a4cSMasahiro Yamada
915ec873a4cSMasahiro Yamadaifdef missing-configs
916214c0eeaSMasahiro YamadaPHONY += $(objtree)/include/config/auto.conf
9179ee4e336SSam Ravnborg
918214c0eeaSMasahiro Yamada$(objtree)/include/config/auto.conf:
919ec873a4cSMasahiro Yamada	@echo   >&2 '***'
920ec873a4cSMasahiro Yamada	@echo   >&2 '***  ERROR: Kernel configuration is invalid. The following files are missing:'
921ec873a4cSMasahiro Yamada	@printf >&2 '***    - %s\n' $(missing-configs)
922ec873a4cSMasahiro Yamada	@echo   >&2 '***  Run "make oldconfig && make prepare" on kernel source to fix it.'
923ec873a4cSMasahiro Yamada	@echo   >&2 '***'
924ec873a4cSMasahiro Yamada	@/bin/false
925ec873a4cSMasahiro Yamadaendif
9269ee4e336SSam Ravnborg
927d7942413SMasahiro Yamadaendif # may-sync-config
9282042b548SMasahiro Yamadaendif # need-config
9291da177e4SLinus Torvalds
9307d73c3e9SNick DesaulniersKBUILD_CFLAGS	+= -fno-delete-null-pointer-checks
931a1c48bb1SGeert Uytterhoeven
93215f5db60SMasahiro Yamadaifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
933a0f97e06SSam RavnborgKBUILD_CFLAGS += -O2
9342f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += -Copt-level=2
93515f5db60SMasahiro Yamadaelse ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
93615f5db60SMasahiro YamadaKBUILD_CFLAGS += -Os
9372f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += -Copt-level=s
9381da177e4SLinus Torvaldsendif
9391da177e4SLinus Torvalds
9402f7ab126SMiguel Ojeda# Always set `debug-assertions` and `overflow-checks` because their default
9412f7ab126SMiguel Ojeda# depends on `opt-level` and `debug-assertions`, respectively.
9422f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += -Cdebug-assertions=$(if $(CONFIG_RUST_DEBUG_ASSERTIONS),y,n)
9432f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
9442f7ab126SMiguel Ojeda
94569102311SJiri Kosina# Tell gcc to never replace conditional load with a non-conditional one
9467d73c3e9SNick Desaulniersifdef CONFIG_CC_IS_GCC
9477d73c3e9SNick Desaulniers# gcc-10 renamed --param=allow-store-data-races=0 to
9487d73c3e9SNick Desaulniers# -fno-allow-store-data-races.
94969102311SJiri KosinaKBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
950b1112139SSergei TrofimovichKBUILD_CFLAGS	+= $(call cc-option,-fno-allow-store-data-races)
9517d73c3e9SNick Desaulniersendif
95269102311SJiri Kosina
9531873e870SAndi Kleenifdef CONFIG_READABLE_ASM
9541873e870SAndi Kleen# Disable optimizations that make assembler listings hard to read.
9551873e870SAndi Kleen# reorder blocks reorders the control in the function
9561873e870SAndi Kleen# ipa clone creates specialized cloned functions
9571873e870SAndi Kleen# partial inlining inlines only parts of functions
9587d73c3e9SNick DesaulniersKBUILD_CFLAGS += -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining
9591873e870SAndi Kleenendif
9601873e870SAndi Kleen
961893ab004SMasahiro Yamadastackp-flags-y                                    := -fno-stack-protector
962050e9baaSLinus Torvaldsstackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
963050e9baaSLinus Torvaldsstackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
9642a61f474SMasahiro Yamada
9652a61f474SMasahiro YamadaKBUILD_CFLAGS += $(stackp-flags-y)
966e06b8b98SSam Ravnborg
9671da177e4SLinus Torvaldsifdef CONFIG_FRAME_POINTER
968a0f97e06SSam RavnborgKBUILD_CFLAGS	+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
9692f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += -Cforce-frame-pointers=y
970191f49f1SAlice Ryhl# Work around rustc bug on compilers without
971191f49f1SAlice Ryhl# https://github.com/rust-lang/rust/pull/156980.
972191f49f1SAlice RyhlKBUILD_RUSTFLAGS += $(if $(call rustc-min-version,109800),,-Zllvm_module_flag=frame-pointer:u32:2:max)
9731da177e4SLinus Torvaldselse
9747e9501fdSRabin Vincent# Some targets (ARM with Thumb2, for example), can't be built with frame
9757e9501fdSRabin Vincent# pointers.  For those, we don't have FUNCTION_TRACER automatically
9767e9501fdSRabin Vincent# select FRAME_POINTER.  However, FUNCTION_TRACER adds -pg, and this is
9777e9501fdSRabin Vincent# incompatible with -fomit-frame-pointer with current GCC, so we don't use
9787e9501fdSRabin Vincent# -fomit-frame-pointer with FUNCTION_TRACER.
9792f7ab126SMiguel Ojeda# In the Rust target specification, "frame-pointer" is set explicitly
9802f7ab126SMiguel Ojeda# to "may-omit".
9817e9501fdSRabin Vincentifndef CONFIG_FUNCTION_TRACER
982a0f97e06SSam RavnborgKBUILD_CFLAGS	+= -fomit-frame-pointer
9831da177e4SLinus Torvaldsendif
9847e9501fdSRabin Vincentendif
9851da177e4SLinus Torvalds
986f0fe00d4Sglider@google.com# Initialize all stack variables with a 0xAA pattern.
987f0fe00d4Sglider@google.comifdef CONFIG_INIT_STACK_ALL_PATTERN
988709a972eSKees CookKBUILD_CFLAGS	+= -ftrivial-auto-var-init=pattern
989709a972eSKees Cookendif
990709a972eSKees Cook
991f0fe00d4Sglider@google.com# Initialize all stack variables with a zero value.
992f0fe00d4Sglider@google.comifdef CONFIG_INIT_STACK_ALL_ZERO
993f0fe00d4Sglider@google.comKBUILD_CFLAGS	+= -ftrivial-auto-var-init=zero
994607e57c6SKees Cookifdef CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
995607e57c6SKees Cook# https://github.com/llvm/llvm-project/issues/44842
9964e3feaadSNathan ChancellorCC_AUTO_VAR_INIT_ZERO_ENABLER := -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
9974e3feaadSNathan Chancellorexport CC_AUTO_VAR_INIT_ZERO_ENABLER
9984e3feaadSNathan ChancellorKBUILD_CFLAGS	+= $(CC_AUTO_VAR_INIT_ZERO_ENABLER)
999f0fe00d4Sglider@google.comendif
1000f02003c8SKees Cookendif
1001f0fe00d4Sglider@google.com
1002feb662d9SMarco Elverifdef CONFIG_KMALLOC_PARTITION_TYPED
1003feb662d9SMarco Elver# KMALLOC_PARTITION_CACHES_NR + 1
1004feb662d9SMarco ElverKBUILD_CFLAGS	+= -falloc-token-max=16
1005feb662d9SMarco Elverendif
1006feb662d9SMarco Elver
1007150a04d8SBill Wendlingifdef CONFIG_CC_IS_CLANG
1008150a04d8SBill Wendlingifdef CONFIG_CC_HAS_COUNTED_BY_PTR
1009150a04d8SBill WendlingKBUILD_CFLAGS	+= -fexperimental-late-parse-attributes
1010150a04d8SBill Wendlingendif
1011150a04d8SBill Wendlingendif
1012150a04d8SBill Wendling
1013c1525349SMasahiro Yamada# Explicitly clear padding bits during variable initialization
1014c1525349SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
1015c1525349SMasahiro Yamada
101639218ff4SKees Cook# While VLAs have been removed, GCC produces unreachable stack probes
101739218ff4SKees Cook# for the randomize_kstack_offset feature. Disable it for all compilers.
101839218ff4SKees CookKBUILD_CFLAGS	+= $(call cc-option, -fno-stack-clash-protection)
101939218ff4SKees Cook
10207454048dSKees Cook# Get details on warnings generated due to GCC value tracking.
10217454048dSKees CookKBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-context=2)
10227454048dSKees Cook
102340492775SJustin Stitt# Show inlining notes for __attribute__((warning/error)) call chains.
102440492775SJustin Stitt# GCC supports this unconditionally while Clang 23+ provides a flag.
102540492775SJustin StittKBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-inlining-chain)
102640492775SJustin Stitt
1027a82adfd5SKees Cook# Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
1028a82adfd5SKees Cookifdef CONFIG_ZERO_CALL_USED_REGS
1029a82adfd5SKees CookKBUILD_CFLAGS	+= -fzero-call-used-regs=used-gpr
1030a82adfd5SKees Cookendif
1031a82adfd5SKees Cook
1032606576ceSSteven Rostedtifdef CONFIG_FUNCTION_TRACER
10333b15cdc1SSami Tolvanenifdef CONFIG_FTRACE_MCOUNT_USE_CC
103407d04081SVasily Gorbik  CC_FLAGS_FTRACE	+= -mrecord-mcount
10352f4df001SVasily Gorbik  ifdef CONFIG_HAVE_NOP_MCOUNT
10362f4df001SVasily Gorbik    ifeq ($(call cc-option-yn, -mnop-mcount),y)
10372f4df001SVasily Gorbik      CC_FLAGS_FTRACE	+= -mnop-mcount
10382f4df001SVasily Gorbik      CC_FLAGS_USING	+= -DCC_USING_NOP_MCOUNT
10392f4df001SVasily Gorbik    endif
10402f4df001SVasily Gorbik  endif
104107d04081SVasily Gorbikendif
104222c8542dSSami Tolvanenifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
1043280981d6SSathvika Vasireddy  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
104422c8542dSSami Tolvanen    CC_FLAGS_USING	+= -DCC_USING_NOP_MCOUNT
104522c8542dSSami Tolvanen  endif
1046280981d6SSathvika Vasireddyendif
10473b15cdc1SSami Tolvanenifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
10483b15cdc1SSami Tolvanen  ifdef CONFIG_HAVE_C_RECORDMCOUNT
10493b15cdc1SSami Tolvanen    BUILD_C_RECORDMCOUNT := y
10503b15cdc1SSami Tolvanen    export BUILD_C_RECORDMCOUNT
10513b15cdc1SSami Tolvanen  endif
10523b15cdc1SSami Tolvanenendif
1053a2546faeSSteven Rostedtifdef CONFIG_HAVE_FENTRY
10547d73c3e9SNick Desaulniers  # s390-linux-gnu-gcc did not support -mfentry until gcc-9.
1055f28bc3c3SVasily Gorbik  ifeq ($(call cc-option-yn, -mfentry),y)
1056f28bc3c3SVasily Gorbik    CC_FLAGS_FTRACE	+= -mfentry
1057f28bc3c3SVasily Gorbik    CC_FLAGS_USING	+= -DCC_USING_FENTRY
1058a2546faeSSteven Rostedt  endif
1059f28bc3c3SVasily Gorbikendif
1060f28bc3c3SVasily Gorbikexport CC_FLAGS_FTRACE
1061f28bc3c3SVasily GorbikKBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
1062f28bc3c3SVasily GorbikKBUILD_AFLAGS	+= $(CC_FLAGS_USING)
106316444a8aSArnaldo Carvalho de Meloendif
106416444a8aSArnaldo Carvalho de Melo
106591341d4bSSam Ravnborg# We trigger additional mismatches with less inlining
106691341d4bSSam Ravnborgifdef CONFIG_DEBUG_SECTION_MISMATCH
10677d73c3e9SNick DesaulniersKBUILD_CFLAGS += -fno-inline-functions-called-once
106891341d4bSSam Ravnborgendif
106991341d4bSSam Ravnborg
10702f7ab126SMiguel Ojeda# `rustc`'s `-Zfunction-sections` applies to data too (as of 1.59.0).
107190ad4052SMasahiro Yamadaifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
1072e85d1d65SMasahiro YamadaKBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
10732f7ab126SMiguel OjedaKBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
1074e85d1d65SMasahiro YamadaLDFLAGS_vmlinux += --gc-sections
107590ad4052SMasahiro Yamadaendif
107690ad4052SMasahiro Yamada
1077d08b9f0cSSami Tolvanenifdef CONFIG_SHADOW_CALL_STACK
10789beccca0SArd Biesheuvelifndef CONFIG_DYNAMIC_SCS
1079d08b9f0cSSami TolvanenCC_FLAGS_SCS	:= -fsanitize=shadow-call-stack
1080d08b9f0cSSami TolvanenKBUILD_CFLAGS	+= $(CC_FLAGS_SCS)
1081d077242dSAlice RyhlKBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack
10829beccca0SArd Biesheuvelendif
1083d08b9f0cSSami Tolvanenexport CC_FLAGS_SCS
1084d08b9f0cSSami Tolvanenendif
1085d08b9f0cSSami Tolvanen
10860bff7711SMiguel Ojedaifdef CONFIG_RUST_INLINE_HELPERS
10870bff7711SMiguel Ojeda# `rustc` normally emits traps for unreachable paths during code generation.
10880bff7711SMiguel Ojeda# With inline helpers, Clang performs code generation from the linked bitcode
10890bff7711SMiguel Ojeda# instead, so request the same behavior explicitly. Otherwise `objtool` may
10900bff7711SMiguel Ojeda# follow an impossible Rust path into the next function.
10910bff7711SMiguel OjedaCC_FLAGS_RUST_INLINE_HELPERS := -mllvm -trap-unreachable \
10920bff7711SMiguel Ojeda				-mllvm -no-trap-after-noreturn
10930bff7711SMiguel Ojedaexport CC_FLAGS_RUST_INLINE_HELPERS
10940bff7711SMiguel Ojedaendif
10950bff7711SMiguel Ojeda
1096dc5723b0SSami Tolvanenifdef CONFIG_LTO_CLANG
10979f2aee8fSRong Xuifdef CONFIG_LTO_CLANG_FULL
10982b868952SAlexander LobakinCC_FLAGS_LTO	:= -flto
10999f2aee8fSRong Xuelse
11009f2aee8fSRong XuCC_FLAGS_LTO	:= -flto=thin -fsplit-lto-unit
11019f2aee8fSRong Xu
11029f2aee8fSRong Xu# These LLVM options were initially added with only in-process ThinLTO
11039f2aee8fSRong Xu# support, so avoid distributed ThinLTO support for now.
11049f2aee8fSRong Xuifdef CONFIG_LTO_CLANG_THIN
11059f2aee8fSRong XuKBUILD_LDFLAGS += $(call ld-option,--lto-whole-program-visibility -mllvm -always-rename-promoted-locals=false)
11069f2aee8fSRong Xuendif
1107dc5723b0SSami Tolvanenendif
1108dc5723b0SSami TolvanenCC_FLAGS_LTO	+= -fvisibility=hidden
110922d429e7SSami Tolvanen
111022d429e7SSami Tolvanen# Limit inlining across translation units to reduce binary size
111122d429e7SSami TolvanenKBUILD_LDFLAGS += -mllvm -import-instr-limit=5
11120236526dSTor Vicendif
1113dc5723b0SSami Tolvanen
1114dc5723b0SSami Tolvanenifdef CONFIG_LTO
11155e95325fSSami TolvanenKBUILD_CFLAGS	+= -fno-lto $(CC_FLAGS_LTO)
11165e95325fSSami TolvanenKBUILD_AFLAGS	+= -fno-lto
1117dc5723b0SSami Tolvanenexport CC_FLAGS_LTO
1118dc5723b0SSami Tolvanenendif
1119dc5723b0SSami Tolvanen
112023ef9d43SKees Cookifdef CONFIG_CFI
112189245600SSami TolvanenCC_FLAGS_CFI	:= -fsanitize=kcfi
1122ce4a2620SAlice Ryhlifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS
1123ce4a2620SAlice Ryhl	CC_FLAGS_CFI	+= -fsanitize-cfi-icall-experimental-normalize-integers
1124ce4a2620SAlice Ryhlendif
11250c92385dSPeter Zijlstraifdef CONFIG_FINEIBT_BHI
11260c92385dSPeter Zijlstra	CC_FLAGS_CFI	+= -fsanitize-kcfi-arity
11270c92385dSPeter Zijlstraendif
1128ca627e63SMatthew Maurerifdef CONFIG_RUST
1129ca627e63SMatthew Maurer	# Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects
1130ca627e63SMatthew Maurer	# CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
113129b03d1dSGary Guo	# Disable function merging as LLVM incorrectly merges functions with different KCFI types.
113229b03d1dSGary Guo	RUSTC_FLAGS_CFI   := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers -Zmerge-functions=disabled
1133ca627e63SMatthew Maurer	KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI)
1134ca627e63SMatthew Maurer	export RUSTC_FLAGS_CFI
1135ca627e63SMatthew Maurerendif
1136cf68fffbSSami TolvanenKBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
1137cf68fffbSSami Tolvanenexport CC_FLAGS_CFI
1138cf68fffbSSami Tolvanenendif
1139cf68fffbSSami Tolvanen
11406cbd1d6dSSamuel Holland# Architectures can define flags to add/remove for floating-point support
11416cbd1d6dSSamuel HollandCC_FLAGS_FPU	+= -D_LINUX_FPU_COMPILATION_UNIT
11426cbd1d6dSSamuel Hollandexport CC_FLAGS_FPU
11436cbd1d6dSSamuel Hollandexport CC_FLAGS_NO_FPU
11446cbd1d6dSSamuel Holland
1145d49a0626SPeter Zijlstraifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
11465270316cSPetr Pavlu# Set the minimal function alignment. Use the newer GCC option
11475270316cSPetr Pavlu# -fmin-function-alignment if it is available, or fall back to -falign-funtions.
11485270316cSPetr Pavlu# See also CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
11495270316cSPetr Pavluifdef CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT
11505270316cSPetr PavluKBUILD_CFLAGS += -fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT)
11515270316cSPetr Pavluelse
1152d49a0626SPeter ZijlstraKBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
115309c60546SFeng Tangendif
11545270316cSPetr Pavluendif
115509c60546SFeng Tang
1156e8e69931SSam Ravnborg# arch Makefile may override CC so keep this after arch Makefile is included
115704e85bbfSAlexey DobriyanNOSTDINC_FLAGS += -nostdinc
1158e8e69931SSam Ravnborg
1159df8fc4e9SKees Cook# To gain proper coverage for CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE,
1160df8fc4e9SKees Cook# the kernel uses only C99 flexible arrays for dynamically sized trailing
1161df8fc4e9SKees Cook# arrays. Enforce this for everything that may examine structure sizes and
1162df8fc4e9SKees Cook# perform bounds checking.
1163df8fc4e9SKees CookKBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
1164df8fc4e9SKees Cook
1165fe8d0a41SKirill Smelkov# disable invalid "can't wrap" optimizations for signed / pointers
11668b42cf2fSMasahiro YamadaKBUILD_CFLAGS	+= -fno-strict-overflow
1167d0115552SLinus Torvalds
11683ce120b1SLinus Torvalds# Make sure -fstack-check isn't enabled (like gentoo apparently did)
11697d4eb0d8SMasahiro YamadaKBUILD_CFLAGS  += -fno-stack-check
11703ce120b1SLinus Torvalds
11718f7f5c9fSAndi Kleen# conserve stack if available
11727d73c3e9SNick Desaulniersifdef CONFIG_CC_IS_GCC
11737d73c3e9SNick DesaulniersKBUILD_CFLAGS   += -fconserve-stack
11747d73c3e9SNick Desaulniersendif
11758f7f5c9fSAndi Kleen
117684ffc79bSNathan Chancellor# Ensure compilers do not transform certain loops into calls to wcslen()
117784ffc79bSNathan ChancellorKBUILD_CFLAGS += -fno-builtin-wcslen
117884ffc79bSNathan Chancellor
117956cb9b7dSKonstantin KhorenkoCFLAGS_GCOV	:= -fprofile-arcs -ftest-coverage
118056cb9b7dSKonstantin Khorenkoifdef CONFIG_CC_IS_GCC
118156cb9b7dSKonstantin KhorenkoCFLAGS_GCOV	+= -fno-tree-loop-im
118256cb9b7dSKonstantin Khorenko# Use atomic counter updates to avoid concurrent-access crashes in GCOV.
118356cb9b7dSKonstantin Khorenko# Only enable if -fprofile-update=prefer-atomic does not introduce new
118456cb9b7dSKonstantin Khorenko# undefined symbols (e.g. libatomic calls that the kernel cannot link).
118556cb9b7dSKonstantin KhorenkoCFLAGS_GCOV	+= $(call try-run,\
118656cb9b7dSKonstantin Khorenko	echo 'long long x; void f(void){x++;}' | \
118756cb9b7dSKonstantin Khorenko	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -w -fprofile-arcs \
118856cb9b7dSKonstantin Khorenko	-ftest-coverage -x c - -c -o "$$TMP.base" && \
118956cb9b7dSKonstantin Khorenko	echo 'long long x; void f(void){x++;}' | \
119056cb9b7dSKonstantin Khorenko	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -w -fprofile-arcs \
119156cb9b7dSKonstantin Khorenko	-ftest-coverage -fprofile-update=prefer-atomic \
119256cb9b7dSKonstantin Khorenko	-x c - -c -o "$$TMP" && \
119356cb9b7dSKonstantin Khorenko	$(NM) "$$TMP.base" | grep ' U ' > "$$TMP.ubase" || true ; \
119456cb9b7dSKonstantin Khorenko	$(NM) "$$TMP" | grep ' U ' > "$$TMP.utest" || true ; \
119556cb9b7dSKonstantin Khorenko	cmp -s "$$TMP.ubase" "$$TMP.utest",\
119656cb9b7dSKonstantin Khorenko	-fprofile-update=prefer-atomic)
119756cb9b7dSKonstantin Khorenkoendif
119856cb9b7dSKonstantin Khorenkoexport CFLAGS_GCOV
119956cb9b7dSKonstantin Khorenko
12001d3730f0SMasahiro Yamada# change __FILE__ to the relative path to the source directory
12011d3730f0SMasahiro Yamadaifdef building_out_of_srctree
1202379b749aSThomas WeißschuhKBUILD_CPPFLAGS += -fmacro-prefix-map=$(srcroot)/=
1203dda13507SGary Guoifeq ($(call rustc-option-yn, --remap-path-scope=macro),y)
1204dda13507SGary GuoKBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/= --remap-path-scope=macro
1205dda13507SGary Guoendif
12061d3730f0SMasahiro Yamadaendif
1207a73619a8SMasahiro Yamada
1208e0fe0bbeSMasahiro Yamada# include additional Makefiles when needed
12097319256dSNathan Chancellorinclude-y			:= scripts/Makefile.warn
12106947fd96SMasahiro Yamadainclude-$(CONFIG_DEBUG_INFO)	+= scripts/Makefile.debug
121172d09184SMasahiro Yamadainclude-$(CONFIG_DEBUG_INFO_BTF)+= scripts/Makefile.btf
1212e0fe0bbeSMasahiro Yamadainclude-$(CONFIG_KASAN)		+= scripts/Makefile.kasan
1213e0fe0bbeSMasahiro Yamadainclude-$(CONFIG_KCSAN)		+= scripts/Makefile.kcsan
1214f80be457SAlexander Potapenkoinclude-$(CONFIG_KMSAN)		+= scripts/Makefile.kmsan
1215e0fe0bbeSMasahiro Yamadainclude-$(CONFIG_UBSAN)		+= scripts/Makefile.ubsan
1216e0fe0bbeSMasahiro Yamadainclude-$(CONFIG_KCOV)		+= scripts/Makefile.kcov
1217613f4b3eSKees Cookinclude-$(CONFIG_RANDSTRUCT)	+= scripts/Makefile.randstruct
121876261fc7SKees Cookinclude-$(CONFIG_KSTACK_ERASE)	+= scripts/Makefile.kstack_erase
1219315ad878SRong Xuinclude-$(CONFIG_AUTOFDO_CLANG)	+= scripts/Makefile.autofdo
1220d5dc9583SRong Xuinclude-$(CONFIG_PROPELLER_CLANG)	+= scripts/Makefile.propeller
12213269701cSMarco Elverinclude-$(CONFIG_WARN_CONTEXT_ANALYSIS) += scripts/Makefile.context-analysis
1222e0fe0bbeSMasahiro Yamadainclude-$(CONFIG_GCC_PLUGINS)	+= scripts/Makefile.gcc-plugins
1223e0fe0bbeSMasahiro Yamada
1224e0fe0bbeSMasahiro Yamadainclude $(addprefix $(srctree)/, $(include-y))
1225a86fe353SMasahiro Yamada
1226132305b3SMasahiro Yamada# scripts/Makefile.gcc-plugins is intentionally included last.
1227132305b3SMasahiro Yamada# Do not add $(call cc-option,...) below this line. When you build the kernel
1228132305b3SMasahiro Yamada# from the clean source tree, the GCC plugins do not exist at this point.
1229bf5438fcSJason Baron
12302f7ab126SMiguel Ojeda# Add user supplied CPPFLAGS, AFLAGS, CFLAGS and RUSTFLAGS as the last assignments
12318cc7af75SMasahiro YamadaKBUILD_CPPFLAGS += $(KCPPFLAGS)
12328cc7af75SMasahiro YamadaKBUILD_AFLAGS   += $(KAFLAGS)
12338cc7af75SMasahiro YamadaKBUILD_CFLAGS   += $(KCFLAGS)
12342f7ab126SMiguel OjedaKBUILD_RUSTFLAGS += $(KRUSTFLAGS)
123552bcc330SSam Ravnborg
1236a9684337SBill WendlingKBUILD_LDFLAGS_MODULE += --build-id=sha1
1237a9684337SBill WendlingLDFLAGS_vmlinux += --build-id=sha1
123818991197SRoland McGrath
123954747629SPetr Pavlu# Specific code, such as outlined KASAN checks, may be placed in
124054747629SPetr Pavlu# COMDAT-deduplicated sections. Use --force-group-allocation to resolve these
124154747629SPetr Pavlu# groups when linking modules. The option is available from ld.bfd 2.29 and
124254747629SPetr Pavlu# ld.lld 19.1.0.
124354747629SPetr PavluKBUILD_LDFLAGS_MODULE += $(call ld-option,--force-group-allocation)
124454747629SPetr Pavlu
12450d362be5SNick DesaulniersKBUILD_LDFLAGS	+= -z noexecstack
12460d362be5SNick Desaulniersifeq ($(CONFIG_LD_IS_BFD),y)
12470d362be5SNick DesaulniersKBUILD_LDFLAGS	+= $(call ld-option,--no-warn-rwx-segments)
12480d362be5SNick Desaulniersendif
12490d362be5SNick Desaulniers
12505d7d18f5SDavid Howellsifeq ($(CONFIG_STRIP_ASM_SYMS),y)
12510f1fe9d6SMasahiro YamadaLDFLAGS_vmlinux	+= -X
12525d7d18f5SDavid Howellsendif
12535d7d18f5SDavid Howells
12545cf896fbSPeter Collingbourneifeq ($(CONFIG_RELR),y)
1255ccb2d173SFangrui Song# ld.lld before 15 did not support -z pack-relative-relocs.
1256ccb2d173SFangrui SongLDFLAGS_vmlinux	+= $(call ld-option,--pack-dyn-relocs=relr,-z pack-relative-relocs)
12575cf896fbSPeter Collingbourneendif
12585cf896fbSPeter Collingbourne
125959612b24SNathan Chancellor# We never want expected sections to be placed heuristically by the
126059612b24SNathan Chancellor# linker. All sections should be explicitly named in the linker script.
126159612b24SNathan Chancellorifdef CONFIG_LD_ORPHAN_WARN
1262e1789d7cSXin LiLDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
126359612b24SNathan Chancellorendif
126459612b24SNathan Chancellor
12659b400d17SArd Biesheuvelifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),)
12669b400d17SArd BiesheuvelLDFLAGS_vmlinux	+= --emit-relocs --discard-none
12679b400d17SArd Biesheuvelendif
12689b400d17SArd Biesheuvel
1269deab487eSThomas Weißschuh# Align the architecture of userspace programs with the kernel
1270deab487eSThomas WeißschuhUSERFLAGS_FROM_KERNEL := --target=%
1271deab487eSThomas Weißschuh
1272deab487eSThomas Weißschuhifdef CONFIG_ARCH_USERFLAGS
1273deab487eSThomas WeißschuhKBUILD_USERCFLAGS += $(CONFIG_ARCH_USERFLAGS)
1274deab487eSThomas WeißschuhKBUILD_USERLDFLAGS += $(CONFIG_ARCH_USERFLAGS)
1275deab487eSThomas Weißschuhelse
1276deab487eSThomas Weißschuh# If not overridden also inherit the bit size
1277deab487eSThomas WeißschuhUSERFLAGS_FROM_KERNEL += -m32 -m64
1278deab487eSThomas Weißschuhendif
1279deab487eSThomas Weißschuh
12802d0ec4a9SThomas WeißschuhKBUILD_USERCFLAGS  += $(filter $(USERFLAGS_FROM_KERNEL), $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
12812d0ec4a9SThomas WeißschuhKBUILD_USERLDFLAGS += $(filter $(USERFLAGS_FROM_KERNEL), $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
12827f3a59dbSMasahiro Yamada
1283dfc1b168SThomas Weißschuh# userspace programs are linked via the compiler, use the correct linker
1284936599caSThomas Weißschuhifdef CONFIG_CC_IS_CLANG
1285dfc1b168SThomas WeißschuhKBUILD_USERLDFLAGS += --ld-path=$(LD)
1286dfc1b168SThomas Weißschuhendif
1287dfc1b168SThomas Weißschuh
128880591e61SLuc Van Oostenryck# make the checker run with the right architecture
128980591e61SLuc Van OostenryckCHECKFLAGS += --arch=$(ARCH)
129080591e61SLuc Van Oostenryck
129114516765SLuc Van Oostenryck# insure the checker run with the right endianness
129214516765SLuc Van OostenryckCHECKFLAGS += $(if $(CONFIG_CPU_BIG_ENDIAN),-mbig-endian,-mlittle-endian)
129314516765SLuc Van Oostenryck
12941f2f01b1SLuc Van Oostenryck# the checker needs the correct machine size
12951f2f01b1SLuc Van OostenryckCHECKFLAGS += $(if $(CONFIG_64BIT),-m64,-m32)
12961f2f01b1SLuc Van Oostenryck
1297fd69b2f7SPeter Zijlstra# Validate the checker is available and functional
1298fd69b2f7SPeter Zijlstraifneq ($(KBUILD_CHECKSRC), 0)
1299fd69b2f7SPeter Zijlstra  ifneq ($(shell $(srctree)/scripts/checker-valid.sh $(CHECK) $(CHECKFLAGS)), 1)
1300fd69b2f7SPeter Zijlstra    $(warning C=$(KBUILD_CHECKSRC) specified, but $(CHECK) is not available or not up to date)
1301fd69b2f7SPeter Zijlstra    KBUILD_CHECKSRC = 0
1302fd69b2f7SPeter Zijlstra  endif
1303fd69b2f7SPeter Zijlstraendif
1304fd69b2f7SPeter Zijlstra
13051da177e4SLinus Torvalds# Default kernel image to build when no specific target is given.
13061da177e4SLinus Torvalds# KBUILD_IMAGE may be overruled on the command line or
13071da177e4SLinus Torvalds# set in the environment
13081da177e4SLinus Torvalds# Also any assignments in arch/$(ARCH)/Makefile take precedence over
13091da177e4SLinus Torvalds# this default value
13101da177e4SLinus Torvaldsexport KBUILD_IMAGE ?= vmlinux
13111da177e4SLinus Torvalds
13121da177e4SLinus Torvalds#
13131da177e4SLinus Torvalds# INSTALL_PATH specifies where to place the updated kernel and system map
13141da177e4SLinus Torvalds# images. Default is /boot, but you can set it to other values
13151da177e4SLinus Torvaldsexport	INSTALL_PATH ?= /boot
13161da177e4SLinus Torvalds
13171da177e4SLinus Torvalds#
1318f4d4ffc0SJason Cooper# INSTALL_DTBS_PATH specifies a prefix for relocations required by build roots.
1319f4d4ffc0SJason Cooper# Like INSTALL_MOD_PATH, it isn't defined in the Makefile, but can be passed as
1320f4d4ffc0SJason Cooper# an argument if needed. Otherwise it defaults to the kernel install path
1321f4d4ffc0SJason Cooper#
1322f4d4ffc0SJason Cooperexport INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
1323f4d4ffc0SJason Cooper
1324f4d4ffc0SJason Cooper#
13251da177e4SLinus Torvalds# INSTALL_MOD_PATH specifies a prefix to MODLIB for module directory
13261da177e4SLinus Torvalds# relocations required by build roots.  This is not defined in the
1327070b98bfSSam Ravnborg# makefile but the argument can be passed to make if needed.
13281da177e4SLinus Torvalds#
13291da177e4SLinus Torvalds
1330df9df036SSam RavnborgMODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
13311da177e4SLinus Torvaldsexport MODLIB
13321da177e4SLinus Torvalds
1333e00d8880SMasahiro YamadaPHONY += prepare0
1334e2a666d5SRusty Russell
13351da177e4SLinus Torvaldsifeq ($(KBUILD_EXTMOD),)
13361da177e4SLinus Torvalds
13375750121aSMasahiro Yamadabuild-dir	:= .
13385750121aSMasahiro Yamadaclean-dirs	:= $(sort . Documentation \
133923febe37SMasahiro Yamada		     $(patsubst %/,%,$(filter %/, $(core-) \
134095fb6317SMasahiro Yamada			$(drivers-) $(libs-))))
13411da177e4SLinus Torvalds
13425750121aSMasahiro Yamadaexport ARCH_CORE	:= $(core-y)
13435750121aSMasahiro Yamadaexport ARCH_LIB		:= $(filter %/, $(libs-y))
13445750121aSMasahiro Yamadaexport ARCH_DRIVERS	:= $(drivers-y) $(drivers-m)
13451f2bfbd0SSam Ravnborg# Externally visible symbols (used by link-vmlinux.sh)
13465750121aSMasahiro Yamada
134741a77d43SMasahiro YamadaKBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)))
1348f0d50ca0SMasahiro YamadaKBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
1349f0d50ca0SMasahiro Yamada
13509c72d26eSMasahiro Yamadaexport KBUILD_VMLINUX_OBJS KBUILD_VMLINUX_LIBS
135195698570SSam Ravnborgexport KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
13521da177e4SLinus Torvalds
13532441e78bSNicolas Pitreifdef CONFIG_TRIM_UNUSED_KSYMS
13541f50b80aSMasahiro Yamada# For the kernel to actually contain only the needed exported symbols,
13551f50b80aSMasahiro Yamada# we have to build modules as well to determine what those symbols are.
13561bf807b8SMasahiro YamadaKBUILD_MODULES := y
13571f50b80aSMasahiro Yamadaendif
13581f50b80aSMasahiro Yamada
13599c72d26eSMasahiro YamadaPHONY += vmlinux_a
13609c72d26eSMasahiro Yamadavmlinux_a: $(KBUILD_VMLINUX_OBJS) scripts/head-object-list.txt FORCE
13619c72d26eSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_a
1362fbe6e37dSNicholas Piggin
13639c72d26eSMasahiro Yamadavmlinux.a: vmlinux_a
13649c72d26eSMasahiro Yamada	@:
13652441e78bSNicolas Pitre
13667a342e6cSMasahiro YamadaPHONY += vmlinux_o
13677a342e6cSMasahiro Yamadavmlinux_o: vmlinux.a $(KBUILD_VMLINUX_LIBS)
13689c5a0ac3SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_o
1369741f98feSSam Ravnborg
137069ef2707SYuntao Wangvmlinux.o: vmlinux_o
13717a342e6cSMasahiro Yamada	@:
13727a342e6cSMasahiro Yamada
13735d4aeffbSMasahiro YamadaPHONY += vmlinux
13748debed3eSMasahiro Yamada# LDFLAGS_vmlinux in the top Makefile defines linker flags for the top vmlinux,
13758debed3eSMasahiro Yamada# not for decompressors. LDFLAGS_vmlinux in arch/*/boot/compressed/Makefile is
13768debed3eSMasahiro Yamada# unrelated; the decompressors just happen to have the same base name,
13778debed3eSMasahiro Yamada# arch/*/boot/compressed/vmlinux.
13788debed3eSMasahiro Yamada# Export LDFLAGS_vmlinux only to scripts/Makefile.vmlinux.
13798debed3eSMasahiro Yamada#
13808debed3eSMasahiro Yamada# _LDFLAGS_vmlinux is a workaround for the 'private export' bug:
13818debed3eSMasahiro Yamada#   https://savannah.gnu.org/bugs/?61463
13828debed3eSMasahiro Yamada# For Make > 4.4, the following simple code will work:
13838debed3eSMasahiro Yamada#  vmlinux: private export LDFLAGS_vmlinux := $(LDFLAGS_vmlinux)
13848debed3eSMasahiro Yamadavmlinux: private _LDFLAGS_vmlinux := $(LDFLAGS_vmlinux)
13858debed3eSMasahiro Yamadavmlinux: export LDFLAGS_vmlinux = $(_LDFLAGS_vmlinux)
13865d4aeffbSMasahiro Yamadavmlinux: vmlinux.o $(KBUILD_LDS) modpost
13875d4aeffbSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux
1388392885eeSMasahiro Yamada
13891da177e4SLinus Torvalds# The actual objects are generated when descending,
13901da177e4SLinus Torvalds# make sure no implicit rule kicks in
13919c5a0ac3SMasahiro Yamada$(sort $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)): . ;
13921da177e4SLinus Torvalds
13931cb86b6cSMasahiro Yamadaifeq ($(origin KERNELRELEASE),file)
1394ec31f868SMasahiro Yamadafilechk_kernel.release = $(srctree)/scripts/setlocalversion $(srctree)
13951cb86b6cSMasahiro Yamadaelse
13961cb86b6cSMasahiro Yamadafilechk_kernel.release = echo $(KERNELRELEASE)
13971cb86b6cSMasahiro Yamadaendif
13980d0e7718SMichal Marek
139983a35e36SGeert Uytterhoeven# Store (new) KERNELRELEASE string in include/config/kernel.release
140024512795SMasahiro Yamadainclude/config/kernel.release: FORCE
14010d0e7718SMichal Marek	$(call filechk,kernel.release)
1402cb58455cSSam Ravnborg
1403d8821622SMasahiro Yamada# Additional helpers built in scripts/
1404d8821622SMasahiro Yamada# Carefully list dependencies so we do not try to build scripts twice
1405d8821622SMasahiro Yamada# in parallel
1406d8821622SMasahiro YamadaPHONY += scripts
140760df1aeeSMasahiro Yamadascripts: scripts_basic scripts_dtc
1408d8821622SMasahiro Yamada	$(Q)$(MAKE) $(build)=$(@)
1409cb58455cSSam Ravnborg
14101da177e4SLinus Torvalds# Things we need to do before we recursively start building the kernel
14115bb78269SSam Ravnborg# or the modules are listed in "prepare".
14125bb78269SSam Ravnborg# A multi level approach is used. prepareN is processed before prepareN-1.
14135bb78269SSam Ravnborg# archprepare is used in arch Makefiles and when processed asm symlink,
14145bb78269SSam Ravnborg# version.h and scripts_basic is processed / created.
14151da177e4SLinus Torvalds
1416a5139fb3SMasahiro YamadaPHONY += prepare archprepare
14175bb78269SSam Ravnborg
141836de077bSMasahiro Yamadaarchprepare: outputmakefile archheaders archscripts scripts include/config/kernel.release \
14195e9e95ccSMasahiro Yamada	asm-generic $(version_h) include/generated/utsrelease.h \
1420985d6cccSMasahiro Yamada	include/generated/compile.h include/generated/autoconf.h \
1421985d6cccSMasahiro Yamada	include/generated/rustc_cfg remove-stale-files
14225bb78269SSam Ravnborg
142365bba042SMasahiro Yamadaprepare0: archprepare
142460df1aeeSMasahiro Yamada	$(Q)$(MAKE) $(build)=scripts/mod
1425ed7ceac1SMasahiro Yamada	$(Q)$(MAKE) $(build)=. prepare
142686feeaa8SSam Ravnborg
14271da177e4SLinus Torvalds# All the preparing..
14280d989ac2SMasahiro Yamadaprepare: prepare0
14292f7ab126SMiguel Ojedaifdef CONFIG_RUST
1430ecab4115SMiguel Ojeda	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
14312f7ab126SMiguel Ojeda	$(Q)$(MAKE) $(build)=rust
14322f7ab126SMiguel Ojedaendif
1433b9ab5ebbSJosh Poimboeuf
14341476fee5SMasahiro YamadaPHONY += remove-stale-files
14351476fee5SMasahiro Yamadaremove-stale-files:
14361476fee5SMasahiro Yamada	$(Q)$(srctree)/scripts/remove-stale-files
14371476fee5SMasahiro Yamada
14382c1f4f12SMasahiro Yamada# Support for using generic headers in asm-generic
1439fbb5c060SArnd Bergmannasm-generic := -f $(srctree)/scripts/Makefile.asm-headers obj
14407d0e5c20SMasahiro Yamada
14412c1f4f12SMasahiro YamadaPHONY += asm-generic uapi-asm-generic
14422c1f4f12SMasahiro Yamadaasm-generic: uapi-asm-generic
1443037fc336SMasahiro Yamada	$(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/asm \
1444037fc336SMasahiro Yamada	generic=include/asm-generic
14452c1f4f12SMasahiro Yamadauapi-asm-generic:
1446037fc336SMasahiro Yamada	$(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/uapi/asm \
1447037fc336SMasahiro Yamada	generic=include/uapi/asm-generic
14482c1f4f12SMasahiro Yamada
14491da177e4SLinus Torvalds# Generate some files
14501da177e4SLinus Torvalds# ---------------------------------------------------------------------------
14511da177e4SLinus Torvalds
14521da177e4SLinus Torvalds# KERNELRELEASE can change from a few different places, meaning version.h
14531da177e4SLinus Torvalds# needs to be updated, so this check is forced on all builds
14541da177e4SLinus Torvalds
14551da177e4SLinus Torvaldsuts_len := 64
145663104eecSSam Ravnborgdefine filechk_utsrelease.h
14571da177e4SLinus Torvalds	if [ `echo -n "$(KERNELRELEASE)" | wc -c ` -gt $(uts_len) ]; then \
14581da177e4SLinus Torvalds	  echo '"$(KERNELRELEASE)" exceeds $(uts_len) characters' >&2;    \
14591da177e4SLinus Torvalds	  exit 1;                                                         \
14601da177e4SLinus Torvalds	fi;                                                               \
1461ad774086SMasahiro Yamada	echo \#define UTS_RELEASE \"$(KERNELRELEASE)\"
14621da177e4SLinus Torvaldsendef
14631da177e4SLinus Torvalds
146463104eecSSam Ravnborgdefine filechk_version.h
14659b82f13eSSasha Levin	if [ $(SUBLEVEL) -gt 255 ]; then                                 \
1466ad774086SMasahiro Yamada		echo \#define LINUX_VERSION_CODE $(shell                 \
1467207da4c8SMasahiro Yamada		expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + 255); \
14689b82f13eSSasha Levin	else                                                             \
14699b82f13eSSasha Levin		echo \#define LINUX_VERSION_CODE $(shell                 \
1470207da4c8SMasahiro Yamada		expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + $(SUBLEVEL)); \
14719b82f13eSSasha Levin	fi;                                                              \
14729b82f13eSSasha Levin	echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) +  \
147388a68672SSasha Levin	((c) > 255 ? 255 : (c)))';                                       \
147488a68672SSasha Levin	echo \#define LINUX_VERSION_MAJOR $(VERSION);                    \
147588a68672SSasha Levin	echo \#define LINUX_VERSION_PATCHLEVEL $(PATCHLEVEL);            \
147688a68672SSasha Levin	echo \#define LINUX_VERSION_SUBLEVEL $(SUBLEVEL)
147763104eecSSam Ravnborgendef
147863104eecSSam Ravnborg
1479d98dba88SMasahiro Yamada$(version_h): private PATCHLEVEL := $(or $(PATCHLEVEL), 0)
1480d98dba88SMasahiro Yamada$(version_h): private SUBLEVEL := $(or $(SUBLEVEL), 0)
148143fee2b2SMasahiro Yamada$(version_h): FORCE
14821da177e4SLinus Torvalds	$(call filechk,version.h)
14831da177e4SLinus Torvalds
1484273b281fSSam Ravnborginclude/generated/utsrelease.h: include/config/kernel.release FORCE
148563104eecSSam Ravnborg	$(call filechk,utsrelease.h)
148663104eecSSam Ravnborg
1487a55f283eSMasahiro Yamadafilechk_compile.h = $(srctree)/scripts/mkcompile_h \
1488a55f283eSMasahiro Yamada	"$(UTS_MACHINE)" "$(CONFIG_CC_VERSION_TEXT)" "$(LD)"
1489a55f283eSMasahiro Yamada
1490a55f283eSMasahiro Yamadainclude/generated/compile.h: FORCE
1491a55f283eSMasahiro Yamada	$(call filechk,compile.h)
1492a55f283eSMasahiro Yamada
1493179efcb4SVegard NossumPHONY += headerdep
1494179efcb4SVegard Nossumheaderdep:
14959663d989SPeter Foley	$(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1 \
14969663d989SPeter Foley	$(srctree)/scripts/headerdep.pl -I$(srctree)/include
1497179efcb4SVegard Nossum
14981da177e4SLinus Torvalds# ---------------------------------------------------------------------------
14998d730cfbSDavid Woodhouse# Kernel headers
15008d730cfbSDavid Woodhouse
1501e6883b18SSam Ravnborg#Default location for installed headers
1502e6883b18SSam Ravnborgexport INSTALL_HDR_PATH = $(objtree)/usr
1503e6883b18SSam Ravnborg
150459b2bd05SMasahiro Yamadaquiet_cmd_headers_install = INSTALL $(INSTALL_HDR_PATH)/include
150559b2bd05SMasahiro Yamada      cmd_headers_install = \
150659b2bd05SMasahiro Yamada	mkdir -p $(INSTALL_HDR_PATH); \
150759b2bd05SMasahiro Yamada	rsync -mrl --include='*/' --include='*\.h' --exclude='*' \
150859b2bd05SMasahiro Yamada	usr/include $(INSTALL_HDR_PATH)
15096d716275SDavid Woodhouse
15108d730cfbSDavid WoodhousePHONY += headers_install
151159b2bd05SMasahiro Yamadaheaders_install: headers
151259b2bd05SMasahiro Yamada	$(call cmd,headers_install)
15134f193362SPaul Smith
15141da177e4SLinus TorvaldsPHONY += archheaders archscripts
15151da177e4SLinus Torvalds
1516a5bae54cSMasahiro Yamadahdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
15171da177e4SLinus Torvalds
151859b2bd05SMasahiro YamadaPHONY += headers
15192adde2ebSHenrik Lindströmheaders: $(version_h) scripts_unifdef uapi-asm-generic archheaders
1520268d191aSThomas Weißschuhifdef HEADER_ARCH
1521268d191aSThomas Weißschuh	$(Q)$(MAKE) -f $(srctree)/Makefile HEADER_ARCH= SRCARCH=$(HEADER_ARCH) headers
1522268d191aSThomas Weißschuhelse
1523d5470d14SMasahiro Yamada	$(Q)$(MAKE) $(hdr-inst)=include/uapi
1524d5470d14SMasahiro Yamada	$(Q)$(MAKE) $(hdr-inst)=arch/$(SRCARCH)/include/uapi
1525268d191aSThomas Weißschuhendif
15261f85712eSMike Frysinger
1527e949f4c2SMasahiro Yamadaifdef CONFIG_HEADERS_INSTALL
152859b2bd05SMasahiro Yamadaprepare: headers
1529e949f4c2SMasahiro Yamadaendif
153068475359SDavid Woodhouse
1531f8e05c10SNicolas SchierPHONY += usr_gen_init_cpio
1532f8e05c10SNicolas Schierusr_gen_init_cpio: scripts_basic
1533f8e05c10SNicolas Schier	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio
1534f8e05c10SNicolas Schier
1535bdd7714bSMasahiro YamadaPHONY += scripts_unifdef
1536bdd7714bSMasahiro Yamadascripts_unifdef: scripts_basic
1537bdd7714bSMasahiro Yamada	$(Q)$(MAKE) $(build)=scripts scripts/unifdef
1538bdd7714bSMasahiro Yamada
153941d7ea30SVladimir OlteanPHONY += scripts_gen_packed_field_checks
154041d7ea30SVladimir Olteanscripts_gen_packed_field_checks: scripts_basic
154141d7ea30SVladimir Oltean	$(Q)$(MAKE) $(build)=scripts scripts/gen_packed_field_checks
154241d7ea30SVladimir Oltean
15438d730cfbSDavid Woodhouse# ---------------------------------------------------------------------------
154414ccc638SMasahiro Yamada# Install
154514ccc638SMasahiro Yamada
154614ccc638SMasahiro Yamada# Many distributions have the custom install script, /sbin/installkernel.
154775ef3122SRandy Dunlap# If DKMS is installed, 'make install' will eventually recurse back
154875ef3122SRandy Dunlap# to this Makefile to build and install external modules.
154914ccc638SMasahiro Yamada# Cancel sub_make_done so that options such as M=, V=, etc. are parsed.
155014ccc638SMasahiro Yamada
1551f774f5bbSMasahiro Yamadaquiet_cmd_install = INSTALL $(INSTALL_PATH)
1552f774f5bbSMasahiro Yamada      cmd_install = unset sub_make_done; $(srctree)/scripts/install.sh
155314ccc638SMasahiro Yamada
155414ccc638SMasahiro Yamada# ---------------------------------------------------------------------------
155556769ba4SMasahiro Yamada# vDSO install
155656769ba4SMasahiro Yamada
155756769ba4SMasahiro YamadaPHONY += vdso_install
155856769ba4SMasahiro Yamadavdso_install: export INSTALL_FILES = $(vdso-install-y)
155956769ba4SMasahiro Yamadavdso_install:
156056769ba4SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vdsoinst
156156769ba4SMasahiro Yamada
156256769ba4SMasahiro Yamada# ---------------------------------------------------------------------------
15631bb0b18aSMasahiro Yamada# Tools
15641bb0b18aSMasahiro Yamada
156503f16cd0SJosh Poimboeufifdef CONFIG_OBJTOOL
15660d989ac2SMasahiro Yamadaprepare: tools/objtool
15670d989ac2SMasahiro Yamadaendif
15680d989ac2SMasahiro Yamada
15690d989ac2SMasahiro Yamadaifdef CONFIG_BPF
15700d989ac2SMasahiro Yamadaifdef CONFIG_DEBUG_INFO_BTF
15710d989ac2SMasahiro Yamadaprepare: tools/bpf/resolve_btfids
15720d989ac2SMasahiro Yamadaendif
15730d989ac2SMasahiro Yamadaendif
15740d989ac2SMasahiro Yamada
1575fc9d9047SBreno Leitao# tools/bootconfig renders the embedded bootconfig into a cmdline at build time.
1576fc9d9047SBreno Leitaoifdef CONFIG_CMDLINE_FROM_BOOTCONFIG
1577fc9d9047SBreno Leitaoprepare: tools/bootconfig
1578fc9d9047SBreno Leitaoendif
1579fc9d9047SBreno Leitao
1580fc9d9047SBreno Leitao# tools/bootconfig is run on the build host during prepare, so force a host
1581fc9d9047SBreno Leitao# binary here; its own Makefile keeps $(CC) for standalone and cross builds.
1582fc9d9047SBreno Leitao# CROSS_COMPILE= is cleared so tools/scripts/Makefile.include does not inject
1583fc9d9047SBreno Leitao# the target's --target=/--sysroot= flags into the host clang invocation under
1584fc9d9047SBreno Leitao# LLVM=1 cross builds (which would produce a target binary that fails to exec).
1585fc9d9047SBreno Leitaotools/bootconfig: export CC := $(HOSTCC)
1586fc9d9047SBreno Leitaotools/bootconfig: FORCE
1587fc9d9047SBreno Leitao	$(Q)mkdir -p $(objtree)/tools
1588fc9d9047SBreno Leitao	$(Q)$(MAKE) O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/ \
1589fc9d9047SBreno Leitao		bootconfig CROSS_COMPILE=
1590fc9d9047SBreno Leitao
15916e6ef2daSMasahiro Yamada# The tools build system is not a part of Kbuild and tends to introduce
15926e6ef2daSMasahiro Yamada# its own unique issues. If you need to integrate a new tool into Kbuild,
15936e6ef2daSMasahiro Yamada# please consider locating that tool outside the tools/ tree and using the
15946e6ef2daSMasahiro Yamada# standard Kbuild "hostprogs" syntax instead of adding a new tools/* entry
15956e6ef2daSMasahiro Yamada# here. See Documentation/kbuild/makefiles.rst for details.
15966e6ef2daSMasahiro Yamada
15970d989ac2SMasahiro YamadaPHONY += resolve_btfids_clean
15980d989ac2SMasahiro Yamada
15990d989ac2SMasahiro Yamadaresolve_btfids_O = $(abspath $(objtree))/tools/bpf/resolve_btfids
16000d989ac2SMasahiro Yamada
16010d989ac2SMasahiro Yamada# tools/bpf/resolve_btfids directory might not exist
16020d989ac2SMasahiro Yamada# in output directory, skip its clean in that case
16030d989ac2SMasahiro Yamadaresolve_btfids_clean:
16040d989ac2SMasahiro Yamadaifneq ($(wildcard $(resolve_btfids_O)),)
16050d989ac2SMasahiro Yamada	$(Q)$(MAKE) -sC $(srctree)/tools/bpf/resolve_btfids O=$(resolve_btfids_O) clean
16060d989ac2SMasahiro Yamadaendif
16070d989ac2SMasahiro Yamada
1608fdb12c8aSNathan ChancellorPHONY += objtool_clean objtool_mrproper
160968b4fe32SJosh Poimboeuf
161068b4fe32SJosh Poimboeufobjtool_O = $(abspath $(objtree))/tools/objtool
161168b4fe32SJosh Poimboeuf
1612fdb12c8aSNathan Chancellorobjtool_clean objtool_mrproper:
161368b4fe32SJosh Poimboeufifneq ($(wildcard $(objtool_O)),)
1614fdb12c8aSNathan Chancellor	$(Q)$(MAKE) -sC $(abs_srctree)/tools/objtool O=$(objtool_O) srctree=$(abs_srctree) $(patsubst objtool_%,%,$@)
161568b4fe32SJosh Poimboeufendif
161668b4fe32SJosh Poimboeuf
16171bbab483SBreno LeitaoPHONY += bootconfig_clean
16181bbab483SBreno Leitao
16191bbab483SBreno Leitaobootconfig_O = $(abspath $(objtree))/tools/bootconfig
16201bbab483SBreno Leitao
16211bbab483SBreno Leitaobootconfig_clean:
16221bbab483SBreno Leitaoifneq ($(wildcard $(bootconfig_O)),)
16231bbab483SBreno Leitao	$(Q)$(MAKE) -sC $(srctree)/tools/bootconfig O=$(bootconfig_O) clean
16241bbab483SBreno Leitaoendif
16251bbab483SBreno Leitao
16261bb0b18aSMasahiro Yamadatools/: FORCE
16271bb0b18aSMasahiro Yamada	$(Q)mkdir -p $(objtree)/tools
1628ee916dccSLinus Torvalds	$(Q)$(MAKE) O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/
16291bb0b18aSMasahiro Yamada
16301bb0b18aSMasahiro Yamadatools/%: FORCE
16311bb0b18aSMasahiro Yamada	$(Q)mkdir -p $(objtree)/tools
1632ee916dccSLinus Torvalds	$(Q)$(MAKE) O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/ $*
16331bb0b18aSMasahiro Yamada
16341bb0b18aSMasahiro Yamada# ---------------------------------------------------------------------------
16355a5da78bSShuah Khan# Kernel selftest
16365a5da78bSShuah Khan
16375a5da78bSShuah KhanPHONY += kselftest
16384062eba9SGuillaume Tuckerkselftest: headers
1639e77f165dSThomas Weißschuh	$(Q)unset sub_make_done; $(MAKE) -C $(srctree)/tools/testing/selftests run_tests
16405a5da78bSShuah Khan
16414062eba9SGuillaume Tuckerkselftest-%: headers FORCE
1642e77f165dSThomas Weißschuh	$(Q)unset sub_make_done; $(MAKE) -C $(srctree)/tools/testing/selftests $*
1643dcb825a9SWang Long
16443d6dee7aSBamvor Jian ZhangPHONY += kselftest-merge
16453d6dee7aSBamvor Jian Zhangkselftest-merge:
16463d6dee7aSBamvor Jian Zhang	$(if $(wildcard $(objtree)/.config),, $(error No .config exists, config your kernel first!))
164737013b55SBjörn Töpel	$(Q)find $(srctree)/tools/testing/selftests -name config -o -name config.$(UTS_MACHINE) | \
16486d3d638dSBjörn Töpel		xargs $(srctree)/scripts/kconfig/merge_config.sh -y -m $(objtree)/.config
16493e4c6948SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/Makefile olddefconfig
16503d6dee7aSBamvor Jian Zhang
16515a5da78bSShuah Khan# ---------------------------------------------------------------------------
165237c8a5faSRob Herring# Devicetree files
165337c8a5faSRob Herring
165437c8a5faSRob Herringifneq ($(wildcard $(srctree)/arch/$(SRCARCH)/boot/dts/),)
165537c8a5faSRob Herringdtstree := arch/$(SRCARCH)/boot/dts
165637c8a5faSRob Herringendif
165737c8a5faSRob Herring
1658400fbf4bSRob Herring (Arm)dtbindingtree := Documentation/devicetree/bindings
1659400fbf4bSRob Herring (Arm)
1660400fbf4bSRob Herring (Arm)%.yaml: dtbs_prepare
1661400fbf4bSRob Herring (Arm)	$(Q)$(MAKE) $(build)=$(dtbindingtree) \
1662400fbf4bSRob Herring (Arm)		    $(dtbindingtree)/$(patsubst %.yaml,%.example.dtb,$@) dt_binding_check_one
1663400fbf4bSRob Herring (Arm)
166437c8a5faSRob Herringifneq ($(dtstree),)
166537c8a5faSRob Herring
1666ee476203SMasahiro Yamada%.dtb: dtbs_prepare
166775e89534SRob Herring	$(Q)$(MAKE) $(build)=$(dtstree) $(dtstree)/$@
166837c8a5faSRob Herring
1669ee476203SMasahiro Yamada%.dtbo: dtbs_prepare
167075e89534SRob Herring	$(Q)$(MAKE) $(build)=$(dtstree) $(dtstree)/$@
1671ce88c9c7SViresh Kumar
1672ee476203SMasahiro YamadaPHONY += dtbs dtbs_prepare dtbs_install dtbs_check
1673ee476203SMasahiro Yamadadtbs: dtbs_prepare
167424507871SMasahiro Yamada	$(Q)$(MAKE) $(build)=$(dtstree) need-dtbslist=1
167537c8a5faSRob Herring
1676ee476203SMasahiro Yamada# include/config/kernel.release is actually needed when installing DTBs because
1677ee476203SMasahiro Yamada# INSTALL_DTBS_PATH contains $(KERNELRELEASE). However, we do not want to make
1678ee476203SMasahiro Yamada# dtbs_install depend on it as dtbs_install may run as root.
1679ee476203SMasahiro Yamadadtbs_prepare: include/config/kernel.release scripts_dtc
1680ee476203SMasahiro Yamada
1681400fbf4bSRob Herring (Arm)ifneq ($(filter dtbs_check %.yaml, $(MAKECMDGOALS)),)
1682e10c4321SMasahiro Yamadaexport CHECK_DTBS=y
1683ec201955SRob Herringendif
1684ec201955SRob Herring
1685ec201955SRob Herringifneq ($(CHECK_DTBS),)
1686604a57baSRob Herringdtbs_prepare: dt_binding_schemas
1687b5154bf6SMasahiro Yamadaendif
1688b5154bf6SMasahiro Yamada
1689b5154bf6SMasahiro Yamadadtbs_check: dtbs
16904f0e3a57SRob Herring
169137c8a5faSRob Herringdtbs_install:
16928f66864cSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.dtbinst obj=$(dtstree)
169337c8a5faSRob Herring
169437c8a5faSRob Herringifdef CONFIG_OF_EARLY_FLATTREE
169537c8a5faSRob Herringall: dtbs
169637c8a5faSRob Herringendif
169737c8a5faSRob Herring
1698654102dfSMasahiro Yamadaifdef CONFIG_GENERIC_BUILTIN_DTB
1699654102dfSMasahiro Yamadavmlinux: dtbs
1700654102dfSMasahiro Yamadaendif
1701654102dfSMasahiro Yamada
170237c8a5faSRob Herringendif
170337c8a5faSRob Herring
170437c8a5faSRob HerringPHONY += scripts_dtc
170537c8a5faSRob Herringscripts_dtc: scripts_basic
170637c8a5faSRob Herring	$(Q)$(MAKE) $(build)=scripts/dtc
170737c8a5faSRob Herring
1708e10c4321SMasahiro Yamadaifneq ($(filter dt_binding_check, $(MAKECMDGOALS)),)
1709604a57baSRob Herringexport CHECK_DTBS=y
1710e10c4321SMasahiro Yamadaendif
1711e10c4321SMasahiro Yamada
1712604a57baSRob HerringPHONY += dt_binding_check dt_binding_schemas
1713604a57baSRob Herringdt_binding_check: dt_binding_schemas scripts_dtc
1714400fbf4bSRob Herring (Arm)	$(Q)$(MAKE) $(build)=$(dtbindingtree) $@
1715604a57baSRob Herring
1716604a57baSRob Herringdt_binding_schemas:
1717400fbf4bSRob Herring (Arm)	$(Q)$(MAKE) $(build)=$(dtbindingtree)
17184f0e3a57SRob Herring
1719b6acf807SRob HerringPHONY += dt_compatible_check
1720604a57baSRob Herringdt_compatible_check: dt_binding_schemas
1721400fbf4bSRob Herring (Arm)	$(Q)$(MAKE) $(build)=$(dtbindingtree) $@
1722b6acf807SRob Herring
1723efb6347dSDaniel GollePHONY += dt_style_selftest
1724efb6347dSDaniel Golledt_style_selftest:
1725efb6347dSDaniel Golle	$(Q)$(srctree)/scripts/dtc/dt-style-selftest/run.sh
1726efb6347dSDaniel Golle
172737c8a5faSRob Herring# ---------------------------------------------------------------------------
17281da177e4SLinus Torvalds# Modules
17291da177e4SLinus Torvalds
17301da177e4SLinus Torvaldsifdef CONFIG_MODULES
17311da177e4SLinus Torvalds
17321da177e4SLinus Torvalds# By default, build modules as well
17331da177e4SLinus Torvalds
173473d1393eSMichal Marekall: modules
17351da177e4SLinus Torvalds
17364b50c8c4SMasahiro Yamada# When we're building modules with modversions, we need to consider
17374b50c8c4SMasahiro Yamada# the built-in objects during the descend as well, in order to
17384b50c8c4SMasahiro Yamada# make sure the checksums are up to date before we record them.
17394b50c8c4SMasahiro Yamadaifdef CONFIG_MODVERSIONS
17401bf807b8SMasahiro Yamada  KBUILD_BUILTIN := y
17414b50c8c4SMasahiro Yamadaendif
17424b50c8c4SMasahiro Yamada
17431da177e4SLinus Torvalds# Build modules
1744551559e1STejun Heo#
17451da177e4SLinus Torvalds
1746feb113adSCarlos Llamas# *.ko are usually independent of vmlinux, but CONFIG_DEBUG_INFO_BTF_MODULES
1747f73edc89SMasahiro Yamada# is an exception.
1748f73edc89SMasahiro Yamadaifdef CONFIG_DEBUG_INFO_BTF_MODULES
17491bf807b8SMasahiro YamadaKBUILD_BUILTIN := y
1750f73edc89SMasahiro Yamadamodules: vmlinux
1751f73edc89SMasahiro Yamadaendif
17521da177e4SLinus Torvalds
1753f73edc89SMasahiro Yamadamodules: modules_prepare
1754a6c36632SMichal Marek
17551da177e4SLinus Torvalds# Target to prepare building external modules
1756059bc9fcSMasahiro Yamadamodules_prepare: prepare
1757596b0474SMasahiro Yamada	$(Q)$(MAKE) $(build)=scripts scripts/module.lds
17581da177e4SLinus Torvalds
17598ae071fcSMasahiro Yamadaendif # CONFIG_MODULES
17608ae071fcSMasahiro Yamada
17611da177e4SLinus Torvalds###
17621da177e4SLinus Torvalds# Cleaning is done on three levels.
17631da177e4SLinus Torvalds# make clean     Delete most generated files
17641da177e4SLinus Torvalds#                Leave enough to build external modules
17651da177e4SLinus Torvalds# make mrproper  Delete the current configuration, and all generated files
17661da177e4SLinus Torvalds# make distclean Remove editor backup files, patch leftover files and the like
17671da177e4SLinus Torvalds
17681da177e4SLinus Torvalds# Directories & files removed with 'make clean'
17691fdd7290SMasahiro YamadaCLEAN_FILES += vmlinux.symvers modules-only.symvers \
17703d32285fSMasahiro Yamada	       modules.builtin modules.builtin.modinfo modules.nsdeps \
1771ac4f0678SArd Biesheuvel	       modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \
17729f2aee8fSRong Xu	       vmlinux.thinlto-index builtin.order \
1773aba09154SNathan Chancellor	       compile_commands.json rust/test \
1774654102dfSMasahiro Yamada	       rust-project.json .vmlinux.objs .vmlinux.export.c \
1775e72b635cSLuis Augenstein	       .builtin-dtbs-list .builtin-dtbs.S sbom-*.spdx.json
17761da177e4SLinus Torvalds
17771da177e4SLinus Torvalds# Directories & files removed with 'make mrproper'
17780663c68cSMasahiro YamadaMRPROPER_FILES += include/config include/generated          \
1779b0d6207bSMasahiro Yamada		  arch/$(SRCARCH)/include/generated .objdiff \
1780c8578539SThomas Weißschuh		  debian snap tar-install PKGBUILD pacman \
17810663c68cSMasahiro Yamada		  .config .config.old .version \
178246457133SMasahiro Yamada		  Module.symvers \
17835cca3606SMasahiro Yamada		  certs/signing_key.pem \
1784b31f2a49SNayna Jain		  certs/x509.genkey \
1785b31f2a49SNayna Jain		  vmlinux-gdb.py \
1786ffa46bbcSMasahiro Yamada		  rpmbuild \
1787a44bfed9SChen Miao		  rust/libmacros.so rust/libmacros.dylib \
17888b6bdbd7SMiguel Ojeda		  rust/libpin_init_internal.so rust/libpin_init_internal.dylib \
17898b6bdbd7SMiguel Ojeda		  rust/libzerocopy_derive.so rust/libzerocopy_derive.dylib
17901da177e4SLinus Torvalds
17911da177e4SLinus Torvalds# clean - Delete most, but leave enough to build external modules
17921da177e4SLinus Torvalds#
1793d98dba88SMasahiro Yamadaclean: private rm-files := $(CLEAN_FILES)
17941da177e4SLinus Torvalds
179576cd306dSMasahiro YamadaPHONY += archclean vmlinuxclean
17961da177e4SLinus Torvalds
1797bd1ee804SPawel Mollvmlinuxclean:
1798bd1ee804SPawel Moll	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-vmlinux.sh clean
1799fbe6e37dSNicholas Piggin	$(Q)$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) clean)
1800bd1ee804SPawel Moll
18011bbab483SBreno Leitaoclean: archclean vmlinuxclean resolve_btfids_clean objtool_clean bootconfig_clean
18021da177e4SLinus Torvalds
18031da177e4SLinus Torvalds# mrproper - Delete all generated files, including .config
18041da177e4SLinus Torvalds#
1805d98dba88SMasahiro Yamadamrproper: private rm-files := $(MRPROPER_FILES)
1806cb43fb57SMauro Carvalho Chehabmrproper-dirs      := $(addprefix _mrproper_,scripts)
18071da177e4SLinus Torvalds
1808b421b8a6SMasahiro YamadaPHONY += $(mrproper-dirs) mrproper
18091da177e4SLinus Torvalds$(mrproper-dirs):
18101da177e4SLinus Torvalds	$(Q)$(MAKE) $(clean)=$(patsubst _mrproper_%,%,$@)
18111da177e4SLinus Torvalds
1812fdb12c8aSNathan Chancellormrproper: clean objtool_mrproper $(mrproper-dirs)
18131da177e4SLinus Torvalds	$(call cmd,rmfiles)
18142f7ab126SMiguel Ojeda	@find . $(RCS_FIND_IGNORE) \
18152f7ab126SMiguel Ojeda		\( -name '*.rmeta' \) \
18162f7ab126SMiguel Ojeda		-type f -print | xargs rm -f
18171da177e4SLinus Torvalds
18181da177e4SLinus Torvalds# distclean
18191da177e4SLinus Torvalds#
18204f193362SPaul SmithPHONY += distclean
18211da177e4SLinus Torvalds
18221da177e4SLinus Torvaldsdistclean: mrproper
182319c8d912SMasahiro Yamada	@find . $(RCS_FIND_IGNORE) \
18241da177e4SLinus Torvalds		\( -name '*.orig' -o -name '*.rej' -o -name '*~' \
1825f78271dfSMasahiro Yamada		-o -name '*.bak' -o -name '#*#' -o -name '*%' \
18267a02cec5SMasahiro Yamada		-o -name 'core' -o -name tags -o -name TAGS -o -name 'cscope*' \
18277a02cec5SMasahiro Yamada		-o -name GPATH -o -name GRTAGS -o -name GSYMS -o -name GTAGS \) \
18281da177e4SLinus Torvalds		-type f -print | xargs rm -f
18291da177e4SLinus Torvalds
18301da177e4SLinus Torvalds
18311da177e4SLinus Torvalds# Packaging of the kernel to various formats
18321da177e4SLinus Torvalds# ---------------------------------------------------------------------------
18331da177e4SLinus Torvalds
1834f8e05c10SNicolas Schiermodules-cpio-pkg: usr_gen_init_cpio
1835f8e05c10SNicolas Schier
1836bafb6747SArnaldo Carvalho de Melo%src-pkg: FORCE
1837000ec95fSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
1838031ecc6dSZach Brown%pkg: include/config/kernel.release FORCE
1839000ec95fSMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
18401da177e4SLinus Torvalds
18411da177e4SLinus Torvalds# Brief documentation of the typical targets used
18421da177e4SLinus Torvalds# ---------------------------------------------------------------------------
18431da177e4SLinus Torvalds
18445dffbe81SSegher Boessenkoolboards := $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*_defconfig)
1845a1e7b7bbSKonstantin Khlebnikovboards := $(sort $(notdir $(boards)))
18465dffbe81SSegher Boessenkoolboard-dirs := $(dir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*/*_defconfig))
18475dffbe81SSegher Boessenkoolboard-dirs := $(sort $(notdir $(board-dirs:/=)))
18481da177e4SLinus Torvalds
1849fe69b420SMasahiro YamadaPHONY += help
18501da177e4SLinus Torvaldshelp:
18511da177e4SLinus Torvalds	@echo  'Cleaning targets:'
18525ea084efSSamuel Tardieu	@echo  '  clean		  - Remove most generated files but keep the config and'
18535cc8d246SJesper Juhl	@echo  '                    enough build support to build external modules'
18545ea084efSSamuel Tardieu	@echo  '  mrproper	  - Remove all generated files + config + various backup files'
18555cc8d246SJesper Juhl	@echo  '  distclean	  - mrproper + remove editor backup and patch files'
18561da177e4SLinus Torvalds	@echo  ''
18571da177e4SLinus Torvalds	@$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
18581da177e4SLinus Torvalds	@echo  ''
18591da177e4SLinus Torvalds	@echo  'Other generic targets:'
18601da177e4SLinus Torvalds	@echo  '  all		  - Build all targets marked with [*]'
18611da177e4SLinus Torvalds	@echo  '* vmlinux	  - Build the bare kernel'
18621da177e4SLinus Torvalds	@echo  '* modules	  - Build all modules'
18639cc5d74cSBodo Eggert	@echo  '  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)'
186456769ba4SMasahiro Yamada	@echo  '  vdso_install    - Install unstripped vdso to INSTALL_MOD_PATH (default: /)'
18651da177e4SLinus Torvalds	@echo  '  dir/            - Build all files in dir and below'
186640ab87a4SWang YanQing	@echo  '  dir/file.[ois]  - Build specified target only'
1867433db3e2SVinícius Tinti	@echo  '  dir/file.ll     - Build the LLVM assembly file'
1868433db3e2SVinícius Tinti	@echo  '                    (requires compiler support for LLVM assembly generation)'
186962718979SJoe Perches	@echo  '  dir/file.lst    - Build specified mixed source/assembly target only'
187062718979SJoe Perches	@echo  '                    (requires a recent binutils and recent build (System.map))'
1871155ad605SSam Ravnborg	@echo  '  dir/file.ko     - Build module including final link'
1872c4d5ee67SRobert P. J. Day	@echo  '  modules_prepare - Set up for building external modules'
18731da177e4SLinus Torvalds	@echo  '  tags/TAGS	  - Generate tags file for editors'
18741da177e4SLinus Torvalds	@echo  '  cscope	  - Generate cscope index'
1875f4ed1009SJianbin Kang	@echo  '  gtags           - Generate GNU GLOBAL index'
18763f1d9a6cSMichal Marek	@echo  '  kernelrelease	  - Output the release version string (use with make -s)'
18773f1d9a6cSMichal Marek	@echo  '  kernelversion	  - Output the version stored in Makefile (use with make -s)'
18783f1d9a6cSMichal Marek	@echo  '  image_name	  - Output the image name (use with make -s)'
187900e81f4fSXin Li (Intel)	@echo  '  headers	  - Build ready-to-install UAPI headers in usr/include'
188000e81f4fSXin Li (Intel)	@echo  '  headers_install - Install sanitised kernel UAPI headers to INSTALL_HDR_PATH'; \
188159df3230SGeert Uytterhoeven	 echo  '                    (default: $(INSTALL_HDR_PATH))'; \
18822fb9b1bdSSam Ravnborg	 echo  ''
188331b8cc80SRandy Dunlap	@echo  'Static analysers:'
188466242cfaSHeiko Carstens	@echo  '  checkstack        - Generate a list of stack hogs and consider all functions'
188566242cfaSHeiko Carstens	@echo  '                      with a stack size larger than MINSTACKSIZE (default: 100)'
1886aa025e7dSSam Ravnborg	@echo  '  versioncheck      - Sanity check on version.h usage'
1887ec2d987fSRandy Dunlap	@echo  '  includecheck      - Check for duplicate included header files'
188874425eeeSNicolas Palix	@echo  '  headerdep         - Detect inclusion cycles in headers'
18897f855fc8SMasahiro Yamada	@echo  '  coccicheck        - Check with Coccinelle'
1890f58316a4SAndrew Jones	@echo  '  kconfig-sym-check - Check for dangling Kconfig symbol references'
18916ad7cbc0SNathan Huckleberry	@echo  '  clang-analyzer    - Check with clang static analyzer'
18926ad7cbc0SNathan Huckleberry	@echo  '  clang-tidy        - Check with clang-tidy'
189374425eeeSNicolas Palix	@echo  ''
1894eb8305aeSMatthias Maennich	@echo  'Tools:'
1895eb8305aeSMatthias Maennich	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
1896e72b635cSLuis Augenstein	@echo  '  sbom            - Generate Software Bill of Materials'
1897eb8305aeSMatthias Maennich	@echo  ''
189831b8cc80SRandy Dunlap	@echo  'Kernel selftest:'
1899e51d8dacSShuah Khan	@echo  '  kselftest         - Build and run kernel selftest'
19005a5da78bSShuah Khan	@echo  '                      Build, install, and boot kernel before'
19015a5da78bSShuah Khan	@echo  '                      running kselftest on it'
1902e51d8dacSShuah Khan	@echo  '                      Run as root for full coverage'
1903e51d8dacSShuah Khan	@echo  '  kselftest-all     - Build kernel selftest'
1904e51d8dacSShuah Khan	@echo  '  kselftest-install - Build and install kernel selftest'
1905dcb825a9SWang Long	@echo  '  kselftest-clean   - Remove all generated kselftest files'
1906e51d8dacSShuah Khan	@echo  '  kselftest-merge   - Merge all the config dependencies of'
1907e51d8dacSShuah Khan	@echo  '		      kselftest to existing .config.'
19085a5da78bSShuah Khan	@echo  ''
19092f7ab126SMiguel Ojeda	@echo  'Rust targets:'
19102f7ab126SMiguel Ojeda	@echo  '  rustavailable   - Checks whether the Rust toolchain is'
19112f7ab126SMiguel Ojeda	@echo  '		    available and, if not, explains why.'
19122f7ab126SMiguel Ojeda	@echo  '  rustfmt	  - Reformat all the Rust code in the kernel'
19132f7ab126SMiguel Ojeda	@echo  '  rustfmtcheck	  - Checks if all the Rust code in the kernel'
19142f7ab126SMiguel Ojeda	@echo  '		    is formatted, printing a diff otherwise.'
19152f7ab126SMiguel Ojeda	@echo  '  rustdoc	  - Generate Rust documentation'
19162f7ab126SMiguel Ojeda	@echo  '		    (requires kernel .config)'
19172f7ab126SMiguel Ojeda	@echo  '  rusttest        - Runs the Rust tests'
19182f7ab126SMiguel Ojeda	@echo  '                    (requires kernel .config; downloads external repos)'
19192f7ab126SMiguel Ojeda	@echo  '  rust-analyzer	  - Generate rust-project.json rust-analyzer support file'
19202f7ab126SMiguel Ojeda	@echo  '		    (requires kernel .config)'
19212f7ab126SMiguel Ojeda	@echo  '  dir/file.[os]   - Build specified target only'
19222f7ab126SMiguel Ojeda	@echo  '  dir/file.rsi    - Build macro expanded source, similar to C preprocessing.'
19232f7ab126SMiguel Ojeda	@echo  '                    Run with RUSTFMT=n to skip reformatting if needed.'
19242f7ab126SMiguel Ojeda	@echo  '                    The output is not intended to be compilable.'
19252f7ab126SMiguel Ojeda	@echo  '  dir/file.ll     - Build the LLVM assembly file'
19262f7ab126SMiguel Ojeda	@echo  ''
192737c8a5faSRob Herring	@$(if $(dtstree), \
192837c8a5faSRob Herring		echo 'Devicetree:'; \
192937c8a5faSRob Herring		echo '* dtbs               - Build device tree blobs for enabled boards'; \
193037c8a5faSRob Herring		echo '  dtbs_install       - Install dtbs to $(INSTALL_DTBS_PATH)'; \
1931604a57baSRob Herring		echo '  dt_binding_check   - Validate device tree binding documents and examples'; \
1932d939881aSXu Yang		echo '  dt_binding_schemas - Build processed device tree binding schemas'; \
1933efb6347dSDaniel Golle		echo '  dt_style_selftest  - Run dt-check-style fixture tests'; \
19347aa8dd91SStephen Boyd		echo '  dtbs_check         - Validate device tree source files';\
193537c8a5faSRob Herring		echo '')
193637c8a5faSRob Herring
193731b8cc80SRandy Dunlap	@echo 'Userspace tools targets:'
193831b8cc80SRandy Dunlap	@echo '  use "make tools/help"'
193931b8cc80SRandy Dunlap	@echo '  or  "cd tools; make help"'
194031b8cc80SRandy Dunlap	@echo  ''
19411da177e4SLinus Torvalds	@echo  'Kernel packaging:'
1942000ec95fSMasahiro Yamada	@$(MAKE) -f $(srctree)/scripts/Makefile.package help
19431da177e4SLinus Torvalds	@echo  ''
19441da177e4SLinus Torvalds	@echo  'Documentation targets:'
1945cb43fb57SMauro Carvalho Chehab	@$(MAKE) -f $(srctree)/Documentation/Makefile dochelp
19461da177e4SLinus Torvalds	@echo  ''
1947766b7007SSimon Glass	@echo  'Architecture-specific targets ($(SRCARCH)):'
19485c816641SMasahiro Yamada	@$(or $(archhelp),\
1949766b7007SSimon Glass		echo '  No architecture-specific help defined for $(SRCARCH)')
19501da177e4SLinus Torvalds	@echo  ''
19511da177e4SLinus Torvalds	@$(if $(boards), \
19521da177e4SLinus Torvalds		$(foreach b, $(boards), \
19534234448bSGeert Uytterhoeven		printf "  %-27s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));) \
19541da177e4SLinus Torvalds		echo '')
19555dffbe81SSegher Boessenkool	@$(if $(board-dirs), \
19565dffbe81SSegher Boessenkool		$(foreach b, $(board-dirs), \
19575dffbe81SSegher Boessenkool		printf "  %-16s - Show %s-specific targets\\n" help-$(b) $(b);) \
19585dffbe81SSegher Boessenkool		printf "  %-16s - Show all of the above\\n" help-boards; \
19595dffbe81SSegher Boessenkool		echo '')
19601da177e4SLinus Torvalds
196183d98d73SMasahiro Yamada	@echo  '  make V=n   [targets] 1: verbose build'
19626ae4b986SMasahiro Yamada	@echo  '                       2: give reason for rebuild of target'
19636ae4b986SMasahiro Yamada	@echo  '                       V=1 and V=2 can be combined with V=12'
19641da177e4SLinus Torvalds	@echo  '  make O=dir [targets] Locate all output files in "dir", including .config'
1965a64c0440SGeert Uytterhoeven	@echo  '  make C=1   [targets] Check re-compiled c source with $$CHECK'
1966a64c0440SGeert Uytterhoeven	@echo  '                       (sparse by default)'
1967701842e3SDustin Kirkland	@echo  '  make C=2   [targets] Force check of all c source with $$CHECK'
1968af07ce3eSIngo Molnar	@echo  '  make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections'
196989876175SMasahiro Yamada	@echo  '  make W=n   [targets] Enable extra build checks, n=1,2,3,c,e where'
197028bc20dcSSam Ravnborg	@echo  '		1: warnings which may be relevant and do not occur too often'
197128bc20dcSSam Ravnborg	@echo  '		2: warnings which occur quite often but may still be relevant'
197228bc20dcSSam Ravnborg	@echo  '		3: more obscure warnings, can most likely be ignored'
197392ef432fSMasahiro Yamada	@echo  '		c: extra checks in the configuration stage (Kconfig)'
1974c77d06e7SYann Droneaud	@echo  '		e: warnings are being treated as errors'
1975a6de553dSMichal Marek	@echo  '		Multiple levels can be combined with W=12 or W=123'
1976e30f8e61SSteven Rostedt	@echo  '  make UT=1   [targets] Warn if a tracepoint is defined but not used.'
1977e30f8e61SSteven Rostedt	@echo  '          [ This will be removed when all current unused tracepoints are eliminated. ]'
1978ec201955SRob Herring	@$(if $(dtstree), \
1979ec201955SRob Herring		echo '  make CHECK_DTBS=1 [targets] Check all generated dtb files against schema'; \
1980ec201955SRob Herring		echo '         This can be applied both to "dtbs" and to individual "foo.dtb" targets' ; \
1981ec201955SRob Herring		)
19821da177e4SLinus Torvalds	@echo  ''
19831da177e4SLinus Torvalds	@echo  'Execute "make" or "make all" to build all targets marked with [*] '
19841da177e4SLinus Torvalds	@echo  'For further info see the ./README file'
19851da177e4SLinus Torvalds
19861da177e4SLinus Torvalds
19875dffbe81SSegher Boessenkoolhelp-board-dirs := $(addprefix help-,$(board-dirs))
19885dffbe81SSegher Boessenkool
19895dffbe81SSegher Boessenkoolhelp-boards: $(help-board-dirs)
19905dffbe81SSegher Boessenkool
1991fbae4d58SMichal Marekboards-per-dir = $(sort $(notdir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/$*/*_defconfig)))
19925dffbe81SSegher Boessenkool
19935dffbe81SSegher Boessenkool$(help-board-dirs): help-%:
1994766b7007SSimon Glass	@echo  'Architecture-specific targets ($(SRCARCH) $*):'
19955dffbe81SSegher Boessenkool	@$(if $(boards-per-dir), \
19965dffbe81SSegher Boessenkool		$(foreach b, $(boards-per-dir), \
19975dffbe81SSegher Boessenkool		printf "  %-24s - Build for %s\\n" $*/$(b) $(subst _defconfig,,$(b));) \
19985dffbe81SSegher Boessenkool		echo '')
19995dffbe81SSegher Boessenkool
20005dffbe81SSegher Boessenkool
20011da177e4SLinus Torvalds# Documentation targets
20021da177e4SLinus Torvalds# ---------------------------------------------------------------------------
20037e8a8143SMauro Carvalho ChehabDOC_TARGETS := xmldocs latexdocs pdfdocs htmldocs epubdocs cleandocs \
20047e8a8143SMauro Carvalho Chehab	       linkcheckdocs dochelp refcheckdocs texinfodocs infodocs mandocs \
20057e8a8143SMauro Carvalho Chehab	       htmldocs-redirects
20067e8a8143SMauro Carvalho Chehab
200722cba31bSJani NikulaPHONY += $(DOC_TARGETS)
2008bc7b752aSMasahiro Yamada$(DOC_TARGETS):
2009cb43fb57SMauro Carvalho Chehab	$(Q)$(MAKE) $(build)=Documentation $@
20101da177e4SLinus Torvalds
20112f7ab126SMiguel Ojeda
20122f7ab126SMiguel Ojeda# Rust targets
20132f7ab126SMiguel Ojeda# ---------------------------------------------------------------------------
20142f7ab126SMiguel Ojeda
20152f7ab126SMiguel Ojeda# "Is Rust available?" target
20162f7ab126SMiguel OjedaPHONY += rustavailable
20172f7ab126SMiguel Ojedarustavailable:
2018ecab4115SMiguel Ojeda	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
20192f7ab126SMiguel Ojeda
20202f7ab126SMiguel Ojeda# Documentation target
20212f7ab126SMiguel Ojeda#
20222f7ab126SMiguel Ojeda# Using the singular to avoid running afoul of `no-dot-config-targets`.
20232f7ab126SMiguel OjedaPHONY += rustdoc
20242f7ab126SMiguel Ojedarustdoc: prepare
20252f7ab126SMiguel Ojeda	$(Q)$(MAKE) $(build)=rust $@
20262f7ab126SMiguel Ojeda
20272f7ab126SMiguel Ojeda# Testing target
20282f7ab126SMiguel OjedaPHONY += rusttest
20292f7ab126SMiguel Ojedarusttest: prepare
20302f7ab126SMiguel Ojeda	$(Q)$(MAKE) $(build)=rust $@
20312f7ab126SMiguel Ojeda
20322f7ab126SMiguel Ojeda# Formatting targets
2033158a3b72SMiguel Ojeda#
2034158a3b72SMiguel Ojeda# Generated files as well as vendored crates are skipped.
20352f7ab126SMiguel OjedaPHONY += rustfmt rustfmtcheck
20362f7ab126SMiguel Ojeda
20372f7ab126SMiguel Ojedarustfmt:
2038397a479bSMasahiro Yamada	$(Q)find $(srctree) $(RCS_FIND_IGNORE) \
2039158a3b72SMiguel Ojeda		\( \
2040158a3b72SMiguel Ojeda			-path $(srctree)/rust/proc-macro2 \
204188de91ccSMiguel Ojeda			-o -path $(srctree)/rust/quote \
204273740175SMiguel Ojeda			-o -path $(srctree)/rust/syn \
204356762152SMiguel Ojeda			-o -path $(srctree)/rust/zerocopy \
204450605498SMiguel Ojeda			-o -path $(srctree)/rust/zerocopy-derive \
2045158a3b72SMiguel Ojeda		\) -prune -o \
2046397a479bSMasahiro Yamada		-type f -a -name '*.rs' -a ! -name '*generated*' -print \
20472f7ab126SMiguel Ojeda		| xargs $(RUSTFMT) $(rustfmt_flags)
20482f7ab126SMiguel Ojeda
20492f7ab126SMiguel Ojedarustfmtcheck: rustfmt_flags = --check
20502f7ab126SMiguel Ojedarustfmtcheck: rustfmt
20512f7ab126SMiguel Ojeda
205267274c08SMasahiro Yamada# Misc
205367274c08SMasahiro Yamada# ---------------------------------------------------------------------------
205467274c08SMasahiro Yamada
205591ecf7ffSMasahiro YamadaPHONY += misc-check
205691ecf7ffSMasahiro Yamadamisc-check:
205791ecf7ffSMasahiro Yamada	$(Q)$(srctree)/scripts/misc-check
205891ecf7ffSMasahiro Yamada
205991ecf7ffSMasahiro Yamadaall: misc-check
206091ecf7ffSMasahiro Yamada
206167274c08SMasahiro YamadaPHONY += scripts_gdb
20627a739ce5SMasahiro Yamadascripts_gdb: prepare0
20631e5ff84fSMasahiro Yamada	$(Q)$(MAKE) $(build)=scripts/gdb
20648d2e5200SMasahiro Yamada	$(Q)ln -fsn $(abspath $(srctree)/scripts/gdb/vmlinux-gdb.py)
206567274c08SMasahiro Yamada
206667274c08SMasahiro Yamadaifdef CONFIG_GDB_SCRIPTS
206767274c08SMasahiro Yamadaall: scripts_gdb
206867274c08SMasahiro Yamadaendif
206967274c08SMasahiro Yamada
20701da177e4SLinus Torvaldselse # KBUILD_EXTMOD
20711da177e4SLinus Torvalds
2072c753ccb2STzafrir Cohenfilechk_kernel.release = echo $(KERNELRELEASE)
2073c753ccb2STzafrir Cohen
20741da177e4SLinus Torvalds###
20751da177e4SLinus Torvalds# External module support.
20761da177e4SLinus Torvalds# When building external modules the kernel used as basis is considered
20771da177e4SLinus Torvalds# read-only, and no consistency checks are made and the make
20781da177e4SLinus Torvalds# system is not used on the basis kernel. If updates are required
207911122b86SMasahiro Yamada# in the basis kernel ordinary make commands (without M=...) must be used.
20801da177e4SLinus Torvalds
20816212804fSMasahiro Yamada# We are always building only modules.
20826212804fSMasahiro YamadaKBUILD_BUILTIN :=
20831bf807b8SMasahiro YamadaKBUILD_MODULES := y
20841da177e4SLinus Torvalds
208513b25489SMasahiro Yamadabuild-dir := .
20863d32285fSMasahiro Yamada
208713b25489SMasahiro Yamadaclean-dirs := .
208813b25489SMasahiro Yamadaclean: private rm-files := Module.symvers modules.nsdeps compile_commands.json
20891da177e4SLinus Torvalds
20906072b2c4SMasahiro YamadaPHONY += prepare
20916072b2c4SMasahiro Yamada# now expand this into a simple variable to reduce the cost of shell evaluations
20926072b2c4SMasahiro Yamadaprepare: CC_VERSION_TEXT := $(CC_VERSION_TEXT)
209390e5b38aSIhor Solodraiprepare: PAHOLE_VERSION := $(PAHOLE_VERSION)
20946072b2c4SMasahiro Yamadaprepare:
2095129ab0d2SMasahiro Yamada	@if [ "$(CC_VERSION_TEXT)" != "$(CONFIG_CC_VERSION_TEXT)" ]; then \
20966072b2c4SMasahiro Yamada		echo >&2 "warning: the compiler differs from the one used to build the kernel"; \
2097129ab0d2SMasahiro Yamada		echo >&2 "  The kernel was built by: $(CONFIG_CC_VERSION_TEXT)"; \
20986072b2c4SMasahiro Yamada		echo >&2 "  You are using:           $(CC_VERSION_TEXT)"; \
20996072b2c4SMasahiro Yamada	fi
210090e5b38aSIhor Solodrai	@if [ "$(PAHOLE_VERSION)" != "$(CONFIG_PAHOLE_VERSION)" ]; then \
210190e5b38aSIhor Solodrai		echo >&2 "warning: pahole version differs from the one used to build the kernel"; \
210290e5b38aSIhor Solodrai		echo >&2 "  The kernel was built with: $(CONFIG_PAHOLE_VERSION)"; \
210390e5b38aSIhor Solodrai		echo >&2 "  You are using:             $(PAHOLE_VERSION)"; \
210490e5b38aSIhor Solodrai	fi
21056072b2c4SMasahiro Yamada
2106fe69b420SMasahiro YamadaPHONY += help
21071da177e4SLinus Torvaldshelp:
21081da177e4SLinus Torvalds	@echo  '  Building external modules.'
21091da177e4SLinus Torvalds	@echo  '  Syntax: make -C path/to/kernel/src M=$$PWD target'
21101da177e4SLinus Torvalds	@echo  ''
21111da177e4SLinus Torvalds	@echo  '  modules         - default target, build the module(s)'
21121da177e4SLinus Torvalds	@echo  '  modules_install - install the module'
21131da177e4SLinus Torvalds	@echo  '  clean           - remove generated files in module directory only'
211449a9ef76SVinay Varma	@echo  '  rust-analyzer	  - generate rust-project.json rust-analyzer support file'
21151da177e4SLinus Torvalds	@echo  ''
211606300b21SSam Ravnborg
21175e02797bSMasahiro Yamadaifndef CONFIG_MODULES
21185e02797bSMasahiro Yamadamodules modules_install: __external_modules_error
21198ae071fcSMasahiro Yamada__external_modules_error:
21208ae071fcSMasahiro Yamada	@echo >&2 '***'
21218ae071fcSMasahiro Yamada	@echo >&2 '*** The present kernel disabled CONFIG_MODULES.'
21228ae071fcSMasahiro Yamada	@echo >&2 '*** You cannot build or install external modules.'
21238ae071fcSMasahiro Yamada	@echo >&2 '***'
21248ae071fcSMasahiro Yamada	@false
21255e02797bSMasahiro Yamadaendif
21268ae071fcSMasahiro Yamada
21271da177e4SLinus Torvaldsendif # KBUILD_EXTMOD
21281da177e4SLinus Torvalds
21293e3005dfSMasahiro Yamada# ---------------------------------------------------------------------------
21303e3005dfSMasahiro Yamada# Modules
21313e3005dfSMasahiro Yamada
2132151aeca2SMasahiro YamadaPHONY += modules modules_install modules_sign modules_prepare
21333e3005dfSMasahiro Yamada
21345e02797bSMasahiro Yamadamodules_install:
2135151aeca2SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst \
2136151aeca2SMasahiro Yamada	sign-only=$(if $(filter modules_install,$(MAKECMDGOALS)),,y)
2137151aeca2SMasahiro Yamada
2138151aeca2SMasahiro Yamadaifeq ($(CONFIG_MODULE_SIG),y)
2139151aeca2SMasahiro Yamada# modules_sign is a subset of modules_install.
2140151aeca2SMasahiro Yamada# 'make modules_install modules_sign' is equivalent to 'make modules_install'.
2141151aeca2SMasahiro Yamadamodules_sign: modules_install
2142151aeca2SMasahiro Yamada	@:
2143151aeca2SMasahiro Yamadaelse
2144151aeca2SMasahiro Yamadamodules_sign:
2145151aeca2SMasahiro Yamada	@echo >&2 '***'
2146151aeca2SMasahiro Yamada	@echo >&2 '*** CONFIG_MODULE_SIG is disabled. You cannot sign modules.'
2147151aeca2SMasahiro Yamada	@echo >&2 '***'
2148151aeca2SMasahiro Yamada	@false
2149151aeca2SMasahiro Yamadaendif
21503e3005dfSMasahiro Yamada
21513e3005dfSMasahiro Yamadaifdef CONFIG_MODULES
21523e3005dfSMasahiro Yamada
2153bad6beb2SMasahiro Yamadamodules.order: $(build-dir)
21545750121aSMasahiro Yamada	@:
21555750121aSMasahiro Yamada
2156f73edc89SMasahiro Yamada# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
2157f73edc89SMasahiro Yamada# This is solely useful to speed up test compiles.
2158f73edc89SMasahiro Yamadamodules: modpost
2159f73edc89SMasahiro Yamadaifneq ($(KBUILD_MODPOST_NOFINAL),1)
2160f73edc89SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
2161f73edc89SMasahiro Yamadaendif
21623e3005dfSMasahiro Yamada
21631a998be6SMasahiro YamadaPHONY += modules_check
2164bad6beb2SMasahiro Yamadamodules_check: modules.order
21651a998be6SMasahiro Yamada	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/modules-check.sh $<
21661a998be6SMasahiro Yamada
21673e3005dfSMasahiro Yamadaelse # CONFIG_MODULES
21683e3005dfSMasahiro Yamada
21695e02797bSMasahiro Yamadamodules:
21708ae071fcSMasahiro Yamada	@:
21713e3005dfSMasahiro Yamada
2172f110e5a2SMasahiro YamadaKBUILD_MODULES :=
2173f110e5a2SMasahiro Yamada
21743e3005dfSMasahiro Yamadaendif # CONFIG_MODULES
21753e3005dfSMasahiro Yamada
2176f73edc89SMasahiro YamadaPHONY += modpost
2177f73edc89SMasahiro Yamadamodpost: $(if $(single-build),, $(if $(KBUILD_BUILTIN), vmlinux.o)) \
2178f73edc89SMasahiro Yamada	 $(if $(KBUILD_MODULES), modules_check)
2179f73edc89SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
2180f73edc89SMasahiro Yamada
2181b1fbfcb4SMasahiro Yamada# Single targets
2182b1fbfcb4SMasahiro Yamada# ---------------------------------------------------------------------------
2183b1fbfcb4SMasahiro Yamada# To build individual files in subdirectories, you can do like this:
2184b1fbfcb4SMasahiro Yamada#
2185b1fbfcb4SMasahiro Yamada#   make foo/bar/baz.s
2186b1fbfcb4SMasahiro Yamada#
2187b1fbfcb4SMasahiro Yamada# The supported suffixes for single-target are listed in 'single-targets'
2188b1fbfcb4SMasahiro Yamada#
2189b1fbfcb4SMasahiro Yamada# To build only under specific subdirectories, you can do like this:
2190b1fbfcb4SMasahiro Yamada#
2191b1fbfcb4SMasahiro Yamada#   make foo/bar/baz/
2192b1fbfcb4SMasahiro Yamada
2193b1fbfcb4SMasahiro Yamadaifdef single-build
2194b1fbfcb4SMasahiro Yamada
2195b1fbfcb4SMasahiro Yamada# .ko is special because modpost is needed
2196b1fbfcb4SMasahiro Yamadasingle-ko := $(sort $(filter %.ko, $(MAKECMDGOALS)))
2197fc93a4cdSMasahiro Yamadasingle-no-ko := $(filter-out $(single-ko), $(MAKECMDGOALS)) \
2198fc93a4cdSMasahiro Yamada		$(foreach x, o mod, $(patsubst %.ko, %.$x, $(single-ko)))
2199b1fbfcb4SMasahiro Yamada
2200f73edc89SMasahiro Yamada$(single-ko): single_modules
2201b1fbfcb4SMasahiro Yamada	@:
22025750121aSMasahiro Yamada$(single-no-ko): $(build-dir)
2203b1fbfcb4SMasahiro Yamada	@:
2204b1fbfcb4SMasahiro Yamada
2205bad6beb2SMasahiro Yamada# Remove modules.order when done because it is not the real one.
2206f73edc89SMasahiro YamadaPHONY += single_modules
2207f73edc89SMasahiro Yamadasingle_modules: $(single-no-ko) modules_prepare
2208bad6beb2SMasahiro Yamada	$(Q){ $(foreach m, $(single-ko), echo $(m:%.ko=%.o);) } > modules.order
2209b1fbfcb4SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
2210f73edc89SMasahiro Yamadaifneq ($(KBUILD_MODPOST_NOFINAL),1)
2211f73edc89SMasahiro Yamada	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
2212f73edc89SMasahiro Yamadaendif
2213bad6beb2SMasahiro Yamada	$(Q)rm -f modules.order
2214b1fbfcb4SMasahiro Yamada
22155750121aSMasahiro Yamadasingle-goals := $(addprefix $(build-dir)/, $(single-no-ko))
2216b1fbfcb4SMasahiro Yamada
22171bf807b8SMasahiro YamadaKBUILD_MODULES := y
22183753af77SMasahiro Yamada
2219b1fbfcb4SMasahiro Yamadaendif
2220b1fbfcb4SMasahiro Yamada
222111b3d517SMasahiro Yamadaprepare: outputmakefile
222211b3d517SMasahiro Yamada
2223c99f3918SMasahiro Yamada# Preset locale variables to speed up the build process. Limit locale
2224c99f3918SMasahiro Yamada# tweaks to this spot to avoid wrong language settings when running
2225c99f3918SMasahiro Yamada# make menuconfig etc.
2226c99f3918SMasahiro Yamada# Error messages still appears in the original language
22275750121aSMasahiro YamadaPHONY += $(build-dir)
22285750121aSMasahiro Yamada$(build-dir): prepare
22295750121aSMasahiro Yamada	$(Q)$(MAKE) $(build)=$@ need-builtin=1 need-modorder=1 $(single-goals)
2230c99f3918SMasahiro Yamada
223176cd306dSMasahiro Yamadaclean-dirs := $(addprefix _clean_, $(clean-dirs))
223276cd306dSMasahiro YamadaPHONY += $(clean-dirs) clean
223376cd306dSMasahiro Yamada$(clean-dirs):
223476cd306dSMasahiro Yamada	$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
223576cd306dSMasahiro Yamada
223688d7be03SMichal Marekclean: $(clean-dirs)
223788d7be03SMichal Marek	$(call cmd,rmfiles)
223813b25489SMasahiro Yamada	@find . $(RCS_FIND_IGNORE) \
22392f7ab126SMiguel Ojeda		\( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \
22409f2aee8fSRong Xu		-o -name '*.ko.*' -o -name '*.o.thinlto.bc' \
2241dcad240cSAndrew Davis		-o -name '*.dtb' -o -name '*.dtbo' \
2242dcad240cSAndrew Davis		-o -name '*.dtb.S' -o -name '*.dtbo.S' \
224324507871SMasahiro Yamada		-o -name '*.dt.yaml' -o -name 'dtbs-list' \
2244ef46d9b3SMasahiro Yamada		-o -name '*.dwo' -o -name '*.lst' \
22455e9e95ccSMasahiro Yamada		-o -name '*.su' -o -name '*.mod' \
224688d7be03SMichal Marek		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
22479a8dfb39SMasahiro Yamada		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
22484fa8bc94SMasahiro Yamada		-o -name '*.asn1.[ch]' \
224988d7be03SMichal Marek		-o -name '*.symtypes' -o -name 'modules.order' \
22506b90bd4bSEmese Revfy		-o -name '*.c.[012]*.*' \
2251433db3e2SVinícius Tinti		-o -name '*.ll' \
225238e89184SSami Tolvanen		-o -name '*.gcno' \
2253025fd4b4SJoel Kamminga		-o -name '*.long-type-*.txt' \
225487af9388SMasahiro Yamada		\) -type f -print \
22559c73bcfaSThomas Weißschuh		-o -name '.tmp_*' -print \
22569c73bcfaSThomas Weißschuh		| xargs rm -rf
225788d7be03SMichal Marek
22581da177e4SLinus Torvalds# Generate tags for editors
22591da177e4SLinus Torvalds# ---------------------------------------------------------------------------
2260a680eedcSSam Ravnborgquiet_cmd_tags = GEN     $@
2261858805b3SMasahiro Yamada      cmd_tags = $(BASH) $(srctree)/scripts/tags.sh $@
22621da177e4SLinus Torvalds
2263f4ed1009SJianbin Kangtags TAGS cscope gtags: FORCE
22641da177e4SLinus Torvalds	$(call cmd,tags)
22651da177e4SLinus Torvalds
22665045b460SJohn Hubbard# Generate rust-project.json (a file that describes the structure of non-Cargo
22675045b460SJohn Hubbard# Rust projects) for rust-analyzer (an implementation of the Language Server
22685045b460SJohn Hubbard# Protocol).
226949a9ef76SVinay VarmaPHONY += rust-analyzer
227049a9ef76SVinay Varmarust-analyzer:
2271d734422bSMiguel Ojeda	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
227213b25489SMasahiro Yamadaifdef KBUILD_EXTMOD
227313b25489SMasahiro Yamada# FIXME: external modules must not descend into a sub-directory of the kernel
227413b25489SMasahiro Yamada	$(Q)$(MAKE) $(build)=$(objtree)/rust src=$(srctree)/rust $@
227513b25489SMasahiro Yamadaelse
227649a9ef76SVinay Varma	$(Q)$(MAKE) $(build)=rust $@
227713b25489SMasahiro Yamadaendif
227849a9ef76SVinay Varma
2279eb8305aeSMatthias Maennich# Script to generate missing namespace dependencies
2280eb8305aeSMatthias Maennich# ---------------------------------------------------------------------------
2281eb8305aeSMatthias Maennich
2282eb8305aeSMatthias MaennichPHONY += nsdeps
2283bff9c62bSMasahiro Yamadansdeps: export KBUILD_NSDEPS=1
2284eb8305aeSMatthias Maennichnsdeps: modules
2285bff9c62bSMasahiro Yamada	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/nsdeps
2286eb8305aeSMatthias Maennich
2287e72b635cSLuis Augenstein# Script to generate .spdx.json SBOM documents describing the build
2288e72b635cSLuis Augenstein# ---------------------------------------------------------------------------
2289e72b635cSLuis Augenstein
2290e72b635cSLuis Augensteinifdef building_out_of_srctree
2291e72b635cSLuis Augensteinsbom_targets := sbom-source.spdx.json
2292e72b635cSLuis Augensteinendif
2293e72b635cSLuis Augensteinsbom_targets += sbom-build.spdx.json sbom-output.spdx.json
2294e72b635cSLuis Augensteinquiet_cmd_sbom = GEN     $(sbom_targets)
2295e72b635cSLuis Augenstein      cmd_sbom = printf "%s\n" "$(KBUILD_IMAGE)" >"$(tmp-target)"; \
2296e72b635cSLuis Augenstein                 $(if $(CONFIG_MODULES),sed 's/\.o$$/.ko/' $(objtree)/modules.order >> "$(tmp-target)";) \
22979c16c1eaSLuis Augenstein                 $(PYTHON3) $(srctree)/scripts/sbom/sbom.py \
22989c16c1eaSLuis Augenstein                     --src-tree $(abspath $(srctree)) \
22999c16c1eaSLuis Augenstein                     --obj-tree $(abspath $(objtree)) \
23009c16c1eaSLuis Augenstein                     --roots-file "$(tmp-target)" \
2301a68a29a1SLuis Augenstein                     --output-directory $(abspath $(objtree)) \
2302b0191211SLuis Augenstein                     --generate-spdx \
2303b0191211SLuis Augenstein                     --package-license "GPL-2.0 WITH Linux-syscall-note" \
2304b0191211SLuis Augenstein                     --package-version "$(KERNELVERSION)" \
2305b0191211SLuis Augenstein                     --write-output-on-error;
2306e72b635cSLuis AugensteinPHONY += sbom
2307e72b635cSLuis Augensteinsbom: $(notdir $(KBUILD_IMAGE)) include/generated/autoconf.h $(if $(CONFIG_MODULES),modules modules.order)
2308e72b635cSLuis Augenstein	$(call cmd,sbom)
2309e72b635cSLuis Augenstein
23103d32285fSMasahiro Yamada# Clang Tooling
23113d32285fSMasahiro Yamada# ---------------------------------------------------------------------------
23123d32285fSMasahiro Yamada
23133d32285fSMasahiro Yamadaquiet_cmd_gen_compile_commands = GEN     $@
23143d32285fSMasahiro Yamada      cmd_gen_compile_commands = $(PYTHON3) $< -a $(AR) -o $@ $(filter-out $<, $(real-prereqs))
23153d32285fSMasahiro Yamada
2316bad6beb2SMasahiro Yamadacompile_commands.json: $(srctree)/scripts/clang-tools/gen_compile_commands.py \
231732164845SMasahiro Yamada	$(if $(KBUILD_EXTMOD),, vmlinux.a $(KBUILD_VMLINUX_LIBS)) \
2318bad6beb2SMasahiro Yamada	$(if $(CONFIG_MODULES), modules.order) FORCE
23193d32285fSMasahiro Yamada	$(call if_changed,gen_compile_commands)
23203d32285fSMasahiro Yamada
2321bad6beb2SMasahiro Yamadatargets += compile_commands.json
23223d32285fSMasahiro Yamada
23236ad7cbc0SNathan HuckleberryPHONY += clang-tidy clang-analyzer
23246ad7cbc0SNathan Huckleberry
23256ad7cbc0SNathan Huckleberryifdef CONFIG_CC_IS_CLANG
23266ad7cbc0SNathan Huckleberryquiet_cmd_clang_tools = CHECK   $<
23276ad7cbc0SNathan Huckleberry      cmd_clang_tools = $(PYTHON3) $(srctree)/scripts/clang-tools/run-clang-tools.py $@ $<
23286ad7cbc0SNathan Huckleberry
2329bad6beb2SMasahiro Yamadaclang-tidy clang-analyzer: compile_commands.json
23306ad7cbc0SNathan Huckleberry	$(call cmd,clang_tools)
23316ad7cbc0SNathan Huckleberryelse
23326ad7cbc0SNathan Huckleberryclang-tidy clang-analyzer:
23336ad7cbc0SNathan Huckleberry	@echo "$@ requires CC=clang" >&2
23346ad7cbc0SNathan Huckleberry	@false
23356ad7cbc0SNathan Huckleberryendif
23366ad7cbc0SNathan Huckleberry
23371da177e4SLinus Torvalds# Scripts to check various things for consistency
23381da177e4SLinus Torvalds# ---------------------------------------------------------------------------
23391da177e4SLinus Torvalds
2340f58316a4SAndrew JonesPHONY += includecheck versioncheck coccicheck kconfig-sym-check
2341279f3dd3SPeter Foley
23421da177e4SLinus Torvaldsincludecheck:
2343436f876cSPeter Foley	find $(srctree)/* $(RCS_FIND_IGNORE) \
23441da177e4SLinus Torvalds		-name '*.[hcS]' -type f -print | sort \
234580007434SGeert Uytterhoeven		| xargs $(PERL) -w $(srctree)/scripts/checkincludes.pl
23461da177e4SLinus Torvalds
23471da177e4SLinus Torvaldsversioncheck:
23482ee2d292SPeter Foley	find $(srctree)/* $(RCS_FIND_IGNORE) \
23491da177e4SLinus Torvalds		-name '*.[hcS]' -type f -print | sort \
235080007434SGeert Uytterhoeven		| xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
23511da177e4SLinus Torvalds
235274425eeeSNicolas Palixcoccicheck:
2353858805b3SMasahiro Yamada	$(Q)$(BASH) $(srctree)/scripts/$@
235474425eeeSNicolas Palix
2355f58316a4SAndrew Joneskconfig-sym-check:
2356f58316a4SAndrew Jones	$(Q)$(PERL) $(srctree)/scripts/kconfig/kconfig-sym-check.pl $(srctree) $(KCONFIG_SYM_CHECK_EXCLUDES)
2357f58316a4SAndrew Jones
2358c398ff00SMike MarciniszynPHONY += checkstack kernelrelease kernelversion image_name
2359e3ccf6e3SJeff Dike
2360011e3a9aSJeff Dike# UML needs a little special treatment here.  It wants to use the host
2361011e3a9aSJeff Dike# toolchain, so needs $(SUBARCH) passed to checkstack.pl.  Everyone
2362011e3a9aSJeff Dike# else wants $(ARCH), including people doing cross-builds, which means
2363011e3a9aSJeff Dike# that $(SUBARCH) doesn't work here.
2364011e3a9aSJeff Dikeifeq ($(ARCH), um)
2365011e3a9aSJeff DikeCHECKSTACK_ARCH := $(SUBARCH)
2366011e3a9aSJeff Dikeelse
2367011e3a9aSJeff DikeCHECKSTACK_ARCH := $(ARCH)
2368011e3a9aSJeff Dikeendif
236966242cfaSHeiko CarstensMINSTACKSIZE	?= 100
23701da177e4SLinus Torvaldscheckstack:
23711da177e4SLinus Torvalds	$(OBJDUMP) -d vmlinux $$(find . -name '*.ko') | \
237266242cfaSHeiko Carstens	$(PERL) $(srctree)/scripts/checkstack.pl $(CHECKSTACK_ARCH) $(MINSTACKSIZE)
23731da177e4SLinus Torvalds
23747b8ea53dSAmerigo Wangkernelrelease:
23751cb86b6cSMasahiro Yamada	@$(filechk_kernel.release)
237601ab1788SAmerigo Wang
2377cb58455cSSam Ravnborgkernelversion:
23782244cbd8SSam Ravnborg	@echo $(KERNELVERSION)
23791da177e4SLinus Torvalds
2380c398ff00SMike Marciniszynimage_name:
2381c398ff00SMike Marciniszyn	@echo $(KBUILD_IMAGE)
2382c398ff00SMike Marciniszyn
238376a48b8fSMasahiro YamadaPHONY += run-command
238476a48b8fSMasahiro Yamadarun-command:
238576a48b8fSMasahiro Yamada	$(Q)$(KBUILD_RUN_COMMAND)
238676a48b8fSMasahiro Yamada
23871da177e4SLinus Torvaldsquiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN   $(wildcard $(rm-files)))
23880663c68cSMasahiro Yamada      cmd_rmfiles = rm -rf $(rm-files)
23891da177e4SLinus Torvalds
2390392885eeSMasahiro Yamada# read saved command lines for existing targets
2391392885eeSMasahiro Yamadaexisting-targets := $(wildcard $(sort $(targets)))
23921da177e4SLinus Torvalds
2393b999923cSMasahiro Yamada-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
23941da177e4SLinus Torvalds
239546b7c492SSZ Lin (林上智)endif # config-build
23962042b548SMasahiro Yamadaendif # mixed-build
2397688931a5SMasahiro Yamadaendif # need-sub-make
23981da177e4SLinus Torvalds
23994f193362SPaul SmithPHONY += FORCE
24001da177e4SLinus TorvaldsFORCE:
24014f193362SPaul Smith
2402bd412d81SUlf Magnusson# Declare the contents of the PHONY variable as phony.  We keep that
2403fe8d0a41SKirill Smelkov# information in a variable so we can use it in if_changed and friends.
24044f193362SPaul Smith.PHONY: $(PHONY)
2405