|
| 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=$("$RUSTC" --version | cut -f2 -d' ' | cut -f1 -d'-') |
| 48 | +rust_compiler_min_version=$($min_tool_version rustc) |
| 49 | +rust_compiler_cversion=$(get_canonical_version $rust_compiler_version) |
| 50 | +rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version) |
| 51 | +if [ "$rust_compiler_cversion" -lt "$rust_compiler_min_cversion" ]; then |
| 52 | + if [ "$1" = -v ]; then |
| 53 | + echo >&2 "***" |
| 54 | + echo >&2 "*** Rust compiler '$RUSTC' is too old." |
| 55 | + echo >&2 "*** Your version: $rust_compiler_version" |
| 56 | + echo >&2 "*** Minimum version: $rust_compiler_min_version" |
| 57 | + echo >&2 "***" |
| 58 | + fi |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | +if [ "$1" = -v ] && [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then |
| 62 | + echo >&2 "***" |
| 63 | + echo >&2 "*** Rust compiler '$RUSTC' is too new. This may or may not work." |
| 64 | + echo >&2 "*** Your version: $rust_compiler_version" |
| 65 | + echo >&2 "*** Expected version: $rust_compiler_min_version" |
| 66 | + echo >&2 "***" |
| 67 | +fi |
| 68 | + |
| 69 | +# Check that the Rust bindings generator is suitable. |
| 70 | +# |
| 71 | +# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`. |
| 72 | +rust_bindings_generator_version=$("$BINDGEN" --version | cut -f2 -d' ' | cut -f1 -d'-') |
| 73 | +rust_bindings_generator_min_version=$($min_tool_version bindgen) |
| 74 | +rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version) |
| 75 | +rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version) |
| 76 | +if [ "$rust_bindings_generator_cversion" -lt "$rust_bindings_generator_min_cversion" ]; then |
| 77 | + if [ "$1" = -v ]; then |
| 78 | + echo >&2 "***" |
| 79 | + echo >&2 "*** Rust bindings generator '$BINDGEN' is too old." |
| 80 | + echo >&2 "*** Your version: $rust_bindings_generator_version" |
| 81 | + echo >&2 "*** Minimum version: $rust_bindings_generator_min_version" |
| 82 | + echo >&2 "***" |
| 83 | + fi |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | +if [ "$1" = -v ] && [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then |
| 87 | + echo >&2 "***" |
| 88 | + echo >&2 "*** Rust bindings generator '$BINDGEN' is too new. This may or may not work." |
| 89 | + echo >&2 "*** Your version: $rust_bindings_generator_version" |
| 90 | + echo >&2 "*** Expected version: $rust_bindings_generator_min_version" |
| 91 | + echo >&2 "***" |
| 92 | +fi |
| 93 | + |
| 94 | +# Check that the `libclang` used by the Rust bindings generator is suitable. |
| 95 | +# |
| 96 | +# There may be a better way to do this in the future, |
| 97 | +# see https://github.com/rust-lang/rust-bindgen/issues/2138 |
| 98 | +bindgen_libclang_full_version=$(\ |
| 99 | + "$BINDGEN" $(dirname $0)/rust-is-available-bindgen-libclang.h 2>&1 >/dev/null \ |
| 100 | + | grep -F 'clang version' \ |
| 101 | + | sed -E 's:^.*(clang version .*) \[.*$:\1:' \ |
| 102 | +) |
| 103 | +bindgen_libclang_version=$(echo "$bindgen_libclang_full_version" | cut -f3 -d' ') |
| 104 | +bindgen_libclang_min_version=$($min_tool_version llvm) |
| 105 | +bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version) |
| 106 | +bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version) |
| 107 | +if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then |
| 108 | + if [ "$1" = -v ]; then |
| 109 | + echo >&2 "***" |
| 110 | + echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') is too old." |
| 111 | + echo >&2 "*** Your version: $bindgen_libclang_version" |
| 112 | + echo >&2 "*** Minimum version: $bindgen_libclang_min_version" |
| 113 | + echo >&2 "***" |
| 114 | + fi |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | + |
| 118 | +# If the C compiler is Clang, then we can also check whether its full version |
| 119 | +# matches exactly the `libclang` used by the Rust bindings generator. |
| 120 | +if [ "$1" = -v ]; then |
| 121 | + cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ') |
| 122 | + if [ "$cc_name" = Clang ]; then |
| 123 | + clang_full_version=$("$CC" --version | grep -F 'clang version') |
| 124 | + if [ "$clang_full_version" != "$bindgen_libclang_full_version" ]; then |
| 125 | + echo >&2 "***" |
| 126 | + echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') full version does not match Clang's. This may be a problem." |
| 127 | + echo >&2 "*** libclang: $bindgen_libclang_full_version" |
| 128 | + echo >&2 "*** Clang: $clang_full_version" |
| 129 | + echo >&2 "***" |
| 130 | + fi |
| 131 | + fi |
| 132 | +fi |
| 133 | + |
| 134 | +# Check that the source code for the `core` standard library exists. |
| 135 | +# |
| 136 | +# `$KRUSTFLAGS` is passed in case the user added `--sysroot`. |
| 137 | +rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print sysroot) |
| 138 | +rustc_src="$rustc_sysroot/lib/rustlib/src/rust/library" |
| 139 | +rustc_src_core="$rustc_src/core/src/lib.rs" |
| 140 | +if [ ! -e "$rustc_src_core" ]; then |
| 141 | + if [ "$1" = -v ]; then |
| 142 | + echo >&2 "***" |
| 143 | + echo >&2 "*** Source code for the 'core' standard library could not be found" |
| 144 | + echo >&2 "*** at '$rustc_src_core'." |
| 145 | + echo >&2 "***" |
| 146 | + fi |
| 147 | + exit 1 |
| 148 | +fi |
| 149 | + |
| 150 | +# Success! |
| 151 | +if [ "$1" = -v ]; then |
| 152 | + echo >&2 "Rust is available!" |
| 153 | +fi |
0 commit comments