Skip to contentSkip to conversation

`scripts/rust_is_available.sh` improvements

PATCH Applied Miguel Ojeda 7 participants 40 messages Jun 16, 2023 – Aug 9, 2023

Subsystems (3)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

v2 cover `scripts/rust_is_available.sh` improvements

This is the patch series to improve scripts/rust_is_available.sh.

The major addition in v2 is the test suite in the last commit. I added it because I wanted to have a proper way to test any further changes to it (such as the suggested set -- idea to avoid forking by Masahiro), and so that adding new checks was easier to justify too (i.e. vs. the added complexity).

In addition, there are also a few new checks in the script, to cover for even some more cases, which hopefully make problematic setups easier to identify and solve by users building the kernel. For instance, running the script externally gives:

    $ scripts/rust_is_available.sh
    ***
    *** Environment variable 'RUSTC' is not set.
    ***
    *** This script is intended to be called from Kbuild.
    *** Please use the 'rustavailable' target to call it instead.
    *** Otherwise, the results may not be meaningful.
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***

I also changed it to avoid setting -e as Masahiro suggested. Similarly, I now check for $RUSTC, $BINDGEN and $CC, instead of $MAKEFLAGS, as he also suggested (but I gave it their own error message rather than use the ${CC?: is not set} approach. This goes in line with the reasons outlined above, i.e. trying to give users a clear error of what step exactly failed).

In the test suite I included previously problematic compiler version strings we got reports for. The test suite covers all current branches in the script, and we should keep it that way in the future.

The patch series also include Masahiro's patch to remove the -v option, as well as Russell's patch for supporting multiple arguments in $CC.

All in all, this should solve all the issues we got so far (unless I have missed something) and improve things further with the new checks plus the test suite to hopefully have an easier time in the future.

Testers for this one are appreciated, especially if you have uncommon or custom setups for building the kernel.

This could go through either the Kbuild or the Rust tree.

Masahiro Yamada (1):
kbuild: rust_is_available: remove -v option

Miguel Ojeda (9):
docs: rust: add paragraph about finding a suitable libclang
kbuild: rust_is_available: print docs reference
kbuild: rust_is_available: add check for bindgen invocation
kbuild: rust_is_available: check that environment variables are set
kbuild: rust_is_available: fix confusion when a version appears in the
path
kbuild: rust_is_available: normalize version matching
kbuild: rust_is_available: handle failures calling $RUSTC/$BINDGEN
kbuild: rust_is_available: check that output looks as expected
kbuild: rust_is_available: add test suite

Russell Currey (1):
kbuild: rust_is_available: fix version check when CC has multiple
arguments

 Documentation/rust/quick-start.rst |  17 ++
 Makefile                           |   4 +-
 scripts/rust_is_available.sh       | 233 +++++++++++++------
 scripts/rust_is_available_test.py  | 346 +++++++++++++++++++++++++++++
 4 files changed, 532 insertions(+), 68 deletions(-)
 create mode 100755 scripts/rust_is_available_test.py

base-commit: 858fd168a95c5b9669aac8db6c14a9aeab446375

Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH v2 1/11 kbuild: rust_is_available: remove -v option

From: Masahiro Yamada <[email protected]>

The -v option is passed when this script is invoked from Makefile, but not when invoked from Kconfig.

As you can see in scripts/Kconfig.include, the 'success' macro suppresses stdout and stderr anyway, so this script does not need to be quiet.

---
 Makefile                     |  4 +-
 scripts/rust_is_available.sh | 96 +++++++++++++++---------------------
 2 files changed, 42 insertions(+), 58 deletions(-)
Makefile +2 -2
index 0d3a9d3e73c1..f0c50c7bd6d2 100644
@@ -1289,7 +1289,7 @@ prepare0: archprepare
 # All the preparing..
 prepare: prepare0
 ifdef CONFIG_RUST
-	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh -v
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
 	$(Q)$(MAKE) $(build)=rust
 endif
 
@@ -1823,7 +1823,7 @@ $(DOC_TARGETS):
 # "Is Rust available?" target
 PHONY += rustavailable
 rustavailable:
-	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh -v && echo "Rust is available!"
+	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
 
 # Documentation target
 #
scripts/rust_is_available.sh +40 -56

146 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v2 1/11
#2 lore raw

Re: [PATCH v2 01/11] kbuild: rust_is_available: remove -v option

Quoted 8 lines · Miguel Ojeda

From: Masahiro Yamada <[email protected]>

The -v option is passed when this script is invoked from Makefile, but not when invoked from Kconfig.

As you can see in scripts/Kconfig.include, the 'success' macro suppresses stdout and stderr anyway, so this script does not need to be quiet.

Quoted 9 lines

---

author maintainer
#3 lore raw

PATCH v2 2/11 kbuild: rust_is_available: fix version check when CC has multiple arguments

In reply to Miguel Ojeda

From: Russell Currey <[email protected]>

rust_is_available.sh uses cc-version.sh to identify which C compiler is in use, as scripts/Kconfig.include does. cc-version.sh isn't designed to be able to handle multiple arguments in one variable, i.e. "ccache clang". Its invocation in rust_is_available.sh quotes "$CC", which makes $1 == "ccache clang" instead of the intended $1 == ccache & $2 == clang.

cc-version.sh could also be changed to handle having "ccache clang" as one argument, but it only has the one consumer upstream, making it simpler to fix the caller here.

Fixes 78521f3399ab ("scripts: add `rust_is_available.sh`")
[ Reworded title prefix and reflow line to 75 columns. ]
---
 scripts/rust_is_available.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/rust_is_available.sh +2 -2
index f43a010eaf30..0c9be438e4cd 100755
@@ -113,10 +113,10 @@ fi
 #
 # In the future, we might be able to perform a full version check, see
 # https://github.com/rust-lang/rust-bindgen/issues/2138.
-cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ')
+cc_name=$($(dirname $0)/cc-version.sh $CC | cut -f1 -d' ')
 if [ "$cc_name" = Clang ]; then
 	clang_version=$( \
-		LC_ALL=C "$CC" --version 2>/dev/null \
+		LC_ALL=C $CC --version 2>/dev/null \
 			| sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 	)
 	if [ "$clang_version" != "$bindgen_libclang_version" ]; then
Signature · 1 line
2.41.0
Reviewed-by on v2 2/11
#4 lore raw

Re: [PATCH v2 02/11] kbuild: rust_is_available: fix version check when CC has multiple arguments

Quoted 12 lines · Miguel Ojeda

From: Russell Currey <[email protected]>

rust_is_available.sh uses cc-version.sh to identify which C compiler is in use, as scripts/Kconfig.include does. cc-version.sh isn't designed to be able to handle multiple arguments in one variable, i.e. "ccache clang". Its invocation in rust_is_available.sh quotes "$CC", which makes $1 == "ccache clang" instead of the intended $1 == ccache & $2 == clang.

cc-version.sh could also be changed to handle having "ccache clang" as one argument, but it only has the one consumer upstream, making it simpler to fix the caller here.

Quoted 7 lines
Fixes 78521f3399ab ("scripts: add `rust_is_available.sh`")
[ Reworded title prefix and reflow line to 75 columns. ]

---
[...]

maintainer Reviewed-by on v2 2/11
#5 lore raw

Re: [PATCH v2 02/11] kbuild: rust_is_available: fix version check when CC has multiple arguments

In reply to Miguel Ojeda
Quoted 12 lines · Miguel Ojeda

From: Russell Currey <[email protected]>

rust_is_available.sh uses cc-version.sh to identify which C compiler is in use, as scripts/Kconfig.include does. cc-version.sh isn't designed to be able to handle multiple arguments in one variable, i.e. "ccache clang". Its invocation in rust_is_available.sh quotes "$CC", which makes $1 == "ccache clang" instead of the intended $1 == ccache & $2 == clang.

cc-version.sh could also be changed to handle having "ccache clang" as one argument, but it only has the one consumer upstream, making it simpler to fix the caller here.

Quoted 5 lines
Fixes 78521f3399ab ("scripts: add `rust_is_available.sh`")
[ Reworded title prefix and reflow line to 75 columns. ]
Quoted 24 lines
---
 scripts/rust_is_available.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/rust_is_available.sh +2 -2
index f43a010eaf30..0c9be438e4cd 100755
@@ -113,10 +113,10 @@ fi
 #
 # In the future, we might be able to perform a full version check, see
 # https://github.com/rust-lang/rust-bindgen/issues/2138.
-cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ')
+cc_name=$($(dirname $0)/cc-version.sh $CC | cut -f1 -d' ')
 if [ "$cc_name" = Clang ]; then
 	clang_version=$( \
-		LC_ALL=C "$CC" --version 2>/dev/null \
+		LC_ALL=C $CC --version 2>/dev/null \
 			| sed -nE '1s:.*version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 	)
 	if [ "$clang_version" != "$bindgen_libclang_version" ]; then
Signature · 1 line
2.41.0
author maintainer
#6 lore raw

PATCH v2 3/11 docs: rust: add paragraph about finding a suitable `libclang`

In reply to Miguel Ojeda

Sometimes users need to tweak the finding process of libclang for bindgen via the clang-sys-provided environment variables.

Thus add a paragraph to the setting up guide, including a reference to clang-sys's relevant documentation.

---
 Documentation/rust/quick-start.rst | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/rust/quick-start.rst +17 -0
index 13b7744b1e27..a635be69e062 100644
@@ -100,6 +100,23 @@ Install it via (note that this will download and build the tool from source)::
 
 	cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
 
+``bindgen`` needs to find a suitable ``libclang`` in order to work. If it is
+not found (or a different ``libclang`` than the one found should be used),
+the process can be tweaked using the environment variables understood by
+``clang-sys`` (the Rust bindings crate that ``bindgen`` uses to access
+``libclang``):
+
+* ``LLVM_CONFIG_PATH`` can be pointed to an ``llvm-config`` executable.
+
+* Or ``LIBCLANG_PATH`` can be pointed to a ``libclang`` shared library
+  or to the directory containing it.
+
+* Or ``CLANG_PATH`` can be pointed to a ``clang`` executable.
+
+For details, please see ``clang-sys``'s documentation at:
+
+	https://github.com/KyleMayes/clang-sys#environment-variables
+
 
 Requirements: Developing
 ------------------------
Signature · 1 line
2.41.0
Reviewed-by on v2 3/11
#7 lore raw

Re: [PATCH v2 03/11] docs: rust: add paragraph about finding a suitable `libclang`

Quoted 11 lines · Miguel Ojeda

Sometimes users need to tweak the finding process of libclang for bindgen via the clang-sys-provided environment variables.

Thus add a paragraph to the setting up guide, including a reference to clang-sys's relevant documentation.

---
[...]

maintainer Reviewed-by on v2 3/11
#8 lore raw

Re: [PATCH v2 03/11] docs: rust: add paragraph about finding a suitable `libclang`

In reply to Miguel Ojeda
Quoted 9 lines · Miguel Ojeda

Sometimes users need to tweak the finding process of libclang for bindgen via the clang-sys-provided environment variables.

Thus add a paragraph to the setting up guide, including a reference to clang-sys's relevant documentation.

Quoted 35 lines
---
 Documentation/rust/quick-start.rst | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/rust/quick-start.rst +17 -0
index 13b7744b1e27..a635be69e062 100644
@@ -100,6 +100,23 @@ Install it via (note that this will download and build the tool from source)::
 
 	cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
 
+``bindgen`` needs to find a suitable ``libclang`` in order to work. If it is
+not found (or a different ``libclang`` than the one found should be used),
+the process can be tweaked using the environment variables understood by
+``clang-sys`` (the Rust bindings crate that ``bindgen`` uses to access
+``libclang``):
+
+* ``LLVM_CONFIG_PATH`` can be pointed to an ``llvm-config`` executable.
+
+* Or ``LIBCLANG_PATH`` can be pointed to a ``libclang`` shared library
+  or to the directory containing it.
+
+* Or ``CLANG_PATH`` can be pointed to a ``clang`` executable.
+
+For details, please see ``clang-sys``'s documentation at:
+
+	https://github.com/KyleMayes/clang-sys#environment-variables
+
 
 Requirements: Developing
 ------------------------
Signature · 1 line
2.41.0
author maintainer
#9 lore raw

PATCH v2 4/11 kbuild: rust_is_available: print docs reference

In reply to Miguel Ojeda

People trying out the Rust support in the kernel may get warnings and errors from scripts/rust_is_available.sh from the rustavailable target or the build step.

Some of those users may be following the Quick Start guide, but others may not (likely those getting warnings from the build step instead of the target).

While the messages are fairly clear on what the problem is, it may not be clear how to solve the particular issue, especially for those not aware of the documentation.

We could add all sorts of details on the script for each one, but it is better to point users to the documentation instead, where it is easily readable in different formats. It also avoids duplication.

Thus add a reference to the documentation whenever the script fails or there is at least a warning.

---
 scripts/rust_is_available.sh | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/rust_is_available.sh +17 -0
index 0c9be438e4cd..6b8131d5b547 100755
@@ -19,6 +19,20 @@ get_canonical_version()
 	echo $((100000 * $1 + 100 * $2 + $3))
 }
 
+# Print a reference to the Quick Start guide in the documentation.
+print_docs_reference()
+{
+	echo >&2 "***"
+	echo >&2 "*** Please see Documentation/rust/quick-start.rst for details"
+	echo >&2 "*** on how to set up the Rust support."
+	echo >&2 "***"
+}
+
+# If the script fails for any reason, or if there was any warning, then
+# print a reference to the documentation on exit.
+warning=0
+trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
 	echo >&2 "***"
@@ -60,6 +74,7 @@ if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
 	echo >&2 "***   Your version:     $rust_compiler_version"
 	echo >&2 "***   Expected version: $rust_compiler_min_version"
 	echo >&2 "***"
+	warning=1
 fi
 
 # Check that the Rust bindings generator is suitable.
@@ -87,6 +102,7 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
 	echo >&2 "***   Your version:     $rust_bindings_generator_version"
 	echo >&2 "***   Expected version: $rust_bindings_generator_min_version"
 	echo >&2 "***"
+	warning=1
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
@@ -126,6 +142,7 @@ if [ "$cc_name" = Clang ]; then
 		echo >&2 "***   libclang version: $bindgen_libclang_version"
 		echo >&2 "***   Clang version:    $clang_version"
 		echo >&2 "***"
+		warning=1
 	fi
 fi
 
Signature · 1 line
2.41.0
Reviewed-by on v2 4/11
#10 lore raw

Re: [PATCH v2 04/11] kbuild: rust_is_available: print docs reference

Quoted 17 lines · Miguel Ojeda

People trying out the Rust support in the kernel may get warnings and errors from scripts/rust_is_available.sh from the rustavailable target or the build step.

Some of those users may be following the Quick Start guide, but others may not (likely those getting warnings from the build step instead of the target).

While the messages are fairly clear on what the problem is, it may not be clear how to solve the particular issue, especially for those not aware of the documentation.

We could add all sorts of details on the script for each one, but it is better to point users to the documentation instead, where it is easily readable in different formats. It also avoids duplication.

Quoted 7 lines

Thus add a reference to the documentation whenever the script fails or there is at least a warning.

---
[...]

maintainer Reviewed-by on v2 4/11
#11 lore raw

Re: [PATCH v2 04/11] kbuild: rust_is_available: print docs reference

In reply to Miguel Ojeda
Quoted 17 lines · Miguel Ojeda

People trying out the Rust support in the kernel may get warnings and errors from scripts/rust_is_available.sh from the rustavailable target or the build step.

Some of those users may be following the Quick Start guide, but others may not (likely those getting warnings from the build step instead of the target).

While the messages are fairly clear on what the problem is, it may not be clear how to solve the particular issue, especially for those not aware of the documentation.

We could add all sorts of details on the script for each one, but it is better to point users to the documentation instead, where it is easily readable in different formats. It also avoids duplication.

Quoted 5 lines

Thus add a reference to the documentation whenever the script fails or there is at least a warning.

Quoted 56 lines
---
 scripts/rust_is_available.sh | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/rust_is_available.sh +17 -0
index 0c9be438e4cd..6b8131d5b547 100755
@@ -19,6 +19,20 @@ get_canonical_version()
 	echo $((100000 * $1 + 100 * $2 + $3))
 }
 
+# Print a reference to the Quick Start guide in the documentation.
+print_docs_reference()
+{
+	echo >&2 "***"
+	echo >&2 "*** Please see Documentation/rust/quick-start.rst for details"
+	echo >&2 "*** on how to set up the Rust support."
+	echo >&2 "***"
+}
+
+# If the script fails for any reason, or if there was any warning, then
+# print a reference to the documentation on exit.
+warning=0
+trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
 	echo >&2 "***"
@@ -60,6 +74,7 @@ if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
 	echo >&2 "***   Your version:     $rust_compiler_version"
 	echo >&2 "***   Expected version: $rust_compiler_min_version"
 	echo >&2 "***"
+	warning=1
 fi
 
 # Check that the Rust bindings generator is suitable.
@@ -87,6 +102,7 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
 	echo >&2 "***   Your version:     $rust_bindings_generator_version"
 	echo >&2 "***   Expected version: $rust_bindings_generator_min_version"
 	echo >&2 "***"
+	warning=1
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
@@ -126,6 +142,7 @@ if [ "$cc_name" = Clang ]; then
 		echo >&2 "***   libclang version: $bindgen_libclang_version"
 		echo >&2 "***   Clang version:    $clang_version"
 		echo >&2 "***"
+		warning=1
 	fi
 fi
 
Signature · 1 line
2.41.0
Reviewed-by on v2 4/11
#12 lore raw

Re: [PATCH v2 04/11] kbuild: rust_is_available: print docs reference

In reply to Miguel Ojeda
Quoted 38 lines · Miguel Ojeda

People trying out the Rust support in the kernel may get warnings and errors from scripts/rust_is_available.sh from the rustavailable target or the build step.

Some of those users may be following the Quick Start guide, but others may not (likely those getting warnings from the build step instead of the target).

While the messages are fairly clear on what the problem is, it may not be clear how to solve the particular issue, especially for those not aware of the documentation.

We could add all sorts of details on the script for each one, but it is better to point users to the documentation instead, where it is easily readable in different formats. It also avoids duplication.

Thus add a reference to the documentation whenever the script fails or there is at least a warning.

---
 scripts/rust_is_available.sh | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/rust_is_available.sh +3 -0
index 0c9be438e4cd..6b8131d5b547 100755
@@ -19,6 +19,20 @@ get_canonical_version()
        echo $((100000 * $1 + 100 * $2 + $3))
 }
 
+# Print a reference to the Quick Start guide in the documentation.
+print_docs_reference()
+{

scripts/rust_is_available.sh get_canonical_version() · line 34

Quoted 10 lines
scripts/rust_is_available.sh +10 -0
+       echo >&2 "***"
+       echo >&2 "*** Please see Documentation/rust/quick-start.rst for details"
+       echo >&2 "*** on how to set up the Rust support."
+       echo >&2 "***"
+}
+
+# If the script fails for any reason, or if there was any warning, then
+# print a reference to the documentation on exit.
+warning=0
+trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT

I confirmed that
pressing Ctrl-C while rust_is_available.sh is running does not invoke print_docs_reference().
(and I believe that is the right behavior).

I also checked bash and dash work in the same way.
(I usually test both because the POSIX does not define the exact condition when the EXIT handler is invoked.)

Quoted 29 lines
scripts/rust_is_available.sh +4 -1
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
        echo >&2 "***"
@@ -60,6 +74,7 @@ if [ "$rust_compiler_cversion" -gt "$rust_compiler_min_cversion" ]; then
        echo >&2 "***   Your version:     $rust_compiler_version"
        echo >&2 "***   Expected version: $rust_compiler_min_version"
        echo >&2 "***"
+       warning=1
 fi
 
 # Check that the Rust bindings generator is suitable.
@@ -87,6 +102,7 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
        echo >&2 "***   Your version:     $rust_bindings_generator_version"
        echo >&2 "***   Expected version: $rust_bindings_generator_min_version"
        echo >&2 "***"
+       warning=1
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
@@ -126,6 +142,7 @@ if [ "$cc_name" = Clang ]; then
                echo >&2 "***   libclang version: $bindgen_libclang_version"
                echo >&2 "***   Clang version:    $clang_version"
                echo >&2 "***"
+               warning=1
        fi
 fi
 
--
Quoted 2 lines

2.41.0

--
Best Regards
Masahiro Yamada

author maintainer
#13 lore raw

PATCH v2 5/11 kbuild: rust_is_available: add check for `bindgen` invocation

In reply to Miguel Ojeda

scripts/rust_is_available.sh calls bindgen with a special header in order to check whether the libclang version in use is suitable.

However, the invocation itself may fail if, for instance, bindgen cannot locate libclang. This is fine for Kconfig (since the script will still fail and therefore disable Rust as it should), but it is pretty confusing for users of the rustavailable target given the error will be unrelated:

    ./scripts/rust_is_available.sh: 21: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
    make: *** [Makefile:1816: rustavailable] Error 2

Instead, run the bindgen invocation independently in a previous step, saving its output and return code. If it fails, then show the user a proper error message. Otherwise, continue as usual with the saved output.

Since the previous patch we show a reference to the docs, and the docs now explain how bindgen looks for libclang, thus the error message can leverage the documentation, avoiding duplication here (and making users aware of the setup guide in the documentation).

---
 scripts/rust_is_available.sh | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
scripts/rust_is_available.sh +21 -1
index 6b8131d5b547..1bdff4472cbe 100755
@@ -106,8 +106,28 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
+#
+# In order to do that, first invoke `bindgen` to get the `libclang` version
+# found by `bindgen`. This step may already fail if, for instance, `libclang`
+# is not found, thus inform the user in such a case.
+bindgen_libclang_output=$( \
+	LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null
+) || bindgen_libclang_code=$?
+if [ -n "$bindgen_libclang_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
+	echo >&2 "*** bindings generator) failed with code $bindgen_libclang_code. This may be caused by"
+	echo >&2 "*** a failure to locate libclang. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$bindgen_libclang_output"
+	echo >&2 "***"
+	exit 1
+fi
+
+# `bindgen` returned successfully, thus use the output to check that the version
+# of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
-	LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null \
+	echo "$bindgen_libclang_output" \
 		| grep -F 'clang version ' \
 		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
 		| head -n 1 \
Signature · 1 line
2.41.0
Reviewed-by on v2 5/11
#14 lore raw

Re: [PATCH v2 05/11] kbuild: rust_is_available: add check for `bindgen` invocation

Quoted 34 lines · Miguel Ojeda

scripts/rust_is_available.sh calls bindgen with a special header in order to check whether the libclang version in use is suitable.

However, the invocation itself may fail if, for instance, bindgen cannot locate libclang. This is fine for Kconfig (since the script will still fail and therefore disable Rust as it should), but it is pretty confusing for users of the rustavailable target given the error will be unrelated:

     ./scripts/rust_is_available.sh: 21: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
     make: *** [Makefile:1816: rustavailable] Error 2

Instead, run the bindgen invocation independently in a previous step, saving its output and return code. If it fails, then show the user a proper error message. Otherwise, continue as usual with the saved output.

Since the previous patch we show a reference to the docs, and the docs now explain how bindgen looks for libclang, thus the error message can leverage the documentation, avoiding duplication here (and making users aware of the setup guide in the documentation).

Quoted 2 lines

---
[...]

maintainer Reviewed-by on v2 5/11
#15 lore raw

Re: [PATCH v2 05/11] kbuild: rust_is_available: add check for `bindgen` invocation

In reply to Miguel Ojeda
Quoted 24 lines · Miguel Ojeda

scripts/rust_is_available.sh calls bindgen with a special header in order to check whether the libclang version in use is suitable.

However, the invocation itself may fail if, for instance, bindgen cannot locate libclang. This is fine for Kconfig (since the script will still fail and therefore disable Rust as it should), but it is pretty confusing for users of the rustavailable target given the error will be unrelated:

    ./scripts/rust_is_available.sh: 21: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
    make: *** [Makefile:1816: rustavailable] Error 2

Instead, run the bindgen invocation independently in a previous step, saving its output and return code. If it fails, then show the user a proper error message. Otherwise, continue as usual with the saved output.

Since the previous patch we show a reference to the docs, and the docs now explain how bindgen looks for libclang, thus the error message can leverage the documentation, avoiding duplication here (and making users aware of the setup guide in the documentation).

Quoted 10 lines
Quoted 41 lines
---
 scripts/rust_is_available.sh | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
scripts/rust_is_available.sh +21 -1
index 6b8131d5b547..1bdff4472cbe 100755
@@ -106,8 +106,28 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
+#
+# In order to do that, first invoke `bindgen` to get the `libclang` version
+# found by `bindgen`. This step may already fail if, for instance, `libclang`
+# is not found, thus inform the user in such a case.
+bindgen_libclang_output=$( \
+	LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null
+) || bindgen_libclang_code=$?
+if [ -n "$bindgen_libclang_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
+	echo >&2 "*** bindings generator) failed with code $bindgen_libclang_code. This may be caused by"
+	echo >&2 "*** a failure to locate libclang. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$bindgen_libclang_output"
+	echo >&2 "***"
+	exit 1
+fi
+
+# `bindgen` returned successfully, thus use the output to check that the version
+# of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
-	LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null \
+	echo "$bindgen_libclang_output" \
 		| grep -F 'clang version ' \
 		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
 		| head -n 1 \
Signature · 1 line
2.41.0
Reviewed-by on v2 5/11
#16 lore raw

Re: [PATCH v2 05/11] kbuild: rust_is_available: add check for `bindgen` invocation

In reply to Miguel Ojeda
Quoted 35 lines · Miguel Ojeda

scripts/rust_is_available.sh calls bindgen with a special header in order to check whether the libclang version in use is suitable.

However, the invocation itself may fail if, for instance, bindgen cannot locate libclang. This is fine for Kconfig (since the script will still fail and therefore disable Rust as it should), but it is pretty confusing for users of the rustavailable target given the error will be unrelated:

    ./scripts/rust_is_available.sh: 21: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
    make: *** [Makefile:1816: rustavailable] Error 2

Instead, run the bindgen invocation independently in a previous step, saving its output and return code. If it fails, then show the user a proper error message. Otherwise, continue as usual with the saved output.

Since the previous patch we show a reference to the docs, and the docs now explain how bindgen looks for libclang, thus the error message can leverage the documentation, avoiding duplication here (and making users aware of the setup guide in the documentation).

Quoted 1 line

---

Quoted 8 lines
 scripts/rust_is_available.sh | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
scripts/rust_is_available.sh +0 -0
index 6b8131d5b547..1bdff4472cbe 100755

scripts/rust_is_available.sh if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers · line 114

Quoted 9 lines
scripts/rust_is_available.sh +6 -0
@@ -106,8 +106,28 @@ if [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_generator_min_cvers
 fi
 
 # Check that the `libclang` used by the Rust bindings generator is suitable.
+#
+# In order to do that, first invoke `bindgen` to get the `libclang` version
+# found by `bindgen`. This step may already fail if, for instance, `libclang`
+# is not found, thus inform the user in such a case.
+bindgen_libclang_output=$( \
+       LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null

Nit.

To avoid shellcheck warnings, you can quote as follows:

"$(dirname "$0")/rust_is_available_bindgen_libclang.h"

You can do shellcheck scanning to avoid new warnings.

(Of course, 02/11 intentionally unquote $CC, though)

Quoted 23 lines
scripts/rust_is_available.sh +15 -2
+) || bindgen_libclang_code=$?
+if [ -n "$bindgen_libclang_code" ]; then
+       echo >&2 "***"
+       echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
+       echo >&2 "*** bindings generator) failed with code $bindgen_libclang_code. This may be caused by"
+       echo >&2 "*** a failure to locate libclang. See output and docs below for details:"
+       echo >&2 "***"
+       echo >&2 "$bindgen_libclang_output"
+       echo >&2 "***"
+       exit 1
+fi
+
+# `bindgen` returned successfully, thus use the output to check that the version
+# of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
-       LC_ALL=C "$BINDGEN" $(dirname $0)/rust_is_available_bindgen_libclang.h 2>&1 >/dev/null \
+       echo "$bindgen_libclang_output" \
                | grep -F 'clang version ' \
                | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
                | head -n 1 \
--

2.41.0

Signature · 2 lines
Best Regards
Masahiro Yamada
author maintainer
#17 lore raw

PATCH v2 6/11 kbuild: rust_is_available: check that environment variables are set

In reply to Miguel Ojeda

Sometimes [1] users may attempt to setup the Rust support by checking what Kbuild does and they end up finding out about scripts/rust_is_available.sh. Inevitably, they run the script directly, but unless they setup the required variables, the result of the script is not meaningful.

We could add some defaults to the variables, but that could be confusing for those that may override the defaults (compared to their kernel builds), and $CC would not be a simple default in any case.

Therefore, instead, explicitly check whether the expected variables are set ($RUSTC, $BINDGEN and $CC). If not, print an explanation about the fact that the script is meant to be called from Kbuild, since that is the most likely cause for the variables not being set.

---
 scripts/rust_is_available.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
scripts/rust_is_available.sh +29 -0
index 1bdff4472cbe..7e0368babe64 100755
@@ -28,11 +28,40 @@ print_docs_reference()
 	echo >&2 "***"
 }
 
+# Print an explanation about the fact that the script is meant to be called from Kbuild.
+print_kbuild_explanation()
+{
+	echo >&2 "***"
+	echo >&2 "*** This script is intended to be called from Kbuild."
+	echo >&2 "*** Please use the 'rustavailable' target to call it instead."
+	echo >&2 "*** Otherwise, the results may not be meaningful."
+	exit 1
+}
+
 # If the script fails for any reason, or if there was any warning, then
 # print a reference to the documentation on exit.
 warning=0
 trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
 
+# Check that the expected environment variables are set.
+if [ -z "${RUSTC+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'RUSTC' is not set."
+	print_kbuild_explanation
+fi
+
+if [ -z "${BINDGEN+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'BINDGEN' is not set."
+	print_kbuild_explanation
+fi
+
+if [ -z "${CC+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'CC' is not set."
+	print_kbuild_explanation
+fi
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
 	echo >&2 "***"
Signature · 1 line
2.41.0
Reviewed-by on v2 6/11
#18 lore raw

Re: [PATCH v2 06/11] kbuild: rust_is_available: check that environment variables are set

Quoted 16 lines · Miguel Ojeda

Sometimes [1] users may attempt to setup the Rust support by checking what Kbuild does and they end up finding out about scripts/rust_is_available.sh. Inevitably, they run the script directly, but unless they setup the required variables, the result of the script is not meaningful.

We could add some defaults to the variables, but that could be confusing for those that may override the defaults (compared to their kernel builds), and $CC would not be a simple default in any case.

Therefore, instead, explicitly check whether the expected variables are set ($RUSTC, $BINDGEN and $CC). If not, print an explanation about the fact that the script is meant to be called from Kbuild, since that is the most likely cause for the variables not being set.

Quoted 4 lines

---
[...]

maintainer Reviewed-by on v2 6/11
#19 lore raw

Re: [PATCH v2 06/11] kbuild: rust_is_available: check that environment variables are set

In reply to Miguel Ojeda
Quoted 11 lines · Miguel Ojeda

Sometimes [1] users may attempt to setup the Rust support by checking what Kbuild does and they end up finding out about scripts/rust_is_available.sh. Inevitably, they run the script directly, but unless they setup the required variables, the result of the script is not meaningful.

We could add some defaults to the variables, but that could be confusing for those that may override the defaults (compared to their kernel builds), and $CC would not be a simple default in any case.

Quoted 7 lines

Therefore, instead, explicitly check whether the expected variables are set ($RUSTC, $BINDGEN and $CC). If not, print an explanation about the fact that the script is meant to be called from Kbuild, since that is the most likely cause for the variables not being set.

Quoted 52 lines
---
 scripts/rust_is_available.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
scripts/rust_is_available.sh +29 -0
index 1bdff4472cbe..7e0368babe64 100755
@@ -28,11 +28,40 @@ print_docs_reference()
 	echo >&2 "***"
 }
 
+# Print an explanation about the fact that the script is meant to be called from Kbuild.
+print_kbuild_explanation()
+{
+	echo >&2 "***"
+	echo >&2 "*** This script is intended to be called from Kbuild."
+	echo >&2 "*** Please use the 'rustavailable' target to call it instead."
+	echo >&2 "*** Otherwise, the results may not be meaningful."
+	exit 1
+}
+
 # If the script fails for any reason, or if there was any warning, then
 # print a reference to the documentation on exit.
 warning=0
 trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
 
+# Check that the expected environment variables are set.
+if [ -z "${RUSTC+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'RUSTC' is not set."
+	print_kbuild_explanation
+fi
+
+if [ -z "${BINDGEN+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'BINDGEN' is not set."
+	print_kbuild_explanation
+fi
+
+if [ -z "${CC+x}" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Environment variable 'CC' is not set."
+	print_kbuild_explanation
+fi
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
 	echo >&2 "***"
Signature · 1 line
2.41.0
#20 lore raw

Re: [PATCH v2 06/11] kbuild: rust_is_available: check that environment variables are set

In reply to Miguel Ojeda
Quoted 42 lines · Miguel Ojeda

Sometimes [1] users may attempt to setup the Rust support by checking what Kbuild does and they end up finding out about scripts/rust_is_available.sh. Inevitably, they run the script directly, but unless they setup the required variables, the result of the script is not meaningful.

We could add some defaults to the variables, but that could be confusing for those that may override the defaults (compared to their kernel builds), and $CC would not be a simple default in any case.

Therefore, instead, explicitly check whether the expected variables are set ($RUSTC, $BINDGEN and $CC). If not, print an explanation about the fact that the script is meant to be called from Kbuild, since that is the most likely cause for the variables not being set.

---
 scripts/rust_is_available.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
scripts/rust_is_available.sh +10 -0
index 1bdff4472cbe..7e0368babe64 100755
@@ -28,11 +28,40 @@ print_docs_reference()
        echo >&2 "***"
 }
 
+# Print an explanation about the fact that the script is meant to be called from Kbuild.
+print_kbuild_explanation()
+{
+       echo >&2 "***"
+       echo >&2 "*** This script is intended to be called from Kbuild."
+       echo >&2 "*** Please use the 'rustavailable' target to call it instead."
+       echo >&2 "*** Otherwise, the results may not be meaningful."
+       exit 1
+}
+
 # If the script fails for any reason, or if there was any warning, then

scripts/rust_is_available.sh print_docs_reference() · line 51

Quoted 10 lines
scripts/rust_is_available.sh +6 -0
 # print a reference to the documentation on exit.
 warning=0
 trap 'if [ $? -ne 0 ] || [ $warning -ne 0 ]; then print_docs_reference; fi' EXIT
 
+# Check that the expected environment variables are set.
+if [ -z "${RUSTC+x}" ]; then
+       echo >&2 "***"
+       echo >&2 "*** Environment variable 'RUSTC' is not set."
+       print_kbuild_explanation
+fi

So, you want to check whether RUSTC is set or unset. (that is, you allow a case where RUSTC is set, but empty. It will eventually fail with a different error message anyway.)

$ ./scripts/rust_is_available.sh
***
*** Environment variable 'RUSTC' is not set.
***
*** This script is intended to be called from Kbuild.
*** Please use the 'rustavailable' target to call it instead.
*** Otherwise, the results may not be meaningful.
***
*** Please see Documentation/rust/quick-start.rst for details
*** on how to set up the Rust support.
***
$ RUSTC= BINDGEN= CC= ./scripts/rust_is_available.sh
***
*** Rust compiler '' could not be found.
***
***
*** Please see Documentation/rust/quick-start.rst for details
*** on how to set up the Rust support.
***

I would rather check whether RUSTC is empty or not with simpler code.

if [ -z "${RUSTC}" ]; then

         ...

fi

Quoted 19 lines
scripts/rust_is_available.sh +13 -1
+
+if [ -z "${BINDGEN+x}" ]; then
+       echo >&2 "***"
+       echo >&2 "*** Environment variable 'BINDGEN' is not set."
+       print_kbuild_explanation
+fi
+
+if [ -z "${CC+x}" ]; then
+       echo >&2 "***"
+       echo >&2 "*** Environment variable 'CC' is not set."
+       print_kbuild_explanation
+fi
+
 # Check that the Rust compiler exists.
 if ! command -v "$RUSTC" >/dev/null; then
        echo >&2 "***"
--

2.41.0

Signature · 2 lines
Best Regards
Masahiro Yamada
author maintainer
#21 lore raw

PATCH v2 7/11 kbuild: rust_is_available: fix confusion when a version appears in the path

In reply to Miguel Ojeda

bindgen's output for libclang's version check contains paths, which in turn may contain strings that look like version numbers [1][2]:

    .../6.1.0-dev/.../rust_is_available_bindgen_libclang.h:2:9: warning: clang version 11.1.0  [-W#pragma-messages], err: false

which the script will pick up as the version instead of the latter.

It is also the case that versions may appear after the actual version (e.g. distribution's version text), which was the reason behind head [3]:

    .../rust-is-available-bindgen-libclang.h:2:9: warning: clang version 13.0.0 (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false

Thus instead ask for a match after the clang version string.

---
 scripts/rust_is_available.sh | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scripts/rust_is_available.sh +1 -3
index 7e0368babe64..810691af66eb 100755
@@ -157,9 +157,7 @@ fi
 # of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
-		| grep -F 'clang version ' \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
-		| head -n 1 \
+		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 bindgen_libclang_min_version=$($min_tool_version llvm)
 bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
Signature · 1 line
2.41.0
Reviewed-by on v2 7/11
#22 lore raw

Re: [PATCH v2 07/11] kbuild: rust_is_available: fix confusion when a version appears in the path

Quoted 22 lines · Miguel Ojeda

bindgen's output for libclang's version check contains paths, which in turn may contain strings that look like version numbers [1][2]:

     .../6.1.0-dev/.../rust_is_available_bindgen_libclang.h:2:9: warning: clang version 11.1.0  [-W#pragma-messages], err: false

which the script will pick up as the version instead of the latter.

It is also the case that versions may appear after the actual version (e.g. distribution's version text), which was the reason behind head [3]:

     .../rust-is-available-bindgen-libclang.h:2:9: warning: clang version 13.0.0 (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false

Thus instead ask for a match after the clang version string.

Quoted 2 lines

---
[...]

maintainer Reviewed-by on v2 7/11
#23 lore raw

Re: [PATCH v2 07/11] kbuild: rust_is_available: fix confusion when a version appears in the path

In reply to Miguel Ojeda
Quoted 14 lines · Miguel Ojeda

bindgen's output for libclang's version check contains paths, which in turn may contain strings that look like version numbers [1][2]:

    .../6.1.0-dev/.../rust_is_available_bindgen_libclang.h:2:9: warning: clang version 11.1.0  [-W#pragma-messages], err: false

which the script will pick up as the version instead of the latter.

It is also the case that versions may appear after the actual version (e.g. distribution's version text), which was the reason behind head [3]:

    .../rust-is-available-bindgen-libclang.h:2:9: warning: clang version 13.0.0 (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false

Thus instead ask for a match after the clang version string.

Quoted 8 lines
Quoted 22 lines
---
 scripts/rust_is_available.sh | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scripts/rust_is_available.sh +1 -3
index 7e0368babe64..810691af66eb 100755
@@ -157,9 +157,7 @@ fi
 # of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
-		| grep -F 'clang version ' \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
-		| head -n 1 \
+		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 bindgen_libclang_min_version=$($min_tool_version llvm)
 bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
Signature · 1 line
2.41.0
Reviewed-by Tested-by on v2 7/11
#24 lore raw

Re: [PATCH v2 07/11] kbuild: rust_is_available: fix confusion when a version appears in the path

In reply to Miguel Ojeda
Quoted 9 lines · Miguel Ojeda
---
 scripts/rust_is_available.sh | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scripts/rust_is_available.sh +0 -0
index 7e0368babe64..810691af66eb 100755

scripts/rust_is_available.sh fi · line 163

Quoted 10 lines
scripts/rust_is_available.sh +1 -3
@@ -157,9 +157,7 @@ fi
 # of the `libclang` found by the Rust bindings generator is suitable.
 bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
-		| grep -F 'clang version ' \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
-		| head -n 1 \
+		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 bindgen_libclang_min_version=$($min_tool_version llvm)
 bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
author maintainer
#25 lore raw

PATCH v2 8/11 kbuild: rust_is_available: normalize version matching

In reply to Miguel Ojeda

In order to match the version string, sed is used in a couple cases, and grep and head in a couple others.

Make the script more consistent and easier to understand by using the same method, sed, for all of them.

This makes the version matching also a bit more strict for the changed cases, since the strings rustc and bindgen will now be required, which should be fine since rustc complains if one attempts to call it with another program name, and bindgen uses a hardcoded string.

In addition, clarify why one of the existing sed commands does not provide an address like the others.

---
 scripts/rust_is_available.sh | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/rust_is_available.sh +5 -4
index 810691af66eb..b7e0781fdea9 100755
@@ -83,8 +83,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_compiler_version=$( \
 	LC_ALL=C "$RUSTC" --version 2>/dev/null \
-		| head -n 1 \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_compiler_min_version=$($min_tool_version rustc)
 rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
@@ -111,8 +110,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_bindings_generator_version=$( \
 	LC_ALL=C "$BINDGEN" --version 2>/dev/null \
-		| head -n 1 \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
 rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
@@ -155,6 +153,9 @@ fi
 
 # `bindgen` returned successfully, thus use the output to check that the version
 # of the `libclang` found by the Rust bindings generator is suitable.
+#
+# Unlike other version checks, note that this one does not necessarily appear
+# in the first line of the output, thus no `sed` address is provided.
 bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
 		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
Signature · 1 line
2.41.0
Reviewed-by on v2 8/11
#26 lore raw

Re: [PATCH v2 08/11] kbuild: rust_is_available: normalize version matching

Quoted 12 lines · Miguel Ojeda

In order to match the version string, sed is used in a couple cases, and grep and head in a couple others.

Make the script more consistent and easier to understand by using the same method, sed, for all of them.

This makes the version matching also a bit more strict for the changed cases, since the strings rustc and bindgen will now be required, which should be fine since rustc complains if one attempts to call it with another program name, and bindgen uses a hardcoded string.

Quoted 6 lines

In addition, clarify why one of the existing sed commands does not provide an address like the others.

---
[...]

maintainer Reviewed-by on v2 8/11
#27 lore raw

Re: [PATCH v2 08/11] kbuild: rust_is_available: normalize version matching

In reply to Miguel Ojeda
Quoted 12 lines · Miguel Ojeda

In order to match the version string, sed is used in a couple cases, and grep and head in a couple others.

Make the script more consistent and easier to understand by using the same method, sed, for all of them.

This makes the version matching also a bit more strict for the changed cases, since the strings rustc and bindgen will now be required, which should be fine since rustc complains if one attempts to call it with another program name, and bindgen uses a hardcoded string.

Quoted 4 lines

In addition, clarify why one of the existing sed commands does not provide an address like the others.

Quoted 41 lines
---
 scripts/rust_is_available.sh | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/rust_is_available.sh +5 -4
index 810691af66eb..b7e0781fdea9 100755
@@ -83,8 +83,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_compiler_version=$( \
 	LC_ALL=C "$RUSTC" --version 2>/dev/null \
-		| head -n 1 \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_compiler_min_version=$($min_tool_version rustc)
 rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
@@ -111,8 +110,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_bindings_generator_version=$( \
 	LC_ALL=C "$BINDGEN" --version 2>/dev/null \
-		| head -n 1 \
-		| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
 rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
@@ -155,6 +153,9 @@ fi
 
 # `bindgen` returned successfully, thus use the output to check that the version
 # of the `libclang` found by the Rust bindings generator is suitable.
+#
+# Unlike other version checks, note that this one does not necessarily appear
+# in the first line of the output, thus no `sed` address is provided.
 bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
 		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
Signature · 1 line
2.41.0
Reviewed-by on v2 8/11
#28 lore raw

Re: [PATCH v2 08/11] kbuild: rust_is_available: normalize version matching

In reply to Miguel Ojeda
Quoted 13 lines · Miguel Ojeda

In order to match the version string, sed is used in a couple cases, and grep and head in a couple others.

Make the script more consistent and easier to understand by using the same method, sed, for all of them.

This makes the version matching also a bit more strict for the changed cases, since the strings rustc and bindgen will now be required, which should be fine since rustc complains if one attempts to call it with another program name, and bindgen uses a hardcoded string.

Quoted 4 lines

In addition, clarify why one of the existing sed commands does not provide an address like the others.

Quoted 41 lines
---
 scripts/rust_is_available.sh | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/rust_is_available.sh +5 -5
index 810691af66eb..b7e0781fdea9 100755
@@ -83,8 +83,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_compiler_version=$( \
        LC_ALL=C "$RUSTC" --version 2>/dev/null \
-               | head -n 1 \
-               | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+               | sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_compiler_min_version=$($min_tool_version rustc)
 rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
@@ -111,8 +110,7 @@ fi
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
 rust_bindings_generator_version=$( \
        LC_ALL=C "$BINDGEN" --version 2>/dev/null \
-               | head -n 1 \
-               | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
+               | sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
 rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
@@ -155,6 +153,9 @@ fi
 
 # `bindgen` returned successfully, thus use the output to check that the version
 # of the `libclang` found by the Rust bindings generator is suitable.
+#
+# Unlike other version checks, note that this one does not necessarily appear
+# in the first line of the output, thus no `sed` address is provided.
 bindgen_libclang_version=$( \
        echo "$bindgen_libclang_output" \
                | sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
--

2.41.0

Signature · 2 lines
Best Regards
Masahiro Yamada
author maintainer
#29 lore raw

PATCH v2 9/11 kbuild: rust_is_available: handle failures calling `$RUSTC`/`$BINDGEN`

In reply to Miguel Ojeda

The script already checks if $RUSTC and $BINDGEN exists via command, but the environment variables may point to a non-executable file, or the programs may fail for some other reason. While the script successfully exits with a failure as it should, the error given can be quite confusing depending on the shell and the behavior of its command. For instance, with dash:

    $ RUSTC=./mm BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
    scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "

Thus detect failure exit codes when calling $RUSTC and $BINDGEN and print a better message, in a similar way to what we do when extracting the libclang version found by bindgen.

---
 scripts/rust_is_available.sh | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
scripts/rust_is_available.sh +26 -2
index b7e0781fdea9..da8296cd9b8d 100755
@@ -81,8 +81,20 @@ fi
 # Check that the Rust compiler version is suitable.
 #
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
+rust_compiler_output=$( \
+	LC_ALL=C "$RUSTC" --version 2>/dev/null
+) || rust_compiler_code=$?
+if [ -n "$rust_compiler_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$RUSTC' to check the Rust compiler version failed with"
+	echo >&2 "*** code $rust_compiler_code. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_compiler_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_compiler_version=$( \
-	LC_ALL=C "$RUSTC" --version 2>/dev/null \
+	echo "$rust_compiler_output" \
 		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_compiler_min_version=$($min_tool_version rustc)
@@ -108,8 +120,20 @@ fi
 # Check that the Rust bindings generator is suitable.
 #
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
+rust_bindings_generator_output=$( \
+	LC_ALL=C "$BINDGEN" --version 2>/dev/null
+) || rust_bindings_generator_code=$?
+if [ -n "$rust_bindings_generator_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the Rust bindings generator version failed with"
+	echo >&2 "*** code $rust_bindings_generator_code. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_bindings_generator_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_bindings_generator_version=$( \
-	LC_ALL=C "$BINDGEN" --version 2>/dev/null \
+	echo "$rust_bindings_generator_output" \
 		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
Signature · 1 line
2.41.0
Reviewed-by on v2 9/11
#30 lore raw

Re: [PATCH v2 09/11] kbuild: rust_is_available: handle failures calling `$RUSTC`/`$BINDGEN`

Quoted 10 lines · Miguel Ojeda

The script already checks if $RUSTC and $BINDGEN exists via command, but the environment variables may point to a non-executable file, or the programs may fail for some other reason. While the script successfully exits with a failure as it should, the error given can be quite confusing depending on the shell and the behavior of its command. For instance, with dash:

     $ RUSTC=./mm BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
     scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
Quoted 8 lines

Thus detect failure exit codes when calling $RUSTC and $BINDGEN and print a better message, in a similar way to what we do when extracting the libclang version found by bindgen.

---
[...]

maintainer Reviewed-by on v2 9/11
#31 lore raw

Re: [PATCH v2 09/11] kbuild: rust_is_available: handle failures calling `$RUSTC`/`$BINDGEN`

In reply to Miguel Ojeda
Quoted 10 lines · Miguel Ojeda

The script already checks if $RUSTC and $BINDGEN exists via command, but the environment variables may point to a non-executable file, or the programs may fail for some other reason. While the script successfully exits with a failure as it should, the error given can be quite confusing depending on the shell and the behavior of its command. For instance, with dash:

    $ RUSTC=./mm BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
    scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
Quoted 6 lines

Thus detect failure exit codes when calling $RUSTC and $BINDGEN and print a better message, in a similar way to what we do when extracting the libclang version found by bindgen.

Quoted 55 lines
---
 scripts/rust_is_available.sh | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
scripts/rust_is_available.sh +26 -2
index b7e0781fdea9..da8296cd9b8d 100755
@@ -81,8 +81,20 @@ fi
 # Check that the Rust compiler version is suitable.
 #
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
+rust_compiler_output=$( \
+	LC_ALL=C "$RUSTC" --version 2>/dev/null
+) || rust_compiler_code=$?
+if [ -n "$rust_compiler_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$RUSTC' to check the Rust compiler version failed with"
+	echo >&2 "*** code $rust_compiler_code. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_compiler_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_compiler_version=$( \
-	LC_ALL=C "$RUSTC" --version 2>/dev/null \
+	echo "$rust_compiler_output" \
 		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_compiler_min_version=$($min_tool_version rustc)
@@ -108,8 +120,20 @@ fi
 # Check that the Rust bindings generator is suitable.
 #
 # Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
+rust_bindings_generator_output=$( \
+	LC_ALL=C "$BINDGEN" --version 2>/dev/null
+) || rust_bindings_generator_code=$?
+if [ -n "$rust_bindings_generator_code" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the Rust bindings generator version failed with"
+	echo >&2 "*** code $rust_bindings_generator_code. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_bindings_generator_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_bindings_generator_version=$( \
-	LC_ALL=C "$BINDGEN" --version 2>/dev/null \
+	echo "$rust_bindings_generator_output" \
 		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
Signature · 1 line
2.41.0
author maintainer
#32 lore raw

PATCH v2 10/11 kbuild: rust_is_available: check that output looks as expected

In reply to Miguel Ojeda

The script already checks for $RUSTC and $BINDGEN existing and exiting without failure. However, one may still pass an unexpected binary that does not output what the later parsing expects. The script still successfully reports a failure as expected, but the error is confusing. For instance:

    $ RUSTC=true BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
    scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***

Thus add an explicit check and a proper message for unexpected output from the called command.

Similarly, do so for the libclang version parsing, too.

---
 scripts/rust_is_available.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
scripts/rust_is_available.sh +28 -0
index da8296cd9b8d..117018946b57 100755
@@ -97,6 +97,15 @@ rust_compiler_version=$( \
 	echo "$rust_compiler_output" \
 		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$rust_compiler_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$RUSTC' to check the Rust compiler version did not return"
+	echo >&2 "*** an expected output. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_compiler_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_compiler_min_version=$($min_tool_version rustc)
 rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
 rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version)
@@ -136,6 +145,15 @@ rust_bindings_generator_version=$( \
 	echo "$rust_bindings_generator_output" \
 		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$rust_bindings_generator_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the bindings generator version did not return"
+	echo >&2 "*** an expected output. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_bindings_generator_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
 rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
 rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version)
@@ -184,6 +202,16 @@ bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
 		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$bindgen_libclang_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
+	echo >&2 "*** bindings generator) did not return an expected output. See output"
+	echo >&2 "*** and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$bindgen_libclang_output"
+	echo >&2 "***"
+	exit 1
+fi
 bindgen_libclang_min_version=$($min_tool_version llvm)
 bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
 bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version)
Signature · 1 line
2.41.0
Reviewed-by on v2 10/11
#33 lore raw

Re: [PATCH v2 10/11] kbuild: rust_is_available: check that output looks as expected

Quoted 16 lines · Miguel Ojeda

The script already checks for $RUSTC and $BINDGEN existing and exiting without failure. However, one may still pass an unexpected binary that does not output what the later parsing expects. The script still successfully reports a failure as expected, but the error is confusing. For instance:

     $ RUSTC=true BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
     scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
     ***
     *** Please see Documentation/rust/quick-start.rst for details
     *** on how to set up the Rust support.
     ***

Thus add an explicit check and a proper message for unexpected output from the called command.

Quoted 6 lines

Similarly, do so for the libclang version parsing, too.

---
[...]

maintainer Reviewed-by on v2 10/11
#34 lore raw

Re: [PATCH v2 10/11] kbuild: rust_is_available: check that output looks as expected

In reply to Miguel Ojeda
Quoted 13 lines · Miguel Ojeda

The script already checks for $RUSTC and $BINDGEN existing and exiting without failure. However, one may still pass an unexpected binary that does not output what the later parsing expects. The script still successfully reports a failure as expected, but the error is confusing. For instance:

    $ RUSTC=true BINDGEN=bindgen CC=clang scripts/rust_is_available.sh
    scripts/rust_is_available.sh: 19: arithmetic expression: expecting primary: "100000 *  + 100 *  + "
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***
Quoted 7 lines

Thus add an explicit check and a proper message for unexpected output from the called command.

Similarly, do so for the libclang version parsing, too.

Quoted 60 lines
---
 scripts/rust_is_available.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
scripts/rust_is_available.sh +28 -0
index da8296cd9b8d..117018946b57 100755
@@ -97,6 +97,15 @@ rust_compiler_version=$( \
 	echo "$rust_compiler_output" \
 		| sed -nE '1s:.*rustc ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$rust_compiler_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$RUSTC' to check the Rust compiler version did not return"
+	echo >&2 "*** an expected output. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_compiler_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_compiler_min_version=$($min_tool_version rustc)
 rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
 rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version)
@@ -136,6 +145,15 @@ rust_bindings_generator_version=$( \
 	echo "$rust_bindings_generator_output" \
 		| sed -nE '1s:.*bindgen ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$rust_bindings_generator_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the bindings generator version did not return"
+	echo >&2 "*** an expected output. See output and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$rust_bindings_generator_output"
+	echo >&2 "***"
+	exit 1
+fi
 rust_bindings_generator_min_version=$($min_tool_version bindgen)
 rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
 rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version)
@@ -184,6 +202,16 @@ bindgen_libclang_version=$( \
 	echo "$bindgen_libclang_output" \
 		| sed -nE 's:.*clang version ([0-9]+\.[0-9]+\.[0-9]+).*:\1:p'
 )
+if [ -z "$bindgen_libclang_version" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Running '$BINDGEN' to check the libclang version (used by the Rust"
+	echo >&2 "*** bindings generator) did not return an expected output. See output"
+	echo >&2 "*** and docs below for details:"
+	echo >&2 "***"
+	echo >&2 "$bindgen_libclang_output"
+	echo >&2 "***"
+	exit 1
+fi
 bindgen_libclang_min_version=$($min_tool_version llvm)
 bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
 bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version)
Signature · 1 line
2.41.0
author maintainer
#35 lore raw

PATCH v2 11/11 kbuild: rust_is_available: add test suite

In reply to Miguel Ojeda

The rust_is_available.sh script runs for everybody compiling the kernel, even if not using Rust. Therefore, it is important to ensure that the script is correct to avoid breaking people's compilation.

In addition, the script needs to be able to handle a set of subtle cases, including parsing version strings of different tools.

Therefore, maintenance of this script can be greatly eased with a set of tests.

Thus add a test suite to cover hopefully most of the setups that the script may encounter in the wild. Extra setups can be easily added later on if missing.

The script currently covers all the branches of the shell script, including several ways in which they may be entered.

Python is used for this script, since the script under test does not depend on Rust, thus hopefully making it easier for others to use if the need arises.

---
 scripts/rust_is_available_test.py | 346 ++++++++++++++++++++++++++++++
 1 file changed, 346 insertions(+)
 create mode 100755 scripts/rust_is_available_test.py
scripts/rust_is_available_test.py new file +346 -0
new file mode 100755
index 000000000000..57613fe5ed75
@@ -0,0 +1,346 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+"""Tests the `rust_is_available.sh` script.
+
+Some of the tests require the real programs to be available in `$PATH`
+under their canonical name (and with the expected versions).
+"""
+
+import enum
+import os
+import pathlib
+import stat
+import subprocess
+import tempfile
+import unittest
+
+class TestRustIsAvailable(unittest.TestCase):
+    @enum.unique
+    class Expected(enum.Enum):
+        SUCCESS = enum.auto()
+        SUCCESS_WITH_WARNINGS = enum.auto()
+        SUCCESS_WITH_EXTRA_OUTPUT = enum.auto()
+        FAILURE = enum.auto()
+
+    @classmethod
+    def generate_executable(cls, content):
+        path = pathlib.Path(cls.tempdir.name)
+        name = str(len(tuple(path.iterdir())))
+        path = path / name
+        with open(path, "w") as file_:
+            file_.write(content)
+        os.chmod(path, os.stat(path).st_mode | stat.S_IXUSR)
+        return path
+
+    @classmethod
+    def generate_clang(cls, stdout):
+        return cls.generate_executable(f"""#!/usr/bin/env python3
+import sys
+if "-E" in " ".join(sys.argv):
+    print({repr("Clang " + " ".join(cls.llvm_default_version.split(" ")))})
+else:
+    print({repr(stdout)})
+""")
+
+    @classmethod
+    def generate_rustc(cls, stdout):
+        return cls.generate_executable(f"""#!/usr/bin/env python3
+import sys
+if "--print sysroot" in " ".join(sys.argv):
+    print({repr(cls.rust_default_sysroot)})
+else:
+    print({repr(stdout)})
+""")
+
+    @classmethod
+    def generate_bindgen(cls, version_stdout, libclang_stderr):
+        return cls.generate_executable(f"""#!/usr/bin/env python3
+import sys
+if "rust_is_available_bindgen_libclang.h" in " ".join(sys.argv):
+    print({repr(libclang_stderr)}, file=sys.stderr)
+else:
+    print({repr(version_stdout)})
+""")
+
+    @classmethod
+    def generate_bindgen_version(cls, stdout):
+        return cls.generate_bindgen(stdout, cls.bindgen_default_bindgen_libclang_stderr)
+
+    @classmethod
+    def generate_bindgen_libclang(cls, stderr):
+        return cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, stderr)
+
+    @classmethod
+    def setUpClass(cls):
+        cls.tempdir = tempfile.TemporaryDirectory()
+
+        cls.missing = pathlib.Path(cls.tempdir.name) / "missing"
+
+        cls.nonexecutable = pathlib.Path(cls.tempdir.name) / "nonexecutable"
+        with open(cls.nonexecutable, "w") as file_:
+            file_.write("nonexecutable")
+
+        cls.unexpected_binary = "true"
+
+        cls.rustc_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "rustc")).decode().strip()
+        cls.bindgen_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "bindgen")).decode().strip()
+        cls.llvm_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "llvm")).decode().strip()
+        cls.rust_default_sysroot = subprocess.check_output(("rustc", "--print", "sysroot")).decode().strip()
+
+        cls.bindgen_default_bindgen_version_stdout = f"bindgen {cls.bindgen_default_version}"
+        cls.bindgen_default_bindgen_libclang_stderr = f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {cls.llvm_default_version} [-W#pragma-messages], err: false"
+
+        cls.default_rustc = cls.generate_rustc(f"rustc {cls.rustc_default_version}")
+        cls.default_bindgen =  cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, cls.bindgen_default_bindgen_libclang_stderr)
+        cls.default_cc = cls.generate_clang(f"clang version {cls.llvm_default_version}")
+
+    def run_script(self, expected, override_env):
+        env = {
+            "RUSTC": self.default_rustc,
+            "BINDGEN": self.default_bindgen,
+            "CC": self.default_cc,
+        }
+
+        for key, value in override_env.items():
+            if value is None:
+                del env[key]
+                continue
+            env[key] = value
+
+        result = subprocess.run("scripts/rust_is_available.sh", env=env, capture_output=True)
+
+        # The script should never output anything to `stdout`.
+        self.assertEqual(result.stdout, b"")
+
+        if expected == self.Expected.SUCCESS:
+            # When expecting a success, the script should return 0
+            # and it should not output anything to `stderr`.
+            self.assertEqual(result.returncode, 0)
+            self.assertEqual(result.stderr, b"")
+        elif expected == self.Expected.SUCCESS_WITH_EXTRA_OUTPUT:
+            # When expecting a success with extra output (that is not warnings,
+            # which is the common case), the script should return 0 and it
+            # should output at least something to `stderr` (the output should
+            # be checked further by the test).
+            self.assertEqual(result.returncode, 0)
+            self.assertNotEqual(result.stderr, b"")
+        elif expected == self.Expected.SUCCESS_WITH_WARNINGS:
+            # When expecting a success with warnings, the script should return 0
+            # and it should output at least the instructions to `stderr`.
+            self.assertEqual(result.returncode, 0)
+            self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
+        else:
+            # When expecting a failure, the script should return non-0
+            # and it should output at least the instructions to `stderr`.
+            self.assertNotEqual(result.returncode, 0)
+            self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
+
+        # The output will generally be UTF-8 (i.e. unless the user has
+        # put strange values in the environment).
+        result.stderr = result.stderr.decode()
+
+        return result
+
+    def test_rustc_unset(self):
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": None })
+        self.assertIn("Environment variable 'RUSTC' is not set.", result.stderr)
+        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
+
+    def test_bindgen_unset(self):
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": None })
+        self.assertIn("Environment variable 'BINDGEN' is not set.", result.stderr)
+        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
+
+    def test_cc_unset(self):
+        result = self.run_script(self.Expected.FAILURE, { "CC": None })
+        self.assertIn("Environment variable 'CC' is not set.", result.stderr)
+        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
+
+    def test_rustc_missing(self):
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.missing })
+        self.assertIn(f"Rust compiler '{self.missing}' could not be found.", result.stderr)
+
+    def test_bindgen_missing(self):
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.missing })
+        self.assertIn(f"Rust bindings generator '{self.missing}' could not be found.", result.stderr)
+
+    def test_rustc_nonexecutable(self):
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.nonexecutable })
+        self.assertIn(f"Running '{self.nonexecutable}' to check the Rust compiler version failed with", result.stderr)
+
+    def test_rustc_unexpected_binary(self):
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.unexpected_binary })
+        self.assertIn(f"Running '{self.unexpected_binary}' to check the Rust compiler version did not return", result.stderr)
+
+    def test_rustc_unexpected_name(self):
+        rustc = self.generate_rustc(f"unexpected {self.rustc_default_version} (a8314ef7d 2022-06-27)")
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
+        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
+
+    def test_rustc_unexpected_version(self):
+        rustc = self.generate_rustc("rustc unexpected (a8314ef7d 2022-06-27)")
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
+        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
+
+    def test_rustc_no_minor(self):
+        rustc = self.generate_rustc(f"rustc {'.'.join(self.rustc_default_version.split('.')[:2])} (a8314ef7d 2022-06-27)")
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
+        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
+
+    def test_rustc_old_version(self):
+        rustc = self.generate_rustc("rustc 1.60.0 (a8314ef7d 2022-06-27)")
+        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
+        self.assertIn(f"Rust compiler '{rustc}' is too old.", result.stderr)
+
+    def test_rustc_new_version(self):
+        rustc = self.generate_rustc("rustc 1.999.0 (a8314ef7d 2099-06-27)")
+        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "RUSTC": rustc })
+        self.assertIn(f"Rust compiler '{rustc}' is too new. This may or may not work.", result.stderr)
+
+    def test_bindgen_nonexecutable(self):
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.nonexecutable })
+        self.assertIn(f"Running '{self.nonexecutable}' to check the Rust bindings generator version failed with", result.stderr)
+
+    def test_bindgen_unexpected_binary(self):
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.unexpected_binary })
+        self.assertIn(f"Running '{self.unexpected_binary}' to check the bindings generator version did not return", result.stderr)
+
+    def test_bindgen_unexpected_name(self):
+        bindgen = self.generate_bindgen_version(f"unexpected {self.bindgen_default_version}")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
+
+    def test_bindgen_unexpected_version(self):
+        bindgen = self.generate_bindgen_version("bindgen unexpected")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
+
+    def test_bindgen_no_minor(self):
+        bindgen = self.generate_bindgen_version(f"bindgen {'.'.join(self.bindgen_default_version.split('.')[:2])}")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
+
+    def test_bindgen_old_version(self):
+        bindgen = self.generate_bindgen_version("bindgen 0.50.0")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"Rust bindings generator '{bindgen}' is too old.", result.stderr)
+
+    def test_bindgen_new_version(self):
+        bindgen = self.generate_bindgen_version("bindgen 0.999.0")
+        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
+        self.assertIn(f"Rust bindings generator '{bindgen}' is too new. This may or may not work.", result.stderr)
+
+    def test_bindgen_libclang_failure(self):
+        for env in (
+            { "LLVM_CONFIG_PATH": self.missing },
+            { "LIBCLANG_PATH": self.missing },
+            { "CLANG_PATH": self.missing },
+        ):
+            with self.subTest(env=env):
+                result = self.run_script(self.Expected.FAILURE, env | { "PATH": os.environ["PATH"], "BINDGEN": "bindgen" })
+                self.assertIn("Running 'bindgen' to check the libclang version (used by the Rust", result.stderr)
+                self.assertIn("bindings generator) failed with code ", result.stderr)
+
+    def test_bindgen_libclang_unexpected_version(self):
+        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version unexpected [-W#pragma-messages], err: false")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"Running '{bindgen}' to check the libclang version (used by the Rust", result.stderr)
+        self.assertIn("bindings generator) did not return an expected output. See output", result.stderr)
+
+    def test_bindgen_libclang_old_version(self):
+        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version 10.0.0 [-W#pragma-messages], err: false")
+        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
+        self.assertIn(f"libclang (used by the Rust bindings generator '{bindgen}') is too old.", result.stderr)
+
+    def test_clang_matches_bindgen_libclang_different_bindgen(self):
+        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version 999.0.0 [-W#pragma-messages], err: false")
+        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
+        self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
+
+    def test_clang_matches_bindgen_libclang_different_clang(self):
+        cc = self.generate_clang("clang version 999.0.0")
+        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "CC": cc })
+        self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
+
+    def test_rustc_src_core_krustflags(self):
+        result = self.run_script(self.Expected.FAILURE, { "PATH": os.environ["PATH"], "RUSTC": "rustc", "KRUSTFLAGS": f"--sysroot={self.missing}" })
+        self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
+
+    def test_rustc_src_core_rustlibsrc(self):
+        result = self.run_script(self.Expected.FAILURE, { "RUST_LIB_SRC": self.missing })
+        self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
+
+    def test_success_cc_unknown(self):
+        result = self.run_script(self.Expected.SUCCESS_WITH_EXTRA_OUTPUT, { "CC": self.missing })
+        self.assertIn("unknown C compiler", result.stderr)
+
+    def test_success_cc_multiple_arguments_ccache(self):
+        clang = self.generate_clang(f"""Ubuntu clang version {self.llvm_default_version}-1ubuntu1
+Target: x86_64-pc-linux-gnu
+Thread model: posix
+InstalledDir: /usr/bin
+""")
+        result = self.run_script(self.Expected.SUCCESS, { "CC": f"{clang} clang" })
+
+    def test_success_rustc_version(self):
+        for rustc_stdout in (
+            f"rustc {self.rustc_default_version} (a8314ef7d 2022-06-27)",
+            f"rustc {self.rustc_default_version}-dev (a8314ef7d 2022-06-27)",
+            f"rustc {self.rustc_default_version}-1.60.0 (a8314ef7d 2022-06-27)",
+        ):
+            with self.subTest(rustc_stdout=rustc_stdout):
+                rustc = self.generate_rustc(rustc_stdout)
+                result = self.run_script(self.Expected.SUCCESS, { "RUSTC": rustc })
+
+    def test_success_bindgen_version(self):
+        for bindgen_stdout in (
+            f"bindgen {self.bindgen_default_version}",
+            f"bindgen {self.bindgen_default_version}-dev",
+            f"bindgen {self.bindgen_default_version}-0.999.0",
+        ):
+            with self.subTest(bindgen_stdout=bindgen_stdout):
+                bindgen = self.generate_bindgen_version(bindgen_stdout)
+                result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
+
+    def test_success_bindgen_libclang(self):
+        for stderr in (
+            f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1) [-W#pragma-messages], err: false",
+            f"/home/jd/Documents/dev/kernel-module-flake/linux-6.1/outputs/dev/lib/modules/6.1.0-development/source/scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version}  [-W#pragma-messages], err: false",
+            f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false",
+            f"""
+/nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
+scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version}  [-W#pragma-messages], err: false
+""",
+            f"""
+/nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1.0-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
+/home/jd/Documents/dev/kernel-module-flake/linux-6.1/outputs/dev/lib/modules/6.1.0-development/source/scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false
+"""
+        ):
+            with self.subTest(stderr=stderr):
+                bindgen = self.generate_bindgen_libclang(stderr)
+                result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
+
+    def test_success_clang_version(self):
+        for clang_stdout in (
+            f"clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1)",
+            f"clang version {self.llvm_default_version}-dev",
+            f"clang version {self.llvm_default_version}-2~ubuntu20.04.1",
+            f"Ubuntu clang version {self.llvm_default_version}-2~ubuntu20.04.1",
+        ):
+            with self.subTest(clang_stdout=clang_stdout):
+                clang = self.generate_clang(clang_stdout)
+                result = self.run_script(self.Expected.SUCCESS, { "CC": clang })
+
+    def test_success_real_programs(self):
+        for cc in ["gcc", "clang"]:
+            with self.subTest(cc=cc):
+                result = self.run_script(self.Expected.SUCCESS, {
+                    "PATH": os.environ["PATH"],
+                    "RUSTC": "rustc",
+                    "BINDGEN": "bindgen",
+                    "CC": cc,
+                })
+
+if __name__ == "__main__":
+    unittest.main()
Signature · 1 line
2.41.0
Reviewed-by on v2 11/11
#36 lore raw

Re: [PATCH v2 11/11] kbuild: rust_is_available: add test suite

Quoted 17 lines · Miguel Ojeda

The rust_is_available.sh script runs for everybody compiling the kernel, even if not using Rust. Therefore, it is important to ensure that the script is correct to avoid breaking people's compilation.

In addition, the script needs to be able to handle a set of subtle cases, including parsing version strings of different tools.

Therefore, maintenance of this script can be greatly eased with a set of tests.

Thus add a test suite to cover hopefully most of the setups that the script may encounter in the wild. Extra setups can be easily added later on if missing.

The script currently covers all the branches of the shell script, including several ways in which they may be entered.

Quoted 7 lines

Python is used for this script, since the script under test does not depend on Rust, thus hopefully making it easier for others to use if the need arises.

---
[...]

#37 lore raw
In reply to Miguel Ojeda
Quoted 40 lines · Miguel Ojeda

This is the patch series to improve scripts/rust_is_available.sh.

The major addition in v2 is the test suite in the last commit. I added it because I wanted to have a proper way to test any further changes to it (such as the suggested set -- idea to avoid forking by Masahiro), and so that adding new checks was easier to justify too (i.e. vs. the added complexity).

In addition, there are also a few new checks in the script, to cover for even some more cases, which hopefully make problematic setups easier to identify and solve by users building the kernel. For instance, running the script externally gives:

    $ scripts/rust_is_available.sh
    ***
    *** Environment variable 'RUSTC' is not set.
    ***
    *** This script is intended to be called from Kbuild.
    *** Please use the 'rustavailable' target to call it instead.
    *** Otherwise, the results may not be meaningful.
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***

I also changed it to avoid setting -e as Masahiro suggested. Similarly, I now check for $RUSTC, $BINDGEN and $CC, instead of $MAKEFLAGS, as he also suggested (but I gave it their own error message rather than use the ${CC?: is not set} approach. This goes in line with the reasons outlined above, i.e. trying to give users a clear error of what step exactly failed).

In the test suite I included previously problematic compiler version strings we got reports for. The test suite covers all current branches in the script, and we should keep it that way in the future.

The patch series also include Masahiro's patch to remove the -v option, as well as Russell's patch for supporting multiple arguments in $CC.

Quoted 6 lines

All in all, this should solve all the issues we got so far (unless I have missed something) and improve things further with the new checks plus the test suite to hopefully have an easier time in the future.

Testers for this one are appreciated, especially if you have uncommon or custom setups for building the kernel.

I gave this patch series a spin and is a nice improvement when trying to use a broken setup.

When previously I had:
./scripts/rust_is_available.sh: line 21: 100000 * + 100 * + : syntax error: operand expected (error token is "+ ")
make: *** [Makefile:1883: rustavailable] Error 1

Now I have:

***
*** Running 'bindgen' to check the libclang version (used by the Rust
*** bindings generator) failed with code 101. This may be caused by
*** a failure to locate libclang. See output and docs below for details:
***
thread 'main' panicked at 'Unable to find libclang: "the libclang shared library at /opt/sdk/sysroots/x86_64-pokysdk-linux/usr/lib/libclang.so.14.0.3 could not be opened: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by /opt/idc23/sysroots/x86_64-pokysdk-linux/usr/lib/libclang.so.14.0.3)"', /home/heghedusrazvan/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.56.0/src/lib.rs:1922:31
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
***
***
*** Please see Documentation/rust/quick-start.rst for details
*** on how to set up the Rust support.
***
make: *** [Makefile:1883: rustavailable] Error 1

or:

***
*** Rust compiler 'rustc' is too new. This may or may not work.
*** Your version: 1.70.0
*** Expected version: 1.68.2
***
***
*** Running 'bindgen' to check the libclang version (used by the Rust
*** bindings generator) failed with code 101. This may be caused by
*** a failure to locate libclang. See output and docs below for details:
***
thread 'main' panicked at 'Unable to find libclang: "the libclang shared library at /opt/sdk/sysroots/x86_64-pokysdk-linux/usr/lib/libclang.so.14.0.3 could not be opened: libncurses.so.5: cannot open shared object file: No such file or directory"', /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.56.0/src/lib.rs:1922:31
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
***
***
*** Please see Documentation/rust/quick-start.rst for details
*** on how to set up the Rust support.
***
make: *** [Makefile:1883: rustavailable] Error 1

-- Razvan

Quoted 33 lines

This could go through either the Kbuild or the Rust tree.

Masahiro Yamada (1):
kbuild: rust_is_available: remove -v option

Miguel Ojeda (9):
docs: rust: add paragraph about finding a suitable libclang
kbuild: rust_is_available: print docs reference
kbuild: rust_is_available: add check for bindgen invocation
kbuild: rust_is_available: check that environment variables are set
kbuild: rust_is_available: fix confusion when a version appears in the
path
kbuild: rust_is_available: normalize version matching
kbuild: rust_is_available: handle failures calling $RUSTC/$BINDGEN
kbuild: rust_is_available: check that output looks as expected
kbuild: rust_is_available: add test suite

Russell Currey (1):
kbuild: rust_is_available: fix version check when CC has multiple
arguments

 Documentation/rust/quick-start.rst |  17 ++
 Makefile                           |   4 +-
 scripts/rust_is_available.sh       | 233 +++++++++++++------
 scripts/rust_is_available_test.py  | 346 +++++++++++++++++++++++++++++
 4 files changed, 532 insertions(+), 68 deletions(-)
 create mode 100755 scripts/rust_is_available_test.py

base-commit: 858fd168a95c5b9669aac8db6c14a9aeab446375 --
2.41.0

#38 lore raw
In reply to Miguel Ojeda
Quoted 41 lines · Miguel Ojeda

This is the patch series to improve scripts/rust_is_available.sh.

The major addition in v2 is the test suite in the last commit. I added it because I wanted to have a proper way to test any further changes to it (such as the suggested set -- idea to avoid forking by Masahiro), and so that adding new checks was easier to justify too (i.e. vs. the added complexity).

In addition, there are also a few new checks in the script, to cover for even some more cases, which hopefully make problematic setups easier to identify and solve by users building the kernel. For instance, running the script externally gives:

    $ scripts/rust_is_available.sh
    ***
    *** Environment variable 'RUSTC' is not set.
    ***
    *** This script is intended to be called from Kbuild.
    *** Please use the 'rustavailable' target to call it instead.
    *** Otherwise, the results may not be meaningful.
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***

I also changed it to avoid setting -e as Masahiro suggested. Similarly, I now check for $RUSTC, $BINDGEN and $CC, instead of $MAKEFLAGS, as he also suggested (but I gave it their own error message rather than use the ${CC?: is not set} approach. This goes in line with the reasons outlined above, i.e. trying to give users a clear error of what step exactly failed).

In the test suite I included previously problematic compiler version strings we got reports for. The test suite covers all current branches in the script, and we should keep it that way in the future.

The patch series also include Masahiro's patch to remove the -v option, as well as Russell's patch for supporting multiple arguments in $CC.

Quoted 8 lines

All in all, this should solve all the issues we got so far (unless I have missed something) and improve things further with the new checks plus the test suite to hopefully have an easier time in the future.

Testers for this one are appreciated, especially if you have uncommon or custom setups for building the kernel.

This could go through either the Kbuild or the Rust tree.

Please feel free to apply it to the Rust tree. Perhaps I may add minor comments, but it is up to you.

Best Regards
Masahiro Yamada

Applied
#39 lore raw
In reply to Miguel Ojeda
Quoted 41 lines · Miguel Ojeda

This is the patch series to improve scripts/rust_is_available.sh.

The major addition in v2 is the test suite in the last commit. I added it because I wanted to have a proper way to test any further changes to it (such as the suggested set -- idea to avoid forking by Masahiro), and so that adding new checks was easier to justify too (i.e. vs. the added complexity).

In addition, there are also a few new checks in the script, to cover for even some more cases, which hopefully make problematic setups easier to identify and solve by users building the kernel. For instance, running the script externally gives:

    $ scripts/rust_is_available.sh
    ***
    *** Environment variable 'RUSTC' is not set.
    ***
    *** This script is intended to be called from Kbuild.
    *** Please use the 'rustavailable' target to call it instead.
    *** Otherwise, the results may not be meaningful.
    ***
    *** Please see Documentation/rust/quick-start.rst for details
    *** on how to set up the Rust support.
    ***

I also changed it to avoid setting -e as Masahiro suggested. Similarly, I now check for $RUSTC, $BINDGEN and $CC, instead of $MAKEFLAGS, as he also suggested (but I gave it their own error message rather than use the ${CC?: is not set} approach. This goes in line with the reasons outlined above, i.e. trying to give users a clear error of what step exactly failed).

In the test suite I included previously problematic compiler version strings we got reports for. The test suite covers all current branches in the script, and we should keep it that way in the future.

The patch series also include Masahiro's patch to remove the -v option, as well as Russell's patch for supporting multiple arguments in $CC.

Quoted 8 lines

All in all, this should solve all the issues we got so far (unless I have missed something) and improve things further with the new checks plus the test suite to hopefully have an easier time in the future.

Testers for this one are appreciated, especially if you have uncommon or custom setups for building the kernel.

This could go through either the Kbuild or the Rust tree.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel