Skip to content

Commit 89a9dc6

Browse files
nbdd0121ojeda
authored andcommitted
kbuild: rust: add CONFIG_RUSTC_LLVM_VERSION
Each version of Rust supports a range of LLVM versions. There are cases where we want to gate a config on the LLVM version instead of the Rust version. Normalized cfi integer tags are one example [1]. For consistency with cc-version and ld-version, the new version number is added to the existing rustc-version script, rather than being added to a new script. The invocation of rustc-version is being moved from init/Kconfig to scripts/Kconfig.include to avoid invoking rustc-version.sh twice and for consistency with cc-version. Link: https://lore.kernel.org/all/[email protected]/ [1] Signed-off-by: Gary Guo <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent e72a076 commit 89a9dc6

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

init/Kconfig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ config LLD_VERSION
6262

6363
config RUSTC_VERSION
6464
int
65-
default $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
65+
default $(rustc-version)
6666
help
6767
It does not depend on `RUST` since that one may need to use the version
6868
in a `depends on`.
@@ -78,6 +78,10 @@ config RUST_IS_AVAILABLE
7878
In particular, the Makefile target 'rustavailable' is useful to check
7979
why the Rust toolchain is not being detected.
8080

81+
config RUSTC_LLVM_VERSION
82+
int
83+
default $(rustc-llvm-version)
84+
8185
config CC_CAN_LINK
8286
bool
8387
default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT

scripts/Kconfig.include

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ cc-option-bit = $(if-success,$(CC) -Werror $(1) -E -x c /dev/null -o /dev/null,$
6565
m32-flag := $(cc-option-bit,-m32)
6666
m64-flag := $(cc-option-bit,-m64)
6767

68+
rustc-info := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
69+
rustc-version := $(shell,set -- $(rustc-info) && echo $1)
70+
rustc-llvm-version := $(shell,set -- $(rustc-info) && echo $2)
71+
6872
# $(rustc-option,<flag>)
6973
# Return y if the Rust compiler supports <flag>, n otherwise
7074
# Calls to this should be guarded so that they are not evaluated if

scripts/rustc-version.sh

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
#
44
# Usage: $ ./rustc-version.sh rustc
55
#
6-
# Print the Rust compiler version in a 6 or 7-digit form.
6+
# Print the Rust compiler version and the LLVM version it uses in a 6 or
7+
# 7-digit form.
8+
9+
# Convert the version string x.y.z to a canonical up-to-6-digits form.
10+
get_llvm_canonical_version()
11+
{
12+
IFS=.
13+
set -- $1
14+
echo $((10000 * $1 + 100 * $2 + $3))
15+
}
716

817
# Convert the version string x.y.z to a canonical up-to-7-digits form.
918
#
10-
# Note that this function uses one more digit (compared to other
11-
# instances in other version scripts) to give a bit more space to
19+
# Note that this function uses one more digit (compared to other instances in
20+
# other version scripts and the instance above) to give a bit more space to
1221
# `rustc` since it will reach 1.100.0 in late 2026.
13-
get_canonical_version()
22+
get_rustc_canonical_version()
1423
{
1524
IFS=.
1625
set -- $1
@@ -19,8 +28,18 @@ get_canonical_version()
1928

2029
if output=$("$@" --version 2>/dev/null); then
2130
set -- $output
22-
get_canonical_version $2
31+
rustc_version=$(get_rustc_canonical_version $2)
2332
else
24-
echo 0
33+
echo 0 0
2534
exit 1
2635
fi
36+
37+
if output=$("$@" --version --verbose 2>/dev/null | grep LLVM); then
38+
set -- $output
39+
rustc_llvm_version=$(get_llvm_canonical_version $3)
40+
else
41+
echo 0 0
42+
exit 1
43+
fi
44+
45+
echo $rustc_version $rustc_llvm_version

0 commit comments

Comments
 (0)