xref: /linux/Makefile (revision 56cb9b7d96b28a1173a510ab25354b6599ad3a33)
1# SPDX-License-Identifier: GPL-2.0
2VERSION = 7
3PATCHLEVEL = 1
4SUBLEVEL = 0
5EXTRAVERSION = -rc5
6NAME = Baby Opossum Posse
7
8# *DOCUMENTATION*
9# To see a list of typical targets execute "make help"
10# More info can be located in ./README
11# Comments in this file are targeted only to the developer, do not
12# expect to learn how to build the kernel reading this file.
13
14ifeq ($(filter output-sync,$(.FEATURES)),)
15$(error GNU Make >= 4.0 is required. Your Make version is $(MAKE_VERSION))
16endif
17
18$(if $(filter __%, $(MAKECMDGOALS)), \
19	$(error targets prefixed with '__' are only for internal use))
20
21# That's our default target when none is given on the command line
22PHONY := __all
23__all:
24
25# We are using a recursive build, so we need to do a little thinking
26# to get the ordering right.
27#
28# Most importantly: sub-Makefiles should only ever modify files in
29# their own directory. If in some directory we have a dependency on
30# a file in another dir (which doesn't happen often, but it's often
31# unavoidable when linking the built-in.a targets which finally
32# turn into vmlinux), we will call a sub make in that other dir, and
33# after that we are sure that everything which is in that other dir
34# is now up to date.
35#
36# The only cases where we need to modify files which have global
37# effects are thus separated out and done before the recursive
38# descending is started. They are now explicitly listed as the
39# prepare rule.
40
41this-makefile := $(lastword $(MAKEFILE_LIST))
42abs_srctree := $(realpath $(dir $(this-makefile)))
43abs_output := $(CURDIR)
44
45ifneq ($(sub_make_done),1)
46
47# Do not use make's built-in rules and variables
48# (this increases performance and avoids hard-to-debug behaviour)
49MAKEFLAGS += -rR
50
51# Avoid funny character set dependencies
52unexport LC_ALL
53LC_COLLATE=C
54LC_NUMERIC=C
55export LC_COLLATE LC_NUMERIC
56
57# Avoid interference with shell env settings
58unexport GREP_OPTIONS
59
60# Beautify output
61# ---------------------------------------------------------------------------
62#
63# Most of build commands in Kbuild start with "cmd_". You can optionally define
64# "quiet_cmd_*". If defined, the short log is printed. Otherwise, no log from
65# that command is printed by default.
66#
67# e.g.)
68#    quiet_cmd_depmod = DEPMOD  $(MODLIB)
69#          cmd_depmod = $(srctree)/scripts/depmod.sh $(DEPMOD) $(KERNELRELEASE)
70#
71# A simple variant is to prefix commands with $(Q) - that's useful
72# for commands that shall be hidden in non-verbose mode.
73#
74#    $(Q)$(MAKE) $(build)=scripts/basic
75#
76# If KBUILD_VERBOSE contains 1, the whole command is echoed.
77# If KBUILD_VERBOSE contains 2, the reason for rebuilding is printed.
78#
79# To put more focus on warnings, be less verbose as default
80# Use 'make V=1' to see the full commands
81
82ifeq ("$(origin V)", "command line")
83  KBUILD_VERBOSE = $(V)
84endif
85
86quiet = quiet_
87Q = @
88
89ifneq ($(findstring 1, $(KBUILD_VERBOSE)),)
90  quiet =
91  Q =
92endif
93
94# If the user is running make -s (silent mode), suppress echoing of
95# commands
96ifneq ($(findstring s,$(firstword -$(MAKEFLAGS))),)
97quiet=silent_
98override KBUILD_VERBOSE :=
99endif
100
101export quiet Q KBUILD_VERBOSE
102
103# Call a source code checker (by default, "sparse") as part of the
104# C compilation.
105#
106# Use 'make C=1' to enable checking of only re-compiled files.
107# Use 'make C=2' to enable checking of *all* source files, regardless
108# of whether they are re-compiled or not.
109#
110# See the file "Documentation/dev-tools/sparse.rst" for more details,
111# including where to get the "sparse" utility.
112
113ifeq ("$(origin C)", "command line")
114  KBUILD_CHECKSRC = $(C)
115endif
116ifndef KBUILD_CHECKSRC
117  KBUILD_CHECKSRC = 0
118endif
119
120export KBUILD_CHECKSRC
121
122# Enable "clippy" (a linter) as part of the Rust compilation.
123#
124# Use 'make CLIPPY=1' to enable it.
125ifeq ("$(origin CLIPPY)", "command line")
126  KBUILD_CLIPPY := $(CLIPPY)
127endif
128
129export KBUILD_CLIPPY
130
131# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the
132# directory of external module to build. Setting M= takes precedence.
133ifeq ("$(origin M)", "command line")
134  KBUILD_EXTMOD := $(M)
135endif
136
137ifeq ("$(origin MO)", "command line")
138  KBUILD_EXTMOD_OUTPUT := $(MO)
139endif
140
141$(if $(word 2, $(KBUILD_EXTMOD)), \
142	$(error building multiple external modules is not supported))
143
144$(foreach x, % :, $(if $(findstring $x, $(KBUILD_EXTMOD)), \
145	$(error module directory path cannot contain '$x')))
146
147# Remove trailing slashes
148ifneq ($(filter %/, $(KBUILD_EXTMOD)),)
149KBUILD_EXTMOD := $(shell dirname $(KBUILD_EXTMOD).)
150endif
151
152export KBUILD_EXTMOD
153
154ifeq ("$(origin W)", "command line")
155  KBUILD_EXTRA_WARN := $(W)
156endif
157
158export KBUILD_EXTRA_WARN
159
160# Kbuild will save output files in the current working directory.
161# This does not need to match to the root of the kernel source tree.
162#
163# For example, you can do this:
164#
165#  cd /dir/to/store/output/files; make -f /dir/to/kernel/source/Makefile
166#
167# If you want to save output files in a different location, there are
168# two syntaxes to specify it.
169#
170# 1) O=
171# Use "make O=dir/to/store/output/files/"
172#
173# 2) Set KBUILD_OUTPUT
174# Set the environment variable KBUILD_OUTPUT to point to the output directory.
175# export KBUILD_OUTPUT=dir/to/store/output/files/; make
176#
177# The O= assignment takes precedence over the KBUILD_OUTPUT environment
178# variable.
179
180ifeq ("$(origin O)", "command line")
181  KBUILD_OUTPUT := $(O)
182endif
183
184ifdef KBUILD_EXTMOD
185    ifdef KBUILD_OUTPUT
186        objtree := $(realpath $(KBUILD_OUTPUT))
187        $(if $(objtree),,$(error specified kernel directory "$(KBUILD_OUTPUT)" does not exist))
188    else
189        objtree := $(abs_srctree)
190    endif
191    # If Make is invoked from the kernel directory (either kernel
192    # source directory or kernel build directory), external modules
193    # are built in $(KBUILD_EXTMOD) for backward compatibility,
194    # otherwise, built in the current directory.
195    output := $(or $(KBUILD_EXTMOD_OUTPUT),$(if $(filter $(CURDIR),$(objtree) $(abs_srctree)),$(KBUILD_EXTMOD)))
196    # KBUILD_EXTMOD might be a relative path. Remember its absolute path before
197    # Make changes the working directory.
198    srcroot := $(realpath $(KBUILD_EXTMOD))
199    $(if $(srcroot),,$(error specified external module directory "$(KBUILD_EXTMOD)" does not exist))
200else
201    objtree := .
202    output := $(KBUILD_OUTPUT)
203endif
204
205export objtree srcroot
206
207# Do we want to change the working directory?
208ifneq ($(output),)
209# $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first.
210$(shell mkdir -p "$(output)")
211# $(realpath ...) resolves symlinks
212abs_output := $(realpath $(output))
213$(if $(abs_output),,$(error failed to create output directory "$(output)"))
214endif
215
216ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
217$(error source directory cannot contain spaces or colons)
218endif
219
220export sub_make_done := 1
221
222endif # sub_make_done
223
224ifeq ($(abs_output),$(CURDIR))
225# Suppress "Entering directory ..." if we are at the final work directory.
226no-print-directory := --no-print-directory
227else
228# Recursion to show "Entering directory ..."
229need-sub-make := 1
230endif
231
232ifeq ($(filter --no-print-directory, $(MAKEFLAGS)),)
233# If --no-print-directory is unset, recurse once again to set it.
234# You may end up recursing into __sub-make twice. This is needed due to the
235# behavior change in GNU Make 4.4.1.
236need-sub-make := 1
237endif
238
239ifeq ($(need-sub-make),1)
240
241PHONY += $(MAKECMDGOALS) __sub-make
242
243$(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
244	@:
245
246# Invoke a second make in the output directory, passing relevant variables
247__sub-make:
248	$(Q)$(MAKE) $(no-print-directory) -C $(abs_output) \
249	-f $(abs_srctree)/Makefile $(MAKECMDGOALS)
250
251else # need-sub-make
252
253# We process the rest of the Makefile if this is the final invocation of make
254
255ifndef KBUILD_EXTMOD
256srcroot := $(abs_srctree)
257endif
258
259ifeq ($(srcroot),$(CURDIR))
260building_out_of_srctree :=
261else
262export building_out_of_srctree := 1
263endif
264
265ifdef KBUILD_ABS_SRCTREE
266    # Do nothing. Use the absolute path.
267else ifeq ($(srcroot),$(CURDIR))
268    # Building in the source.
269    srcroot := .
270else ifeq ($(srcroot)/,$(dir $(CURDIR)))
271    # Building in a subdirectory of the source.
272    srcroot := ..
273endif
274
275export srctree := $(if $(KBUILD_EXTMOD),$(abs_srctree),$(srcroot))
276
277ifdef building_out_of_srctree
278export VPATH := $(srcroot)
279else
280VPATH :=
281endif
282
283# To make sure we do not include .config for any of the *config targets
284# catch them early, and hand them over to scripts/kconfig/Makefile
285# It is allowed to specify more targets when calling make, including
286# mixing *config targets and build targets.
287# For example 'make oldconfig all'.
288# Detect when mixed targets is specified, and make a second invocation
289# of make so .config is not included in this case either (for *config).
290
291version_h := include/generated/uapi/linux/version.h
292
293clean-targets := %clean mrproper cleandocs
294no-dot-config-targets := $(clean-targets) \
295			 cscope gtags TAGS tags help% %docs check% coccicheck \
296			 $(version_h) headers headers_% archheaders archscripts \
297			 %asm-generic kernelversion %src-pkg dt_binding_check \
298			 outputmakefile rustavailable rustfmt rustfmtcheck \
299			 run-command
300no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \
301			  image_name
302single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %/
303
304config-build	:=
305mixed-build	:=
306need-config	:= 1
307may-sync-config	:= 1
308single-build	:=
309
310ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
311    ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
312        need-config :=
313    endif
314endif
315
316ifneq ($(filter $(no-sync-config-targets), $(MAKECMDGOALS)),)
317    ifeq ($(filter-out $(no-sync-config-targets), $(MAKECMDGOALS)),)
318        may-sync-config :=
319    endif
320endif
321
322need-compiler := $(may-sync-config)
323
324ifneq ($(KBUILD_EXTMOD),)
325    may-sync-config :=
326endif
327
328ifeq ($(KBUILD_EXTMOD),)
329    ifneq ($(filter %config,$(MAKECMDGOALS)),)
330        config-build := 1
331        ifneq ($(words $(MAKECMDGOALS)),1)
332            mixed-build := 1
333        endif
334    endif
335endif
336
337# We cannot build single targets and the others at the same time
338ifneq ($(filter $(single-targets), $(MAKECMDGOALS)),)
339    single-build := 1
340    ifneq ($(filter-out $(single-targets), $(MAKECMDGOALS)),)
341        mixed-build := 1
342    endif
343endif
344
345# For "make -j clean all", "make -j mrproper defconfig all", etc.
346ifneq ($(filter $(clean-targets),$(MAKECMDGOALS)),)
347    ifneq ($(filter-out $(clean-targets),$(MAKECMDGOALS)),)
348        mixed-build := 1
349    endif
350endif
351
352# install and modules_install need also be processed one by one
353ifneq ($(filter install,$(MAKECMDGOALS)),)
354    ifneq ($(filter modules_install,$(MAKECMDGOALS)),)
355        mixed-build := 1
356    endif
357endif
358
359ifdef mixed-build
360# ===========================================================================
361# We're called with mixed targets (*config and build targets).
362# Handle them one by one.
363
364PHONY += $(MAKECMDGOALS) __build_one_by_one
365
366$(MAKECMDGOALS): __build_one_by_one
367	@:
368
369__build_one_by_one:
370	$(Q)set -e; \
371	for i in $(MAKECMDGOALS); do \
372		$(MAKE) -f $(srctree)/Makefile $$i; \
373	done
374
375else # !mixed-build
376
377include $(srctree)/scripts/Kbuild.include
378
379# Read KERNELRELEASE from include/config/kernel.release (if it exists)
380KERNELRELEASE = $(call read-file, $(objtree)/include/config/kernel.release)
381KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
382export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
383
384include $(srctree)/scripts/subarch.include
385
386# Cross compiling and selecting different set of gcc/bin-utils
387# ---------------------------------------------------------------------------
388#
389# When performing cross compilation for other architectures ARCH shall be set
390# to the target architecture. (See arch/* for the possibilities).
391# ARCH can be set during invocation of make:
392# make ARCH=arm64
393# Another way is to have ARCH set in the environment.
394# The default ARCH is the host where make is executed.
395
396# CROSS_COMPILE specify the prefix used for all executables used
397# during compilation. Only gcc and related bin-utils executables
398# are prefixed with $(CROSS_COMPILE).
399# CROSS_COMPILE can be set on the command line
400# make CROSS_COMPILE=aarch64-linux-gnu-
401# Alternatively CROSS_COMPILE can be set in the environment.
402# Default value for CROSS_COMPILE is not to prefix executables
403# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
404ARCH		?= $(SUBARCH)
405
406# Architecture as present in compile.h
407UTS_MACHINE 	:= $(ARCH)
408SRCARCH 	:= $(ARCH)
409
410# Additional ARCH settings for x86
411ifeq ($(ARCH),i386)
412        SRCARCH := x86
413endif
414ifeq ($(ARCH),x86_64)
415        SRCARCH := x86
416endif
417
418# Additional ARCH settings for sparc
419ifeq ($(ARCH),sparc32)
420       SRCARCH := sparc
421endif
422ifeq ($(ARCH),sparc64)
423       SRCARCH := sparc
424endif
425
426# Additional ARCH settings for parisc
427ifeq ($(ARCH),parisc64)
428       SRCARCH := parisc
429endif
430
431export cross_compiling :=
432ifneq ($(SRCARCH),$(SUBARCH))
433cross_compiling := 1
434endif
435
436KCONFIG_CONFIG	?= .config
437export KCONFIG_CONFIG
438
439# SHELL used by kbuild
440CONFIG_SHELL := sh
441
442HOST_LFS_CFLAGS := $(shell getconf LFS_CFLAGS 2>/dev/null)
443HOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
444HOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
445
446ifneq ($(LLVM),)
447ifneq ($(filter %/,$(LLVM)),)
448LLVM_PREFIX := $(LLVM)
449else ifneq ($(filter -%,$(LLVM)),)
450LLVM_SUFFIX := $(LLVM)
451else ifneq ($(LLVM),1)
452$(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst)
453endif
454
455HOSTCC	= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
456HOSTCXX	= $(LLVM_PREFIX)clang++$(LLVM_SUFFIX)
457else
458HOSTCC	= gcc
459HOSTCXX	= g++
460endif
461HOSTRUSTC = rustc
462HOSTPKG_CONFIG	= pkg-config
463
464# the KERNELDOC macro needs to be exported, as scripts/Makefile.build
465# has a logic to call it
466KERNELDOC       = $(srctree)/tools/docs/kernel-doc
467export KERNELDOC
468
469KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
470			 -O2 -fomit-frame-pointer -std=gnu11
471KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
472KBUILD_USERLDFLAGS := $(USERLDFLAGS)
473
474# These flags apply to all Rust code in the tree, including the kernel and
475# host programs.
476export rust_common_flags := --edition=2021 \
477			    -Zbinary_dep_depinfo=y \
478			    -Astable_features \
479			    -Aunused_features \
480			    -Dnon_ascii_idents \
481			    -Dunsafe_op_in_unsafe_fn \
482			    -Wmissing_docs \
483			    -Wrust_2018_idioms \
484			    -Wunreachable_pub \
485			    -Wclippy::all \
486			    -Wclippy::as_ptr_cast_mut \
487			    -Wclippy::as_underscore \
488			    -Wclippy::cast_lossless \
489			    -Aclippy::collapsible_if \
490			    -Aclippy::collapsible_match \
491			    -Wclippy::ignored_unit_patterns \
492			    -Aclippy::incompatible_msrv \
493			    -Wclippy::mut_mut \
494			    -Wclippy::needless_bitwise_bool \
495			    -Aclippy::needless_lifetimes \
496			    -Wclippy::no_mangle_with_rust_abi \
497			    -Wclippy::ptr_as_ptr \
498			    -Wclippy::ptr_cast_constness \
499			    -Wclippy::ref_as_ptr \
500			    -Wclippy::undocumented_unsafe_blocks \
501			    -Aclippy::uninlined_format_args \
502			    -Wclippy::unnecessary_safety_comment \
503			    -Wclippy::unnecessary_safety_doc \
504			    -Wrustdoc::missing_crate_level_docs \
505			    -Wrustdoc::unescaped_backticks
506
507KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \
508		       $(HOSTCFLAGS) -I $(srctree)/scripts/include
509KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS) \
510		       -I $(srctree)/scripts/include
511KBUILD_HOSTRUSTFLAGS := $(rust_common_flags) -O -Cstrip=debuginfo \
512			-Zallow-features=
513KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
514KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
515KBUILD_PROCMACROLDFLAGS := $(or $(PROCMACROLDFLAGS),$(KBUILD_HOSTLDFLAGS))
516
517# Make variables (CC, etc...)
518CPP		= $(CC) -E
519ifneq ($(LLVM),)
520CC		= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
521LD		= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
522AR		= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
523LLVM_LINK	= $(LLVM_PREFIX)llvm-link$(LLVM_SUFFIX)
524NM		= $(LLVM_PREFIX)llvm-nm$(LLVM_SUFFIX)
525OBJCOPY		= $(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)
526OBJDUMP		= $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX)
527READELF		= $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
528STRIP		= $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
529else
530CC		= $(CROSS_COMPILE)gcc
531LD		= $(CROSS_COMPILE)ld
532AR		= $(CROSS_COMPILE)ar
533NM		= $(CROSS_COMPILE)nm
534OBJCOPY		= $(CROSS_COMPILE)objcopy
535OBJDUMP		= $(CROSS_COMPILE)objdump
536READELF		= $(CROSS_COMPILE)readelf
537STRIP		= $(CROSS_COMPILE)strip
538endif
539RUSTC		= rustc
540RUSTDOC		= rustdoc
541RUSTFMT		= rustfmt
542CLIPPY_DRIVER	= clippy-driver
543BINDGEN		= bindgen
544PAHOLE		= pahole
545RESOLVE_BTFIDS	= $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
546LEX		= flex
547YACC		= bison
548AWK		= awk
549INSTALLKERNEL  := installkernel
550PERL		= perl
551PYTHON3		= python3
552CHECK		= sparse
553BASH		= bash
554KGZIP		= gzip
555KBZIP2		= bzip2
556KLZOP		= lzop
557LZMA		= lzma
558LZ4		= lz4
559XZ		= xz
560ZSTD		= zstd
561TAR		= tar
562
563CHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
564		  -Wbitwise -Wno-return-void -Wno-unknown-attribute $(CF)
565NOSTDINC_FLAGS :=
566CFLAGS_MODULE   =
567RUSTFLAGS_MODULE =
568AFLAGS_MODULE   =
569LDFLAGS_MODULE  =
570CFLAGS_KERNEL	=
571RUSTFLAGS_KERNEL =
572AFLAGS_KERNEL	=
573LDFLAGS_vmlinux =
574
575# Use USERINCLUDE when you must reference the UAPI directories only.
576USERINCLUDE    := \
577		-I$(srctree)/arch/$(SRCARCH)/include/uapi \
578		-I$(objtree)/arch/$(SRCARCH)/include/generated/uapi \
579		-I$(srctree)/include/uapi \
580		-I$(objtree)/include/generated/uapi \
581                -include $(srctree)/include/linux/compiler-version.h \
582                -include $(srctree)/include/linux/kconfig.h
583
584# Use LINUXINCLUDE when you must reference the include/ directory.
585# Needed to be compatible with the O= option
586LINUXINCLUDE    := \
587		-I$(srctree)/arch/$(SRCARCH)/include \
588		-I$(objtree)/arch/$(SRCARCH)/include/generated \
589		-I$(srctree)/include \
590		-I$(objtree)/include \
591		$(USERINCLUDE)
592
593KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-PIE
594
595KBUILD_CFLAGS :=
596KBUILD_CFLAGS += -fshort-wchar
597KBUILD_CFLAGS += -funsigned-char
598KBUILD_CFLAGS += -fno-common
599KBUILD_CFLAGS += -fno-PIE
600KBUILD_CFLAGS += -fno-strict-aliasing
601
602KBUILD_CPPFLAGS := -D__KERNEL__
603KBUILD_RUSTFLAGS := $(rust_common_flags) \
604		    -Cpanic=abort -Cembed-bitcode=n -Clto=n \
605		    -Cforce-unwind-tables=n -Ccodegen-units=1 \
606		    -Csymbol-mangling-version=v0 \
607		    -Crelocation-model=static \
608		    -Zfunction-sections=n \
609		    -Wclippy::float_arithmetic
610
611KBUILD_AFLAGS_KERNEL :=
612KBUILD_CFLAGS_KERNEL :=
613KBUILD_RUSTFLAGS_KERNEL :=
614KBUILD_AFLAGS_MODULE  := -DMODULE
615KBUILD_CFLAGS_MODULE  := -DMODULE
616KBUILD_RUSTFLAGS_MODULE := --cfg MODULE
617KBUILD_LDFLAGS_MODULE :=
618KBUILD_LDFLAGS :=
619CLANG_FLAGS :=
620
621ifeq ($(KBUILD_CLIPPY),1)
622	RUSTC_OR_CLIPPY_QUIET := CLIPPY
623	RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER)
624else
625	RUSTC_OR_CLIPPY_QUIET := RUSTC
626	RUSTC_OR_CLIPPY = $(RUSTC)
627endif
628
629# Allows the usage of unstable features in stable compilers.
630export RUSTC_BOOTSTRAP := 1
631
632# Allows finding `.clippy.toml` in out-of-srctree builds.
633export CLIPPY_CONF_DIR := $(srctree)
634
635export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC HOSTPKG_CONFIG
636export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK
637export HOSTRUSTC KBUILD_HOSTRUSTFLAGS
638export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
639export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
640export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD TAR
641export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS KBUILD_PROCMACROLDFLAGS LDFLAGS_MODULE
642export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
643
644export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
645export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
646export KBUILD_RUSTFLAGS RUSTFLAGS_KERNEL RUSTFLAGS_MODULE
647export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
648export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_RUSTFLAGS_MODULE KBUILD_LDFLAGS_MODULE
649export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL KBUILD_RUSTFLAGS_KERNEL
650
651# Files to ignore in find ... statements
652
653export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o    \
654			  -name CVS -o -name .pc -o -name .hg -o -name .git \) \
655			  -prune -o
656
657# ===========================================================================
658# Rules shared between *config targets and build targets
659
660# Basic helpers built in scripts/basic/
661PHONY += scripts_basic
662scripts_basic: KBUILD_HOSTCFLAGS := $(KBUILD_HOSTCFLAGS)
663scripts_basic: KBUILD_HOSTLDFLAGS := $(KBUILD_HOSTLDFLAGS)
664scripts_basic:
665	$(Q)$(MAKE) $(build)=scripts/basic
666
667PHONY += outputmakefile
668ifdef building_out_of_srctree
669# Before starting out-of-tree build, make sure the source tree is clean.
670# outputmakefile generates a Makefile in the output directory, if using a
671# separate output directory. This allows convenient use of make in the
672# output directory.
673# At the same time when output Makefile generated, generate .gitignore to
674# ignore whole output directory
675
676ifdef KBUILD_EXTMOD
677print_env_for_makefile = \
678	echo "export KBUILD_OUTPUT = $(objtree)"; \
679	echo "export KBUILD_EXTMOD = $(realpath $(srcroot))" ; \
680	echo "export KBUILD_EXTMOD_OUTPUT = $(CURDIR)"
681else
682print_env_for_makefile = \
683	echo "export KBUILD_OUTPUT = $(CURDIR)"
684endif
685
686filechk_makefile = { \
687	echo "\# Automatically generated by $(abs_srctree)/Makefile: don't edit"; \
688	$(print_env_for_makefile); \
689	echo "include $(abs_srctree)/Makefile"; \
690	}
691
692$(objtree)/Makefile: FORCE
693	$(call filechk,makefile)
694
695# Prevent $(srcroot)/Makefile from inhibiting the rule to run.
696PHONY += $(objtree)/Makefile
697
698outputmakefile: $(objtree)/Makefile
699ifeq ($(KBUILD_EXTMOD),)
700	@if [ -f $(srctree)/.config -o \
701		 -d $(srctree)/include/config -o \
702		 -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \
703		echo >&2 "***"; \
704		echo >&2 "*** The source tree is not clean, please run 'make$(if $(findstring command line, $(origin ARCH)), ARCH=$(ARCH)) mrproper'"; \
705		echo >&2 "*** in $(abs_srctree)";\
706		echo >&2 "***"; \
707		false; \
708	fi
709else
710	@if [ -f $(srcroot)/modules.order ]; then \
711		echo >&2 "***"; \
712		echo >&2 "*** The external module source tree is not clean."; \
713		echo >&2 "*** Please run 'make -C $(abs_srctree) M=$(realpath $(srcroot)) clean'"; \
714		echo >&2 "***"; \
715		false; \
716	fi
717endif
718	$(Q)ln -fsn $(srcroot) source
719	$(Q)test -e .gitignore || \
720	{ echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
721endif
722
723# The expansion should be delayed until arch/$(SRCARCH)/Makefile is included.
724# Some architectures define CROSS_COMPILE in arch/$(SRCARCH)/Makefile.
725# CC_VERSION_TEXT, RUSTC_VERSION_TEXT and PAHOLE_VERSION are referenced from
726# Kconfig (so they need export), and from include/config/auto.conf.cmd to
727# detect the version changes between builds.
728CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
729RUSTC_VERSION_TEXT = $(subst $(pound),,$(shell $(RUSTC) --version 2>/dev/null))
730PAHOLE_VERSION = $(shell $(srctree)/scripts/pahole-version.sh $(PAHOLE))
731
732ifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
733include $(srctree)/scripts/Makefile.clang
734endif
735
736# Include this also for config targets because some architectures need
737# cc-cross-prefix to determine CROSS_COMPILE.
738ifdef need-compiler
739include $(srctree)/scripts/Makefile.compiler
740endif
741
742ifdef config-build
743# ===========================================================================
744# *config targets only - make sure prerequisites are updated, and descend
745# in scripts/kconfig to make the *config target
746
747# Read arch-specific Makefile to set KBUILD_DEFCONFIG as needed.
748# KBUILD_DEFCONFIG may point out an alternative default configuration
749# used for 'make defconfig'
750include $(srctree)/arch/$(SRCARCH)/Makefile
751export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT RUSTC_VERSION_TEXT PAHOLE_VERSION
752
753config: outputmakefile scripts_basic FORCE
754	$(Q)$(MAKE) $(build)=scripts/kconfig $@
755
756%config: outputmakefile scripts_basic FORCE
757	$(Q)$(MAKE) $(build)=scripts/kconfig $@
758
759else #!config-build
760# ===========================================================================
761# Build targets only - this includes vmlinux, arch-specific targets, clean
762# targets and others. In general all targets except *config targets.
763
764# If building an external module we do not care about the all: rule
765# but instead __all depend on modules
766PHONY += all
767ifeq ($(KBUILD_EXTMOD),)
768__all: all
769else
770__all: modules
771endif
772
773targets :=
774
775# Decide whether to build built-in, modular, or both.
776# Normally, just do built-in.
777
778KBUILD_MODULES :=
779KBUILD_BUILTIN := y
780
781# If we have only "make modules", don't compile built-in objects.
782ifeq ($(MAKECMDGOALS),modules)
783  KBUILD_BUILTIN :=
784endif
785
786# If we have "make <whatever> modules", compile modules
787# in addition to whatever we do anyway.
788# Just "make" or "make all" shall build modules as well
789
790ifneq ($(filter all modules nsdeps compile_commands.json clang-%,$(MAKECMDGOALS)),)
791  KBUILD_MODULES := y
792endif
793
794ifeq ($(MAKECMDGOALS),)
795  KBUILD_MODULES := y
796endif
797
798export KBUILD_MODULES KBUILD_BUILTIN
799
800ifdef need-config
801include $(objtree)/include/config/auto.conf
802endif
803
804CC_FLAGS_DIALECT := -std=gnu11
805# Allow including a tagged struct or union anonymously in another struct/union.
806CC_FLAGS_DIALECT += $(CONFIG_CC_MS_EXTENSIONS)
807# Clang enables warnings about GNU and Microsoft extensions by default, disable
808# them because this is expected with the above options.
809ifdef CONFIG_CC_IS_CLANG
810CC_FLAGS_DIALECT += -Wno-gnu
811CC_FLAGS_DIALECT += -Wno-microsoft-anon-tag
812endif
813export CC_FLAGS_DIALECT
814KBUILD_CFLAGS += $(CC_FLAGS_DIALECT)
815
816ifeq ($(KBUILD_EXTMOD),)
817# Objects we will link into vmlinux / subdirs we need to visit
818core-y		:=
819drivers-y	:=
820libs-y		:= lib/
821endif # KBUILD_EXTMOD
822
823# The all: target is the default when no target is given on the
824# command line.
825# This allow a user to issue only 'make' to build a kernel including modules
826# Defaults to vmlinux, but the arch makefile usually adds further targets
827all: vmlinux
828
829# The arch Makefiles can override CC_FLAGS_FTRACE. We may also append it later.
830ifdef CONFIG_FUNCTION_TRACER
831  CC_FLAGS_FTRACE := -pg
832endif
833
834ifdef CONFIG_TRACEPOINTS
835# To check for unused tracepoints (tracepoints that are defined but never
836# called), run with:
837#
838# make UT=1
839#
840# Each unused tracepoints can take up to 5KB of memory in the running kernel.
841# It is best to remove any that are not used.
842#
843# This command line option will be removed when all current unused
844# tracepoints are removed.
845
846ifeq ("$(origin UT)", "command line")
847  WARN_ON_UNUSED_TRACEPOINTS := $(UT)
848endif
849endif # CONFIG_TRACEPOINTS
850
851export WARN_ON_UNUSED_TRACEPOINTS
852
853# Per-version Rust flags. These are like `rust_common_flags`, but may
854# depend on the Rust compiler version (e.g. using `rustc-min-version`).
855#
856# `-Aclippy::precedence`: the lint was extended in Rust 1.85.0 to
857# include bitmasking and shift operations. However, because it generated
858# many hits, in Rust 1.86.0 it was split into a new `precedence_bits`
859# lint which is not enabled by default.
860rust_common_flags_per_version := \
861    $(if $(call rustc-min-version,108600),,-Aclippy::precedence)
862
863rust_common_flags += $(rust_common_flags_per_version)
864KBUILD_HOSTRUSTFLAGS += $(rust_common_flags_per_version) $(HOSTRUSTFLAGS)
865KBUILD_RUSTFLAGS += $(rust_common_flags_per_version)
866
867include $(srctree)/arch/$(SRCARCH)/Makefile
868
869ifdef need-config
870ifdef may-sync-config
871# Read in dependencies to all Kconfig* files, make sure to run syncconfig if
872# changes are detected. This should be included after arch/$(SRCARCH)/Makefile
873# because some architectures define CROSS_COMPILE there.
874include include/config/auto.conf.cmd
875
876$(KCONFIG_CONFIG):
877	@echo >&2 '***'
878	@echo >&2 '*** Configuration file "$@" not found!'
879	@echo >&2 '***'
880	@echo >&2 '*** Please run some configurator (e.g. "make oldconfig" or'
881	@echo >&2 '*** "make menuconfig" or "make xconfig").'
882	@echo >&2 '***'
883	@/bin/false
884
885# The actual configuration files used during the build are stored in
886# include/generated/ and include/config/. Update them if .config is newer than
887# include/config/auto.conf (which mirrors .config).
888#
889# This exploits the 'multi-target pattern rule' trick.
890# The syncconfig should be executed only once to make all the targets.
891# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
892#
893# Do not use $(call cmd,...) here. That would suppress prompts from syncconfig,
894# so you cannot notice that Kconfig is waiting for the user input.
895%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg: $(KCONFIG_CONFIG)
896	$(Q)$(kecho) "  SYNC    $@"
897	$(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
898else # !may-sync-config
899# External modules and some install targets need include/generated/autoconf.h
900# and include/config/auto.conf but do not care if they are up-to-date.
901# Use auto.conf to show the error message
902
903checked-configs := $(addprefix $(objtree)/, include/generated/autoconf.h include/generated/rustc_cfg include/config/auto.conf)
904missing-configs := $(filter-out $(wildcard $(checked-configs)), $(checked-configs))
905
906ifdef missing-configs
907PHONY += $(objtree)/include/config/auto.conf
908
909$(objtree)/include/config/auto.conf:
910	@echo   >&2 '***'
911	@echo   >&2 '***  ERROR: Kernel configuration is invalid. The following files are missing:'
912	@printf >&2 '***    - %s\n' $(missing-configs)
913	@echo   >&2 '***  Run "make oldconfig && make prepare" on kernel source to fix it.'
914	@echo   >&2 '***'
915	@/bin/false
916endif
917
918endif # may-sync-config
919endif # need-config
920
921KBUILD_CFLAGS	+= -fno-delete-null-pointer-checks
922
923ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
924KBUILD_CFLAGS += -O2
925KBUILD_RUSTFLAGS += -Copt-level=2
926else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
927KBUILD_CFLAGS += -Os
928KBUILD_RUSTFLAGS += -Copt-level=s
929endif
930
931# Always set `debug-assertions` and `overflow-checks` because their default
932# depends on `opt-level` and `debug-assertions`, respectively.
933KBUILD_RUSTFLAGS += -Cdebug-assertions=$(if $(CONFIG_RUST_DEBUG_ASSERTIONS),y,n)
934KBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
935
936# Tell gcc to never replace conditional load with a non-conditional one
937ifdef CONFIG_CC_IS_GCC
938# gcc-10 renamed --param=allow-store-data-races=0 to
939# -fno-allow-store-data-races.
940KBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
941KBUILD_CFLAGS	+= $(call cc-option,-fno-allow-store-data-races)
942endif
943
944ifdef CONFIG_READABLE_ASM
945# Disable optimizations that make assembler listings hard to read.
946# reorder blocks reorders the control in the function
947# ipa clone creates specialized cloned functions
948# partial inlining inlines only parts of functions
949KBUILD_CFLAGS += -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining
950endif
951
952stackp-flags-y                                    := -fno-stack-protector
953stackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
954stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
955
956KBUILD_CFLAGS += $(stackp-flags-y)
957
958ifdef CONFIG_FRAME_POINTER
959KBUILD_CFLAGS	+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
960KBUILD_RUSTFLAGS += -Cforce-frame-pointers=y
961else
962# Some targets (ARM with Thumb2, for example), can't be built with frame
963# pointers.  For those, we don't have FUNCTION_TRACER automatically
964# select FRAME_POINTER.  However, FUNCTION_TRACER adds -pg, and this is
965# incompatible with -fomit-frame-pointer with current GCC, so we don't use
966# -fomit-frame-pointer with FUNCTION_TRACER.
967# In the Rust target specification, "frame-pointer" is set explicitly
968# to "may-omit".
969ifndef CONFIG_FUNCTION_TRACER
970KBUILD_CFLAGS	+= -fomit-frame-pointer
971endif
972endif
973
974# Initialize all stack variables with a 0xAA pattern.
975ifdef CONFIG_INIT_STACK_ALL_PATTERN
976KBUILD_CFLAGS	+= -ftrivial-auto-var-init=pattern
977endif
978
979# Initialize all stack variables with a zero value.
980ifdef CONFIG_INIT_STACK_ALL_ZERO
981KBUILD_CFLAGS	+= -ftrivial-auto-var-init=zero
982ifdef CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
983# https://github.com/llvm/llvm-project/issues/44842
984CC_AUTO_VAR_INIT_ZERO_ENABLER := -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
985export CC_AUTO_VAR_INIT_ZERO_ENABLER
986KBUILD_CFLAGS	+= $(CC_AUTO_VAR_INIT_ZERO_ENABLER)
987endif
988endif
989
990ifdef CONFIG_CC_IS_CLANG
991ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
992KBUILD_CFLAGS	+= -fexperimental-late-parse-attributes
993endif
994endif
995
996# Explicitly clear padding bits during variable initialization
997KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
998
999# While VLAs have been removed, GCC produces unreachable stack probes
1000# for the randomize_kstack_offset feature. Disable it for all compilers.
1001KBUILD_CFLAGS	+= $(call cc-option, -fno-stack-clash-protection)
1002
1003# Get details on warnings generated due to GCC value tracking.
1004KBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-context=2)
1005
1006# Show inlining notes for __attribute__((warning/error)) call chains.
1007# GCC supports this unconditionally while Clang 23+ provides a flag.
1008KBUILD_CFLAGS	+= $(call cc-option, -fdiagnostics-show-inlining-chain)
1009
1010# Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
1011ifdef CONFIG_ZERO_CALL_USED_REGS
1012KBUILD_CFLAGS	+= -fzero-call-used-regs=used-gpr
1013endif
1014
1015ifdef CONFIG_FUNCTION_TRACER
1016ifdef CONFIG_FTRACE_MCOUNT_USE_CC
1017  CC_FLAGS_FTRACE	+= -mrecord-mcount
1018  ifdef CONFIG_HAVE_NOP_MCOUNT
1019    ifeq ($(call cc-option-yn, -mnop-mcount),y)
1020      CC_FLAGS_FTRACE	+= -mnop-mcount
1021      CC_FLAGS_USING	+= -DCC_USING_NOP_MCOUNT
1022    endif
1023  endif
1024endif
1025ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
1026  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
1027    CC_FLAGS_USING	+= -DCC_USING_NOP_MCOUNT
1028  endif
1029endif
1030ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
1031  ifdef CONFIG_HAVE_C_RECORDMCOUNT
1032    BUILD_C_RECORDMCOUNT := y
1033    export BUILD_C_RECORDMCOUNT
1034  endif
1035endif
1036ifdef CONFIG_HAVE_FENTRY
1037  # s390-linux-gnu-gcc did not support -mfentry until gcc-9.
1038  ifeq ($(call cc-option-yn, -mfentry),y)
1039    CC_FLAGS_FTRACE	+= -mfentry
1040    CC_FLAGS_USING	+= -DCC_USING_FENTRY
1041  endif
1042endif
1043export CC_FLAGS_FTRACE
1044KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
1045KBUILD_AFLAGS	+= $(CC_FLAGS_USING)
1046endif
1047
1048# We trigger additional mismatches with less inlining
1049ifdef CONFIG_DEBUG_SECTION_MISMATCH
1050KBUILD_CFLAGS += -fno-inline-functions-called-once
1051endif
1052
1053# `rustc`'s `-Zfunction-sections` applies to data too (as of 1.59.0).
1054ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
1055KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
1056KBUILD_RUSTFLAGS_KERNEL += -Zfunction-sections=y
1057LDFLAGS_vmlinux += --gc-sections
1058endif
1059
1060ifdef CONFIG_SHADOW_CALL_STACK
1061ifndef CONFIG_DYNAMIC_SCS
1062CC_FLAGS_SCS	:= -fsanitize=shadow-call-stack
1063KBUILD_CFLAGS	+= $(CC_FLAGS_SCS)
1064KBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack
1065endif
1066export CC_FLAGS_SCS
1067endif
1068
1069ifdef CONFIG_LTO_CLANG
1070ifdef CONFIG_LTO_CLANG_THIN
1071CC_FLAGS_LTO	:= -flto=thin -fsplit-lto-unit
1072KBUILD_LDFLAGS += $(call ld-option,--lto-whole-program-visibility -mllvm -always-rename-promoted-locals=false)
1073else
1074CC_FLAGS_LTO	:= -flto
1075endif
1076CC_FLAGS_LTO	+= -fvisibility=hidden
1077
1078# Limit inlining across translation units to reduce binary size
1079KBUILD_LDFLAGS += -mllvm -import-instr-limit=5
1080endif
1081
1082ifdef CONFIG_LTO
1083KBUILD_CFLAGS	+= -fno-lto $(CC_FLAGS_LTO)
1084KBUILD_AFLAGS	+= -fno-lto
1085export CC_FLAGS_LTO
1086endif
1087
1088ifdef CONFIG_CFI
1089CC_FLAGS_CFI	:= -fsanitize=kcfi
1090ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS
1091	CC_FLAGS_CFI	+= -fsanitize-cfi-icall-experimental-normalize-integers
1092endif
1093ifdef CONFIG_FINEIBT_BHI
1094	CC_FLAGS_CFI	+= -fsanitize-kcfi-arity
1095endif
1096ifdef CONFIG_RUST
1097	# Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects
1098	# CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
1099	RUSTC_FLAGS_CFI   := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
1100	KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI)
1101	export RUSTC_FLAGS_CFI
1102endif
1103KBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
1104export CC_FLAGS_CFI
1105endif
1106
1107# Architectures can define flags to add/remove for floating-point support
1108CC_FLAGS_FPU	+= -D_LINUX_FPU_COMPILATION_UNIT
1109export CC_FLAGS_FPU
1110export CC_FLAGS_NO_FPU
1111
1112ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
1113# Set the minimal function alignment. Use the newer GCC option
1114# -fmin-function-alignment if it is available, or fall back to -falign-funtions.
1115# See also CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
1116ifdef CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT
1117KBUILD_CFLAGS += -fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT)
1118else
1119KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
1120endif
1121endif
1122
1123# arch Makefile may override CC so keep this after arch Makefile is included
1124NOSTDINC_FLAGS += -nostdinc
1125
1126# To gain proper coverage for CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE,
1127# the kernel uses only C99 flexible arrays for dynamically sized trailing
1128# arrays. Enforce this for everything that may examine structure sizes and
1129# perform bounds checking.
1130KBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
1131
1132# disable invalid "can't wrap" optimizations for signed / pointers
1133KBUILD_CFLAGS	+= -fno-strict-overflow
1134
1135# Make sure -fstack-check isn't enabled (like gentoo apparently did)
1136KBUILD_CFLAGS  += -fno-stack-check
1137
1138# conserve stack if available
1139ifdef CONFIG_CC_IS_GCC
1140KBUILD_CFLAGS   += -fconserve-stack
1141endif
1142
1143# Ensure compilers do not transform certain loops into calls to wcslen()
1144KBUILD_CFLAGS += -fno-builtin-wcslen
1145
1146CFLAGS_GCOV	:= -fprofile-arcs -ftest-coverage
1147ifdef CONFIG_CC_IS_GCC
1148CFLAGS_GCOV	+= -fno-tree-loop-im
1149# Use atomic counter updates to avoid concurrent-access crashes in GCOV.
1150# Only enable if -fprofile-update=prefer-atomic does not introduce new
1151# undefined symbols (e.g. libatomic calls that the kernel cannot link).
1152CFLAGS_GCOV	+= $(call try-run,\
1153	echo 'long long x; void f(void){x++;}' | \
1154	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -w -fprofile-arcs \
1155	-ftest-coverage -x c - -c -o "$$TMP.base" && \
1156	echo 'long long x; void f(void){x++;}' | \
1157	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -w -fprofile-arcs \
1158	-ftest-coverage -fprofile-update=prefer-atomic \
1159	-x c - -c -o "$$TMP" && \
1160	$(NM) "$$TMP.base" | grep ' U ' > "$$TMP.ubase" || true ; \
1161	$(NM) "$$TMP" | grep ' U ' > "$$TMP.utest" || true ; \
1162	cmp -s "$$TMP.ubase" "$$TMP.utest",\
1163	-fprofile-update=prefer-atomic)
1164endif
1165export CFLAGS_GCOV
1166
1167# change __FILE__ to the relative path to the source directory
1168ifdef building_out_of_srctree
1169KBUILD_CPPFLAGS += -fmacro-prefix-map=$(srcroot)/=
1170ifeq ($(call rustc-option-yn, --remap-path-scope=macro),y)
1171KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/= --remap-path-scope=macro
1172endif
1173endif
1174
1175# include additional Makefiles when needed
1176include-y			:= scripts/Makefile.warn
1177include-$(CONFIG_DEBUG_INFO)	+= scripts/Makefile.debug
1178include-$(CONFIG_DEBUG_INFO_BTF)+= scripts/Makefile.btf
1179include-$(CONFIG_KASAN)		+= scripts/Makefile.kasan
1180include-$(CONFIG_KCSAN)		+= scripts/Makefile.kcsan
1181include-$(CONFIG_KMSAN)		+= scripts/Makefile.kmsan
1182include-$(CONFIG_UBSAN)		+= scripts/Makefile.ubsan
1183include-$(CONFIG_KCOV)		+= scripts/Makefile.kcov
1184include-$(CONFIG_RANDSTRUCT)	+= scripts/Makefile.randstruct
1185include-$(CONFIG_KSTACK_ERASE)	+= scripts/Makefile.kstack_erase
1186include-$(CONFIG_AUTOFDO_CLANG)	+= scripts/Makefile.autofdo
1187include-$(CONFIG_PROPELLER_CLANG)	+= scripts/Makefile.propeller
1188include-$(CONFIG_WARN_CONTEXT_ANALYSIS) += scripts/Makefile.context-analysis
1189include-$(CONFIG_GCC_PLUGINS)	+= scripts/Makefile.gcc-plugins
1190
1191include $(addprefix $(srctree)/, $(include-y))
1192
1193# scripts/Makefile.gcc-plugins is intentionally included last.
1194# Do not add $(call cc-option,...) below this line. When you build the kernel
1195# from the clean source tree, the GCC plugins do not exist at this point.
1196
1197# Add user supplied CPPFLAGS, AFLAGS, CFLAGS and RUSTFLAGS as the last assignments
1198KBUILD_CPPFLAGS += $(KCPPFLAGS)
1199KBUILD_AFLAGS   += $(KAFLAGS)
1200KBUILD_CFLAGS   += $(KCFLAGS)
1201KBUILD_RUSTFLAGS += $(KRUSTFLAGS)
1202
1203KBUILD_LDFLAGS_MODULE += --build-id=sha1
1204LDFLAGS_vmlinux += --build-id=sha1
1205
1206KBUILD_LDFLAGS	+= -z noexecstack
1207ifeq ($(CONFIG_LD_IS_BFD),y)
1208KBUILD_LDFLAGS	+= $(call ld-option,--no-warn-rwx-segments)
1209endif
1210
1211ifeq ($(CONFIG_STRIP_ASM_SYMS),y)
1212LDFLAGS_vmlinux	+= -X
1213endif
1214
1215ifeq ($(CONFIG_RELR),y)
1216# ld.lld before 15 did not support -z pack-relative-relocs.
1217LDFLAGS_vmlinux	+= $(call ld-option,--pack-dyn-relocs=relr,-z pack-relative-relocs)
1218endif
1219
1220# We never want expected sections to be placed heuristically by the
1221# linker. All sections should be explicitly named in the linker script.
1222ifdef CONFIG_LD_ORPHAN_WARN
1223LDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
1224endif
1225
1226ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),)
1227LDFLAGS_vmlinux	+= --emit-relocs --discard-none
1228endif
1229
1230# Align the architecture of userspace programs with the kernel
1231USERFLAGS_FROM_KERNEL := --target=%
1232
1233ifdef CONFIG_ARCH_USERFLAGS
1234KBUILD_USERCFLAGS += $(CONFIG_ARCH_USERFLAGS)
1235KBUILD_USERLDFLAGS += $(CONFIG_ARCH_USERFLAGS)
1236else
1237# If not overridden also inherit the bit size
1238USERFLAGS_FROM_KERNEL += -m32 -m64
1239endif
1240
1241KBUILD_USERCFLAGS  += $(filter $(USERFLAGS_FROM_KERNEL), $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
1242KBUILD_USERLDFLAGS += $(filter $(USERFLAGS_FROM_KERNEL), $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
1243
1244# userspace programs are linked via the compiler, use the correct linker
1245ifdef CONFIG_CC_IS_CLANG
1246KBUILD_USERLDFLAGS += --ld-path=$(LD)
1247endif
1248
1249# make the checker run with the right architecture
1250CHECKFLAGS += --arch=$(ARCH)
1251
1252# insure the checker run with the right endianness
1253CHECKFLAGS += $(if $(CONFIG_CPU_BIG_ENDIAN),-mbig-endian,-mlittle-endian)
1254
1255# the checker needs the correct machine size
1256CHECKFLAGS += $(if $(CONFIG_64BIT),-m64,-m32)
1257
1258# Validate the checker is available and functional
1259ifneq ($(KBUILD_CHECKSRC), 0)
1260  ifneq ($(shell $(srctree)/scripts/checker-valid.sh $(CHECK) $(CHECKFLAGS)), 1)
1261    $(warning C=$(KBUILD_CHECKSRC) specified, but $(CHECK) is not available or not up to date)
1262    KBUILD_CHECKSRC = 0
1263  endif
1264endif
1265
1266# Default kernel image to build when no specific target is given.
1267# KBUILD_IMAGE may be overruled on the command line or
1268# set in the environment
1269# Also any assignments in arch/$(ARCH)/Makefile take precedence over
1270# this default value
1271export KBUILD_IMAGE ?= vmlinux
1272
1273#
1274# INSTALL_PATH specifies where to place the updated kernel and system map
1275# images. Default is /boot, but you can set it to other values
1276export	INSTALL_PATH ?= /boot
1277
1278#
1279# INSTALL_DTBS_PATH specifies a prefix for relocations required by build roots.
1280# Like INSTALL_MOD_PATH, it isn't defined in the Makefile, but can be passed as
1281# an argument if needed. Otherwise it defaults to the kernel install path
1282#
1283export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
1284
1285#
1286# INSTALL_MOD_PATH specifies a prefix to MODLIB for module directory
1287# relocations required by build roots.  This is not defined in the
1288# makefile but the argument can be passed to make if needed.
1289#
1290
1291MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
1292export MODLIB
1293
1294PHONY += prepare0
1295
1296ifeq ($(KBUILD_EXTMOD),)
1297
1298build-dir	:= .
1299clean-dirs	:= $(sort . Documentation \
1300		     $(patsubst %/,%,$(filter %/, $(core-) \
1301			$(drivers-) $(libs-))))
1302
1303export ARCH_CORE	:= $(core-y)
1304export ARCH_LIB		:= $(filter %/, $(libs-y))
1305export ARCH_DRIVERS	:= $(drivers-y) $(drivers-m)
1306# Externally visible symbols (used by link-vmlinux.sh)
1307
1308KBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)))
1309KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
1310
1311export KBUILD_VMLINUX_LIBS
1312export KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
1313
1314ifdef CONFIG_TRIM_UNUSED_KSYMS
1315# For the kernel to actually contain only the needed exported symbols,
1316# we have to build modules as well to determine what those symbols are.
1317KBUILD_MODULES := y
1318endif
1319
1320# '$(AR) mPi' needs 'T' to workaround the bug of llvm-ar <= 14
1321quiet_cmd_ar_vmlinux.a = AR      $@
1322      cmd_ar_vmlinux.a = \
1323	rm -f $@; \
1324	$(AR) cDPrST $@ $(KBUILD_VMLINUX_OBJS); \
1325	$(AR) mPiT $$($(AR) t $@ | sed -n 1p) $@ $$($(AR) t $@ | grep -F -f $(srctree)/scripts/head-object-list.txt)
1326
1327targets += vmlinux.a
1328vmlinux.a: $(KBUILD_VMLINUX_OBJS) scripts/head-object-list.txt FORCE
1329	$(call if_changed,ar_vmlinux.a)
1330
1331PHONY += vmlinux_o
1332vmlinux_o: vmlinux.a $(KBUILD_VMLINUX_LIBS)
1333	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_o
1334
1335vmlinux.o modules.builtin.modinfo modules.builtin: vmlinux_o
1336	@:
1337
1338PHONY += vmlinux
1339# LDFLAGS_vmlinux in the top Makefile defines linker flags for the top vmlinux,
1340# not for decompressors. LDFLAGS_vmlinux in arch/*/boot/compressed/Makefile is
1341# unrelated; the decompressors just happen to have the same base name,
1342# arch/*/boot/compressed/vmlinux.
1343# Export LDFLAGS_vmlinux only to scripts/Makefile.vmlinux.
1344#
1345# _LDFLAGS_vmlinux is a workaround for the 'private export' bug:
1346#   https://savannah.gnu.org/bugs/?61463
1347# For Make > 4.4, the following simple code will work:
1348#  vmlinux: private export LDFLAGS_vmlinux := $(LDFLAGS_vmlinux)
1349vmlinux: private _LDFLAGS_vmlinux := $(LDFLAGS_vmlinux)
1350vmlinux: export LDFLAGS_vmlinux = $(_LDFLAGS_vmlinux)
1351vmlinux: vmlinux.o $(KBUILD_LDS) modpost
1352	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux
1353
1354# The actual objects are generated when descending,
1355# make sure no implicit rule kicks in
1356$(sort $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)): . ;
1357
1358ifeq ($(origin KERNELRELEASE),file)
1359filechk_kernel.release = $(srctree)/scripts/setlocalversion $(srctree)
1360else
1361filechk_kernel.release = echo $(KERNELRELEASE)
1362endif
1363
1364# Store (new) KERNELRELEASE string in include/config/kernel.release
1365include/config/kernel.release: FORCE
1366	$(call filechk,kernel.release)
1367
1368# Additional helpers built in scripts/
1369# Carefully list dependencies so we do not try to build scripts twice
1370# in parallel
1371PHONY += scripts
1372scripts: scripts_basic scripts_dtc
1373	$(Q)$(MAKE) $(build)=$(@)
1374
1375# Things we need to do before we recursively start building the kernel
1376# or the modules are listed in "prepare".
1377# A multi level approach is used. prepareN is processed before prepareN-1.
1378# archprepare is used in arch Makefiles and when processed asm symlink,
1379# version.h and scripts_basic is processed / created.
1380
1381PHONY += prepare archprepare
1382
1383archprepare: outputmakefile archheaders archscripts scripts include/config/kernel.release \
1384	asm-generic $(version_h) include/generated/utsrelease.h \
1385	include/generated/compile.h include/generated/autoconf.h \
1386	include/generated/rustc_cfg remove-stale-files
1387
1388prepare0: archprepare
1389	$(Q)$(MAKE) $(build)=scripts/mod
1390	$(Q)$(MAKE) $(build)=. prepare
1391
1392# All the preparing..
1393prepare: prepare0
1394ifdef CONFIG_RUST
1395	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
1396	$(Q)$(MAKE) $(build)=rust
1397endif
1398
1399PHONY += remove-stale-files
1400remove-stale-files:
1401	$(Q)$(srctree)/scripts/remove-stale-files
1402
1403# Support for using generic headers in asm-generic
1404asm-generic := -f $(srctree)/scripts/Makefile.asm-headers obj
1405
1406PHONY += asm-generic uapi-asm-generic
1407asm-generic: uapi-asm-generic
1408	$(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/asm \
1409	generic=include/asm-generic
1410uapi-asm-generic:
1411	$(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/uapi/asm \
1412	generic=include/uapi/asm-generic
1413
1414# Generate some files
1415# ---------------------------------------------------------------------------
1416
1417# KERNELRELEASE can change from a few different places, meaning version.h
1418# needs to be updated, so this check is forced on all builds
1419
1420uts_len := 64
1421define filechk_utsrelease.h
1422	if [ `echo -n "$(KERNELRELEASE)" | wc -c ` -gt $(uts_len) ]; then \
1423	  echo '"$(KERNELRELEASE)" exceeds $(uts_len) characters' >&2;    \
1424	  exit 1;                                                         \
1425	fi;                                                               \
1426	echo \#define UTS_RELEASE \"$(KERNELRELEASE)\"
1427endef
1428
1429define filechk_version.h
1430	if [ $(SUBLEVEL) -gt 255 ]; then                                 \
1431		echo \#define LINUX_VERSION_CODE $(shell                 \
1432		expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + 255); \
1433	else                                                             \
1434		echo \#define LINUX_VERSION_CODE $(shell                 \
1435		expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + $(SUBLEVEL)); \
1436	fi;                                                              \
1437	echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) +  \
1438	((c) > 255 ? 255 : (c)))';                                       \
1439	echo \#define LINUX_VERSION_MAJOR $(VERSION);                    \
1440	echo \#define LINUX_VERSION_PATCHLEVEL $(PATCHLEVEL);            \
1441	echo \#define LINUX_VERSION_SUBLEVEL $(SUBLEVEL)
1442endef
1443
1444$(version_h): private PATCHLEVEL := $(or $(PATCHLEVEL), 0)
1445$(version_h): private SUBLEVEL := $(or $(SUBLEVEL), 0)
1446$(version_h): FORCE
1447	$(call filechk,version.h)
1448
1449include/generated/utsrelease.h: include/config/kernel.release FORCE
1450	$(call filechk,utsrelease.h)
1451
1452filechk_compile.h = $(srctree)/scripts/mkcompile_h \
1453	"$(UTS_MACHINE)" "$(CONFIG_CC_VERSION_TEXT)" "$(LD)"
1454
1455include/generated/compile.h: FORCE
1456	$(call filechk,compile.h)
1457
1458PHONY += headerdep
1459headerdep:
1460	$(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1 \
1461	$(srctree)/scripts/headerdep.pl -I$(srctree)/include
1462
1463# ---------------------------------------------------------------------------
1464# Kernel headers
1465
1466#Default location for installed headers
1467export INSTALL_HDR_PATH = $(objtree)/usr
1468
1469quiet_cmd_headers_install = INSTALL $(INSTALL_HDR_PATH)/include
1470      cmd_headers_install = \
1471	mkdir -p $(INSTALL_HDR_PATH); \
1472	rsync -mrl --include='*/' --include='*\.h' --exclude='*' \
1473	usr/include $(INSTALL_HDR_PATH)
1474
1475PHONY += headers_install
1476headers_install: headers
1477	$(call cmd,headers_install)
1478
1479PHONY += archheaders archscripts
1480
1481hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
1482
1483PHONY += headers
1484headers: $(version_h) scripts_unifdef uapi-asm-generic archheaders
1485ifdef HEADER_ARCH
1486	$(Q)$(MAKE) -f $(srctree)/Makefile HEADER_ARCH= SRCARCH=$(HEADER_ARCH) headers
1487else
1488	$(Q)$(MAKE) $(hdr-inst)=include/uapi
1489	$(Q)$(MAKE) $(hdr-inst)=arch/$(SRCARCH)/include/uapi
1490endif
1491
1492ifdef CONFIG_HEADERS_INSTALL
1493prepare: headers
1494endif
1495
1496PHONY += usr_gen_init_cpio
1497usr_gen_init_cpio: scripts_basic
1498	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio
1499
1500PHONY += scripts_unifdef
1501scripts_unifdef: scripts_basic
1502	$(Q)$(MAKE) $(build)=scripts scripts/unifdef
1503
1504PHONY += scripts_gen_packed_field_checks
1505scripts_gen_packed_field_checks: scripts_basic
1506	$(Q)$(MAKE) $(build)=scripts scripts/gen_packed_field_checks
1507
1508# ---------------------------------------------------------------------------
1509# Install
1510
1511# Many distributions have the custom install script, /sbin/installkernel.
1512# If DKMS is installed, 'make install' will eventually recurse back
1513# to this Makefile to build and install external modules.
1514# Cancel sub_make_done so that options such as M=, V=, etc. are parsed.
1515
1516quiet_cmd_install = INSTALL $(INSTALL_PATH)
1517      cmd_install = unset sub_make_done; $(srctree)/scripts/install.sh
1518
1519# ---------------------------------------------------------------------------
1520# vDSO install
1521
1522PHONY += vdso_install
1523vdso_install: export INSTALL_FILES = $(vdso-install-y)
1524vdso_install:
1525	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vdsoinst
1526
1527# ---------------------------------------------------------------------------
1528# Tools
1529
1530ifdef CONFIG_OBJTOOL
1531prepare: tools/objtool
1532endif
1533
1534ifdef CONFIG_BPF
1535ifdef CONFIG_DEBUG_INFO_BTF
1536prepare: tools/bpf/resolve_btfids
1537endif
1538endif
1539
1540# The tools build system is not a part of Kbuild and tends to introduce
1541# its own unique issues. If you need to integrate a new tool into Kbuild,
1542# please consider locating that tool outside the tools/ tree and using the
1543# standard Kbuild "hostprogs" syntax instead of adding a new tools/* entry
1544# here. See Documentation/kbuild/makefiles.rst for details.
1545
1546PHONY += resolve_btfids_clean
1547
1548resolve_btfids_O = $(abspath $(objtree))/tools/bpf/resolve_btfids
1549
1550# tools/bpf/resolve_btfids directory might not exist
1551# in output directory, skip its clean in that case
1552resolve_btfids_clean:
1553ifneq ($(wildcard $(resolve_btfids_O)),)
1554	$(Q)$(MAKE) -sC $(srctree)/tools/bpf/resolve_btfids O=$(resolve_btfids_O) clean
1555endif
1556
1557PHONY += objtool_clean objtool_mrproper
1558
1559objtool_O = $(abspath $(objtree))/tools/objtool
1560
1561objtool_clean objtool_mrproper:
1562ifneq ($(wildcard $(objtool_O)),)
1563	$(Q)$(MAKE) -sC $(abs_srctree)/tools/objtool O=$(objtool_O) srctree=$(abs_srctree) $(patsubst objtool_%,%,$@)
1564endif
1565
1566tools/: FORCE
1567	$(Q)mkdir -p $(objtree)/tools
1568	$(Q)$(MAKE) O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/
1569
1570tools/%: FORCE
1571	$(Q)mkdir -p $(objtree)/tools
1572	$(Q)$(MAKE) O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/ $*
1573
1574# ---------------------------------------------------------------------------
1575# Kernel selftest
1576
1577PHONY += kselftest
1578kselftest: headers
1579	$(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests
1580
1581kselftest-%: headers FORCE
1582	$(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*
1583
1584PHONY += kselftest-merge
1585kselftest-merge:
1586	$(if $(wildcard $(objtree)/.config),, $(error No .config exists, config your kernel first!))
1587	$(Q)find $(srctree)/tools/testing/selftests -name config -o -name config.$(UTS_MACHINE) | \
1588		xargs $(srctree)/scripts/kconfig/merge_config.sh -y -m $(objtree)/.config
1589	$(Q)$(MAKE) -f $(srctree)/Makefile olddefconfig
1590
1591# ---------------------------------------------------------------------------
1592# Devicetree files
1593
1594ifneq ($(wildcard $(srctree)/arch/$(SRCARCH)/boot/dts/),)
1595dtstree := arch/$(SRCARCH)/boot/dts
1596endif
1597
1598dtbindingtree := Documentation/devicetree/bindings
1599
1600%.yaml: dtbs_prepare
1601	$(Q)$(MAKE) $(build)=$(dtbindingtree) \
1602		    $(dtbindingtree)/$(patsubst %.yaml,%.example.dtb,$@) dt_binding_check_one
1603
1604ifneq ($(dtstree),)
1605
1606%.dtb: dtbs_prepare
1607	$(Q)$(MAKE) $(build)=$(dtstree) $(dtstree)/$@
1608
1609%.dtbo: dtbs_prepare
1610	$(Q)$(MAKE) $(build)=$(dtstree) $(dtstree)/$@
1611
1612PHONY += dtbs dtbs_prepare dtbs_install dtbs_check
1613dtbs: dtbs_prepare
1614	$(Q)$(MAKE) $(build)=$(dtstree) need-dtbslist=1
1615
1616# include/config/kernel.release is actually needed when installing DTBs because
1617# INSTALL_DTBS_PATH contains $(KERNELRELEASE). However, we do not want to make
1618# dtbs_install depend on it as dtbs_install may run as root.
1619dtbs_prepare: include/config/kernel.release scripts_dtc
1620
1621ifneq ($(filter dtbs_check %.yaml, $(MAKECMDGOALS)),)
1622export CHECK_DTBS=y
1623endif
1624
1625ifneq ($(CHECK_DTBS),)
1626dtbs_prepare: dt_binding_schemas
1627endif
1628
1629dtbs_check: dtbs
1630
1631dtbs_install:
1632	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.dtbinst obj=$(dtstree)
1633
1634ifdef CONFIG_OF_EARLY_FLATTREE
1635all: dtbs
1636endif
1637
1638ifdef CONFIG_GENERIC_BUILTIN_DTB
1639vmlinux: dtbs
1640endif
1641
1642endif
1643
1644PHONY += scripts_dtc
1645scripts_dtc: scripts_basic
1646	$(Q)$(MAKE) $(build)=scripts/dtc
1647
1648ifneq ($(filter dt_binding_check, $(MAKECMDGOALS)),)
1649export CHECK_DTBS=y
1650endif
1651
1652PHONY += dt_binding_check dt_binding_schemas
1653dt_binding_check: dt_binding_schemas scripts_dtc
1654	$(Q)$(MAKE) $(build)=$(dtbindingtree) $@
1655
1656dt_binding_schemas:
1657	$(Q)$(MAKE) $(build)=$(dtbindingtree)
1658
1659PHONY += dt_compatible_check
1660dt_compatible_check: dt_binding_schemas
1661	$(Q)$(MAKE) $(build)=$(dtbindingtree) $@
1662
1663# ---------------------------------------------------------------------------
1664# Modules
1665
1666ifdef CONFIG_MODULES
1667
1668# By default, build modules as well
1669
1670all: modules
1671
1672# When we're building modules with modversions, we need to consider
1673# the built-in objects during the descend as well, in order to
1674# make sure the checksums are up to date before we record them.
1675ifdef CONFIG_MODVERSIONS
1676  KBUILD_BUILTIN := y
1677endif
1678
1679# Build modules
1680#
1681
1682# *.ko are usually independent of vmlinux, but CONFIG_DEBUG_INFO_BTF_MODULES
1683# is an exception.
1684ifdef CONFIG_DEBUG_INFO_BTF_MODULES
1685KBUILD_BUILTIN := y
1686modules: vmlinux
1687endif
1688
1689modules: modules_prepare
1690
1691# Target to prepare building external modules
1692modules_prepare: prepare
1693	$(Q)$(MAKE) $(build)=scripts scripts/module.lds
1694
1695endif # CONFIG_MODULES
1696
1697###
1698# Cleaning is done on three levels.
1699# make clean     Delete most generated files
1700#                Leave enough to build external modules
1701# make mrproper  Delete the current configuration, and all generated files
1702# make distclean Remove editor backup files, patch leftover files and the like
1703
1704# Directories & files removed with 'make clean'
1705CLEAN_FILES += vmlinux.symvers modules-only.symvers \
1706	       modules.builtin modules.builtin.modinfo modules.nsdeps \
1707	       modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \
1708	       compile_commands.json rust/test \
1709	       rust-project.json .vmlinux.objs .vmlinux.export.c \
1710               .builtin-dtbs-list .builtin-dtbs.S
1711
1712# Directories & files removed with 'make mrproper'
1713MRPROPER_FILES += include/config include/generated          \
1714		  arch/$(SRCARCH)/include/generated .objdiff \
1715		  debian snap tar-install PKGBUILD pacman \
1716		  .config .config.old .version \
1717		  Module.symvers \
1718		  certs/signing_key.pem \
1719		  certs/x509.genkey \
1720		  vmlinux-gdb.py \
1721		  rpmbuild \
1722		  rust/libmacros.so rust/libmacros.dylib \
1723		  rust/libpin_init_internal.so rust/libpin_init_internal.dylib
1724
1725# clean - Delete most, but leave enough to build external modules
1726#
1727clean: private rm-files := $(CLEAN_FILES)
1728
1729PHONY += archclean vmlinuxclean
1730
1731vmlinuxclean:
1732	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-vmlinux.sh clean
1733	$(Q)$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) clean)
1734
1735clean: archclean vmlinuxclean resolve_btfids_clean objtool_clean
1736
1737# mrproper - Delete all generated files, including .config
1738#
1739mrproper: private rm-files := $(MRPROPER_FILES)
1740mrproper-dirs      := $(addprefix _mrproper_,scripts)
1741
1742PHONY += $(mrproper-dirs) mrproper
1743$(mrproper-dirs):
1744	$(Q)$(MAKE) $(clean)=$(patsubst _mrproper_%,%,$@)
1745
1746mrproper: clean objtool_mrproper $(mrproper-dirs)
1747	$(call cmd,rmfiles)
1748	@find . $(RCS_FIND_IGNORE) \
1749		\( -name '*.rmeta' \) \
1750		-type f -print | xargs rm -f
1751
1752# distclean
1753#
1754PHONY += distclean
1755
1756distclean: mrproper
1757	@find . $(RCS_FIND_IGNORE) \
1758		\( -name '*.orig' -o -name '*.rej' -o -name '*~' \
1759		-o -name '*.bak' -o -name '#*#' -o -name '*%' \
1760		-o -name 'core' -o -name tags -o -name TAGS -o -name 'cscope*' \
1761		-o -name GPATH -o -name GRTAGS -o -name GSYMS -o -name GTAGS \) \
1762		-type f -print | xargs rm -f
1763
1764
1765# Packaging of the kernel to various formats
1766# ---------------------------------------------------------------------------
1767
1768modules-cpio-pkg: usr_gen_init_cpio
1769
1770%src-pkg: FORCE
1771	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
1772%pkg: include/config/kernel.release FORCE
1773	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
1774
1775# Brief documentation of the typical targets used
1776# ---------------------------------------------------------------------------
1777
1778boards := $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*_defconfig)
1779boards := $(sort $(notdir $(boards)))
1780board-dirs := $(dir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*/*_defconfig))
1781board-dirs := $(sort $(notdir $(board-dirs:/=)))
1782
1783PHONY += help
1784help:
1785	@echo  'Cleaning targets:'
1786	@echo  '  clean		  - Remove most generated files but keep the config and'
1787	@echo  '                    enough build support to build external modules'
1788	@echo  '  mrproper	  - Remove all generated files + config + various backup files'
1789	@echo  '  distclean	  - mrproper + remove editor backup and patch files'
1790	@echo  ''
1791	@$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
1792	@echo  ''
1793	@echo  'Other generic targets:'
1794	@echo  '  all		  - Build all targets marked with [*]'
1795	@echo  '* vmlinux	  - Build the bare kernel'
1796	@echo  '* modules	  - Build all modules'
1797	@echo  '  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)'
1798	@echo  '  vdso_install    - Install unstripped vdso to INSTALL_MOD_PATH (default: /)'
1799	@echo  '  dir/            - Build all files in dir and below'
1800	@echo  '  dir/file.[ois]  - Build specified target only'
1801	@echo  '  dir/file.ll     - Build the LLVM assembly file'
1802	@echo  '                    (requires compiler support for LLVM assembly generation)'
1803	@echo  '  dir/file.lst    - Build specified mixed source/assembly target only'
1804	@echo  '                    (requires a recent binutils and recent build (System.map))'
1805	@echo  '  dir/file.ko     - Build module including final link'
1806	@echo  '  modules_prepare - Set up for building external modules'
1807	@echo  '  tags/TAGS	  - Generate tags file for editors'
1808	@echo  '  cscope	  - Generate cscope index'
1809	@echo  '  gtags           - Generate GNU GLOBAL index'
1810	@echo  '  kernelrelease	  - Output the release version string (use with make -s)'
1811	@echo  '  kernelversion	  - Output the version stored in Makefile (use with make -s)'
1812	@echo  '  image_name	  - Output the image name (use with make -s)'
1813	@echo  '  headers	  - Build ready-to-install UAPI headers in usr/include'
1814	@echo  '  headers_install - Install sanitised kernel UAPI headers to INSTALL_HDR_PATH'; \
1815	 echo  '                    (default: $(INSTALL_HDR_PATH))'; \
1816	 echo  ''
1817	@echo  'Static analysers:'
1818	@echo  '  checkstack      - Generate a list of stack hogs and consider all functions'
1819	@echo  '                    with a stack size larger than MINSTACKSIZE (default: 100)'
1820	@echo  '  versioncheck    - Sanity check on version.h usage'
1821	@echo  '  includecheck    - Check for duplicate included header files'
1822	@echo  '  headerdep       - Detect inclusion cycles in headers'
1823	@echo  '  coccicheck      - Check with Coccinelle'
1824	@echo  '  clang-analyzer  - Check with clang static analyzer'
1825	@echo  '  clang-tidy      - Check with clang-tidy'
1826	@echo  ''
1827	@echo  'Tools:'
1828	@echo  '  nsdeps          - Generate missing symbol namespace dependencies'
1829	@echo  ''
1830	@echo  'Kernel selftest:'
1831	@echo  '  kselftest         - Build and run kernel selftest'
1832	@echo  '                      Build, install, and boot kernel before'
1833	@echo  '                      running kselftest on it'
1834	@echo  '                      Run as root for full coverage'
1835	@echo  '  kselftest-all     - Build kernel selftest'
1836	@echo  '  kselftest-install - Build and install kernel selftest'
1837	@echo  '  kselftest-clean   - Remove all generated kselftest files'
1838	@echo  '  kselftest-merge   - Merge all the config dependencies of'
1839	@echo  '		      kselftest to existing .config.'
1840	@echo  ''
1841	@echo  'Rust targets:'
1842	@echo  '  rustavailable   - Checks whether the Rust toolchain is'
1843	@echo  '		    available and, if not, explains why.'
1844	@echo  '  rustfmt	  - Reformat all the Rust code in the kernel'
1845	@echo  '  rustfmtcheck	  - Checks if all the Rust code in the kernel'
1846	@echo  '		    is formatted, printing a diff otherwise.'
1847	@echo  '  rustdoc	  - Generate Rust documentation'
1848	@echo  '		    (requires kernel .config)'
1849	@echo  '  rusttest        - Runs the Rust tests'
1850	@echo  '                    (requires kernel .config; downloads external repos)'
1851	@echo  '  rust-analyzer	  - Generate rust-project.json rust-analyzer support file'
1852	@echo  '		    (requires kernel .config)'
1853	@echo  '  dir/file.[os]   - Build specified target only'
1854	@echo  '  dir/file.rsi    - Build macro expanded source, similar to C preprocessing.'
1855	@echo  '                    Run with RUSTFMT=n to skip reformatting if needed.'
1856	@echo  '                    The output is not intended to be compilable.'
1857	@echo  '  dir/file.ll     - Build the LLVM assembly file'
1858	@echo  ''
1859	@$(if $(dtstree), \
1860		echo 'Devicetree:'; \
1861		echo '* dtbs               - Build device tree blobs for enabled boards'; \
1862		echo '  dtbs_install       - Install dtbs to $(INSTALL_DTBS_PATH)'; \
1863		echo '  dt_binding_check   - Validate device tree binding documents and examples'; \
1864		echo '  dt_binding_schemas - Build processed device tree binding schemas'; \
1865		echo '  dtbs_check         - Validate device tree source files';\
1866		echo '')
1867
1868	@echo 'Userspace tools targets:'
1869	@echo '  use "make tools/help"'
1870	@echo '  or  "cd tools; make help"'
1871	@echo  ''
1872	@echo  'Kernel packaging:'
1873	@$(MAKE) -f $(srctree)/scripts/Makefile.package help
1874	@echo  ''
1875	@echo  'Documentation targets:'
1876	@$(MAKE) -f $(srctree)/Documentation/Makefile dochelp
1877	@echo  ''
1878	@echo  'Architecture-specific targets ($(SRCARCH)):'
1879	@$(or $(archhelp),\
1880		echo '  No architecture-specific help defined for $(SRCARCH)')
1881	@echo  ''
1882	@$(if $(boards), \
1883		$(foreach b, $(boards), \
1884		printf "  %-27s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));) \
1885		echo '')
1886	@$(if $(board-dirs), \
1887		$(foreach b, $(board-dirs), \
1888		printf "  %-16s - Show %s-specific targets\\n" help-$(b) $(b);) \
1889		printf "  %-16s - Show all of the above\\n" help-boards; \
1890		echo '')
1891
1892	@echo  '  make V=n   [targets] 1: verbose build'
1893	@echo  '                       2: give reason for rebuild of target'
1894	@echo  '                       V=1 and V=2 can be combined with V=12'
1895	@echo  '  make O=dir [targets] Locate all output files in "dir", including .config'
1896	@echo  '  make C=1   [targets] Check re-compiled c source with $$CHECK'
1897	@echo  '                       (sparse by default)'
1898	@echo  '  make C=2   [targets] Force check of all c source with $$CHECK'
1899	@echo  '  make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections'
1900	@echo  '  make W=n   [targets] Enable extra build checks, n=1,2,3,c,e where'
1901	@echo  '		1: warnings which may be relevant and do not occur too often'
1902	@echo  '		2: warnings which occur quite often but may still be relevant'
1903	@echo  '		3: more obscure warnings, can most likely be ignored'
1904	@echo  '		c: extra checks in the configuration stage (Kconfig)'
1905	@echo  '		e: warnings are being treated as errors'
1906	@echo  '		Multiple levels can be combined with W=12 or W=123'
1907	@echo  '  make UT=1   [targets] Warn if a tracepoint is defined but not used.'
1908	@echo  '          [ This will be removed when all current unused tracepoints are eliminated. ]'
1909	@$(if $(dtstree), \
1910		echo '  make CHECK_DTBS=1 [targets] Check all generated dtb files against schema'; \
1911		echo '         This can be applied both to "dtbs" and to individual "foo.dtb" targets' ; \
1912		)
1913	@echo  ''
1914	@echo  'Execute "make" or "make all" to build all targets marked with [*] '
1915	@echo  'For further info see the ./README file'
1916
1917
1918help-board-dirs := $(addprefix help-,$(board-dirs))
1919
1920help-boards: $(help-board-dirs)
1921
1922boards-per-dir = $(sort $(notdir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/$*/*_defconfig)))
1923
1924$(help-board-dirs): help-%:
1925	@echo  'Architecture-specific targets ($(SRCARCH) $*):'
1926	@$(if $(boards-per-dir), \
1927		$(foreach b, $(boards-per-dir), \
1928		printf "  %-24s - Build for %s\\n" $*/$(b) $(subst _defconfig,,$(b));) \
1929		echo '')
1930
1931
1932# Documentation targets
1933# ---------------------------------------------------------------------------
1934DOC_TARGETS := xmldocs latexdocs pdfdocs htmldocs epubdocs cleandocs \
1935	       linkcheckdocs dochelp refcheckdocs texinfodocs infodocs mandocs \
1936	       htmldocs-redirects
1937
1938PHONY += $(DOC_TARGETS)
1939$(DOC_TARGETS):
1940	$(Q)$(MAKE) $(build)=Documentation $@
1941
1942
1943# Rust targets
1944# ---------------------------------------------------------------------------
1945
1946# "Is Rust available?" target
1947PHONY += rustavailable
1948rustavailable:
1949	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
1950
1951# Documentation target
1952#
1953# Using the singular to avoid running afoul of `no-dot-config-targets`.
1954PHONY += rustdoc
1955rustdoc: prepare
1956	$(Q)$(MAKE) $(build)=rust $@
1957
1958# Testing target
1959PHONY += rusttest
1960rusttest: prepare
1961	$(Q)$(MAKE) $(build)=rust $@
1962
1963# Formatting targets
1964#
1965# Generated files as well as vendored crates are skipped.
1966PHONY += rustfmt rustfmtcheck
1967
1968rustfmt:
1969	$(Q)find $(srctree) $(RCS_FIND_IGNORE) \
1970		\( \
1971			-path $(srctree)/rust/proc-macro2 \
1972			-o -path $(srctree)/rust/quote \
1973			-o -path $(srctree)/rust/syn \
1974		\) -prune -o \
1975		-type f -a -name '*.rs' -a ! -name '*generated*' -print \
1976		| xargs $(RUSTFMT) $(rustfmt_flags)
1977
1978rustfmtcheck: rustfmt_flags = --check
1979rustfmtcheck: rustfmt
1980
1981# Misc
1982# ---------------------------------------------------------------------------
1983
1984PHONY += misc-check
1985misc-check:
1986	$(Q)$(srctree)/scripts/misc-check
1987
1988all: misc-check
1989
1990PHONY += scripts_gdb
1991scripts_gdb: prepare0
1992	$(Q)$(MAKE) $(build)=scripts/gdb
1993	$(Q)ln -fsn $(abspath $(srctree)/scripts/gdb/vmlinux-gdb.py)
1994
1995ifdef CONFIG_GDB_SCRIPTS
1996all: scripts_gdb
1997endif
1998
1999else # KBUILD_EXTMOD
2000
2001filechk_kernel.release = echo $(KERNELRELEASE)
2002
2003###
2004# External module support.
2005# When building external modules the kernel used as basis is considered
2006# read-only, and no consistency checks are made and the make
2007# system is not used on the basis kernel. If updates are required
2008# in the basis kernel ordinary make commands (without M=...) must be used.
2009
2010# We are always building only modules.
2011KBUILD_BUILTIN :=
2012KBUILD_MODULES := y
2013
2014build-dir := .
2015
2016clean-dirs := .
2017clean: private rm-files := Module.symvers modules.nsdeps compile_commands.json
2018
2019PHONY += prepare
2020# now expand this into a simple variable to reduce the cost of shell evaluations
2021prepare: CC_VERSION_TEXT := $(CC_VERSION_TEXT)
2022prepare: PAHOLE_VERSION := $(PAHOLE_VERSION)
2023prepare:
2024	@if [ "$(CC_VERSION_TEXT)" != "$(CONFIG_CC_VERSION_TEXT)" ]; then \
2025		echo >&2 "warning: the compiler differs from the one used to build the kernel"; \
2026		echo >&2 "  The kernel was built by: $(CONFIG_CC_VERSION_TEXT)"; \
2027		echo >&2 "  You are using:           $(CC_VERSION_TEXT)"; \
2028	fi
2029	@if [ "$(PAHOLE_VERSION)" != "$(CONFIG_PAHOLE_VERSION)" ]; then \
2030		echo >&2 "warning: pahole version differs from the one used to build the kernel"; \
2031		echo >&2 "  The kernel was built with: $(CONFIG_PAHOLE_VERSION)"; \
2032		echo >&2 "  You are using:             $(PAHOLE_VERSION)"; \
2033	fi
2034
2035PHONY += help
2036help:
2037	@echo  '  Building external modules.'
2038	@echo  '  Syntax: make -C path/to/kernel/src M=$$PWD target'
2039	@echo  ''
2040	@echo  '  modules         - default target, build the module(s)'
2041	@echo  '  modules_install - install the module'
2042	@echo  '  clean           - remove generated files in module directory only'
2043	@echo  '  rust-analyzer	  - generate rust-project.json rust-analyzer support file'
2044	@echo  ''
2045
2046ifndef CONFIG_MODULES
2047modules modules_install: __external_modules_error
2048__external_modules_error:
2049	@echo >&2 '***'
2050	@echo >&2 '*** The present kernel disabled CONFIG_MODULES.'
2051	@echo >&2 '*** You cannot build or install external modules.'
2052	@echo >&2 '***'
2053	@false
2054endif
2055
2056endif # KBUILD_EXTMOD
2057
2058# ---------------------------------------------------------------------------
2059# Modules
2060
2061PHONY += modules modules_install modules_sign modules_prepare
2062
2063modules_install:
2064	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst \
2065	sign-only=$(if $(filter modules_install,$(MAKECMDGOALS)),,y)
2066
2067ifeq ($(CONFIG_MODULE_SIG),y)
2068# modules_sign is a subset of modules_install.
2069# 'make modules_install modules_sign' is equivalent to 'make modules_install'.
2070modules_sign: modules_install
2071	@:
2072else
2073modules_sign:
2074	@echo >&2 '***'
2075	@echo >&2 '*** CONFIG_MODULE_SIG is disabled. You cannot sign modules.'
2076	@echo >&2 '***'
2077	@false
2078endif
2079
2080ifdef CONFIG_MODULES
2081
2082modules.order: $(build-dir)
2083	@:
2084
2085# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
2086# This is solely useful to speed up test compiles.
2087modules: modpost
2088ifneq ($(KBUILD_MODPOST_NOFINAL),1)
2089	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
2090endif
2091
2092PHONY += modules_check
2093modules_check: modules.order
2094	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/modules-check.sh $<
2095
2096else # CONFIG_MODULES
2097
2098modules:
2099	@:
2100
2101KBUILD_MODULES :=
2102
2103endif # CONFIG_MODULES
2104
2105PHONY += modpost
2106modpost: $(if $(single-build),, $(if $(KBUILD_BUILTIN), vmlinux.o)) \
2107	 $(if $(KBUILD_MODULES), modules_check)
2108	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
2109
2110# Single targets
2111# ---------------------------------------------------------------------------
2112# To build individual files in subdirectories, you can do like this:
2113#
2114#   make foo/bar/baz.s
2115#
2116# The supported suffixes for single-target are listed in 'single-targets'
2117#
2118# To build only under specific subdirectories, you can do like this:
2119#
2120#   make foo/bar/baz/
2121
2122ifdef single-build
2123
2124# .ko is special because modpost is needed
2125single-ko := $(sort $(filter %.ko, $(MAKECMDGOALS)))
2126single-no-ko := $(filter-out $(single-ko), $(MAKECMDGOALS)) \
2127		$(foreach x, o mod, $(patsubst %.ko, %.$x, $(single-ko)))
2128
2129$(single-ko): single_modules
2130	@:
2131$(single-no-ko): $(build-dir)
2132	@:
2133
2134# Remove modules.order when done because it is not the real one.
2135PHONY += single_modules
2136single_modules: $(single-no-ko) modules_prepare
2137	$(Q){ $(foreach m, $(single-ko), echo $(m:%.ko=%.o);) } > modules.order
2138	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
2139ifneq ($(KBUILD_MODPOST_NOFINAL),1)
2140	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
2141endif
2142	$(Q)rm -f modules.order
2143
2144single-goals := $(addprefix $(build-dir)/, $(single-no-ko))
2145
2146KBUILD_MODULES := y
2147
2148endif
2149
2150prepare: outputmakefile
2151
2152# Preset locale variables to speed up the build process. Limit locale
2153# tweaks to this spot to avoid wrong language settings when running
2154# make menuconfig etc.
2155# Error messages still appears in the original language
2156PHONY += $(build-dir)
2157$(build-dir): prepare
2158	$(Q)$(MAKE) $(build)=$@ need-builtin=1 need-modorder=1 $(single-goals)
2159
2160clean-dirs := $(addprefix _clean_, $(clean-dirs))
2161PHONY += $(clean-dirs) clean
2162$(clean-dirs):
2163	$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
2164
2165clean: $(clean-dirs)
2166	$(call cmd,rmfiles)
2167	@find . $(RCS_FIND_IGNORE) \
2168		\( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \
2169		-o -name '*.ko.*' \
2170		-o -name '*.dtb' -o -name '*.dtbo' \
2171		-o -name '*.dtb.S' -o -name '*.dtbo.S' \
2172		-o -name '*.dt.yaml' -o -name 'dtbs-list' \
2173		-o -name '*.dwo' -o -name '*.lst' \
2174		-o -name '*.su' -o -name '*.mod' \
2175		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
2176		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
2177		-o -name '*.asn1.[ch]' \
2178		-o -name '*.symtypes' -o -name 'modules.order' \
2179		-o -name '*.c.[012]*.*' \
2180		-o -name '*.ll' \
2181		-o -name '*.gcno' \
2182		\) -type f -print \
2183		-o -name '.tmp_*' -print \
2184		| xargs rm -rf
2185
2186# Generate tags for editors
2187# ---------------------------------------------------------------------------
2188quiet_cmd_tags = GEN     $@
2189      cmd_tags = $(BASH) $(srctree)/scripts/tags.sh $@
2190
2191tags TAGS cscope gtags: FORCE
2192	$(call cmd,tags)
2193
2194# Generate rust-project.json (a file that describes the structure of non-Cargo
2195# Rust projects) for rust-analyzer (an implementation of the Language Server
2196# Protocol).
2197PHONY += rust-analyzer
2198rust-analyzer:
2199	+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
2200ifdef KBUILD_EXTMOD
2201# FIXME: external modules must not descend into a sub-directory of the kernel
2202	$(Q)$(MAKE) $(build)=$(objtree)/rust src=$(srctree)/rust $@
2203else
2204	$(Q)$(MAKE) $(build)=rust $@
2205endif
2206
2207# Script to generate missing namespace dependencies
2208# ---------------------------------------------------------------------------
2209
2210PHONY += nsdeps
2211nsdeps: export KBUILD_NSDEPS=1
2212nsdeps: modules
2213	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/nsdeps
2214
2215# Clang Tooling
2216# ---------------------------------------------------------------------------
2217
2218quiet_cmd_gen_compile_commands = GEN     $@
2219      cmd_gen_compile_commands = $(PYTHON3) $< -a $(AR) -o $@ $(filter-out $<, $(real-prereqs))
2220
2221compile_commands.json: $(srctree)/scripts/clang-tools/gen_compile_commands.py \
2222	$(if $(KBUILD_EXTMOD),, vmlinux.a $(KBUILD_VMLINUX_LIBS)) \
2223	$(if $(CONFIG_MODULES), modules.order) FORCE
2224	$(call if_changed,gen_compile_commands)
2225
2226targets += compile_commands.json
2227
2228PHONY += clang-tidy clang-analyzer
2229
2230ifdef CONFIG_CC_IS_CLANG
2231quiet_cmd_clang_tools = CHECK   $<
2232      cmd_clang_tools = $(PYTHON3) $(srctree)/scripts/clang-tools/run-clang-tools.py $@ $<
2233
2234clang-tidy clang-analyzer: compile_commands.json
2235	$(call cmd,clang_tools)
2236else
2237clang-tidy clang-analyzer:
2238	@echo "$@ requires CC=clang" >&2
2239	@false
2240endif
2241
2242# Scripts to check various things for consistency
2243# ---------------------------------------------------------------------------
2244
2245PHONY += includecheck versioncheck coccicheck
2246
2247includecheck:
2248	find $(srctree)/* $(RCS_FIND_IGNORE) \
2249		-name '*.[hcS]' -type f -print | sort \
2250		| xargs $(PERL) -w $(srctree)/scripts/checkincludes.pl
2251
2252versioncheck:
2253	find $(srctree)/* $(RCS_FIND_IGNORE) \
2254		-name '*.[hcS]' -type f -print | sort \
2255		| xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
2256
2257coccicheck:
2258	$(Q)$(BASH) $(srctree)/scripts/$@
2259
2260PHONY += checkstack kernelrelease kernelversion image_name
2261
2262# UML needs a little special treatment here.  It wants to use the host
2263# toolchain, so needs $(SUBARCH) passed to checkstack.pl.  Everyone
2264# else wants $(ARCH), including people doing cross-builds, which means
2265# that $(SUBARCH) doesn't work here.
2266ifeq ($(ARCH), um)
2267CHECKSTACK_ARCH := $(SUBARCH)
2268else
2269CHECKSTACK_ARCH := $(ARCH)
2270endif
2271MINSTACKSIZE	?= 100
2272checkstack:
2273	$(OBJDUMP) -d vmlinux $$(find . -name '*.ko') | \
2274	$(PERL) $(srctree)/scripts/checkstack.pl $(CHECKSTACK_ARCH) $(MINSTACKSIZE)
2275
2276kernelrelease:
2277	@$(filechk_kernel.release)
2278
2279kernelversion:
2280	@echo $(KERNELVERSION)
2281
2282image_name:
2283	@echo $(KBUILD_IMAGE)
2284
2285PHONY += run-command
2286run-command:
2287	$(Q)$(KBUILD_RUN_COMMAND)
2288
2289quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN   $(wildcard $(rm-files)))
2290      cmd_rmfiles = rm -rf $(rm-files)
2291
2292# read saved command lines for existing targets
2293existing-targets := $(wildcard $(sort $(targets)))
2294
2295-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
2296
2297endif # config-build
2298endif # mixed-build
2299endif # need-sub-make
2300
2301PHONY += FORCE
2302FORCE:
2303
2304# Declare the contents of the PHONY variable as phony.  We keep that
2305# information in a variable so we can use it in if_changed and friends.
2306.PHONY: $(PHONY)
2307