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