rust_is_available.sh 6.1 KB

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