1 #!/bin/sh 2 # SPDX-License-Identifier: GPL-2.0 3 # 4 # Tests whether a suitable Rust toolchain is a 5 6 set -e 7 8 min_tool_version=$(dirname $0)/min-tool-versio 9 10 # Convert the version string x.y.z to a canoni 11 # 12 # Note that this function uses one more digit 13 # instances in other version scripts) to give 14 # `rustc` since it will reach 1.100.0 in late 15 get_canonical_version() 16 { 17 IFS=. 18 set -- $1 19 echo $((100000 * $1 + 100 * $2 + $3)) 20 } 21 22 # Print a reference to the Quick Start guide i 23 print_docs_reference() 24 { 25 echo >&2 "***" 26 echo >&2 "*** Please see Documentation 27 echo >&2 "*** on how to set up the Rus 28 echo >&2 "***" 29 } 30 31 # Print an explanation about the fact that the 32 print_kbuild_explanation() 33 { 34 echo >&2 "***" 35 echo >&2 "*** This script is intended 36 echo >&2 "*** Please use the 'rustavai 37 echo >&2 "*** Otherwise, the results m 38 exit 1 39 } 40 41 # If the script fails for any reason, or if th 42 # print a reference to the documentation on ex 43 warning=0 44 trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; t 45 46 # Check that the expected environment variable 47 if [ -z "${RUSTC+x}" ]; then 48 echo >&2 "***" 49 echo >&2 "*** Environment variable 'RU 50 print_kbuild_explanation 51 fi 52 53 if [ -z "${BINDGEN+x}" ]; then 54 echo >&2 "***" 55 echo >&2 "*** Environment variable 'BI 56 print_kbuild_explanation 57 fi 58 59 if [ -z "${CC+x}" ]; then 60 echo >&2 "***" 61 echo >&2 "*** Environment variable 'CC 62 print_kbuild_explanation 63 fi 64 65 # Check that the Rust compiler exists. 66 if ! command -v "$RUSTC" >/dev/null; then 67 echo >&2 "***" 68 echo >&2 "*** Rust compiler '$RUSTC' c 69 echo >&2 "***" 70 exit 1 71 fi 72 73 # Check that the Rust bindings generator exist 74 if ! command -v "$BINDGEN" >/dev/null; then 75 echo >&2 "***" 76 echo >&2 "*** Rust bindings generator 77 echo >&2 "***" 78 exit 1 79 fi 80 81 # Check that the Rust compiler version is suit 82 # 83 # Non-stable and distributions' versions may h 84 rust_compiler_output=$( \ 85 LC_ALL=C "$RUSTC" --version 2>/dev/nul 86 ) || rust_compiler_code=$? 87 if [ -n "$rust_compiler_code" ]; then 88 echo >&2 "***" 89 echo >&2 "*** Running '$RUSTC' to chec 90 echo >&2 "*** code $rust_compiler_code 91 echo >&2 "***" 92 echo >&2 "$rust_compiler_output" 93 echo >&2 "***" 94 exit 1 95 fi 96 rust_compiler_version=$( \ 97 echo "$rust_compiler_output" \ 98 | sed -nE '1s:.*rustc ([0-9]+\ 99 ) 100 if [ -z "$rust_compiler_version" ]; then 101 echo >&2 "***" 102 echo >&2 "*** Running '$RUSTC' to chec 103 echo >&2 "*** an expected output. See 104 echo >&2 "***" 105 echo >&2 "$rust_compiler_output" 106 echo >&2 "***" 107 exit 1 108 fi 109 rust_compiler_min_version=$($min_tool_version 110 rust_compiler_cversion=$(get_canonical_version 111 rust_compiler_min_cversion=$(get_canonical_ver 112 if [ "$rust_compiler_cversion" -lt "$rust_comp 113 echo >&2 "***" 114 echo >&2 "*** Rust compiler '$RUSTC' i 115 echo >&2 "*** Your version: $rust 116 echo >&2 "*** Minimum version: $rust 117 echo >&2 "***" 118 exit 1 119 fi 120 121 # Check that the Rust bindings generator is su 122 # 123 # Non-stable and distributions' versions may h 124 # 125 # The dummy parameter `workaround-for-0.69.0` 126 # (https://github.com/rust-lang/rust-bindgen/p 127 # the minimum version is upgraded past that (0 128 rust_bindings_generator_output=$( \ 129 LC_ALL=C "$BINDGEN" --version workarou 130 ) || rust_bindings_generator_code=$? 131 if [ -n "$rust_bindings_generator_code" ]; the 132 echo >&2 "***" 133 echo >&2 "*** Running '$BINDGEN' to ch 134 echo >&2 "*** code $rust_bindings_gene 135 echo >&2 "***" 136 echo >&2 "$rust_bindings_generator_out 137 echo >&2 "***" 138 exit 1 139 fi 140 rust_bindings_generator_version=$( \ 141 echo "$rust_bindings_generator_output" 142 | sed -nE '1s:.*bindgen ([0-9] 143 ) 144 if [ -z "$rust_bindings_generator_version" ]; 145 echo >&2 "***" 146 echo >&2 "*** Running '$BINDGEN' to ch 147 echo >&2 "*** an expected output. See 148 echo >&2 "***" 149 echo >&2 "$rust_bindings_generator_out 150 echo >&2 "***" 151 exit 1 152 fi 153 rust_bindings_generator_min_version=$($min_too 154 rust_bindings_generator_cversion=$(get_canonic 155 rust_bindings_generator_min_cversion=$(get_can 156 if [ "$rust_bindings_generator_cversion" -lt " 157 echo >&2 "***" 158 echo >&2 "*** Rust bindings generator 159 echo >&2 "*** Your version: $rust 160 echo >&2 "*** Minimum version: $rust 161 echo >&2 "***" 162 exit 1 163 fi 164 if [ "$rust_bindings_generator_cversion" -eq 6 165 [ "$rust_bindings_generator_cversion" 166 # Distributions may have patched the i 167 if ! "$BINDGEN" $(dirname $0)/rust_is_ 168 echo >&2 "***" 169 echo >&2 "*** Rust bindings ge 170 echo >&2 "*** work due to a bu 171 echo >&2 "*** unless patched ( 172 echo >&2 "*** Your version: 173 echo >&2 "***" 174 warning=1 175 fi 176 fi 177 178 # Check that the `libclang` used by the Rust b 179 # 180 # In order to do that, first invoke `bindgen` 181 # found by `bindgen`. This step may already fa 182 # is not found, thus inform the user in such a 183 bindgen_libclang_output=$( \ 184 LC_ALL=C "$BINDGEN" $(dirname $0)/rust 185 ) || bindgen_libclang_code=$? 186 if [ -n "$bindgen_libclang_code" ]; then 187 echo >&2 "***" 188 echo >&2 "*** Running '$BINDGEN' to ch 189 echo >&2 "*** bindings generator) fail 190 echo >&2 "*** a failure to locate libc 191 echo >&2 "***" 192 echo >&2 "$bindgen_libclang_output" 193 echo >&2 "***" 194 exit 1 195 fi 196 197 # `bindgen` returned successfully, thus use th 198 # of the `libclang` found by the Rust bindings 199 # 200 # Unlike other version checks, note that this 201 # in the first line of the output, thus no `se 202 bindgen_libclang_version=$( \ 203 echo "$bindgen_libclang_output" \ 204 | sed -nE 's:.*clang version ( 205 ) 206 if [ -z "$bindgen_libclang_version" ]; then 207 echo >&2 "***" 208 echo >&2 "*** Running '$BINDGEN' to ch 209 echo >&2 "*** bindings generator) did 210 echo >&2 "*** and docs below for detai 211 echo >&2 "***" 212 echo >&2 "$bindgen_libclang_output" 213 echo >&2 "***" 214 exit 1 215 fi 216 bindgen_libclang_min_version=$($min_tool_versi 217 bindgen_libclang_cversion=$(get_canonical_vers 218 bindgen_libclang_min_cversion=$(get_canonical_ 219 if [ "$bindgen_libclang_cversion" -lt "$bindge 220 echo >&2 "***" 221 echo >&2 "*** libclang (used by the Ru 222 echo >&2 "*** Your version: $bind 223 echo >&2 "*** Minimum version: $bind 224 echo >&2 "***" 225 exit 1 226 fi 227 228 # If the C compiler is Clang, then we can also 229 # matches the `libclang` version used by the R 230 # 231 # In the future, we might be able to perform a 232 # https://github.com/rust-lang/rust-bindgen/is 233 cc_name=$($(dirname $0)/cc-version.sh $CC | cu 234 if [ "$cc_name" = Clang ]; then 235 clang_version=$( \ 236 LC_ALL=C $CC --version 2>/dev/ 237 | sed -nE '1s:.*versio 238 ) 239 if [ "$clang_version" != "$bindgen_lib 240 echo >&2 "***" 241 echo >&2 "*** libclang (used b 242 echo >&2 "*** version does not 243 echo >&2 "*** libclang versi 244 echo >&2 "*** Clang version: 245 echo >&2 "***" 246 warning=1 247 fi 248 fi 249 250 # Check that the source code for the `core` st 251 # 252 # `$KRUSTFLAGS` is passed in case the user add 253 rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print s 254 rustc_src=${RUST_LIB_SRC:-"$rustc_sysroot/lib/ 255 rustc_src_core="$rustc_src/core/src/lib.rs" 256 if [ ! -e "$rustc_src_core" ]; then 257 echo >&2 "***" 258 echo >&2 "*** Source code for the 'cor 259 echo >&2 "*** at '$rustc_src_core'." 260 echo >&2 "***" 261 exit 1 262 fi
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.