Skip to content

Commit ccf7beb

Browse files
ojedafbq
authored andcommitted
kbuild: rust: add rustc-version support
Now that we are starting to support several Rust versions, introduce `rustc-version` support, mimicking the C side: - `scripts/rustc-version.sh`, that mimics the other version scripts (with one more digit, e.g. Rust 1.79.0 is 107900). - `rustc-{info,name,version}` Kbuild macros. - `CONFIG_RUSTC_VERSION` Kconfig symbol that calls `rustc-version`. - `rustc-min-version` Kbuild macro that uses `CONFIG_RUSTC_VERSION`. With these, we can easily support flags conditionally depending on `rustc`'s version -- a user comes in the next patch. Another user will be the `-Ctarget-feature=+reserve-x18`/`-Zfixed-x18` arm64 flags [1]. Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [1] Signed-off-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9d24066 commit ccf7beb

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

init/Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,12 @@ config RUST
19201920

19211921
If unsure, say N.
19221922

1923+
config RUSTC_VERSION
1924+
int
1925+
depends on RUST
1926+
default $(rustc-version)
1927+
default 0
1928+
19231929
config RUSTC_VERSION_TEXT
19241930
string
19251931
depends on RUST

scripts/Kconfig.include

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ $(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not
4545
cc-name := $(shell,set -- $(cc-info) && echo $1)
4646
cc-version := $(shell,set -- $(cc-info) && echo $2)
4747

48+
# Get the Rust compiler name, version, and error out if it is not supported.
49+
rustc-info := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
50+
$(error-if,$(success,test -z "$(rustc-info)"),Sorry$(comma) this Rust compiler is not supported.)
51+
rustc-name := $(shell,set -- $(rustc-info) && echo $1)
52+
rustc-version := $(shell,set -- $(rustc-info) && echo $2)
53+
4854
# Get the assembler name, version, and error out if it is not supported.
4955
as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
5056
$(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)

scripts/Makefile.compiler

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ gcc-min-version = $(call test-ge, $(CONFIG_GCC_VERSION), $1)
6969
# Usage: cflags-$(call clang-min-version, 110000) += -foo
7070
clang-min-version = $(call test-ge, $(CONFIG_CLANG_VERSION), $1)
7171

72+
# rustc-min-version
73+
# Usage: rustflags-$(call rustc-min-version, 107900) += -foo
74+
rustc-min-version = $(call test-ge, $(CONFIG_RUSTC_VERSION), $1)
75+
7276
# ld-option
7377
# Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
7478
ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))

scripts/rustc-version.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Print the Rust compiler name and its version in a 5 or 6-digit form.
5+
# Also, perform the minimum version check.
6+
7+
set -e
8+
9+
# Convert the version string x.y.z to a canonical up-to-7-digits form.
10+
#
11+
# Note that this function uses one more digit (compared to other
12+
# instances in other version scripts) to give a bit more space to
13+
# `rustc` since it will reach 1.100.0 in late 2026.
14+
get_canonical_version()
15+
{
16+
IFS=.
17+
set -- $1
18+
echo $((100000 * $1 + 100 * $2 + $3))
19+
}
20+
21+
orig_args="$@"
22+
23+
set -- $("$@" --version)
24+
25+
name=$1
26+
27+
min_tool_version=$(dirname $0)/min-tool-version.sh
28+
29+
case "$name" in
30+
rustc)
31+
version=$2
32+
min_version=$($min_tool_version rustc)
33+
;;
34+
*)
35+
echo "$orig_args: unknown Rust compiler" >&2
36+
exit 1
37+
;;
38+
esac
39+
40+
rustcversion=$(get_canonical_version $version)
41+
min_rustcversion=$(get_canonical_version $min_version)
42+
43+
if [ "$rustcversion" -lt "$min_rustcversion" ]; then
44+
echo >&2 "***"
45+
echo >&2 "*** Rust compiler is too old."
46+
echo >&2 "*** Your $name version: $version"
47+
echo >&2 "*** Minimum $name version: $min_version"
48+
echo >&2 "***"
49+
exit 1
50+
fi
51+
52+
echo $name $rustcversion

0 commit comments

Comments
 (0)