kbuild: rust_is_available: check that environment variables are set
[linux-block.git] / scripts / rust_is_available.sh
CommitLineData
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
6set -e
7
8min_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.
15get_canonical_version()
16{
17 IFS=.
18 set -- $1
19 echo $((100000 * $1 + 100 * $2 + $3))
20}
21
aac284b1
MO
22# Print a reference to the Quick Start guide in the documentation.
23print_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
e90db552
MO
31# Print an explanation about the fact that the script is meant to be called from Kbuild.
32print_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
aac284b1
MO
41# If the script fails for any reason, or if there was any warning, then
42# print a reference to the documentation on exit.
43warning=0
44trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
45
e90db552
MO
46# Check that the expected environment variables are set.
47if [ -z "${RUSTC+x}" ]; then
48 echo >&2 "***"
49 echo >&2 "*** Environment variable 'RUSTC' is not set."
50 print_kbuild_explanation
51fi
52
53if [ -z "${BINDGEN+x}" ]; then
54 echo >&2 "***"
55 echo >&2 "*** Environment variable 'BINDGEN' is not set."
56 print_kbuild_explanation
57fi
58
59if [ -z "${CC+x}" ]; then
60 echo >&2 "***"
61 echo >&2 "*** Environment variable 'CC' is not set."
62 print_kbuild_explanation
63fi
64
78521f33
MO
65# Check that the Rust compiler exists.
66if ! command -v "$RUSTC" >/dev/null; then
d824d2f9
MY
67 echo >&2 "***"
68 echo >&2 "*** Rust compiler '$RUSTC' could not be found."
69 echo >&2 "***"
78521f33
MO
70 exit 1
71fi
72
73# Check that the Rust bindings generator exists.
74if ! command -v "$BINDGEN" >/dev/null; then
d824d2f9
MY
75 echo >&2 "***"
76 echo >&2 "*** Rust bindings generator '$BINDGEN' could not be found."
77 echo >&2 "***"
78521f33
MO
78 exit 1
79fi
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`.
84rust_compiler_version=$( \
85 LC_ALL=C "$RUSTC" --version 2>/dev/null \
86 | head -n 1 \
87 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
88)
89rust_compiler_min_version=$($min_tool_version rustc)
90rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
91rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version)
92if [ "$rust_compiler_cversion" -lt "$rust_compiler_min_cversion" ]; then
d824d2f9
MY
93 echo >&2 "***"
94 echo >&2 "*** Rust compiler '$RUSTC' is too old."
95 echo >&2 "*** Your version: $rust_compiler_version"
96 echo >&2 "*** Minimum version: $rust_compiler_min_version"
97 echo >&2 "***"
78521f33
MO
98 exit 1
99fi
d824d2f9 100if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
78521f33
MO
101 echo >&2 "***"
102 echo >&2 "*** Rust compiler '$RUSTC' is too new. This may or may not work."
103 echo >&2 "*** Your version: $rust_compiler_version"
104 echo >&2 "*** Expected version: $rust_compiler_min_version"
105 echo >&2 "***"
aac284b1 106 warning=1
78521f33
MO
107fi
108
109# Check that the Rust bindings generator is suitable.
110#
111# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
112rust_bindings_generator_version=$( \
113 LC_ALL=C "$BINDGEN" --version 2>/dev/null \
114 | head -n 1 \
115 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
116)
117rust_bindings_generator_min_version=$($min_tool_version bindgen)
118rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
119rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version)
120if [ "$rust_bindings_generator_cversion" -lt "$rust_bindings_generator_min_cversion" ]; then
d824d2f9
MY
121 echo >&2 "***"
122 echo >&2 "*** Rust bindings generator '$BINDGEN' is too old."
123 echo >&2 "*** Your version: $rust_bindings_generator_version"
124 echo >&2 "*** Minimum version: $rust_bindings_generator_min_version"
125 echo >&2 "***"
78521f33
MO
126 exit 1
127fi
d824d2f9 128if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cversion" ]; then
78521f33
MO
129 echo >&2 "***"
130 echo >&2 "*** Rust bindings generator '$BINDGEN' is too new. This may or may not work."
131 echo >&2 "*** Your version: $rust_bindings_generator_version"
132 echo >&2 "*** Expected version: $rust_bindings_generator_min_version"
133 echo >&2 "***"
aac284b1 134 warning=1
78521f33
MO
135fi
136
137# Check that the `libclang` used by the Rust bindings generator is suitable.
52cae7f2
MO
138#
139# In order to do that, first invoke `bindgen` to get the `libclang` version
140# found by `bindgen`. This step may already fail if, for instance, `libclang`
141# is not found, thus inform the user in such a case.
142bindgen_libclang_output=$( \
143 LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null
144) || bindgen_libclang_code=$?
145if [ -n "$bindgen_libclang_code" ]; then
146 echo >&2 "***"
147 echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
148 echo >&2 "*** bindings generator) failed with code $bindgen_libclang_code. This may be caused by"
149 echo >&2 "*** a failure to locate libclang. See output and docs below for details:"
150 echo >&2 "***"
151 echo >&2 "$bindgen_libclang_output"
152 echo >&2 "***"
153 exit 1
154fi
155
156# `bindgen` returned successfully, thus use the output to check that the version
157# of the `libclang` found by the Rust bindings generator is suitable.
78521f33 158bindgen_libclang_version=$( \
52cae7f2 159 echo "$bindgen_libclang_output" \
78521f33
MO
160 | grep -F 'clang version ' \
161 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
162 | head -n 1 \
163)
164bindgen_libclang_min_version=$($min_tool_version llvm)
165bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
166bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version)
167if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then
d824d2f9
MY
168 echo >&2 "***"
169 echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') is too old."
170 echo >&2 "*** Your version: $bindgen_libclang_version"
171 echo >&2 "*** Minimum version: $bindgen_libclang_min_version"
172 echo >&2 "***"
78521f33
MO
173 exit 1
174fi
175
176# If the C compiler is Clang, then we can also check whether its version
177# matches the `libclang` version used by the Rust bindings generator.
178#
179# In the future, we might be able to perform a full version check, see
180# https://github.com/rust-lang/rust-bindgen/issues/2138.
dee3a6b8 181cc_name=$($(dirname $0)/cc-version.sh $CC | cut -f1 -d' ')
d824d2f9
MY
182if [ "$cc_name" = Clang ]; then
183 clang_version=$( \
dee3a6b8 184 LC_ALL=C $CC --version 2>/dev/null \
d824d2f9
MY
185 | sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
186 )
187 if [ "$clang_version" != "$bindgen_libclang_version" ]; then
188 echo >&2 "***"
189 echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN')"
190 echo >&2 "*** version does not match Clang's. This may be a problem."
191 echo >&2 "*** libclang version: $bindgen_libclang_version"
192 echo >&2 "*** Clang version: $clang_version"
193 echo >&2 "***"
aac284b1 194 warning=1
78521f33
MO
195 fi
196fi
197
198# Check that the source code for the `core` standard library exists.
199#
200# `$KRUSTFLAGS` is passed in case the user added `--sysroot`.
201rustc_sysroot=$("$RUSTC" $KRUSTFLAGS --print sysroot)
202rustc_src=${RUST_LIB_SRC:-"$rustc_sysroot/lib/rustlib/src/rust/library"}
203rustc_src_core="$rustc_src/core/src/lib.rs"
204if [ ! -e "$rustc_src_core" ]; then
d824d2f9
MY
205 echo >&2 "***"
206 echo >&2 "*** Source code for the 'core' standard library could not be found"
207 echo >&2 "*** at '$rustc_src_core'."
208 echo >&2 "***"
78521f33
MO
209 exit 1
210fi