Skip to content

Commit 78521f3

Browse files
committed
scripts: add rust_is_available.sh
This script tests whether the Rust toolchain requirements are in place to enable Rust support. It uses `min-tool-version.sh` to fetch the version numbers. The build system will call it to set `CONFIG_RUST_IS_AVAILABLE` in a later patch. It also has an option (`-v`) to explain what is missing, which is useful to set up the development environment. This is used via the `make rustavailable` target added in a later patch. Reviewed-by: Kees Cook <[email protected]> Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Finn Behrens <[email protected]> Signed-off-by: Finn Behrens <[email protected]> Co-developed-by: Miguel Cano <[email protected]> Signed-off-by: Miguel Cano <[email protected]> Co-developed-by: Tiago Lam <[email protected]> Signed-off-by: Tiago Lam <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 9a8ff24 commit 78521f3

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

scripts/min-tool-version.sh

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ llvm)
3030
echo 11.0.0
3131
fi
3232
;;
33+
rustc)
34+
echo 1.62.0
35+
;;
36+
bindgen)
37+
echo 0.56.0
38+
;;
3339
*)
3440
echo "$1: unknown tool" >&2
3541
exit 1

scripts/rust_is_available.sh

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Tests whether a suitable Rust toolchain is available.
5+
#
6+
# Pass `-v` for human output and more checks (as warnings).
7+
8+
set -e
9+
10+
min_tool_version=$(dirname $0)/min-tool-version.sh
11+
12+
# Convert the version string x.y.z to a canonical up-to-7-digits form.
13+
#
14+
# Note that this function uses one more digit (compared to other
15+
# instances in other version scripts) to give a bit more space to
16+
# `rustc` since it will reach 1.100.0 in late 2026.
17+
get_canonical_version()
18+
{
19+
IFS=.
20+
set -- $1
21+
echo $((100000 * $1 + 100 * $2 + $3))
22+
}
23+
24+
# Check that the Rust compiler exists.
25+
if ! command -v "$RUSTC" >/dev/null; then
26+
if [ "$1" = -v ]; then
27+
echo >&2 "***"
28+
echo >&2 "*** Rust compiler '$RUSTC' could not be found."
29+
echo >&2 "***"
30+
fi
31+
exit 1
32+
fi
33+
34+
# Check that the Rust bindings generator exists.
35+
if ! command -v "$BINDGEN" >/dev/null; then
36+
if [ "$1" = -v ]; then
37+
echo >&2 "***"
38+
echo >&2 "*** Rust bindings generator '$BINDGEN' could not be found."
39+
echo >&2 "***"
40+
fi
41+
exit 1
42+
fi
43+
44+
# Check that the Rust compiler version is suitable.
45+
#
46+
# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
47+
rust_compiler_version=$( \
48+
LC_ALL=C "$RUSTC" --version 2>/dev/null \
49+
| head -n 1 \
50+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
51+
)
52+
rust_compiler_min_version=$($min_tool_version rustc)
53+
rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
54+
rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version)
55+
if [ "$rust_compiler_cversion" -lt "$rust_compiler_min_cversion" ]; then
56+
if [ "$1" = -v ]; then
57+
echo >&2 "***"
58+
echo >&2 "*** Rust compiler '$RUSTC' is too old."
59+
echo >&2 "*** Your version: $rust_compiler_version"
60+
echo >&2 "*** Minimum version: $rust_compiler_min_version"
61+
echo >&2 "***"
62+
fi
63+
exit 1
64+
fi
65+
if [ "$1" = -v ] && [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
66+
echo >&2 "***"
67+
echo >&2 "*** Rust compiler '$RUSTC' is too new. This may or may not work."
68+
echo >&2 "*** Your version: $rust_compiler_version"
69+
echo >&2 "*** Expected version: $rust_compiler_min_version"
70+
echo >&2 "***"
71+
fi
72+
73+
# Check that the Rust bindings generator is suitable.
74+
#
75+
# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
76+
rust_bindings_generator_version=$( \
77+
LC_ALL=C "$BINDGEN" --version 2>/dev/null \
78+
| head -n 1 \
79+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
80+
)
81+
rust_bindings_generator_min_version=$($min_tool_version bindgen)
82+
rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
83+
rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version)
84+
if [ "$rust_bindings_generator_cversion" -lt "$rust_bindings_generator_min_cversion" ]; then
85+
if [ "$1" = -v ]; then
86+
echo >&2 "***"
87+
echo >&2 "*** Rust bindings generator '$BINDGEN' is too old."
88+
echo >&2 "*** Your version: $rust_bindings_generator_version"
89+
echo >&2 "*** Minimum version: $rust_bindings_generator_min_version"
90+
echo >&2 "***"
91+
fi
92+
exit 1
93+
fi
94+
if [ "$1" = -v ] && [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then
95+
echo >&2 "***"
96+
echo >&2 "*** Rust bindings generator '$BINDGEN' is too new. This may or may not work."
97+
echo >&2 "*** Your version: $rust_bindings_generator_version"
98+
echo >&2 "*** Expected version: $rust_bindings_generator_min_version"
99+
echo >&2 "***"
100+
fi
101+
102+
# Check that the `libclang` used by the Rust bindings generator is suitable.
103+
bindgen_libclang_version=$( \
104+
LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null \
105+
| grep -F 'clang version ' \
106+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
107+
| head -n 1 \
108+
)
109+
bindgen_libclang_min_version=$($min_tool_version llvm)
110+
bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
111+
bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version)
112+
if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then
113+
if [ "$1" = -v ]; then
114+
echo >&2 "***"
115+
echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') is too old."
116+
echo >&2 "*** Your version: $bindgen_libclang_version"
117+
echo >&2 "*** Minimum version: $bindgen_libclang_min_version"
118+
echo >&2 "***"
119+
fi
120+
exit 1
121+
fi
122+
123+
# If the C compiler is Clang, then we can also check whether its version
124+
# matches the `libclang` version used by the Rust bindings generator.
125+
#
126+
# In the future, we might be able to perform a full version check, see
127+
# https://github.com/rust-lang/rust-bindgen/issues/2138.
128+
if [ "$1" = -v ]; then
129+
cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ')
130+
if [ "$cc_name" = Clang ]; then
131+
clang_version=$( \
132+
LC_ALL=C "$CC" --version 2>/dev/null \
133+
| sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
134+
)
135+
if [ "$clang_version" != "$bindgen_libclang_version" ]; then
136+
echo >&2 "***"
137+
echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN')"
138+
echo >&2 "*** version does not match Clang's. This may be a problem."
139+
echo >&2 "*** libclang version: $bindgen_libclang_version"
140+
echo >&2 "*** Clang version: $clang_version"
141+
echo >&2 "***"
142+
fi
143+
fi
144+
fi
145+
146+
# Check that the source code for the `core` standard library exists.
147+
#
148+
# `$KRUSTFLAGS` is passed in case the user added `--sysroot`.
149+
rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print sysroot)
150+
rustc_src=${RUST_LIB_SRC:-"$rustc_sysroot/lib/rustlib/src/rust/library"}
151+
rustc_src_core="$rustc_src/core/src/lib.rs"
152+
if [ ! -e "$rustc_src_core" ]; then
153+
if [ "$1" = -v ]; then
154+
echo >&2 "***"
155+
echo >&2 "*** Source code for the 'core' standard library could not be found"
156+
echo >&2 "*** at '$rustc_src_core'."
157+
echo >&2 "***"
158+
fi
159+
exit 1
160+
fi
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)