blob: bf22df29c4d817ae7cea36aa28757ca456fe7b1d [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001# SPDX-License-Identifier: GPL-2.0
David Brazdil0f672f62019-12-10 10:32:29 +00002VERSION = 5
Olivier Deprez157378f2022-04-04 15:47:50 +02003PATCHLEVEL = 10
Olivier Deprez92d4c212022-12-06 15:05:30 +01004SUBLEVEL = 157
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005EXTRAVERSION =
Olivier Deprez157378f2022-04-04 15:47:50 +02006NAME = Dare mighty things
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007
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
Olivier Deprez157378f2022-04-04 15:47:50 +020014$(if $(filter __%, $(MAKECMDGOALS)), \
15 $(error targets prefixed with '__' are only for internal use))
16
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017# That's our default target when none is given on the command line
Olivier Deprez157378f2022-04-04 15:47:50 +020018PHONY := __all
19__all:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021# We are using a recursive build, so we need to do a little thinking
22# to get the ordering right.
23#
24# Most importantly: sub-Makefiles should only ever modify files in
25# their own directory. If in some directory we have a dependency on
26# a file in another dir (which doesn't happen often, but it's often
27# unavoidable when linking the built-in.a targets which finally
28# turn into vmlinux), we will call a sub make in that other dir, and
29# after that we are sure that everything which is in that other dir
30# is now up to date.
31#
32# The only cases where we need to modify files which have global
33# effects are thus separated out and done before the recursive
34# descending is started. They are now explicitly listed as the
35# prepare rule.
36
David Brazdil0f672f62019-12-10 10:32:29 +000037ifneq ($(sub_make_done),1)
38
39# Do not use make's built-in rules and variables
40# (this increases performance and avoids hard-to-debug behaviour)
41MAKEFLAGS += -rR
42
43# Avoid funny character set dependencies
44unexport LC_ALL
45LC_COLLATE=C
46LC_NUMERIC=C
47export LC_COLLATE LC_NUMERIC
48
49# Avoid interference with shell env settings
50unexport GREP_OPTIONS
51
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052# Beautify output
53# ---------------------------------------------------------------------------
54#
55# Normally, we echo the whole command before executing it. By making
56# that echo $($(quiet)$(cmd)), we now have the possibility to set
57# $(quiet) to choose other forms of output instead, e.g.
58#
59# quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
60# cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
61#
62# If $(quiet) is empty, the whole command will be printed.
63# If it is set to "quiet_", only the short version will be printed.
64# If it is set to "silent_", nothing will be printed at all, since
65# the variable $(silent_cmd_cc_o_c) doesn't exist.
66#
67# A simple variant is to prefix commands with $(Q) - that's useful
68# for commands that shall be hidden in non-verbose mode.
69#
70# $(Q)ln $@ :<
71#
72# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
73# If KBUILD_VERBOSE equals 1 then the above command is displayed.
Olivier Deprez157378f2022-04-04 15:47:50 +020074# If KBUILD_VERBOSE equals 2 then give the reason why each target is rebuilt.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075#
76# To put more focus on warnings, be less verbose as default
77# Use 'make V=1' to see the full commands
78
79ifeq ("$(origin V)", "command line")
80 KBUILD_VERBOSE = $(V)
81endif
82ifndef KBUILD_VERBOSE
83 KBUILD_VERBOSE = 0
84endif
85
86ifeq ($(KBUILD_VERBOSE),1)
87 quiet =
88 Q =
89else
90 quiet=quiet_
91 Q = @
92endif
93
94# If the user is running make -s (silent mode), suppress echoing of
95# commands
96
97ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
98 quiet=silent_
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099endif
100
101export quiet Q KBUILD_VERBOSE
102
David Brazdil0f672f62019-12-10 10:32:29 +0000103# Kbuild will save output files in the current working directory.
104# This does not need to match to the root of the kernel source tree.
105#
106# For example, you can do this:
107#
108# cd /dir/to/store/output/files; make -f /dir/to/kernel/source/Makefile
109#
110# If you want to save output files in a different location, there are
111# two syntaxes to specify it.
112#
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113# 1) O=
114# Use "make O=dir/to/store/output/files/"
115#
116# 2) Set KBUILD_OUTPUT
David Brazdil0f672f62019-12-10 10:32:29 +0000117# Set the environment variable KBUILD_OUTPUT to point to the output directory.
118# export KBUILD_OUTPUT=dir/to/store/output/files/; make
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119#
120# The O= assignment takes precedence over the KBUILD_OUTPUT environment
121# variable.
122
David Brazdil0f672f62019-12-10 10:32:29 +0000123# Do we want to change the working directory?
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124ifeq ("$(origin O)", "command line")
125 KBUILD_OUTPUT := $(O)
126endif
127
David Brazdil0f672f62019-12-10 10:32:29 +0000128ifneq ($(KBUILD_OUTPUT),)
129# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
130# expand a shell special character '~'. We use a somewhat tedious way here.
131abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
132$(if $(abs_objtree),, \
133 $(error failed to create output directory "$(KBUILD_OUTPUT)"))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134
David Brazdil0f672f62019-12-10 10:32:29 +0000135# $(realpath ...) resolves symlinks
136abs_objtree := $(realpath $(abs_objtree))
137else
138abs_objtree := $(CURDIR)
139endif # ifneq ($(KBUILD_OUTPUT),)
140
141ifeq ($(abs_objtree),$(CURDIR))
142# Suppress "Entering directory ..." unless we are changing the work directory.
143MAKEFLAGS += --no-print-directory
144else
145need-sub-make := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146endif
147
David Brazdil0f672f62019-12-10 10:32:29 +0000148abs_srctree := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
149
150ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
151$(error source directory cannot contain spaces or colons)
152endif
153
154ifneq ($(abs_srctree),$(abs_objtree))
155# Look for make include files relative to root of kernel src
156#
157# This does not become effective immediately because MAKEFLAGS is re-parsed
158# once after the Makefile is read. We need to invoke sub-make.
159MAKEFLAGS += --include-dir=$(abs_srctree)
160need-sub-make := 1
161endif
162
Olivier Deprez157378f2022-04-04 15:47:50 +0200163this-makefile := $(lastword $(MAKEFILE_LIST))
164
David Brazdil0f672f62019-12-10 10:32:29 +0000165ifneq ($(filter 3.%,$(MAKE_VERSION)),)
166# 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x
167# We need to invoke sub-make to avoid implicit rules in the top Makefile.
168need-sub-make := 1
169# Cancel implicit rules for this Makefile.
Olivier Deprez157378f2022-04-04 15:47:50 +0200170$(this-makefile): ;
David Brazdil0f672f62019-12-10 10:32:29 +0000171endif
172
173export abs_srctree abs_objtree
174export sub_make_done := 1
175
176ifeq ($(need-sub-make),1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177
Olivier Deprez157378f2022-04-04 15:47:50 +0200178PHONY += $(MAKECMDGOALS) __sub-make
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179
Olivier Deprez157378f2022-04-04 15:47:50 +0200180$(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 @:
182
183# Invoke a second make in the output directory, passing relevant variables
Olivier Deprez157378f2022-04-04 15:47:50 +0200184__sub-make:
David Brazdil0f672f62019-12-10 10:32:29 +0000185 $(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186
David Brazdil0f672f62019-12-10 10:32:29 +0000187endif # need-sub-make
188endif # sub_make_done
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189
190# We process the rest of the Makefile if this is the final invocation of make
David Brazdil0f672f62019-12-10 10:32:29 +0000191ifeq ($(need-sub-make),)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000192
193# Do not print "Entering directory ...",
194# but we want to display it when entering to the output directory
195# so that IDEs/editors are able to understand relative filenames.
196MAKEFLAGS += --no-print-directory
197
198# Call a source code checker (by default, "sparse") as part of the
199# C compilation.
200#
201# Use 'make C=1' to enable checking of only re-compiled files.
202# Use 'make C=2' to enable checking of *all* source files, regardless
203# of whether they are re-compiled or not.
204#
205# See the file "Documentation/dev-tools/sparse.rst" for more details,
206# including where to get the "sparse" utility.
207
208ifeq ("$(origin C)", "command line")
209 KBUILD_CHECKSRC = $(C)
210endif
211ifndef KBUILD_CHECKSRC
212 KBUILD_CHECKSRC = 0
213endif
214
David Brazdil0f672f62019-12-10 10:32:29 +0000215# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the
216# directory of external module to build. Setting M= takes precedence.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217ifeq ("$(origin M)", "command line")
218 KBUILD_EXTMOD := $(M)
219endif
220
Olivier Deprez157378f2022-04-04 15:47:50 +0200221$(if $(word 2, $(KBUILD_EXTMOD)), \
222 $(error building multiple external modules is not supported))
223
David Brazdil0f672f62019-12-10 10:32:29 +0000224export KBUILD_CHECKSRC KBUILD_EXTMOD
225
226extmod-prefix = $(if $(KBUILD_EXTMOD),$(KBUILD_EXTMOD)/)
227
228ifeq ($(abs_srctree),$(abs_objtree))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 # building in the source tree
230 srctree := .
David Brazdil0f672f62019-12-10 10:32:29 +0000231 building_out_of_srctree :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232else
David Brazdil0f672f62019-12-10 10:32:29 +0000233 ifeq ($(abs_srctree)/,$(dir $(abs_objtree)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 # building in a subdirectory of the source tree
235 srctree := ..
236 else
David Brazdil0f672f62019-12-10 10:32:29 +0000237 srctree := $(abs_srctree)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238 endif
David Brazdil0f672f62019-12-10 10:32:29 +0000239 building_out_of_srctree := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000240endif
241
David Brazdil0f672f62019-12-10 10:32:29 +0000242ifneq ($(KBUILD_ABS_SRCTREE),)
243srctree := $(abs_srctree)
244endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245
246objtree := .
David Brazdil0f672f62019-12-10 10:32:29 +0000247VPATH := $(srctree)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248
David Brazdil0f672f62019-12-10 10:32:29 +0000249export building_out_of_srctree srctree objtree VPATH
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250
251# To make sure we do not include .config for any of the *config targets
252# catch them early, and hand them over to scripts/kconfig/Makefile
253# It is allowed to specify more targets when calling make, including
254# mixing *config targets and build targets.
255# For example 'make oldconfig all'.
256# Detect when mixed targets is specified, and make a second invocation
257# of make so .config is not included in this case either (for *config).
258
259version_h := include/generated/uapi/linux/version.h
260old_version_h := include/linux/version.h
261
262clean-targets := %clean mrproper cleandocs
263no-dot-config-targets := $(clean-targets) \
264 cscope gtags TAGS tags help% %docs check% coccicheck \
David Brazdil0f672f62019-12-10 10:32:29 +0000265 $(version_h) headers headers_% archheaders archscripts \
Olivier Deprez157378f2022-04-04 15:47:50 +0200266 %asm-generic kernelversion %src-pkg dt_binding_check \
267 outputmakefile
268no-sync-config-targets := $(no-dot-config-targets) %install kernelrelease \
269 image_name
David Brazdil0f672f62019-12-10 10:32:29 +0000270single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.s %.symtypes %/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271
David Brazdil0f672f62019-12-10 10:32:29 +0000272config-build :=
273mixed-build :=
274need-config := 1
275may-sync-config := 1
276single-build :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277
278ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
279 ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
David Brazdil0f672f62019-12-10 10:32:29 +0000280 need-config :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 endif
282endif
283
284ifneq ($(filter $(no-sync-config-targets), $(MAKECMDGOALS)),)
285 ifeq ($(filter-out $(no-sync-config-targets), $(MAKECMDGOALS)),)
David Brazdil0f672f62019-12-10 10:32:29 +0000286 may-sync-config :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 endif
288endif
289
290ifneq ($(KBUILD_EXTMOD),)
David Brazdil0f672f62019-12-10 10:32:29 +0000291 may-sync-config :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292endif
293
294ifeq ($(KBUILD_EXTMOD),)
Olivier Deprez157378f2022-04-04 15:47:50 +0200295 ifneq ($(filter %config,$(MAKECMDGOALS)),)
David Brazdil0f672f62019-12-10 10:32:29 +0000296 config-build := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 ifneq ($(words $(MAKECMDGOALS)),1)
David Brazdil0f672f62019-12-10 10:32:29 +0000298 mixed-build := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299 endif
300 endif
301endif
302
David Brazdil0f672f62019-12-10 10:32:29 +0000303# We cannot build single targets and the others at the same time
304ifneq ($(filter $(single-targets), $(MAKECMDGOALS)),)
305 single-build := 1
306 ifneq ($(filter-out $(single-targets), $(MAKECMDGOALS)),)
307 mixed-build := 1
308 endif
309endif
310
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311# For "make -j clean all", "make -j mrproper defconfig all", etc.
312ifneq ($(filter $(clean-targets),$(MAKECMDGOALS)),)
313 ifneq ($(filter-out $(clean-targets),$(MAKECMDGOALS)),)
David Brazdil0f672f62019-12-10 10:32:29 +0000314 mixed-build := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 endif
316endif
317
318# install and modules_install need also be processed one by one
319ifneq ($(filter install,$(MAKECMDGOALS)),)
320 ifneq ($(filter modules_install,$(MAKECMDGOALS)),)
David Brazdil0f672f62019-12-10 10:32:29 +0000321 mixed-build := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 endif
323endif
324
David Brazdil0f672f62019-12-10 10:32:29 +0000325ifdef mixed-build
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326# ===========================================================================
327# We're called with mixed targets (*config and build targets).
328# Handle them one by one.
329
330PHONY += $(MAKECMDGOALS) __build_one_by_one
331
Olivier Deprez157378f2022-04-04 15:47:50 +0200332$(MAKECMDGOALS): __build_one_by_one
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333 @:
334
335__build_one_by_one:
336 $(Q)set -e; \
337 for i in $(MAKECMDGOALS); do \
338 $(MAKE) -f $(srctree)/Makefile $$i; \
339 done
340
David Brazdil0f672f62019-12-10 10:32:29 +0000341else # !mixed-build
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343include scripts/Kbuild.include
344
345# Read KERNELRELEASE from include/config/kernel.release (if it exists)
346KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
347KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
348export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
349
350include scripts/subarch.include
351
352# Cross compiling and selecting different set of gcc/bin-utils
353# ---------------------------------------------------------------------------
354#
355# When performing cross compilation for other architectures ARCH shall be set
356# to the target architecture. (See arch/* for the possibilities).
357# ARCH can be set during invocation of make:
358# make ARCH=ia64
359# Another way is to have ARCH set in the environment.
360# The default ARCH is the host where make is executed.
361
362# CROSS_COMPILE specify the prefix used for all executables used
363# during compilation. Only gcc and related bin-utils executables
364# are prefixed with $(CROSS_COMPILE).
365# CROSS_COMPILE can be set on the command line
366# make CROSS_COMPILE=ia64-linux-
367# Alternatively CROSS_COMPILE can be set in the environment.
368# Default value for CROSS_COMPILE is not to prefix executables
369# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
370ARCH ?= $(SUBARCH)
371
372# Architecture as present in compile.h
373UTS_MACHINE := $(ARCH)
374SRCARCH := $(ARCH)
375
376# Additional ARCH settings for x86
377ifeq ($(ARCH),i386)
378 SRCARCH := x86
379endif
380ifeq ($(ARCH),x86_64)
381 SRCARCH := x86
382endif
383
384# Additional ARCH settings for sparc
385ifeq ($(ARCH),sparc32)
386 SRCARCH := sparc
387endif
388ifeq ($(ARCH),sparc64)
389 SRCARCH := sparc
390endif
391
392# Additional ARCH settings for sh
393ifeq ($(ARCH),sh64)
394 SRCARCH := sh
395endif
396
397KCONFIG_CONFIG ?= .config
398export KCONFIG_CONFIG
399
Olivier Deprez157378f2022-04-04 15:47:50 +0200400# Default file for 'make defconfig'. This may be overridden by arch-Makefile.
401export KBUILD_DEFCONFIG := defconfig
402
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403# SHELL used by kbuild
David Brazdil0f672f62019-12-10 10:32:29 +0000404CONFIG_SHELL := sh
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405
406HOST_LFS_CFLAGS := $(shell getconf LFS_CFLAGS 2>/dev/null)
407HOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
408HOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
409
Olivier Deprez0e641232021-09-23 10:07:05 +0200410ifneq ($(LLVM),)
411HOSTCC = clang
412HOSTCXX = clang++
413else
414HOSTCC = gcc
415HOSTCXX = g++
416endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200417
418export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
419 -O2 -fomit-frame-pointer -std=gnu89
420export KBUILD_USERLDFLAGS :=
421
422KBUILD_HOSTCFLAGS := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
423KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
425KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
426
427# Make variables (CC, etc...)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000428CPP = $(CC) -E
Olivier Deprez0e641232021-09-23 10:07:05 +0200429ifneq ($(LLVM),)
430CC = clang
431LD = ld.lld
432AR = llvm-ar
433NM = llvm-nm
434OBJCOPY = llvm-objcopy
435OBJDUMP = llvm-objdump
436READELF = llvm-readelf
Olivier Deprez0e641232021-09-23 10:07:05 +0200437STRIP = llvm-strip
438else
439CC = $(CROSS_COMPILE)gcc
440LD = $(CROSS_COMPILE)ld
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441AR = $(CROSS_COMPILE)ar
442NM = $(CROSS_COMPILE)nm
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443OBJCOPY = $(CROSS_COMPILE)objcopy
444OBJDUMP = $(CROSS_COMPILE)objdump
Olivier Deprez0e641232021-09-23 10:07:05 +0200445READELF = $(CROSS_COMPILE)readelf
Olivier Deprez0e641232021-09-23 10:07:05 +0200446STRIP = $(CROSS_COMPILE)strip
447endif
David Brazdil0f672f62019-12-10 10:32:29 +0000448PAHOLE = pahole
Olivier Deprez157378f2022-04-04 15:47:50 +0200449RESOLVE_BTFIDS = $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450LEX = flex
451YACC = bison
452AWK = awk
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453INSTALLKERNEL := installkernel
Olivier Deprez0e641232021-09-23 10:07:05 +0200454DEPMOD = depmod
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000455PERL = perl
456PYTHON = python
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000457PYTHON3 = python3
458CHECK = sparse
David Brazdil0f672f62019-12-10 10:32:29 +0000459BASH = bash
Olivier Deprez0e641232021-09-23 10:07:05 +0200460KGZIP = gzip
461KBZIP2 = bzip2
462KLZOP = lzop
463LZMA = lzma
464LZ4 = lz4c
465XZ = xz
Olivier Deprez157378f2022-04-04 15:47:50 +0200466ZSTD = zstd
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467
Olivier Deprez92d4c212022-12-06 15:05:30 +0100468PAHOLE_FLAGS = $(shell PAHOLE=$(PAHOLE) $(srctree)/scripts/pahole-flags.sh)
469
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470CHECKFLAGS := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
471 -Wbitwise -Wno-return-void -Wno-unknown-attribute $(CF)
David Brazdil0f672f62019-12-10 10:32:29 +0000472NOSTDINC_FLAGS :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473CFLAGS_MODULE =
474AFLAGS_MODULE =
475LDFLAGS_MODULE =
476CFLAGS_KERNEL =
477AFLAGS_KERNEL =
478LDFLAGS_vmlinux =
479
480# Use USERINCLUDE when you must reference the UAPI directories only.
481USERINCLUDE := \
482 -I$(srctree)/arch/$(SRCARCH)/include/uapi \
483 -I$(objtree)/arch/$(SRCARCH)/include/generated/uapi \
484 -I$(srctree)/include/uapi \
485 -I$(objtree)/include/generated/uapi \
486 -include $(srctree)/include/linux/kconfig.h
487
488# Use LINUXINCLUDE when you must reference the include/ directory.
489# Needed to be compatible with the O= option
490LINUXINCLUDE := \
491 -I$(srctree)/arch/$(SRCARCH)/include \
492 -I$(objtree)/arch/$(SRCARCH)/include/generated \
David Brazdil0f672f62019-12-10 10:32:29 +0000493 $(if $(building_out_of_srctree),-I$(srctree)/include) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494 -I$(objtree)/include \
495 $(USERINCLUDE)
496
David Brazdil0f672f62019-12-10 10:32:29 +0000497KBUILD_AFLAGS := -D__ASSEMBLY__ -fno-PIE
498KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
499 -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \
500 -Werror=implicit-function-declaration -Werror=implicit-int \
Olivier Deprez0e641232021-09-23 10:07:05 +0200501 -Werror=return-type -Wno-format-security \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 -std=gnu89
503KBUILD_CPPFLAGS := -D__KERNEL__
504KBUILD_AFLAGS_KERNEL :=
505KBUILD_CFLAGS_KERNEL :=
506KBUILD_AFLAGS_MODULE := -DMODULE
507KBUILD_CFLAGS_MODULE := -DMODULE
David Brazdil0f672f62019-12-10 10:32:29 +0000508KBUILD_LDFLAGS_MODULE :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000509KBUILD_LDFLAGS :=
David Brazdil0f672f62019-12-10 10:32:29 +0000510CLANG_FLAGS :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511
Olivier Deprez0e641232021-09-23 10:07:05 +0200512export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
Olivier Deprez157378f2022-04-04 15:47:50 +0200513export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
Olivier Deprez0e641232021-09-23 10:07:05 +0200514export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
Olivier Deprez157378f2022-04-04 15:47:50 +0200515export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
David Brazdil0f672f62019-12-10 10:32:29 +0000516export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517
518export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
519export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
521export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
522export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
Olivier Deprez92d4c212022-12-06 15:05:30 +0100523export PAHOLE_FLAGS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524
525# Files to ignore in find ... statements
526
527export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \
528 -name CVS -o -name .pc -o -name .hg -o -name .git \) \
529 -prune -o
530export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn \
531 --exclude CVS --exclude .pc --exclude .hg --exclude .git
532
533# ===========================================================================
534# Rules shared between *config targets and build targets
535
536# Basic helpers built in scripts/basic/
537PHONY += scripts_basic
538scripts_basic:
539 $(Q)$(MAKE) $(build)=scripts/basic
540 $(Q)rm -f .tmp_quiet_recordmcount
541
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542PHONY += outputmakefile
David Brazdil0f672f62019-12-10 10:32:29 +0000543# Before starting out-of-tree build, make sure the source tree is clean.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544# outputmakefile generates a Makefile in the output directory, if using a
545# separate output directory. This allows convenient use of make in the
546# output directory.
David Brazdil0f672f62019-12-10 10:32:29 +0000547# At the same time when output Makefile generated, generate .gitignore to
548# ignore whole output directory
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549outputmakefile:
David Brazdil0f672f62019-12-10 10:32:29 +0000550ifdef building_out_of_srctree
551 $(Q)if [ -f $(srctree)/.config -o \
552 -d $(srctree)/include/config -o \
553 -d $(srctree)/arch/$(SRCARCH)/include/generated ]; then \
554 echo >&2 "***"; \
555 echo >&2 "*** The source tree is not clean, please run 'make$(if $(findstring command line, $(origin ARCH)), ARCH=$(ARCH)) mrproper'"; \
556 echo >&2 "*** in $(abs_srctree)";\
557 echo >&2 "***"; \
558 false; \
559 fi
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560 $(Q)ln -fsn $(srctree) source
David Brazdil0f672f62019-12-10 10:32:29 +0000561 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkmakefile $(srctree)
562 $(Q)test -e .gitignore || \
563 { echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000564endif
565
David Brazdil0f672f62019-12-10 10:32:29 +0000566ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567ifneq ($(CROSS_COMPILE),)
David Brazdil0f672f62019-12-10 10:32:29 +0000568CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
569GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)elfedit))
Olivier Deprez0e641232021-09-23 10:07:05 +0200570CLANG_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000571GCC_TOOLCHAIN := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
572endif
573ifneq ($(GCC_TOOLCHAIN),)
David Brazdil0f672f62019-12-10 10:32:29 +0000574CLANG_FLAGS += --gcc-toolchain=$(GCC_TOOLCHAIN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000575endif
Olivier Deprez0e641232021-09-23 10:07:05 +0200576ifneq ($(LLVM_IAS),1)
David Brazdil0f672f62019-12-10 10:32:29 +0000577CLANG_FLAGS += -no-integrated-as
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578endif
David Brazdil0f672f62019-12-10 10:32:29 +0000579CLANG_FLAGS += -Werror=unknown-warning-option
580KBUILD_CFLAGS += $(CLANG_FLAGS)
581KBUILD_AFLAGS += $(CLANG_FLAGS)
582export CLANG_FLAGS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000583endif
584
585# The expansion should be delayed until arch/$(SRCARCH)/Makefile is included.
586# Some architectures define CROSS_COMPILE in arch/$(SRCARCH)/Makefile.
587# CC_VERSION_TEXT is referenced from Kconfig (so it needs export),
588# and from include/config/auto.conf.cmd to detect the compiler upgrade.
David Brazdil0f672f62019-12-10 10:32:29 +0000589CC_VERSION_TEXT = $(shell $(CC) --version 2>/dev/null | head -n 1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590
David Brazdil0f672f62019-12-10 10:32:29 +0000591ifdef config-build
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592# ===========================================================================
593# *config targets only - make sure prerequisites are updated, and descend
594# in scripts/kconfig to make the *config target
595
596# Read arch specific Makefile to set KBUILD_DEFCONFIG as needed.
597# KBUILD_DEFCONFIG may point out an alternative default configuration
598# used for 'make defconfig'
599include arch/$(SRCARCH)/Makefile
600export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT
601
David Brazdil0f672f62019-12-10 10:32:29 +0000602config: outputmakefile scripts_basic FORCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 $(Q)$(MAKE) $(build)=scripts/kconfig $@
604
David Brazdil0f672f62019-12-10 10:32:29 +0000605%config: outputmakefile scripts_basic FORCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000606 $(Q)$(MAKE) $(build)=scripts/kconfig $@
607
David Brazdil0f672f62019-12-10 10:32:29 +0000608else #!config-build
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609# ===========================================================================
610# Build targets only - this includes vmlinux, arch specific targets, clean
611# targets and others. In general all targets except *config targets.
612
613# If building an external module we do not care about the all: rule
Olivier Deprez157378f2022-04-04 15:47:50 +0200614# but instead __all depend on modules
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000615PHONY += all
616ifeq ($(KBUILD_EXTMOD),)
Olivier Deprez157378f2022-04-04 15:47:50 +0200617__all: all
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618else
Olivier Deprez157378f2022-04-04 15:47:50 +0200619__all: modules
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000620endif
621
622# Decide whether to build built-in, modular, or both.
623# Normally, just do built-in.
624
625KBUILD_MODULES :=
626KBUILD_BUILTIN := 1
627
628# If we have only "make modules", don't compile built-in objects.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000629ifeq ($(MAKECMDGOALS),modules)
Olivier Deprez0e641232021-09-23 10:07:05 +0200630 KBUILD_BUILTIN :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631endif
632
633# If we have "make <whatever> modules", compile modules
634# in addition to whatever we do anyway.
635# Just "make" or "make all" shall build modules as well
636
Olivier Deprez157378f2022-04-04 15:47:50 +0200637ifneq ($(filter all modules nsdeps %compile_commands.json clang-%,$(MAKECMDGOALS)),)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638 KBUILD_MODULES := 1
639endif
640
641ifeq ($(MAKECMDGOALS),)
642 KBUILD_MODULES := 1
643endif
644
645export KBUILD_MODULES KBUILD_BUILTIN
646
David Brazdil0f672f62019-12-10 10:32:29 +0000647ifdef need-config
648include include/config/auto.conf
649endif
650
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651ifeq ($(KBUILD_EXTMOD),)
652# Objects we will link into vmlinux / subdirs we need to visit
Olivier Deprez157378f2022-04-04 15:47:50 +0200653core-y := init/ usr/
David Brazdil0f672f62019-12-10 10:32:29 +0000654drivers-y := drivers/ sound/
655drivers-$(CONFIG_SAMPLES) += samples/
Olivier Deprez157378f2022-04-04 15:47:50 +0200656drivers-y += net/ virt/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000657libs-y := lib/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000658endif # KBUILD_EXTMOD
659
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660# The all: target is the default when no target is given on the
661# command line.
662# This allow a user to issue only 'make' to build a kernel including modules
663# Defaults to vmlinux, but the arch makefile usually adds further targets
664all: vmlinux
665
666CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \
667 $(call cc-option,-fno-tree-loop-im) \
668 $(call cc-disable-warning,maybe-uninitialized,)
669export CFLAGS_GCOV
670
671# The arch Makefiles can override CC_FLAGS_FTRACE. We may also append it later.
672ifdef CONFIG_FUNCTION_TRACER
673 CC_FLAGS_FTRACE := -pg
674endif
675
Olivier Deprez92d4c212022-12-06 15:05:30 +0100676ifdef CONFIG_CC_IS_GCC
677RETPOLINE_CFLAGS := $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
678RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix)
679RETPOLINE_VDSO_CFLAGS := $(call cc-option,-mindirect-branch=thunk-inline -mindirect-branch-register)
680endif
681ifdef CONFIG_CC_IS_CLANG
682RETPOLINE_CFLAGS := -mretpoline-external-thunk
683RETPOLINE_VDSO_CFLAGS := -mretpoline
684endif
685
686ifdef CONFIG_RETHUNK
687RETHUNK_CFLAGS := -mfunction-return=thunk-extern
688RETPOLINE_CFLAGS += $(RETHUNK_CFLAGS)
689endif
690
David Brazdil0f672f62019-12-10 10:32:29 +0000691export RETPOLINE_CFLAGS
692export RETPOLINE_VDSO_CFLAGS
693
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694include arch/$(SRCARCH)/Makefile
695
David Brazdil0f672f62019-12-10 10:32:29 +0000696ifdef need-config
697ifdef may-sync-config
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698# Read in dependencies to all Kconfig* files, make sure to run syncconfig if
699# changes are detected. This should be included after arch/$(SRCARCH)/Makefile
700# because some architectures define CROSS_COMPILE there.
David Brazdil0f672f62019-12-10 10:32:29 +0000701include include/config/auto.conf.cmd
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702
David Brazdil0f672f62019-12-10 10:32:29 +0000703$(KCONFIG_CONFIG):
704 @echo >&2 '***'
705 @echo >&2 '*** Configuration file "$@" not found!'
706 @echo >&2 '***'
707 @echo >&2 '*** Please run some configurator (e.g. "make oldconfig" or'
708 @echo >&2 '*** "make menuconfig" or "make xconfig").'
709 @echo >&2 '***'
710 @/bin/false
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711
712# The actual configuration files used during the build are stored in
713# include/generated/ and include/config/. Update them if .config is newer than
714# include/config/auto.conf (which mirrors .config).
David Brazdil0f672f62019-12-10 10:32:29 +0000715#
716# This exploits the 'multi-target pattern rule' trick.
717# The syncconfig should be executed only once to make all the targets.
Olivier Deprez157378f2022-04-04 15:47:50 +0200718# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
719#
720# Do not use $(call cmd,...) here. That would suppress prompts from syncconfig,
721# so you cannot notice that Kconfig is waiting for the user input.
722%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG)
723 $(Q)$(kecho) " SYNC $@"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
David Brazdil0f672f62019-12-10 10:32:29 +0000725else # !may-sync-config
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726# External modules and some install targets need include/generated/autoconf.h
727# and include/config/auto.conf but do not care if they are up-to-date.
728# Use auto.conf to trigger the test
729PHONY += include/config/auto.conf
730
731include/config/auto.conf:
732 $(Q)test -e include/generated/autoconf.h -a -e $@ || ( \
733 echo >&2; \
734 echo >&2 " ERROR: Kernel configuration is invalid."; \
735 echo >&2 " include/generated/autoconf.h or $@ are missing.";\
736 echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
737 echo >&2 ; \
738 /bin/false)
739
740endif # may-sync-config
David Brazdil0f672f62019-12-10 10:32:29 +0000741endif # need-config
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742
743KBUILD_CFLAGS += $(call cc-option,-fno-delete-null-pointer-checks,)
744KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
745KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
746KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
David Brazdil0f672f62019-12-10 10:32:29 +0000747KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000748
David Brazdil0f672f62019-12-10 10:32:29 +0000749ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
750KBUILD_CFLAGS += -O2
751else ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3
752KBUILD_CFLAGS += -O3
753else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
754KBUILD_CFLAGS += -Os
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000755endif
756
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757# Tell gcc to never replace conditional load with a non-conditional one
758KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0)
Olivier Deprez0e641232021-09-23 10:07:05 +0200759KBUILD_CFLAGS += $(call cc-option,-fno-allow-store-data-races)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000761ifdef CONFIG_READABLE_ASM
762# Disable optimizations that make assembler listings hard to read.
763# reorder blocks reorders the control in the function
764# ipa clone creates specialized cloned functions
765# partial inlining inlines only parts of functions
766KBUILD_CFLAGS += $(call cc-option,-fno-reorder-blocks,) \
767 $(call cc-option,-fno-ipa-cp-clone,) \
768 $(call cc-option,-fno-partial-inlining)
769endif
770
771ifneq ($(CONFIG_FRAME_WARN),0)
Olivier Deprez157378f2022-04-04 15:47:50 +0200772KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773endif
774
Olivier Deprez157378f2022-04-04 15:47:50 +0200775stackp-flags-y := -fno-stack-protector
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776stackp-flags-$(CONFIG_STACKPROTECTOR) := -fstack-protector
777stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG) := -fstack-protector-strong
778
779KBUILD_CFLAGS += $(stackp-flags-y)
780
David Brazdil0f672f62019-12-10 10:32:29 +0000781ifdef CONFIG_CC_IS_CLANG
782KBUILD_CPPFLAGS += -Qunused-arguments
783KBUILD_CFLAGS += -Wno-format-invalid-specifier
784KBUILD_CFLAGS += -Wno-gnu
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785# CLANG uses a _MergedGlobals as optimization, but this breaks modpost, as the
786# source of a reference will be _MergedGlobals and not on of the whitelisted names.
787# See modpost pattern 2
David Brazdil0f672f62019-12-10 10:32:29 +0000788KBUILD_CFLAGS += -mno-global-merge
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789else
790
David Brazdil0f672f62019-12-10 10:32:29 +0000791# Warn about unmarked fall-throughs in switch statement.
792# Disabled for clang while comment to attribute conversion happens and
793# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
794KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough,)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795endif
796
Olivier Deprez0e641232021-09-23 10:07:05 +0200797# These warnings generated too much noise in a regular build.
798# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
799KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
802ifdef CONFIG_FRAME_POINTER
803KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
804else
805# Some targets (ARM with Thumb2, for example), can't be built with frame
806# pointers. For those, we don't have FUNCTION_TRACER automatically
807# select FRAME_POINTER. However, FUNCTION_TRACER adds -pg, and this is
808# incompatible with -fomit-frame-pointer with current GCC, so we don't use
809# -fomit-frame-pointer with FUNCTION_TRACER.
810ifndef CONFIG_FUNCTION_TRACER
811KBUILD_CFLAGS += -fomit-frame-pointer
812endif
813endif
814
Olivier Deprez157378f2022-04-04 15:47:50 +0200815# Initialize all stack variables with a 0xAA pattern.
816ifdef CONFIG_INIT_STACK_ALL_PATTERN
David Brazdil0f672f62019-12-10 10:32:29 +0000817KBUILD_CFLAGS += -ftrivial-auto-var-init=pattern
818endif
819
Olivier Deprez157378f2022-04-04 15:47:50 +0200820# Initialize all stack variables with a zero value.
821ifdef CONFIG_INIT_STACK_ALL_ZERO
Olivier Deprez157378f2022-04-04 15:47:50 +0200822KBUILD_CFLAGS += -ftrivial-auto-var-init=zero
Olivier Deprez92d4c212022-12-06 15:05:30 +0100823ifdef CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO_ENABLER
824# https://github.com/llvm/llvm-project/issues/44842
Olivier Deprez157378f2022-04-04 15:47:50 +0200825KBUILD_CFLAGS += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
826endif
Olivier Deprez92d4c212022-12-06 15:05:30 +0100827endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200828
829DEBUG_CFLAGS :=
830
831# Workaround for GCC versions < 5.0
832# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61801
833ifdef CONFIG_CC_IS_GCC
834DEBUG_CFLAGS += $(call cc-ifversion, -lt, 0500, $(call cc-option, -fno-var-tracking-assignments))
835endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000836
837ifdef CONFIG_DEBUG_INFO
Olivier Deprez157378f2022-04-04 15:47:50 +0200838
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839ifdef CONFIG_DEBUG_INFO_SPLIT
David Brazdil0f672f62019-12-10 10:32:29 +0000840DEBUG_CFLAGS += -gsplit-dwarf
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000841else
David Brazdil0f672f62019-12-10 10:32:29 +0000842DEBUG_CFLAGS += -g
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000843endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200844
Olivier Deprez92d4c212022-12-06 15:05:30 +0100845ifeq ($(LLVM_IAS),1)
846KBUILD_AFLAGS += -g
847else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000848KBUILD_AFLAGS += -Wa,-gdwarf-2
849endif
Olivier Deprez0e641232021-09-23 10:07:05 +0200850
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851ifdef CONFIG_DEBUG_INFO_DWARF4
David Brazdil0f672f62019-12-10 10:32:29 +0000852DEBUG_CFLAGS += -gdwarf-4
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000853endif
854
855ifdef CONFIG_DEBUG_INFO_REDUCED
David Brazdil0f672f62019-12-10 10:32:29 +0000856DEBUG_CFLAGS += $(call cc-option, -femit-struct-debug-baseonly) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000857 $(call cc-option,-fno-var-tracking)
858endif
859
Olivier Deprez157378f2022-04-04 15:47:50 +0200860ifdef CONFIG_DEBUG_INFO_COMPRESSED
861DEBUG_CFLAGS += -gz=zlib
862KBUILD_AFLAGS += -gz=zlib
863KBUILD_LDFLAGS += --compress-debug-sections=zlib
864endif
865
866endif # CONFIG_DEBUG_INFO
867
David Brazdil0f672f62019-12-10 10:32:29 +0000868KBUILD_CFLAGS += $(DEBUG_CFLAGS)
869export DEBUG_CFLAGS
870
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000871ifdef CONFIG_FUNCTION_TRACER
872ifdef CONFIG_FTRACE_MCOUNT_RECORD
873 # gcc 5 supports generating the mcount tables directly
874 ifeq ($(call cc-option-yn,-mrecord-mcount),y)
875 CC_FLAGS_FTRACE += -mrecord-mcount
876 export CC_USING_RECORD_MCOUNT := 1
877 endif
878 ifdef CONFIG_HAVE_NOP_MCOUNT
879 ifeq ($(call cc-option-yn, -mnop-mcount),y)
880 CC_FLAGS_FTRACE += -mnop-mcount
881 CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
882 endif
883 endif
884endif
885ifdef CONFIG_HAVE_FENTRY
886 ifeq ($(call cc-option-yn, -mfentry),y)
887 CC_FLAGS_FTRACE += -mfentry
888 CC_FLAGS_USING += -DCC_USING_FENTRY
889 endif
890endif
891export CC_FLAGS_FTRACE
892KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
893KBUILD_AFLAGS += $(CC_FLAGS_USING)
894ifdef CONFIG_DYNAMIC_FTRACE
895 ifdef CONFIG_HAVE_C_RECORDMCOUNT
896 BUILD_C_RECORDMCOUNT := y
897 export BUILD_C_RECORDMCOUNT
898 endif
899endif
900endif
901
902# We trigger additional mismatches with less inlining
903ifdef CONFIG_DEBUG_SECTION_MISMATCH
904KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
905endif
906
907ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
908KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
909LDFLAGS_vmlinux += --gc-sections
910endif
911
Olivier Deprez157378f2022-04-04 15:47:50 +0200912ifdef CONFIG_SHADOW_CALL_STACK
913CC_FLAGS_SCS := -fsanitize=shadow-call-stack
914KBUILD_CFLAGS += $(CC_FLAGS_SCS)
915export CC_FLAGS_SCS
916endif
917
918ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B
919KBUILD_CFLAGS += -falign-functions=32
David Brazdil0f672f62019-12-10 10:32:29 +0000920endif
921
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922# arch Makefile may override CC so keep this after arch Makefile is included
923NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
924
925# warn about C99 declaration after statement
David Brazdil0f672f62019-12-10 10:32:29 +0000926KBUILD_CFLAGS += -Wdeclaration-after-statement
927
928# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
929KBUILD_CFLAGS += -Wvla
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930
931# disable pointer signed / unsigned warnings in gcc 4.0
David Brazdil0f672f62019-12-10 10:32:29 +0000932KBUILD_CFLAGS += -Wno-pointer-sign
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933
934# disable stringop warnings in gcc 8+
935KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
936
Olivier Deprez0e641232021-09-23 10:07:05 +0200937# We'll want to enable this eventually, but it's not going away for 5.7 at least
938KBUILD_CFLAGS += $(call cc-disable-warning, zero-length-bounds)
939KBUILD_CFLAGS += $(call cc-disable-warning, array-bounds)
940KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow)
941
942# Another good warning that we'll want to enable eventually
943KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
944
945# Enabled with W=2, disabled by default as noisy
946KBUILD_CFLAGS += $(call cc-disable-warning, maybe-uninitialized)
947
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948# disable invalid "can't wrap" optimizations for signed / pointers
Olivier Deprez157378f2022-04-04 15:47:50 +0200949KBUILD_CFLAGS += -fno-strict-overflow
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000950
951# Make sure -fstack-check isn't enabled (like gentoo apparently did)
Olivier Deprez157378f2022-04-04 15:47:50 +0200952KBUILD_CFLAGS += -fno-stack-check
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953
954# conserve stack if available
955KBUILD_CFLAGS += $(call cc-option,-fconserve-stack)
956
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957# Prohibit date/time macros, which would make the build non-deterministic
Olivier Deprez157378f2022-04-04 15:47:50 +0200958KBUILD_CFLAGS += -Werror=date-time
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000959
960# enforce correct pointer usage
961KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
962
963# Require designated initializers for all marked structures
964KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
965
966# change __FILE__ to the relative path from the srctree
Olivier Deprez157378f2022-04-04 15:47:50 +0200967KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000968
Olivier Deprez157378f2022-04-04 15:47:50 +0200969# include additional Makefiles when needed
970include-y := scripts/Makefile.extrawarn
971include-$(CONFIG_KASAN) += scripts/Makefile.kasan
972include-$(CONFIG_KCSAN) += scripts/Makefile.kcsan
973include-$(CONFIG_UBSAN) += scripts/Makefile.ubsan
974include-$(CONFIG_KCOV) += scripts/Makefile.kcov
975include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins
976
977include $(addprefix $(srctree)/, $(include-y))
978
979# scripts/Makefile.gcc-plugins is intentionally included last.
980# Do not add $(call cc-option,...) below this line. When you build the kernel
981# from the clean source tree, the GCC plugins do not exist at this point.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982
David Brazdil0f672f62019-12-10 10:32:29 +0000983# Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
984KBUILD_CPPFLAGS += $(KCPPFLAGS)
985KBUILD_AFLAGS += $(KAFLAGS)
986KBUILD_CFLAGS += $(KCFLAGS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000987
Olivier Deprez157378f2022-04-04 15:47:50 +0200988KBUILD_LDFLAGS_MODULE += --build-id=sha1
989LDFLAGS_vmlinux += --build-id=sha1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000990
Olivier Deprez92d4c212022-12-06 15:05:30 +0100991KBUILD_LDFLAGS += -z noexecstack
992KBUILD_LDFLAGS += $(call ld-option,--no-warn-rwx-segments)
993
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994ifeq ($(CONFIG_STRIP_ASM_SYMS),y)
995LDFLAGS_vmlinux += $(call ld-option, -X,)
996endif
997
David Brazdil0f672f62019-12-10 10:32:29 +0000998ifeq ($(CONFIG_RELR),y)
Olivier Deprez0e641232021-09-23 10:07:05 +0200999LDFLAGS_vmlinux += --pack-dyn-relocs=relr --use-android-relr-tags
David Brazdil0f672f62019-12-10 10:32:29 +00001000endif
1001
Olivier Deprez157378f2022-04-04 15:47:50 +02001002# We never want expected sections to be placed heuristically by the
1003# linker. All sections should be explicitly named in the linker script.
1004ifdef CONFIG_LD_ORPHAN_WARN
1005LDFLAGS_vmlinux += --orphan-handling=warn
1006endif
1007
1008# Align the bit size of userspace programs with the kernel
1009KBUILD_USERCFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CFLAGS))
1010KBUILD_USERLDFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CFLAGS))
1011
David Brazdil0f672f62019-12-10 10:32:29 +00001012# make the checker run with the right architecture
1013CHECKFLAGS += --arch=$(ARCH)
1014
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001015# insure the checker run with the right endianness
1016CHECKFLAGS += $(if $(CONFIG_CPU_BIG_ENDIAN),-mbig-endian,-mlittle-endian)
1017
1018# the checker needs the correct machine size
1019CHECKFLAGS += $(if $(CONFIG_64BIT),-m64,-m32)
1020
1021# Default kernel image to build when no specific target is given.
1022# KBUILD_IMAGE may be overruled on the command line or
1023# set in the environment
1024# Also any assignments in arch/$(ARCH)/Makefile take precedence over
1025# this default value
1026export KBUILD_IMAGE ?= vmlinux
1027
1028#
1029# INSTALL_PATH specifies where to place the updated kernel and system map
1030# images. Default is /boot, but you can set it to other values
1031export INSTALL_PATH ?= /boot
1032
1033#
1034# INSTALL_DTBS_PATH specifies a prefix for relocations required by build roots.
1035# Like INSTALL_MOD_PATH, it isn't defined in the Makefile, but can be passed as
1036# an argument if needed. Otherwise it defaults to the kernel install path
1037#
1038export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
1039
1040#
1041# INSTALL_MOD_PATH specifies a prefix to MODLIB for module directory
1042# relocations required by build roots. This is not defined in the
1043# makefile but the argument can be passed to make if needed.
1044#
1045
1046MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
1047export MODLIB
1048
1049#
1050# INSTALL_MOD_STRIP, if defined, will cause modules to be
1051# stripped after they are installed. If INSTALL_MOD_STRIP is '1', then
1052# the default option --strip-debug will be used. Otherwise,
1053# INSTALL_MOD_STRIP value will be used as the options to the strip command.
1054
1055ifdef INSTALL_MOD_STRIP
1056ifeq ($(INSTALL_MOD_STRIP),1)
1057mod_strip_cmd = $(STRIP) --strip-debug
1058else
1059mod_strip_cmd = $(STRIP) $(INSTALL_MOD_STRIP)
1060endif # INSTALL_MOD_STRIP=1
1061else
1062mod_strip_cmd = true
1063endif # INSTALL_MOD_STRIP
1064export mod_strip_cmd
1065
1066# CONFIG_MODULE_COMPRESS, if defined, will cause module to be compressed
1067# after they are installed in agreement with CONFIG_MODULE_COMPRESS_GZIP
1068# or CONFIG_MODULE_COMPRESS_XZ.
1069
1070mod_compress_cmd = true
1071ifdef CONFIG_MODULE_COMPRESS
1072 ifdef CONFIG_MODULE_COMPRESS_GZIP
Olivier Deprez0e641232021-09-23 10:07:05 +02001073 mod_compress_cmd = $(KGZIP) -n -f
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001074 endif # CONFIG_MODULE_COMPRESS_GZIP
1075 ifdef CONFIG_MODULE_COMPRESS_XZ
Olivier Deprez0e641232021-09-23 10:07:05 +02001076 mod_compress_cmd = $(XZ) -f
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001077 endif # CONFIG_MODULE_COMPRESS_XZ
1078endif # CONFIG_MODULE_COMPRESS
1079export mod_compress_cmd
1080
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001081ifdef CONFIG_MODULE_SIG_ALL
1082$(eval $(call config_filename,MODULE_SIG_KEY))
1083
1084mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY) certs/signing_key.x509
1085else
1086mod_sign_cmd = true
1087endif
1088export mod_sign_cmd
1089
David Brazdil0f672f62019-12-10 10:32:29 +00001090HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf)
1091
Olivier Deprez157378f2022-04-04 15:47:50 +02001092has_libelf = $(call try-run,\
1093 echo "int main() {}" | $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)
1094
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095ifdef CONFIG_STACK_VALIDATION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001096 ifeq ($(has_libelf),1)
1097 objtool_target := tools/objtool FORCE
1098 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 SKIP_STACK_VALIDATION := 1
1100 export SKIP_STACK_VALIDATION
1101 endif
1102endif
1103
Olivier Deprez157378f2022-04-04 15:47:50 +02001104PHONY += resolve_btfids_clean
1105
1106resolve_btfids_O = $(abspath $(objtree))/tools/bpf/resolve_btfids
1107
1108# tools/bpf/resolve_btfids directory might not exist
1109# in output directory, skip its clean in that case
1110resolve_btfids_clean:
1111ifneq ($(wildcard $(resolve_btfids_O)),)
1112 $(Q)$(MAKE) -sC $(srctree)/tools/bpf/resolve_btfids O=$(resolve_btfids_O) clean
1113endif
1114
1115ifdef CONFIG_BPF
1116ifdef CONFIG_DEBUG_INFO_BTF
1117 ifeq ($(has_libelf),1)
1118 resolve_btfids_target := tools/bpf/resolve_btfids FORCE
1119 else
1120 ERROR_RESOLVE_BTFIDS := 1
1121 endif
1122endif # CONFIG_DEBUG_INFO_BTF
1123endif # CONFIG_BPF
1124
David Brazdil0f672f62019-12-10 10:32:29 +00001125PHONY += prepare0
1126
1127export MODORDER := $(extmod-prefix)modules.order
Olivier Deprez157378f2022-04-04 15:47:50 +02001128export MODULES_NSDEPS := $(extmod-prefix)modules.nsdeps
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129
1130ifeq ($(KBUILD_EXTMOD),)
1131core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/
1132
Olivier Deprez157378f2022-04-04 15:47:50 +02001133vmlinux-dirs := $(patsubst %/,%,$(filter %/, \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001134 $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
Olivier Deprez157378f2022-04-04 15:47:50 +02001135 $(libs-y) $(libs-m)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001136
David Brazdil0f672f62019-12-10 10:32:29 +00001137vmlinux-alldirs := $(sort $(vmlinux-dirs) Documentation \
Olivier Deprez157378f2022-04-04 15:47:50 +02001138 $(patsubst %/,%,$(filter %/, $(core-) \
1139 $(drivers-) $(libs-))))
1140
David Brazdil0f672f62019-12-10 10:32:29 +00001141build-dirs := $(vmlinux-dirs)
1142clean-dirs := $(vmlinux-alldirs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143
Olivier Deprez92d4c212022-12-06 15:05:30 +01001144subdir-modorder := $(addsuffix /modules.order, $(build-dirs))
1145
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001146# Externally visible symbols (used by link-vmlinux.sh)
Olivier Deprez157378f2022-04-04 15:47:50 +02001147KBUILD_VMLINUX_OBJS := $(head-y) $(patsubst %/,%/built-in.a, $(core-y))
1148KBUILD_VMLINUX_OBJS += $(addsuffix built-in.a, $(filter %/, $(libs-y)))
1149ifdef CONFIG_MODULES
1150KBUILD_VMLINUX_OBJS += $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)))
1151KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
1152else
1153KBUILD_VMLINUX_LIBS := $(patsubst %/,%/lib.a, $(libs-y))
1154endif
1155KBUILD_VMLINUX_OBJS += $(patsubst %/,%/built-in.a, $(drivers-y))
1156
1157export KBUILD_VMLINUX_OBJS KBUILD_VMLINUX_LIBS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001158export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
David Brazdil0f672f62019-12-10 10:32:29 +00001159# used by scripts/Makefile.package
1160export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) LICENSES arch include scripts tools)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001161
David Brazdil0f672f62019-12-10 10:32:29 +00001162vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001163
1164# Recurse until adjust_autoksyms.sh is satisfied
1165PHONY += autoksyms_recursive
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001166ifdef CONFIG_TRIM_UNUSED_KSYMS
Olivier Deprez157378f2022-04-04 15:47:50 +02001167# For the kernel to actually contain only the needed exported symbols,
1168# we have to build modules as well to determine what those symbols are.
1169# (this can be evaluated only once include/config/auto.conf has been included)
1170KBUILD_MODULES := 1
1171
David Brazdil0f672f62019-12-10 10:32:29 +00001172autoksyms_recursive: descend modules.order
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/adjust_autoksyms.sh \
Olivier Deprez92d4c212022-12-06 15:05:30 +01001174 "$(MAKE) -f $(srctree)/Makefile autoksyms_recursive"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001175endif
1176
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001177autoksyms_h := $(if $(CONFIG_TRIM_UNUSED_KSYMS), include/generated/autoksyms.h)
1178
Olivier Deprez157378f2022-04-04 15:47:50 +02001179quiet_cmd_autoksyms_h = GEN $@
1180 cmd_autoksyms_h = mkdir -p $(dir $@); \
1181 $(CONFIG_SHELL) $(srctree)/scripts/gen_autoksyms.sh $@
1182
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001183$(autoksyms_h):
Olivier Deprez157378f2022-04-04 15:47:50 +02001184 $(call cmd,autoksyms_h)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001185
1186ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
1187
1188# Final link of vmlinux with optional arch pass after final link
1189cmd_link-vmlinux = \
Olivier Deprez157378f2022-04-04 15:47:50 +02001190 $(CONFIG_SHELL) $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)"; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
1192
1193vmlinux: scripts/link-vmlinux.sh autoksyms_recursive $(vmlinux-deps) FORCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001194 +$(call if_changed,link-vmlinux)
1195
David Brazdil0f672f62019-12-10 10:32:29 +00001196targets := vmlinux
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001197
1198# The actual objects are generated when descending,
1199# make sure no implicit rule kicks in
Olivier Deprez157378f2022-04-04 15:47:50 +02001200$(sort $(vmlinux-deps) $(subdir-modorder)): descend ;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001201
David Brazdil0f672f62019-12-10 10:32:29 +00001202filechk_kernel.release = \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001203 echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204
1205# Store (new) KERNELRELEASE string in include/config/kernel.release
David Brazdil0f672f62019-12-10 10:32:29 +00001206include/config/kernel.release: FORCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001207 $(call filechk,kernel.release)
1208
1209# Additional helpers built in scripts/
1210# Carefully list dependencies so we do not try to build scripts twice
1211# in parallel
1212PHONY += scripts
David Brazdil0f672f62019-12-10 10:32:29 +00001213scripts: scripts_basic scripts_dtc
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001214 $(Q)$(MAKE) $(build)=$(@)
1215
1216# Things we need to do before we recursively start building the kernel
1217# or the modules are listed in "prepare".
1218# A multi level approach is used. prepareN is processed before prepareN-1.
1219# archprepare is used in arch Makefiles and when processed asm symlink,
1220# version.h and scripts_basic is processed / created.
1221
David Brazdil0f672f62019-12-10 10:32:29 +00001222PHONY += prepare archprepare
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001223
David Brazdil0f672f62019-12-10 10:32:29 +00001224archprepare: outputmakefile archheaders archscripts scripts include/config/kernel.release \
Olivier Deprez157378f2022-04-04 15:47:50 +02001225 asm-generic $(version_h) $(autoksyms_h) include/generated/utsrelease.h \
1226 include/generated/autoconf.h
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001227
David Brazdil0f672f62019-12-10 10:32:29 +00001228prepare0: archprepare
1229 $(Q)$(MAKE) $(build)=scripts/mod
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001230 $(Q)$(MAKE) $(build)=.
1231
1232# All the preparing..
Olivier Deprez157378f2022-04-04 15:47:50 +02001233prepare: prepare0 prepare-objtool prepare-resolve_btfids
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001234
1235# Support for using generic headers in asm-generic
David Brazdil0f672f62019-12-10 10:32:29 +00001236asm-generic := -f $(srctree)/scripts/Makefile.asm-generic obj
1237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238PHONY += asm-generic uapi-asm-generic
1239asm-generic: uapi-asm-generic
David Brazdil0f672f62019-12-10 10:32:29 +00001240 $(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/asm \
1241 generic=include/asm-generic
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001242uapi-asm-generic:
David Brazdil0f672f62019-12-10 10:32:29 +00001243 $(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/uapi/asm \
1244 generic=include/uapi/asm-generic
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245
Olivier Deprez157378f2022-04-04 15:47:50 +02001246PHONY += prepare-objtool prepare-resolve_btfids
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001247prepare-objtool: $(objtool_target)
David Brazdil0f672f62019-12-10 10:32:29 +00001248ifeq ($(SKIP_STACK_VALIDATION),1)
1249ifdef CONFIG_UNWINDER_ORC
1250 @echo "error: Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
1251 @false
1252else
1253 @echo "warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
1254endif
1255endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001256
Olivier Deprez157378f2022-04-04 15:47:50 +02001257prepare-resolve_btfids: $(resolve_btfids_target)
1258ifeq ($(ERROR_RESOLVE_BTFIDS),1)
1259 @echo "error: Cannot resolve BTF IDs for CONFIG_DEBUG_INFO_BTF, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
1260 @false
1261endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001262# Generate some files
1263# ---------------------------------------------------------------------------
1264
1265# KERNELRELEASE can change from a few different places, meaning version.h
1266# needs to be updated, so this check is forced on all builds
1267
1268uts_len := 64
1269define filechk_utsrelease.h
1270 if [ `echo -n "$(KERNELRELEASE)" | wc -c ` -gt $(uts_len) ]; then \
1271 echo '"$(KERNELRELEASE)" exceeds $(uts_len) characters' >&2; \
1272 exit 1; \
1273 fi; \
David Brazdil0f672f62019-12-10 10:32:29 +00001274 echo \#define UTS_RELEASE \"$(KERNELRELEASE)\"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001275endef
1276
1277define filechk_version.h
Olivier Deprez0e641232021-09-23 10:07:05 +02001278 if [ $(SUBLEVEL) -gt 255 ]; then \
1279 echo \#define LINUX_VERSION_CODE $(shell \
1280 expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + 255); \
1281 else \
1282 echo \#define LINUX_VERSION_CODE $(shell \
1283 expr $(VERSION) \* 65536 + $(PATCHLEVEL) \* 256 + $(SUBLEVEL)); \
1284 fi; \
1285 echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + \
1286 ((c) > 255 ? 255 : (c)))'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001287endef
1288
Olivier Deprez0e641232021-09-23 10:07:05 +02001289$(version_h): PATCHLEVEL := $(if $(PATCHLEVEL), $(PATCHLEVEL), 0)
1290$(version_h): SUBLEVEL := $(if $(SUBLEVEL), $(SUBLEVEL), 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291$(version_h): FORCE
1292 $(call filechk,version.h)
1293 $(Q)rm -f $(old_version_h)
1294
1295include/generated/utsrelease.h: include/config/kernel.release FORCE
1296 $(call filechk,utsrelease.h)
1297
1298PHONY += headerdep
1299headerdep:
1300 $(Q)find $(srctree)/include/ -name '*.h' | xargs --max-args 1 \
1301 $(srctree)/scripts/headerdep.pl -I$(srctree)/include
1302
1303# ---------------------------------------------------------------------------
1304# Kernel headers
1305
1306#Default location for installed headers
1307export INSTALL_HDR_PATH = $(objtree)/usr
1308
David Brazdil0f672f62019-12-10 10:32:29 +00001309quiet_cmd_headers_install = INSTALL $(INSTALL_HDR_PATH)/include
1310 cmd_headers_install = \
1311 mkdir -p $(INSTALL_HDR_PATH); \
1312 rsync -mrl --include='*/' --include='*\.h' --exclude='*' \
1313 usr/include $(INSTALL_HDR_PATH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314
1315PHONY += headers_install
David Brazdil0f672f62019-12-10 10:32:29 +00001316headers_install: headers
1317 $(call cmd,headers_install)
1318
1319PHONY += archheaders archscripts
1320
1321hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
1322
1323PHONY += headers
1324headers: $(version_h) scripts_unifdef uapi-asm-generic archheaders archscripts
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001325 $(if $(wildcard $(srctree)/arch/$(SRCARCH)/include/uapi/asm/Kbuild),, \
1326 $(error Headers not exportable for the $(SRCARCH) architecture))
David Brazdil0f672f62019-12-10 10:32:29 +00001327 $(Q)$(MAKE) $(hdr-inst)=include/uapi
1328 $(Q)$(MAKE) $(hdr-inst)=arch/$(SRCARCH)/include/uapi
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001329
Olivier Deprez0e641232021-09-23 10:07:05 +02001330# Deprecated. It is no-op now.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001331PHONY += headers_check
Olivier Deprez0e641232021-09-23 10:07:05 +02001332headers_check:
1333 @:
David Brazdil0f672f62019-12-10 10:32:29 +00001334
1335ifdef CONFIG_HEADERS_INSTALL
1336prepare: headers
1337endif
1338
David Brazdil0f672f62019-12-10 10:32:29 +00001339PHONY += scripts_unifdef
1340scripts_unifdef: scripts_basic
1341 $(Q)$(MAKE) $(build)=scripts scripts/unifdef
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001342
1343# ---------------------------------------------------------------------------
1344# Kernel selftest
1345
1346PHONY += kselftest
1347kselftest:
1348 $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests
1349
David Brazdil0f672f62019-12-10 10:32:29 +00001350kselftest-%: FORCE
1351 $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001352
1353PHONY += kselftest-merge
1354kselftest-merge:
1355 $(if $(wildcard $(objtree)/.config),, $(error No .config exists, config your kernel first!))
David Brazdil0f672f62019-12-10 10:32:29 +00001356 $(Q)find $(srctree)/tools/testing/selftests -name config | \
1357 xargs $(srctree)/scripts/kconfig/merge_config.sh -m $(objtree)/.config
1358 $(Q)$(MAKE) -f $(srctree)/Makefile olddefconfig
1359
1360# ---------------------------------------------------------------------------
1361# Devicetree files
1362
1363ifneq ($(wildcard $(srctree)/arch/$(SRCARCH)/boot/dts/),)
1364dtstree := arch/$(SRCARCH)/boot/dts
1365endif
1366
1367ifneq ($(dtstree),)
1368
1369%.dtb: include/config/kernel.release scripts_dtc
1370 $(Q)$(MAKE) $(build)=$(dtstree) $(dtstree)/$@
1371
Olivier Deprez0e641232021-09-23 10:07:05 +02001372PHONY += dtbs dtbs_install dtbs_check
1373dtbs: include/config/kernel.release scripts_dtc
David Brazdil0f672f62019-12-10 10:32:29 +00001374 $(Q)$(MAKE) $(build)=$(dtstree)
1375
Olivier Deprez0e641232021-09-23 10:07:05 +02001376ifneq ($(filter dtbs_check, $(MAKECMDGOALS)),)
Olivier Deprez157378f2022-04-04 15:47:50 +02001377export CHECK_DTBS=y
Olivier Deprez0e641232021-09-23 10:07:05 +02001378dtbs: dt_binding_check
1379endif
1380
Olivier Deprez0e641232021-09-23 10:07:05 +02001381dtbs_check: dtbs
David Brazdil0f672f62019-12-10 10:32:29 +00001382
1383dtbs_install:
Olivier Deprez157378f2022-04-04 15:47:50 +02001384 $(Q)$(MAKE) $(dtbinst)=$(dtstree) dst=$(INSTALL_DTBS_PATH)
David Brazdil0f672f62019-12-10 10:32:29 +00001385
1386ifdef CONFIG_OF_EARLY_FLATTREE
1387all: dtbs
1388endif
1389
1390endif
1391
1392PHONY += scripts_dtc
1393scripts_dtc: scripts_basic
1394 $(Q)$(MAKE) $(build)=scripts/dtc
1395
Olivier Deprez157378f2022-04-04 15:47:50 +02001396ifneq ($(filter dt_binding_check, $(MAKECMDGOALS)),)
1397export CHECK_DT_BINDING=y
1398endif
1399
Olivier Deprez0e641232021-09-23 10:07:05 +02001400PHONY += dt_binding_check
David Brazdil0f672f62019-12-10 10:32:29 +00001401dt_binding_check: scripts_dtc
1402 $(Q)$(MAKE) $(build)=Documentation/devicetree/bindings
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001403
1404# ---------------------------------------------------------------------------
1405# Modules
1406
1407ifdef CONFIG_MODULES
1408
1409# By default, build modules as well
1410
1411all: modules
1412
Olivier Deprez0e641232021-09-23 10:07:05 +02001413# When we're building modules with modversions, we need to consider
1414# the built-in objects during the descend as well, in order to
1415# make sure the checksums are up to date before we record them.
1416ifdef CONFIG_MODVERSIONS
1417 KBUILD_BUILTIN := 1
1418endif
1419
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001420# Build modules
1421#
1422# A module can be listed more than once in obj-m resulting in
1423# duplicate lines in modules.order files. Those are removed
1424# using awk while concatenating to the final file.
1425
1426PHONY += modules
Olivier Deprez157378f2022-04-04 15:47:50 +02001427modules: $(if $(KBUILD_BUILTIN),vmlinux) modules_check modules_prepare
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001428 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1429
Olivier Deprez157378f2022-04-04 15:47:50 +02001430PHONY += modules_check
1431modules_check: modules.order
1432 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/modules-check.sh $<
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433
Olivier Deprez157378f2022-04-04 15:47:50 +02001434cmd_modules_order = $(AWK) '!x[$$0]++' $(real-prereqs) > $@
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435
Olivier Deprez157378f2022-04-04 15:47:50 +02001436modules.order: $(subdir-modorder) FORCE
1437 $(call if_changed,modules_order)
David Brazdil0f672f62019-12-10 10:32:29 +00001438
Olivier Deprez157378f2022-04-04 15:47:50 +02001439targets += modules.order
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001440
1441# Target to prepare building external modules
1442PHONY += modules_prepare
David Brazdil0f672f62019-12-10 10:32:29 +00001443modules_prepare: prepare
Olivier Deprez157378f2022-04-04 15:47:50 +02001444 $(Q)$(MAKE) $(build)=scripts scripts/module.lds
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001445
1446# Target to install modules
1447PHONY += modules_install
1448modules_install: _modinst_ _modinst_post
1449
1450PHONY += _modinst_
1451_modinst_:
1452 @rm -rf $(MODLIB)/kernel
1453 @rm -f $(MODLIB)/source
1454 @mkdir -p $(MODLIB)/kernel
1455 @ln -s $(abspath $(srctree)) $(MODLIB)/source
1456 @if [ ! $(objtree) -ef $(MODLIB)/build ]; then \
1457 rm -f $(MODLIB)/build ; \
1458 ln -s $(CURDIR) $(MODLIB)/build ; \
1459 fi
David Brazdil0f672f62019-12-10 10:32:29 +00001460 @sed 's:^:kernel/:' modules.order > $(MODLIB)/modules.order
Olivier Deprez157378f2022-04-04 15:47:50 +02001461 @cp -f modules.builtin $(MODLIB)/
David Brazdil0f672f62019-12-10 10:32:29 +00001462 @cp -f $(objtree)/modules.builtin.modinfo $(MODLIB)/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001463 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst
1464
1465# This depmod is only for convenience to give the initial
1466# boot a modules.dep even before / is mounted read-write. However the
1467# boot script depmod is the master version.
1468PHONY += _modinst_post
1469_modinst_post: _modinst_
1470 $(call cmd,depmod)
1471
1472ifeq ($(CONFIG_MODULE_SIG), y)
1473PHONY += modules_sign
1474modules_sign:
1475 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modsign
1476endif
1477
1478else # CONFIG_MODULES
1479
1480# Modules not configured
1481# ---------------------------------------------------------------------------
1482
1483PHONY += modules modules_install
1484modules modules_install:
1485 @echo >&2
1486 @echo >&2 "The present kernel configuration has modules disabled."
1487 @echo >&2 "Type 'make config' and enable loadable module support."
1488 @echo >&2 "Then build a kernel with module support enabled."
1489 @echo >&2
1490 @exit 1
1491
1492endif # CONFIG_MODULES
1493
1494###
1495# Cleaning is done on three levels.
1496# make clean Delete most generated files
1497# Leave enough to build external modules
1498# make mrproper Delete the current configuration, and all generated files
1499# make distclean Remove editor backup files, patch leftover files and the like
1500
1501# Directories & files removed with 'make clean'
Olivier Deprez157378f2022-04-04 15:47:50 +02001502CLEAN_FILES += include/ksym vmlinux.symvers modules-only.symvers \
1503 modules.builtin modules.builtin.modinfo modules.nsdeps \
1504 compile_commands.json
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001505
1506# Directories & files removed with 'make mrproper'
Olivier Deprez157378f2022-04-04 15:47:50 +02001507MRPROPER_FILES += include/config include/generated \
David Brazdil0f672f62019-12-10 10:32:29 +00001508 arch/$(SRCARCH)/include/generated .tmp_objdiff \
Olivier Deprez157378f2022-04-04 15:47:50 +02001509 debian snap tar-install \
1510 .config .config.old .version \
David Brazdil0f672f62019-12-10 10:32:29 +00001511 Module.symvers \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001512 signing_key.pem signing_key.priv signing_key.x509 \
1513 x509.genkey extra_certificates signing_key.x509.keyid \
David Brazdil0f672f62019-12-10 10:32:29 +00001514 signing_key.x509.signer vmlinux-gdb.py \
1515 *.spec
1516
1517# Directories & files removed with 'make distclean'
David Brazdil0f672f62019-12-10 10:32:29 +00001518DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001519
1520# clean - Delete most, but leave enough to build external modules
1521#
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001522clean: rm-files := $(CLEAN_FILES)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001523
David Brazdil0f672f62019-12-10 10:32:29 +00001524PHONY += archclean vmlinuxclean
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001525
1526vmlinuxclean:
1527 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-vmlinux.sh clean
1528 $(Q)$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) clean)
1529
Olivier Deprez157378f2022-04-04 15:47:50 +02001530clean: archclean vmlinuxclean resolve_btfids_clean
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001531
1532# mrproper - Delete all generated files, including .config
1533#
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001534mrproper: rm-files := $(wildcard $(MRPROPER_FILES))
1535mrproper-dirs := $(addprefix _mrproper_,scripts)
1536
David Brazdil0f672f62019-12-10 10:32:29 +00001537PHONY += $(mrproper-dirs) mrproper
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001538$(mrproper-dirs):
1539 $(Q)$(MAKE) $(clean)=$(patsubst _mrproper_%,%,$@)
1540
David Brazdil0f672f62019-12-10 10:32:29 +00001541mrproper: clean $(mrproper-dirs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001542 $(call cmd,rmfiles)
1543
1544# distclean
1545#
David Brazdil0f672f62019-12-10 10:32:29 +00001546distclean: rm-files := $(wildcard $(DISTCLEAN_FILES))
1547
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001548PHONY += distclean
1549
1550distclean: mrproper
David Brazdil0f672f62019-12-10 10:32:29 +00001551 $(call cmd,rmfiles)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001552 @find $(srctree) $(RCS_FIND_IGNORE) \
1553 \( -name '*.orig' -o -name '*.rej' -o -name '*~' \
1554 -o -name '*.bak' -o -name '#*#' -o -name '*%' \
1555 -o -name 'core' \) \
1556 -type f -print | xargs rm -f
1557
1558
1559# Packaging of the kernel to various formats
1560# ---------------------------------------------------------------------------
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001561
1562%src-pkg: FORCE
David Brazdil0f672f62019-12-10 10:32:29 +00001563 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001564%pkg: include/config/kernel.release FORCE
David Brazdil0f672f62019-12-10 10:32:29 +00001565 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001566
1567# Brief documentation of the typical targets used
1568# ---------------------------------------------------------------------------
1569
1570boards := $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*_defconfig)
1571boards := $(sort $(notdir $(boards)))
1572board-dirs := $(dir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/*/*_defconfig))
1573board-dirs := $(sort $(notdir $(board-dirs:/=)))
1574
1575PHONY += help
1576help:
1577 @echo 'Cleaning targets:'
1578 @echo ' clean - Remove most generated files but keep the config and'
1579 @echo ' enough build support to build external modules'
1580 @echo ' mrproper - Remove all generated files + config + various backup files'
1581 @echo ' distclean - mrproper + remove editor backup and patch files'
1582 @echo ''
1583 @echo 'Configuration targets:'
1584 @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
1585 @echo ''
1586 @echo 'Other generic targets:'
1587 @echo ' all - Build all targets marked with [*]'
1588 @echo '* vmlinux - Build the bare kernel'
1589 @echo '* modules - Build all modules'
1590 @echo ' modules_install - Install all modules to INSTALL_MOD_PATH (default: /)'
1591 @echo ' dir/ - Build all files in dir and below'
1592 @echo ' dir/file.[ois] - Build specified target only'
1593 @echo ' dir/file.ll - Build the LLVM assembly file'
1594 @echo ' (requires compiler support for LLVM assembly generation)'
1595 @echo ' dir/file.lst - Build specified mixed source/assembly target only'
1596 @echo ' (requires a recent binutils and recent build (System.map))'
1597 @echo ' dir/file.ko - Build module including final link'
1598 @echo ' modules_prepare - Set up for building external modules'
1599 @echo ' tags/TAGS - Generate tags file for editors'
1600 @echo ' cscope - Generate cscope index'
1601 @echo ' gtags - Generate GNU GLOBAL index'
1602 @echo ' kernelrelease - Output the release version string (use with make -s)'
1603 @echo ' kernelversion - Output the version stored in Makefile (use with make -s)'
1604 @echo ' image_name - Output the image name (use with make -s)'
1605 @echo ' headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH'; \
1606 echo ' (default: $(INSTALL_HDR_PATH))'; \
1607 echo ''
1608 @echo 'Static analysers:'
1609 @echo ' checkstack - Generate a list of stack hogs'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001610 @echo ' versioncheck - Sanity check on version.h usage'
1611 @echo ' includecheck - Check for duplicate included header files'
1612 @echo ' export_report - List the usages of all exported symbols'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001613 @echo ' headerdep - Detect inclusion cycles in headers'
1614 @echo ' coccicheck - Check with Coccinelle'
Olivier Deprez157378f2022-04-04 15:47:50 +02001615 @echo ' clang-analyzer - Check with clang static analyzer'
1616 @echo ' clang-tidy - Check with clang-tidy'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001617 @echo ''
David Brazdil0f672f62019-12-10 10:32:29 +00001618 @echo 'Tools:'
1619 @echo ' nsdeps - Generate missing symbol namespace dependencies'
1620 @echo ''
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001621 @echo 'Kernel selftest:'
Olivier Deprez157378f2022-04-04 15:47:50 +02001622 @echo ' kselftest - Build and run kernel selftest'
1623 @echo ' Build, install, and boot kernel before'
1624 @echo ' running kselftest on it'
1625 @echo ' Run as root for full coverage'
1626 @echo ' kselftest-all - Build kernel selftest'
1627 @echo ' kselftest-install - Build and install kernel selftest'
1628 @echo ' kselftest-clean - Remove all generated kselftest files'
1629 @echo ' kselftest-merge - Merge all the config dependencies of'
1630 @echo ' kselftest to existing .config.'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001631 @echo ''
David Brazdil0f672f62019-12-10 10:32:29 +00001632 @$(if $(dtstree), \
1633 echo 'Devicetree:'; \
1634 echo '* dtbs - Build device tree blobs for enabled boards'; \
1635 echo ' dtbs_install - Install dtbs to $(INSTALL_DTBS_PATH)'; \
1636 echo ' dt_binding_check - Validate device tree binding documents'; \
1637 echo ' dtbs_check - Validate device tree source files';\
1638 echo '')
1639
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001640 @echo 'Userspace tools targets:'
1641 @echo ' use "make tools/help"'
1642 @echo ' or "cd tools; make help"'
1643 @echo ''
1644 @echo 'Kernel packaging:'
David Brazdil0f672f62019-12-10 10:32:29 +00001645 @$(MAKE) -f $(srctree)/scripts/Makefile.package help
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646 @echo ''
1647 @echo 'Documentation targets:'
1648 @$(MAKE) -f $(srctree)/Documentation/Makefile dochelp
1649 @echo ''
1650 @echo 'Architecture specific targets ($(SRCARCH)):'
1651 @$(if $(archhelp),$(archhelp),\
1652 echo ' No architecture specific help defined for $(SRCARCH)')
1653 @echo ''
1654 @$(if $(boards), \
1655 $(foreach b, $(boards), \
Olivier Deprez157378f2022-04-04 15:47:50 +02001656 printf " %-27s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001657 echo '')
1658 @$(if $(board-dirs), \
1659 $(foreach b, $(board-dirs), \
1660 printf " %-16s - Show %s-specific targets\\n" help-$(b) $(b);) \
1661 printf " %-16s - Show all of the above\\n" help-boards; \
1662 echo '')
1663
1664 @echo ' make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
1665 @echo ' make V=2 [targets] 2 => give reason for rebuild of target'
1666 @echo ' make O=dir [targets] Locate all output files in "dir", including .config'
Olivier Deprez157378f2022-04-04 15:47:50 +02001667 @echo ' make C=1 [targets] Check re-compiled c source with $$CHECK'
1668 @echo ' (sparse by default)'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001669 @echo ' make C=2 [targets] Force check of all c source with $$CHECK'
1670 @echo ' make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections'
David Brazdil0f672f62019-12-10 10:32:29 +00001671 @echo ' make W=n [targets] Enable extra build checks, n=1,2,3 where'
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001672 @echo ' 1: warnings which may be relevant and do not occur too often'
1673 @echo ' 2: warnings which occur quite often but may still be relevant'
1674 @echo ' 3: more obscure warnings, can most likely be ignored'
1675 @echo ' Multiple levels can be combined with W=12 or W=123'
1676 @echo ''
1677 @echo 'Execute "make" or "make all" to build all targets marked with [*] '
1678 @echo 'For further info see the ./README file'
1679
1680
1681help-board-dirs := $(addprefix help-,$(board-dirs))
1682
1683help-boards: $(help-board-dirs)
1684
1685boards-per-dir = $(sort $(notdir $(wildcard $(srctree)/arch/$(SRCARCH)/configs/$*/*_defconfig)))
1686
1687$(help-board-dirs): help-%:
1688 @echo 'Architecture specific targets ($(SRCARCH) $*):'
1689 @$(if $(boards-per-dir), \
1690 $(foreach b, $(boards-per-dir), \
1691 printf " %-24s - Build for %s\\n" $*/$(b) $(subst _defconfig,,$(b));) \
1692 echo '')
1693
1694
1695# Documentation targets
1696# ---------------------------------------------------------------------------
1697DOC_TARGETS := xmldocs latexdocs pdfdocs htmldocs epubdocs cleandocs \
1698 linkcheckdocs dochelp refcheckdocs
1699PHONY += $(DOC_TARGETS)
David Brazdil0f672f62019-12-10 10:32:29 +00001700$(DOC_TARGETS):
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001701 $(Q)$(MAKE) $(build)=Documentation $@
1702
David Brazdil0f672f62019-12-10 10:32:29 +00001703# Misc
1704# ---------------------------------------------------------------------------
1705
1706PHONY += scripts_gdb
1707scripts_gdb: prepare0
1708 $(Q)$(MAKE) $(build)=scripts/gdb
1709 $(Q)ln -fsn $(abspath $(srctree)/scripts/gdb/vmlinux-gdb.py)
1710
1711ifdef CONFIG_GDB_SCRIPTS
1712all: scripts_gdb
1713endif
1714
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001715else # KBUILD_EXTMOD
1716
1717###
1718# External module support.
1719# When building external modules the kernel used as basis is considered
1720# read-only, and no consistency checks are made and the make
1721# system is not used on the basis kernel. If updates are required
1722# in the basis kernel ordinary make commands (without M=...) must
1723# be used.
1724#
1725# The following are the only valid targets when building external
1726# modules.
1727# make M=dir clean Delete all automatically generated files
1728# make M=dir modules Make all modules in specified dir
1729# make M=dir Same as 'make M=dir modules'
1730# make M=dir modules_install
1731# Install the modules built in the module directory
1732# Assumes install directory is already created
1733
Olivier Deprez157378f2022-04-04 15:47:50 +02001734# We are always building only modules.
1735KBUILD_BUILTIN :=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001736KBUILD_MODULES := 1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001737
David Brazdil0f672f62019-12-10 10:32:29 +00001738build-dirs := $(KBUILD_EXTMOD)
1739PHONY += modules
Olivier Deprez157378f2022-04-04 15:47:50 +02001740modules: $(MODORDER)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001741 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1742
Olivier Deprez157378f2022-04-04 15:47:50 +02001743$(MODORDER): descend
1744 @:
1745
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001746PHONY += modules_install
1747modules_install: _emodinst_ _emodinst_post
1748
1749install-dir := $(if $(INSTALL_MOD_DIR),$(INSTALL_MOD_DIR),extra)
1750PHONY += _emodinst_
1751_emodinst_:
1752 $(Q)mkdir -p $(MODLIB)/$(install-dir)
1753 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst
1754
1755PHONY += _emodinst_post
1756_emodinst_post: _emodinst_
1757 $(call cmd,depmod)
1758
Olivier Deprez157378f2022-04-04 15:47:50 +02001759compile_commands.json: $(extmod-prefix)compile_commands.json
1760PHONY += compile_commands.json
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001761
Olivier Deprez157378f2022-04-04 15:47:50 +02001762clean-dirs := $(KBUILD_EXTMOD)
1763clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
1764 $(KBUILD_EXTMOD)/compile_commands.json
David Brazdil0f672f62019-12-10 10:32:29 +00001765
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001766PHONY += help
1767help:
1768 @echo ' Building external modules.'
1769 @echo ' Syntax: make -C path/to/kernel/src M=$$PWD target'
1770 @echo ''
1771 @echo ' modules - default target, build the module(s)'
1772 @echo ' modules_install - install the module'
1773 @echo ' clean - remove generated files in module directory only'
1774 @echo ''
1775
Olivier Deprez157378f2022-04-04 15:47:50 +02001776# no-op for external module builds
1777PHONY += prepare modules_prepare
1778
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001779endif # KBUILD_EXTMOD
1780
Olivier Deprez0e641232021-09-23 10:07:05 +02001781# Single targets
1782# ---------------------------------------------------------------------------
1783# To build individual files in subdirectories, you can do like this:
1784#
1785# make foo/bar/baz.s
1786#
1787# The supported suffixes for single-target are listed in 'single-targets'
1788#
1789# To build only under specific subdirectories, you can do like this:
1790#
1791# make foo/bar/baz/
1792
1793ifdef single-build
1794
1795# .ko is special because modpost is needed
1796single-ko := $(sort $(filter %.ko, $(MAKECMDGOALS)))
1797single-no-ko := $(sort $(patsubst %.ko,%.mod, $(MAKECMDGOALS)))
1798
1799$(single-ko): single_modpost
1800 @:
1801$(single-no-ko): descend
1802 @:
1803
1804ifeq ($(KBUILD_EXTMOD),)
1805# For the single build of in-tree modules, use a temporary file to avoid
1806# the situation of modules_install installing an invalid modules.order.
1807MODORDER := .modules.tmp
1808endif
1809
1810PHONY += single_modpost
Olivier Deprez157378f2022-04-04 15:47:50 +02001811single_modpost: $(single-no-ko) modules_prepare
Olivier Deprez0e641232021-09-23 10:07:05 +02001812 $(Q){ $(foreach m, $(single-ko), echo $(extmod-prefix)$m;) } > $(MODORDER)
1813 $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
1814
1815KBUILD_MODULES := 1
1816
1817export KBUILD_SINGLE_TARGETS := $(addprefix $(extmod-prefix), $(single-no-ko))
1818
1819# trim unrelated directories
1820build-dirs := $(foreach d, $(build-dirs), \
1821 $(if $(filter $(d)/%, $(KBUILD_SINGLE_TARGETS)), $(d)))
1822
1823endif
1824
Olivier Deprez157378f2022-04-04 15:47:50 +02001825ifndef CONFIG_MODULES
1826KBUILD_MODULES :=
1827endif
1828
David Brazdil0f672f62019-12-10 10:32:29 +00001829# Handle descending into subdirectories listed in $(build-dirs)
1830# Preset locale variables to speed up the build process. Limit locale
1831# tweaks to this spot to avoid wrong language settings when running
1832# make menuconfig etc.
1833# Error messages still appears in the original language
1834PHONY += descend $(build-dirs)
1835descend: $(build-dirs)
1836$(build-dirs): prepare
Olivier Deprez0e641232021-09-23 10:07:05 +02001837 $(Q)$(MAKE) $(build)=$@ \
Olivier Deprez157378f2022-04-04 15:47:50 +02001838 single-build=$(if $(filter-out $@/, $(filter $@/%, $(KBUILD_SINGLE_TARGETS))),1) \
Olivier Deprez0e641232021-09-23 10:07:05 +02001839 need-builtin=1 need-modorder=1
David Brazdil0f672f62019-12-10 10:32:29 +00001840
1841clean-dirs := $(addprefix _clean_, $(clean-dirs))
1842PHONY += $(clean-dirs) clean
1843$(clean-dirs):
1844 $(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
1845
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846clean: $(clean-dirs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001847 $(call cmd,rmfiles)
1848 @find $(if $(KBUILD_EXTMOD), $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \
1849 \( -name '*.[aios]' -o -name '*.ko' -o -name '.*.cmd' \
David Brazdil0f672f62019-12-10 10:32:29 +00001850 -o -name '*.ko.*' \
1851 -o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001852 -o -name '*.dwo' -o -name '*.lst' \
Olivier Deprez157378f2022-04-04 15:47:50 +02001853 -o -name '*.su' -o -name '*.mod' \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854 -o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
1855 -o -name '*.lex.c' -o -name '*.tab.[ch]' \
1856 -o -name '*.asn1.[ch]' \
1857 -o -name '*.symtypes' -o -name 'modules.order' \
Olivier Deprez157378f2022-04-04 15:47:50 +02001858 -o -name '.tmp_*.o.*' \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859 -o -name '*.c.[012]*.*' \
1860 -o -name '*.ll' \
1861 -o -name '*.gcno' \) -type f -print | xargs rm -f
1862
1863# Generate tags for editors
1864# ---------------------------------------------------------------------------
1865quiet_cmd_tags = GEN $@
David Brazdil0f672f62019-12-10 10:32:29 +00001866 cmd_tags = $(BASH) $(srctree)/scripts/tags.sh $@
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867
1868tags TAGS cscope gtags: FORCE
1869 $(call cmd,tags)
1870
David Brazdil0f672f62019-12-10 10:32:29 +00001871# Script to generate missing namespace dependencies
1872# ---------------------------------------------------------------------------
1873
1874PHONY += nsdeps
Olivier Deprez157378f2022-04-04 15:47:50 +02001875nsdeps: export KBUILD_NSDEPS=1
David Brazdil0f672f62019-12-10 10:32:29 +00001876nsdeps: modules
Olivier Deprez157378f2022-04-04 15:47:50 +02001877 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/nsdeps
1878
1879# Clang Tooling
1880# ---------------------------------------------------------------------------
1881
1882quiet_cmd_gen_compile_commands = GEN $@
1883 cmd_gen_compile_commands = $(PYTHON3) $< -a $(AR) -o $@ $(filter-out $<, $(real-prereqs))
1884
1885$(extmod-prefix)compile_commands.json: scripts/clang-tools/gen_compile_commands.py \
1886 $(if $(KBUILD_EXTMOD),,$(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)) \
1887 $(if $(CONFIG_MODULES), $(MODORDER)) FORCE
1888 $(call if_changed,gen_compile_commands)
1889
1890targets += $(extmod-prefix)compile_commands.json
1891
1892PHONY += clang-tidy clang-analyzer
1893
1894ifdef CONFIG_CC_IS_CLANG
1895quiet_cmd_clang_tools = CHECK $<
1896 cmd_clang_tools = $(PYTHON3) $(srctree)/scripts/clang-tools/run-clang-tools.py $@ $<
1897
1898clang-tidy clang-analyzer: $(extmod-prefix)compile_commands.json
1899 $(call cmd,clang_tools)
1900else
1901clang-tidy clang-analyzer:
1902 @echo "$@ requires CC=clang" >&2
1903 @false
1904endif
David Brazdil0f672f62019-12-10 10:32:29 +00001905
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001906# Scripts to check various things for consistency
1907# ---------------------------------------------------------------------------
1908
Olivier Deprez157378f2022-04-04 15:47:50 +02001909PHONY += includecheck versioncheck coccicheck export_report
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001910
1911includecheck:
1912 find $(srctree)/* $(RCS_FIND_IGNORE) \
1913 -name '*.[hcS]' -type f -print | sort \
1914 | xargs $(PERL) -w $(srctree)/scripts/checkincludes.pl
1915
1916versioncheck:
1917 find $(srctree)/* $(RCS_FIND_IGNORE) \
1918 -name '*.[hcS]' -type f -print | sort \
1919 | xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
1920
1921coccicheck:
David Brazdil0f672f62019-12-10 10:32:29 +00001922 $(Q)$(BASH) $(srctree)/scripts/$@
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001923
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001924export_report:
1925 $(PERL) $(srctree)/scripts/export_report.pl
1926
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001927PHONY += checkstack kernelrelease kernelversion image_name
1928
1929# UML needs a little special treatment here. It wants to use the host
1930# toolchain, so needs $(SUBARCH) passed to checkstack.pl. Everyone
1931# else wants $(ARCH), including people doing cross-builds, which means
1932# that $(SUBARCH) doesn't work here.
1933ifeq ($(ARCH), um)
1934CHECKSTACK_ARCH := $(SUBARCH)
1935else
1936CHECKSTACK_ARCH := $(ARCH)
1937endif
1938checkstack:
1939 $(OBJDUMP) -d vmlinux $$(find . -name '*.ko') | \
David Brazdil0f672f62019-12-10 10:32:29 +00001940 $(PERL) $(srctree)/scripts/checkstack.pl $(CHECKSTACK_ARCH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001941
1942kernelrelease:
1943 @echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
1944
1945kernelversion:
1946 @echo $(KERNELVERSION)
1947
1948image_name:
1949 @echo $(KBUILD_IMAGE)
1950
1951# Clear a bunch of variables before executing the submake
David Brazdil0f672f62019-12-10 10:32:29 +00001952
1953ifeq ($(quiet),silent_)
1954tools_silent=s
1955endif
1956
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957tools/: FORCE
1958 $(Q)mkdir -p $(objtree)/tools
David Brazdil0f672f62019-12-10 10:32:29 +00001959 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(tools_silent) $(filter --j% -j,$(MAKEFLAGS))" O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001960
1961tools/%: FORCE
1962 $(Q)mkdir -p $(objtree)/tools
David Brazdil0f672f62019-12-10 10:32:29 +00001963 $(Q)$(MAKE) LDFLAGS= MAKEFLAGS="$(tools_silent) $(filter --j% -j,$(MAKEFLAGS))" O=$(abspath $(objtree)) subdir=tools -C $(srctree)/tools/ $*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001964
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001965quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN $(wildcard $(rm-files)))
Olivier Deprez157378f2022-04-04 15:47:50 +02001966 cmd_rmfiles = rm -rf $(rm-files)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001967
1968# Run depmod only if we have System.map and depmod is executable
1969quiet_cmd_depmod = DEPMOD $(KERNELRELEASE)
1970 cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \
1971 $(KERNELRELEASE)
1972
David Brazdil0f672f62019-12-10 10:32:29 +00001973# read saved command lines for existing targets
1974existing-targets := $(wildcard $(sort $(targets)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001975
David Brazdil0f672f62019-12-10 10:32:29 +00001976-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001977
Olivier Deprez157378f2022-04-04 15:47:50 +02001978endif # config-build
David Brazdil0f672f62019-12-10 10:32:29 +00001979endif # mixed-build
1980endif # need-sub-make
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001981
1982PHONY += FORCE
1983FORCE:
1984
1985# Declare the contents of the PHONY variable as phony. We keep that
1986# information in a variable so we can use it in if_changed and friends.
1987.PHONY: $(PHONY)