rust_is_available.sh 10 KB

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