Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
| 4 | # Tests whether a suitable Rust toolchain is available. |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 5 | |
| 6 | set -e |
| 7 | |
| 8 | min_tool_version=$(dirname $0)/min-tool-version.sh |
| 9 | |
| 10 | # Convert the version string x.y.z to a canonical up-to-7-digits form. |
| 11 | # |
| 12 | # Note that this function uses one more digit (compared to other |
| 13 | # instances in other version scripts) to give a bit more space to |
| 14 | # `rustc` since it will reach 1.100.0 in late 2026. |
| 15 | get_canonical_version() |
| 16 | { |
| 17 | IFS=. |
| 18 | set -- $1 |
| 19 | echo $((100000 * $1 + 100 * $2 + $3)) |
| 20 | } |
| 21 | |
Miguel Ojeda | aac284b | 2023-06-16 02:16:24 +0200 | [diff] [blame] | 22 | # Print a reference to the Quick Start guide in the documentation. |
| 23 | print_docs_reference() |
| 24 | { |
| 25 | echo >&2 "***" |
| 26 | echo >&2 "*** Please see Documentation/rust/quick-start.rst for details" |
| 27 | echo >&2 "*** on how to set up the Rust support." |
| 28 | echo >&2 "***" |
| 29 | } |
| 30 | |
Miguel Ojeda | e90db55 | 2023-06-16 02:16:26 +0200 | [diff] [blame] | 31 | # Print an explanation about the fact that the script is meant to be called from Kbuild. |
| 32 | print_kbuild_explanation() |
| 33 | { |
| 34 | echo >&2 "***" |
| 35 | echo >&2 "*** This script is intended to be called from Kbuild." |
| 36 | echo >&2 "*** Please use the 'rustavailable' target to call it instead." |
| 37 | echo >&2 "*** Otherwise, the results may not be meaningful." |
| 38 | exit 1 |
| 39 | } |
| 40 | |
Miguel Ojeda | aac284b | 2023-06-16 02:16:24 +0200 | [diff] [blame] | 41 | # If the script fails for any reason, or if there was any warning, then |
| 42 | # print a reference to the documentation on exit. |
| 43 | warning=0 |
| 44 | trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT |
| 45 | |
Miguel Ojeda | e90db55 | 2023-06-16 02:16:26 +0200 | [diff] [blame] | 46 | # Check that the expected environment variables are set. |
| 47 | if [ -z "${RUSTC+x}" ]; then |
| 48 | echo >&2 "***" |
| 49 | echo >&2 "*** Environment variable 'RUSTC' is not set." |
| 50 | print_kbuild_explanation |
| 51 | fi |
| 52 | |
| 53 | if [ -z "${BINDGEN+x}" ]; then |
| 54 | echo >&2 "***" |
| 55 | echo >&2 "*** Environment variable 'BINDGEN' is not set." |
| 56 | print_kbuild_explanation |
| 57 | fi |
| 58 | |
| 59 | if [ -z "${CC+x}" ]; then |
| 60 | echo >&2 "***" |
| 61 | echo >&2 "*** Environment variable 'CC' is not set." |
| 62 | print_kbuild_explanation |
| 63 | fi |
| 64 | |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 65 | # Check that the Rust compiler exists. |
| 66 | if ! command -v "$RUSTC" >/dev/null; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 67 | echo >&2 "***" |
| 68 | echo >&2 "*** Rust compiler '$RUSTC' could not be found." |
| 69 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | # Check that the Rust bindings generator exists. |
| 74 | if ! command -v "$BINDGEN" >/dev/null; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 75 | echo >&2 "***" |
| 76 | echo >&2 "*** Rust bindings generator '$BINDGEN' could not be found." |
| 77 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 78 | exit 1 |
| 79 | fi |
| 80 | |
| 81 | # Check that the Rust compiler version is suitable. |
| 82 | # |
| 83 | # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`. |
Miguel Ojeda | f295522 | 2023-06-16 02:16:29 +0200 | [diff] [blame] | 84 | rust_compiler_output=$( \ |
| 85 | LC_ALL=C "$RUSTC" --version 2>/dev/null |
| 86 | ) || rust_compiler_code=$? |
| 87 | if [ -n "$rust_compiler_code" ]; then |
| 88 | echo >&2 "***" |
| 89 | echo >&2 "*** Running '$RUSTC' to check the Rust compiler version failed with" |
| 90 | echo >&2 "*** code $rust_compiler_code. See output and docs below for details:" |
| 91 | echo >&2 "***" |
| 92 | echo >&2 "$rust_compiler_output" |
| 93 | echo >&2 "***" |
| 94 | exit 1 |
| 95 | fi |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 96 | rust_compiler_version=$( \ |
Miguel Ojeda | f295522 | 2023-06-16 02:16:29 +0200 | [diff] [blame] | 97 | echo "$rust_compiler_output" \ |
Miguel Ojeda | 7cd6a3e | 2023-06-16 02:16:28 +0200 | [diff] [blame] | 98 | | sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p' |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 99 | ) |
Miguel Ojeda | bc60c93 | 2023-06-16 02:16:30 +0200 | [diff] [blame] | 100 | if [ -z "$rust_compiler_version" ]; then |
| 101 | echo >&2 "***" |
| 102 | echo >&2 "*** Running '$RUSTC' to check the Rust compiler version did not return" |
| 103 | echo >&2 "*** an expected output. See output and docs below for details:" |
| 104 | echo >&2 "***" |
| 105 | echo >&2 "$rust_compiler_output" |
| 106 | echo >&2 "***" |
| 107 | exit 1 |
| 108 | fi |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 109 | rust_compiler_min_version=$($min_tool_version rustc) |
| 110 | rust_compiler_cversion=$(get_canonical_version $rust_compiler_version) |
| 111 | rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version) |
| 112 | if [ "$rust_compiler_cversion" -lt "$rust_compiler_min_cversion" ]; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 113 | echo >&2 "***" |
| 114 | echo >&2 "*** Rust compiler '$RUSTC' is too old." |
| 115 | echo >&2 "*** Your version: $rust_compiler_version" |
| 116 | echo >&2 "*** Minimum version: $rust_compiler_min_version" |
| 117 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 118 | exit 1 |
| 119 | fi |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 120 | if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 121 | echo >&2 "***" |
| 122 | echo >&2 "*** Rust compiler '$RUSTC' is too new. This may or may not work." |
| 123 | echo >&2 "*** Your version: $rust_compiler_version" |
| 124 | echo >&2 "*** Expected version: $rust_compiler_min_version" |
| 125 | echo >&2 "***" |
Miguel Ojeda | aac284b | 2023-06-16 02:16:24 +0200 | [diff] [blame] | 126 | warning=1 |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 127 | fi |
| 128 | |
| 129 | # Check that the Rust bindings generator is suitable. |
| 130 | # |
| 131 | # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`. |
Miguel Ojeda | f295522 | 2023-06-16 02:16:29 +0200 | [diff] [blame] | 132 | rust_bindings_generator_output=$( \ |
| 133 | LC_ALL=C "$BINDGEN" --version 2>/dev/null |
| 134 | ) || rust_bindings_generator_code=$? |
| 135 | if [ -n "$rust_bindings_generator_code" ]; then |
| 136 | echo >&2 "***" |
| 137 | echo >&2 "*** Running '$BINDGEN' to check the Rust bindings generator version failed with" |
| 138 | echo >&2 "*** code $rust_bindings_generator_code. See output and docs below for details:" |
| 139 | echo >&2 "***" |
| 140 | echo >&2 "$rust_bindings_generator_output" |
| 141 | echo >&2 "***" |
| 142 | exit 1 |
| 143 | fi |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 144 | rust_bindings_generator_version=$( \ |
Miguel Ojeda | f295522 | 2023-06-16 02:16:29 +0200 | [diff] [blame] | 145 | echo "$rust_bindings_generator_output" \ |
Miguel Ojeda | 7cd6a3e | 2023-06-16 02:16:28 +0200 | [diff] [blame] | 146 | | sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p' |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 147 | ) |
Miguel Ojeda | bc60c93 | 2023-06-16 02:16:30 +0200 | [diff] [blame] | 148 | if [ -z "$rust_bindings_generator_version" ]; then |
| 149 | echo >&2 "***" |
| 150 | echo >&2 "*** Running '$BINDGEN' to check the bindings generator version did not return" |
| 151 | echo >&2 "*** an expected output. See output and docs below for details:" |
| 152 | echo >&2 "***" |
| 153 | echo >&2 "$rust_bindings_generator_output" |
| 154 | echo >&2 "***" |
| 155 | exit 1 |
| 156 | fi |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 157 | rust_bindings_generator_min_version=$($min_tool_version bindgen) |
| 158 | rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version) |
| 159 | rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version) |
| 160 | if [ "$rust_bindings_generator_cversion" -lt "$rust_bindings_generator_min_cversion" ]; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 161 | echo >&2 "***" |
| 162 | echo >&2 "*** Rust bindings generator '$BINDGEN' is too old." |
| 163 | echo >&2 "*** Your version: $rust_bindings_generator_version" |
| 164 | echo >&2 "*** Minimum version: $rust_bindings_generator_min_version" |
| 165 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 166 | exit 1 |
| 167 | fi |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 168 | if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 169 | echo >&2 "***" |
| 170 | echo >&2 "*** Rust bindings generator '$BINDGEN' is too new. This may or may not work." |
| 171 | echo >&2 "*** Your version: $rust_bindings_generator_version" |
| 172 | echo >&2 "*** Expected version: $rust_bindings_generator_min_version" |
| 173 | echo >&2 "***" |
Miguel Ojeda | aac284b | 2023-06-16 02:16:24 +0200 | [diff] [blame] | 174 | warning=1 |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 175 | fi |
| 176 | |
| 177 | # Check that the `libclang` used by the Rust bindings generator is suitable. |
Miguel Ojeda | 52cae7f | 2023-06-16 02:16:25 +0200 | [diff] [blame] | 178 | # |
| 179 | # In order to do that, first invoke `bindgen` to get the `libclang` version |
| 180 | # found by `bindgen`. This step may already fail if, for instance, `libclang` |
| 181 | # is not found, thus inform the user in such a case. |
| 182 | bindgen_libclang_output=$( \ |
| 183 | LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null |
| 184 | ) || bindgen_libclang_code=$? |
| 185 | if [ -n "$bindgen_libclang_code" ]; then |
| 186 | echo >&2 "***" |
| 187 | echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust" |
| 188 | echo >&2 "*** bindings generator) failed with code $bindgen_libclang_code. This may be caused by" |
| 189 | echo >&2 "*** a failure to locate libclang. See output and docs below for details:" |
| 190 | echo >&2 "***" |
| 191 | echo >&2 "$bindgen_libclang_output" |
| 192 | echo >&2 "***" |
| 193 | exit 1 |
| 194 | fi |
| 195 | |
| 196 | # `bindgen` returned successfully, thus use the output to check that the version |
| 197 | # of the `libclang` found by the Rust bindings generator is suitable. |
Miguel Ojeda | 7cd6a3e | 2023-06-16 02:16:28 +0200 | [diff] [blame] | 198 | # |
| 199 | # Unlike other version checks, note that this one does not necessarily appear |
| 200 | # in the first line of the output, thus no `sed` address is provided. |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 201 | bindgen_libclang_version=$( \ |
Miguel Ojeda | 52cae7f | 2023-06-16 02:16:25 +0200 | [diff] [blame] | 202 | echo "$bindgen_libclang_output" \ |
Miguel Ojeda | 9eb7e20 | 2023-06-16 02:16:27 +0200 | [diff] [blame] | 203 | | sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p' |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 204 | ) |
Miguel Ojeda | bc60c93 | 2023-06-16 02:16:30 +0200 | [diff] [blame] | 205 | if [ -z "$bindgen_libclang_version" ]; then |
| 206 | echo >&2 "***" |
| 207 | echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust" |
| 208 | echo >&2 "*** bindings generator) did not return an expected output. See output" |
| 209 | echo >&2 "*** and docs below for details:" |
| 210 | echo >&2 "***" |
| 211 | echo >&2 "$bindgen_libclang_output" |
| 212 | echo >&2 "***" |
| 213 | exit 1 |
| 214 | fi |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 215 | bindgen_libclang_min_version=$($min_tool_version llvm) |
| 216 | bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version) |
| 217 | bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version) |
| 218 | if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 219 | echo >&2 "***" |
| 220 | echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') is too old." |
| 221 | echo >&2 "*** Your version: $bindgen_libclang_version" |
| 222 | echo >&2 "*** Minimum version: $bindgen_libclang_min_version" |
| 223 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 224 | exit 1 |
| 225 | fi |
| 226 | |
| 227 | # If the C compiler is Clang, then we can also check whether its version |
| 228 | # matches the `libclang` version used by the Rust bindings generator. |
| 229 | # |
| 230 | # In the future, we might be able to perform a full version check, see |
| 231 | # https://github.com/rust-lang/rust-bindgen/issues/2138. |
Russell Currey | dee3a6b | 2023-06-16 02:16:22 +0200 | [diff] [blame] | 232 | cc_name=$($(dirname $0)/cc-version.sh $CC | cut -f1 -d' ') |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 233 | if [ "$cc_name" = Clang ]; then |
| 234 | clang_version=$( \ |
Russell Currey | dee3a6b | 2023-06-16 02:16:22 +0200 | [diff] [blame] | 235 | LC_ALL=C $CC --version 2>/dev/null \ |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 236 | | sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p' |
| 237 | ) |
| 238 | if [ "$clang_version" != "$bindgen_libclang_version" ]; then |
| 239 | echo >&2 "***" |
| 240 | echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN')" |
| 241 | echo >&2 "*** version does not match Clang's. This may be a problem." |
| 242 | echo >&2 "*** libclang version: $bindgen_libclang_version" |
| 243 | echo >&2 "*** Clang version: $clang_version" |
| 244 | echo >&2 "***" |
Miguel Ojeda | aac284b | 2023-06-16 02:16:24 +0200 | [diff] [blame] | 245 | warning=1 |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 246 | fi |
| 247 | fi |
| 248 | |
| 249 | # Check that the source code for the `core` standard library exists. |
| 250 | # |
| 251 | # `$KRUSTFLAGS` is passed in case the user added `--sysroot`. |
| 252 | rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print sysroot) |
| 253 | rustc_src=${RUST_LIB_SRC:-"$rustc_sysroot/lib/rustlib/src/rust/library"} |
| 254 | rustc_src_core="$rustc_src/core/src/lib.rs" |
| 255 | if [ ! -e "$rustc_src_core" ]; then |
Masahiro Yamada | d824d2f | 2023-06-16 02:16:21 +0200 | [diff] [blame] | 256 | echo >&2 "***" |
| 257 | echo >&2 "*** Source code for the 'core' standard library could not be found" |
| 258 | echo >&2 "*** at '$rustc_src_core'." |
| 259 | echo >&2 "***" |
Miguel Ojeda | 78521f3 | 2022-08-04 12:25:53 +0200 | [diff] [blame] | 260 | exit 1 |
| 261 | fi |