2 # SPDX-License-Identifier: GPL-2.0
4 # Tests whether a suitable Rust toolchain is available.
6 # Pass `-v` for human output and more checks (as warnings).
10 min_tool_version=$(dirname $0)/min-tool-version.sh
12 # Convert the version string x.y.z to a canonical up-to-7-digits form.
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()
21 echo $((100000 * $1 + 100 * $2 + $3))
24 # Check that the Rust compiler exists.
25 if ! command -v "$RUSTC" >/dev/null; then
26 if [ "$1" = -v ]; then
28 echo >&2 "*** Rust compiler '$RUSTC' could not be found."
34 # Check that the Rust bindings generator exists.
35 if ! command -v "$BINDGEN" >/dev/null; then
36 if [ "$1" = -v ]; then
38 echo >&2 "*** Rust bindings generator '$BINDGEN' could not be found."
44 # Check that the Rust compiler version is suitable.
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 \
50 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
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
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"
65 if [ "$1" = -v ] && [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
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"
73 # Check that the Rust bindings generator is suitable.
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 \
79 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
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
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"
94 if [ "$1" = -v ] && [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then
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"
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]+' \
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
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"
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.
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
132 LC_ALL=C "$CC" --version 2>/dev/null \
133 | sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
135 if [ "$clang_version" != "$bindgen_libclang_version" ]; then
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"
146 # Check that the source code for the `core` standard library exists.
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
155 echo >&2 "*** Source code for the 'core' standard library could not be found"
156 echo >&2 "*** at '$rustc_src_core'."