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