Skip to content

Commit 27fe495

Browse files
authored
Merge pull request #625 from ojeda/kbuild
Build system review
2 parents 97668ff + cf00942 commit 27fe495

File tree

13 files changed

+272
-77
lines changed

13 files changed

+272
-77
lines changed

Documentation/process/changes.rst

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,12 @@ running a Linux kernel. Also, not all tools are necessary on all
2626
systems; obviously, if you don't have any PC Card hardware, for example,
2727
you probably needn't concern yourself with pcmciautils.
2828

29-
Furthermore, note that newer versions of the Rust toolchain may or may not work
30-
because, for the moment, we depend on some unstable features. Thus, unless you
31-
know what you are doing, use the exact version listed here. Please see
32-
:ref:`Documentation/rust/quick-start.rst <rust_quick_start>` for details.
33-
3429
====================== =============== ========================================
3530
Program Minimal version Command to check the version
3631
====================== =============== ========================================
3732
GNU C 5.1 gcc --version
3833
Clang/LLVM (optional) 10.0.1 clang --version
39-
rustc (optional) 1.58.0 rustc --version
34+
Rust (optional) 1.58.0 rustc --version
4035
bindgen (optional) 0.56.0 bindgen --version
4136
GNU make 3.81 make --version
4237
binutils 2.23 ld -v
@@ -85,6 +80,27 @@ kernels. Older releases aren't guaranteed to work, and we may drop workarounds
8580
from the kernel that were used to support older versions. Please see additional
8681
docs on :ref:`Building Linux with Clang/LLVM <kbuild_llvm>`.
8782

83+
Rust (optional)
84+
---------------
85+
86+
A particular version of the Rust toolchain is required. Newer versions may or
87+
may not work because the kernel depends on some unstable Rust features, for
88+
the moment.
89+
90+
Each Rust toolchain comes with several "components", some of which are required
91+
(like ``rustc``) and some that are optional. The ``rust-src`` component (which
92+
is optional) needs to be installed to build the kernel. Other components are
93+
useful for developing.
94+
95+
Please see :ref:`Documentation/rust/quick-start.rst <rust_quick_start>` for
96+
more information.
97+
98+
bindgen (optional)
99+
------------------
100+
101+
``bindgen`` is used to generate the Rust bindings to the C side of the kernel.
102+
It depends on ``libclang``.
103+
88104
Make
89105
----
90106

@@ -369,6 +385,16 @@ Clang/LLVM
369385

370386
- :ref:`Getting LLVM <getting_llvm>`.
371387

388+
Rust
389+
----
390+
391+
- :ref:`Documentation/rust/quick-start.rst <rust_quick_start>`.
392+
393+
bindgen
394+
-------
395+
396+
- :ref:`Documentation/rust/quick-start.rst <rust_quick_start>`.
397+
372398
Make
373399
----
374400

Documentation/rust/quick-start.rst

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ This section explains how to fetch the tools needed for building.
1313

1414
Some of these requirements might be available from Linux distributions
1515
under names like ``rustc``, ``rust-src``, ``rust-bindgen``, etc. However,
16-
at the time of writing, they are likely not to be recent enough.
16+
at the time of writing, they are likely not to be recent enough unless
17+
the distribution tracks the latest releases.
18+
19+
To easily check whether the requirements are met, the following target
20+
can be used::
21+
22+
make LLVM=1 rustavailable
23+
24+
This triggers the same logic used by Kconfig to determine whether
25+
``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
26+
if that is the case.
1727

1828

1929
rustc
@@ -26,7 +36,7 @@ Rust features.
2636
If ``rustup`` is being used, enter the checked out source code directory
2737
and run::
2838

29-
rustup override set 1.58.0
39+
rustup override set $(scripts/min-tool-version.sh rustc)
3040

3141
Otherwise, fetch a standalone installer or install ``rustup`` from:
3242

@@ -43,10 +53,19 @@ If ``rustup`` is being used, run::
4353

4454
rustup component add rust-src
4555

56+
The components are installed per toolchain, thus upgrading the Rust compiler
57+
version later on requires re-adding the component.
58+
4659
Otherwise, if a standalone installer is used, the Rust repository may be cloned
4760
into the installation folder of the toolchain::
4861

49-
git clone --recurse-submodules https://github.com/rust-lang/rust $(rustc --print sysroot)/lib/rustlib/src/rust
62+
git clone --recurse-submodules \
63+
--branch $(scripts/min-tool-version.sh rustc) \
64+
https://github.com/rust-lang/rust \
65+
$(rustc --print sysroot)/lib/rustlib/src/rust
66+
67+
In this case, upgrading the Rust compiler version later on requires manually
68+
updating this clone.
5069

5170

5271
libclang
@@ -67,8 +86,8 @@ Otherwise, building LLVM takes quite a while, but it is not a complex process:
6786

6887
https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
6988

70-
See Documentation/kbuild/llvm.rst for more information and further ways
71-
to fetch pre-built releases and distribution packages.
89+
See :ref:`Documentation/kbuild/llvm.rst <kbuild_llvm>` for more information and
90+
further ways to fetch pre-built releases and distribution packages.
7291

7392

7493
bindgen
@@ -79,7 +98,7 @@ the ``bindgen`` tool. A particular version is required.
7998

8099
Install it via (note that this will download and build the tool from source)::
81100

82-
cargo install --locked --version 0.56.0 bindgen
101+
cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
83102

84103

85104
Requirements: Developing
@@ -167,8 +186,8 @@ Configuration
167186
-------------
168187

169188
``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
170-
menu. The option is only shown if the build system can locate ``rustc``.
171-
In turn, this will make visible the rest of options that depend on Rust.
189+
menu. The option is only shown if a suitable Rust toolchain is found (see
190+
above). In turn, this will make visible the rest of options that depend on Rust.
172191

173192
Afterwards, go to::
174193

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16591,6 +16591,7 @@ F: rust/
1659116591
F: samples/rust/
1659216592
F: Documentation/rust/
1659316593
F: lib/rust.h
16594+
F: scripts/*rust*
1659416595
K: \b(?i:rust)\b
1659516596

1659616597
RXRPC SOCKETS (AF_RXRPC)

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ no-dot-config-targets := $(clean-targets) \
274274
cscope gtags TAGS tags help% %docs check% coccicheck \
275275
$(version_h) headers headers_% archheaders archscripts \
276276
%asm-generic kernelversion %src-pkg dt_binding_check \
277-
outputmakefile rustfmt rustfmtcheck
277+
outputmakefile rustavailable rustfmt rustfmtcheck
278278
# Installation targets should not require compiler. Unfortunately, vdso_install
279279
# is an exception where build artifacts may be updated. This must be fixed.
280280
no-compiler-targets := $(no-dot-config-targets) install dtbs_install \
@@ -809,7 +809,7 @@ KBUILD_CFLAGS += -O3
809809
KBUILD_RUSTFLAGS_OPT_LEVEL_MAP := 3
810810
else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
811811
KBUILD_CFLAGS += -Os
812-
KBUILD_RUSTFLAGS_OPT_LEVEL_MAP := z
812+
KBUILD_RUSTFLAGS_OPT_LEVEL_MAP := s
813813
endif
814814

815815
# Always set `debug-assertions` and `overflow-checks` because their default
@@ -1696,6 +1696,8 @@ help:
16961696
@echo ' kselftest to existing .config.'
16971697
@echo ''
16981698
@echo 'Rust targets:'
1699+
@echo ' rustavailable - Checks whether the Rust toolchain is'
1700+
@echo ' available and, if not, explains why.'
16991701
@echo ' rustfmt - Reformat all the Rust code in the kernel'
17001702
@echo ' rustfmtcheck - Checks if all the Rust code in the kernel'
17011703
@echo ' is formatted, printing a diff otherwise.'
@@ -1781,6 +1783,11 @@ $(DOC_TARGETS):
17811783
# Rust targets
17821784
# ---------------------------------------------------------------------------
17831785

1786+
# "Is Rust available?" target
1787+
PHONY += rustavailable
1788+
rustavailable:
1789+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust-is-available.sh -v
1790+
17841791
# Documentation target
17851792
#
17861793
# Using the singular to avoid running afoul of `no-dot-config-targets`.

init/Kconfig

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ config LLD_VERSION
6060
default $(ld-version) if LD_IS_LLD
6161
default 0
6262

63-
config HAS_RUST
64-
depends on ARM64 || CPU_32v6 || CPU_32v6K || (PPC64 && CPU_LITTLE_ENDIAN) || X86_64 || RISCV
65-
def_bool $(success,$(RUSTC) --version)
63+
config RUST_IS_AVAILABLE
64+
def_bool $(success,$(srctree)/scripts/rust-is-available.sh)
65+
help
66+
This shows whether a suitable Rust toolchain is available (found).
6667

67-
config RUSTC_VERSION
68-
depends on HAS_RUST
69-
int
70-
default $(shell,$(srctree)/scripts/rust-version.sh $(RUSTC))
68+
Please see Documentation/rust/quick-start.rst for instructions on how
69+
to satify the build requirements of Rust support.
70+
71+
In particular, the Makefile target 'rustavailable' is useful to check
72+
why the Rust toolchain is not being detected.
7173

7274
config CC_CAN_LINK
7375
bool
@@ -2056,9 +2058,11 @@ config PROFILING
20562058

20572059
config RUST
20582060
bool "Rust support"
2059-
depends on HAS_RUST
2061+
depends on RUST_IS_AVAILABLE
2062+
depends on ARM64 || CPU_32v6 || CPU_32v6K || (PPC64 && CPU_LITTLE_ENDIAN) || X86_64 || RISCV
20602063
depends on !COMPILE_TEST
20612064
depends on !MODVERSIONS
2065+
depends on !GCC_PLUGIN_RANDSTRUCT
20622066
default n
20632067
help
20642068
Enables Rust support in the kernel.
@@ -2073,6 +2077,16 @@ config RUST
20732077

20742078
If unsure, say N.
20752079

2080+
config RUSTC_VERSION_TEXT
2081+
depends on RUST
2082+
string
2083+
default $(shell,$(RUSTC) --version)
2084+
2085+
config BINDGEN_VERSION_TEXT
2086+
depends on RUST
2087+
string
2088+
default $(shell,$(BINDGEN) --version)
2089+
20762090
#
20772091
# Place an empty function call at each tracepoint site. Can be
20782092
# dynamically changed for a probe function.

lib/Kconfig.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ config RUST_OVERFLOW_CHECKS
26772677
Enables rustc's `-Coverflow-checks` codegen option.
26782678

26792679
This flag allows you to control the behavior of runtime integer
2680-
overflow. When overflow-checks are enabled, a panic will occur
2680+
overflow. When overflow-checks are enabled, a Rust panic will occur
26812681
on overflow.
26822682

26832683
Note that this will apply to all Rust code, including `core`.

rust/Makefile

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,24 @@ else
188188
# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
189189
# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
190190
#
191-
# For the moment, here we are tweaking the flags on the fly. Some config
192-
# options may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` if we end up using one
193-
# of those structs).
191+
# For the moment, here we are tweaking the flags on the fly. This is a hack,
192+
# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
193+
# if we end up using one of those structs).
194194
bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
195195
-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
196196
-mindirect-branch=thunk-extern -mindirect-branch-register -mrecord-mcount \
197-
-mabi=lp64 -mstack-protector-guard% -fconserve-stack -falign-jumps=% \
198-
-falign-loops=% -femit-struct-debug-baseonly \
199-
-fno-ipa-cp-clone -fno-ipa-sra -fno-partial-inlining \
200-
-fplugin-arg-arm_ssp_per_task_plugin-% \
197+
-mabi=lp64 -mstack-protector-guard% -mtraceback=no \
198+
-mno-pointers-to-nested-functions -mno-string -mno-strict-align \
199+
-fconserve-stack -falign-jumps=% -falign-loops=% \
200+
-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
201+
-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
201202
-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
202203
-Wno-packed-not-aligned -Wno-format-truncation -Wno-format-overflow \
203204
-Wno-stringop-truncation -Wno-unused-but-set-variable \
204205
-Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized \
205206
-Werror=designated-init -Wno-zero-length-bounds -Wimplicit-fallthrough=% \
206-
-Wno-alloc-size-larger-than --param=% --param asan-%
207-
208-
# PowerPC
209-
bindgen_skip_c_flags += -mtraceback=no -mno-pointers-to-nested-functions \
210-
-mno-string -mno-strict-align
207+
-Wno-alloc-size-larger-than \
208+
--param=% --param asan-%
211209

212210
# Derived from `scripts/Makefile.clang`
213211
BINDGEN_TARGET_arm := arm-linux-gnueabi

scripts/Kconfig.include

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ ld-option = $(success,$(LD) -v $(1))
3636
as-instr = $(success,printf "%b\n" "$(1)" | $(CC) $(CLANG_FLAGS) -c -x assembler -o /dev/null -)
3737

3838
# check if $(CC) and $(LD) exist
39-
$(error-if,$(failure,command -v $(CC)),compiler '$(CC)' not found)
39+
$(error-if,$(failure,command -v $(CC)),C compiler '$(CC)' not found)
4040
$(error-if,$(failure,command -v $(LD)),linker '$(LD)' not found)
4141

42-
# Get the compiler name, version, and error out if it is not supported.
42+
# Get the C compiler name, version, and error out if it is not supported.
4343
cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
44-
$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this compiler is not supported.)
44+
$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.)
4545
cc-name := $(shell,set -- $(cc-info) && echo $1)
4646
cc-version := $(shell,set -- $(cc-info) && echo $2)
4747

scripts/cc-version.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
33
#
4-
# Print the compiler name and its version in a 5 or 6-digit form.
4+
# Print the C compiler name and its version in a 5 or 6-digit form.
55
# Also, perform the minimum version check.
66

77
set -e
88

9-
# Print the compiler name and some version components.
10-
get_compiler_info()
9+
# Print the C compiler name and some version components.
10+
get_c_compiler_info()
1111
{
1212
cat <<- EOF | "$@" -E -P -x c - 2>/dev/null
1313
#if defined(__clang__)
@@ -32,7 +32,7 @@ get_canonical_version()
3232

3333
# $@ instead of $1 because multiple words might be given, e.g. CC="ccache gcc".
3434
orig_args="$@"
35-
set -- $(get_compiler_info "$@")
35+
set -- $(get_c_compiler_info "$@")
3636

3737
name=$1
3838

@@ -52,7 +52,7 @@ ICC)
5252
min_version=$($min_tool_version icc)
5353
;;
5454
*)
55-
echo "$orig_args: unknown compiler" >&2
55+
echo "$orig_args: unknown C compiler" >&2
5656
exit 1
5757
;;
5858
esac
@@ -62,7 +62,7 @@ min_cversion=$(get_canonical_version $min_version)
6262

6363
if [ "$cversion" -lt "$min_cversion" ]; then
6464
echo >&2 "***"
65-
echo >&2 "*** Compiler is too old."
65+
echo >&2 "*** C compiler is too old."
6666
echo >&2 "*** Your $name version: $version"
6767
echo >&2 "*** Minimum $name version: $min_version"
6868
echo >&2 "***"

scripts/min-tool-version.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ llvm)
3131
echo 10.0.1
3232
fi
3333
;;
34+
rustc)
35+
echo 1.58.0
36+
;;
37+
bindgen)
38+
echo 0.56.0
39+
;;
3440
*)
3541
echo "$1: unknown tool" >&2
3642
exit 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#pragma message("clang version " __clang_version__)

0 commit comments

Comments
 (0)