]>
Commit | Line | Data |
---|---|---|
78521f33 MO |
1 | #!/bin/sh |
2 | # SPDX-License-Identifier: GPL-2.0 | |
3 | # | |
4 | # Tests whether a suitable Rust toolchain is available. | |
78521f33 MO |
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 | ||
22 | # Check that the Rust compiler exists. | |
23 | if ! command -v "$RUSTC" >/dev/null; then | |
d824d2f9 MY |
24 | echo >&2 "***" |
25 | echo >&2 "*** Rust compiler '$RUSTC' could not be found." | |
26 | echo >&2 "***" | |
78521f33 MO |
27 | exit 1 |
28 | fi | |
29 | ||
30 | # Check that the Rust bindings generator exists. | |
31 | if ! command -v "$BINDGEN" >/dev/null; then | |
d824d2f9 MY |
32 | echo >&2 "***" |
33 | echo >&2 "*** Rust bindings generator '$BINDGEN' could not be found." | |
34 | echo >&2 "***" | |
78521f33 MO |
35 | exit 1 |
36 | fi | |
37 | ||
38 | # Check that the Rust compiler version is suitable. | |
39 | # | |
40 | # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`. | |
41 | rust_compiler_version=$( \ | |
42 | LC_ALL=C "$RUSTC" --version 2>/dev/null \ | |
43 | | head -n 1 \ | |
44 | | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \ | |
45 | ) | |
46 | rust_compiler_min_version=$($min_tool_version rustc) | |
47 | rust_compiler_cversion=$(get_canonical_version $rust_compiler_version) | |
48 | rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version) | |
49 | if [ "$rust_compiler_cversion" -lt "$rust_compiler_min_cversion" ]; then | |
d824d2f9 MY |
50 | echo >&2 "***" |
51 | echo >&2 "*** Rust compiler '$RUSTC' is too old." | |
52 | echo >&2 "*** Your version: $rust_compiler_version" | |
53 | echo >&2 "*** Minimum version: $rust_compiler_min_version" | |
54 | echo >&2 "***" | |
78521f33 MO |
55 | exit 1 |
56 | fi | |
d824d2f9 | 57 | if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then |
78521f33 MO |
58 | echo >&2 "***" |
59 | echo >&2 "*** Rust compiler '$RUSTC' is too new. This may or may not work." | |
60 | echo >&2 "*** Your version: $rust_compiler_version" | |
61 | echo >&2 "*** Expected version: $rust_compiler_min_version" | |
62 | echo >&2 "***" | |
63 | fi | |
64 | ||
65 | # Check that the Rust bindings generator is suitable. | |
66 | # | |
67 | # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`. | |
68 | rust_bindings_generator_version=$( \ | |
69 | LC_ALL=C "$BINDGEN" --version 2>/dev/null \ | |
70 | | head -n 1 \ | |
71 | | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \ | |
72 | ) | |
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 | |
d824d2f9 MY |
77 | echo >&2 "***" |
78 | echo >&2 "*** Rust bindings generator '$BINDGEN' is too old." | |
79 | echo >&2 "*** Your version: $rust_bindings_generator_version" | |
80 | echo >&2 "*** Minimum version: $rust_bindings_generator_min_version" | |
81 | echo >&2 "***" | |
78521f33 MO |
82 | exit 1 |
83 | fi | |
d824d2f9 | 84 | if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then |
78521f33 MO |
85 | echo >&2 "***" |
86 | echo >&2 "*** Rust bindings generator '$BINDGEN' is too new. This may or may not work." | |
87 | echo >&2 "*** Your version: $rust_bindings_generator_version" | |
88 | echo >&2 "*** Expected version: $rust_bindings_generator_min_version" | |
89 | echo >&2 "***" | |
90 | fi | |
91 | ||
92 | # Check that the `libclang` used by the Rust bindings generator is suitable. | |
93 | bindgen_libclang_version=$( \ | |
94 | LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null \ | |
95 | | grep -F 'clang version ' \ | |
96 | | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \ | |
97 | | head -n 1 \ | |
98 | ) | |
99 | bindgen_libclang_min_version=$($min_tool_version llvm) | |
100 | bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version) | |
101 | bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version) | |
102 | if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then | |
d824d2f9 MY |
103 | echo >&2 "***" |
104 | echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') is too old." | |
105 | echo >&2 "*** Your version: $bindgen_libclang_version" | |
106 | echo >&2 "*** Minimum version: $bindgen_libclang_min_version" | |
107 | echo >&2 "***" | |
78521f33 MO |
108 | exit 1 |
109 | fi | |
110 | ||
111 | # If the C compiler is Clang, then we can also check whether its version | |
112 | # matches the `libclang` version used by the Rust bindings generator. | |
113 | # | |
114 | # In the future, we might be able to perform a full version check, see | |
115 | # https://github.com/rust-lang/rust-bindgen/issues/2138. | |
d824d2f9 MY |
116 | cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ') |
117 | if [ "$cc_name" = Clang ]; then | |
118 | clang_version=$( \ | |
119 | LC_ALL=C "$CC" --version 2>/dev/null \ | |
120 | | sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p' | |
121 | ) | |
122 | if [ "$clang_version" != "$bindgen_libclang_version" ]; then | |
123 | echo >&2 "***" | |
124 | echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN')" | |
125 | echo >&2 "*** version does not match Clang's. This may be a problem." | |
126 | echo >&2 "*** libclang version: $bindgen_libclang_version" | |
127 | echo >&2 "*** Clang version: $clang_version" | |
128 | echo >&2 "***" | |
78521f33 MO |
129 | fi |
130 | fi | |
131 | ||
132 | # Check that the source code for the `core` standard library exists. | |
133 | # | |
134 | # `$KRUSTFLAGS` is passed in case the user added `--sysroot`. | |
135 | rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print sysroot) | |
136 | rustc_src=${RUST_LIB_SRC:-"$rustc_sysroot/lib/rustlib/src/rust/library"} | |
137 | rustc_src_core="$rustc_src/core/src/lib.rs" | |
138 | if [ ! -e "$rustc_src_core" ]; then | |
d824d2f9 MY |
139 | echo >&2 "***" |
140 | echo >&2 "*** Source code for the 'core' standard library could not be found" | |
141 | echo >&2 "*** at '$rustc_src_core'." | |
142 | echo >&2 "***" | |
78521f33 MO |
143 | exit 1 |
144 | fi |