| 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 |