* [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
@ 2024-09-02 16:55 Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION` Miguel Ojeda
` (8 more replies)
0 siblings, 9 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
This series mainly adds support for `CONFIG_RUSTC_VERSION` (which is needed for
other upcoming series) as well as support for rebuilding the kernel when the
Rust compiler version text changes, plus other secondary improvements.
v1: https://lore.kernel.org/rust-for-linux/20240808221138.873750-1-ojeda@kernel.org/
v2:
- Dropped patch #2 "kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer
to the `CC` one" (Masahiro, Björn).
In other words, now the `RUSTC_VERSION_TEXT` command is kept as it
was, without `LC_ALL=1` nor `| head -n1`.
- Replaced `macros` crate dependency with a comment in the code that `fixdep`
will find, since we can do it for that one, unlike `core` (Masahiro,
Nicolas).
- Added patch (here #5) to add a warning when the Rust compiler used to build
the kernel differs from the one used to build an out-of-tree module, like
the C side does (Nicolas).
If the patch is not wanted, then it can be skipped without much loss, since
anyway Rust will fail to compile in that case. However, it would be nice to
have to prevent potentially unexpected situations and to clarify the errors
that `rustc` would emit later. See the commit message for more details.
- Rewrapped comment to stay under 80 lines (Nicolas).
- Picked up a couple tags that could more or less be reasonably taken given
the changes to v2. Re-runs of tests welcome!
Miguel Ojeda (6):
kbuild: rust: add `CONFIG_RUSTC_VERSION`
kbuild: rust: re-run Kconfig if the version text changes
kbuild: rust: rebuild if the version text changes
kbuild: rust: replace proc macros dependency on `core.o` with the
version text
kbuild: rust: warn if the out-of-tree compiler differs from the kernel
one
docs: rust: include other expressions in conditional compilation
section
Documentation/rust/general-information.rst | 8 +++++++
Makefile | 18 ++++++++++-----
init/Kconfig | 11 ++++++++-
rust/Makefile | 7 +++---
rust/macros/lib.rs | 4 ++++
scripts/rustc-version.sh | 26 ++++++++++++++++++++++
6 files changed, 64 insertions(+), 10 deletions(-)
create mode 100755 scripts/rustc-version.sh
base-commit: a335e95914046c6bed45c0d17cabcd483682cf5e
--
2.46.0
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION`
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-05 1:08 ` Masahiro Yamada
2024-09-02 16:55 ` [PATCH v2 2/6] kbuild: rust: re-run Kconfig if the version text changes Miguel Ojeda
` (7 subsequent siblings)
8 siblings, 1 reply; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
Now that we support several Rust versions, introduce
`CONFIG_RUSTC_VERSION` so that it can be used in Kconfig to enable and
disable configuration options based on the `rustc` version.
The approach taken resembles `pahole`'s -- see commit 613fe1692377
("kbuild: Add CONFIG_PAHOLE_VERSION"), i.e. a simple version parsing
without trying to identify several kinds of compilers, since so far
there is only one (`rustc`).
However, unlike `pahole`'s, we also print a zero if executing failed for
any reason, rather than checking if the command is found and executable
(which still leaves things like a file that exists and is executable,
but e.g. is built for another platform [1]). An equivalent approach to
the one here was also submitted for `pahole` [2].
Link: https://lore.kernel.org/rust-for-linux/CANiq72=4vX_tJMJLE6e+bg7ZECHkS-AQpm8GBzuK75G1EB7+Nw@mail.gmail.com/ [1]
Link: https://lore.kernel.org/linux-kbuild/20240728125527.690726-1-ojeda@kernel.org/ [2]
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
Tested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
init/Kconfig | 7 +++++++
scripts/rustc-version.sh | 26 ++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100755 scripts/rustc-version.sh
diff --git a/init/Kconfig b/init/Kconfig
index 839c83034006..38c1cfcce821 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -60,6 +60,13 @@ config LLD_VERSION
default $(ld-version) if LD_IS_LLD
default 0
+config RUSTC_VERSION
+ int
+ default $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
+ help
+ It does not depend on `RUST` since that one may need to use the version
+ in a `depends on`.
+
config RUST_IS_AVAILABLE
def_bool $(success,$(srctree)/scripts/rust_is_available.sh)
help
diff --git a/scripts/rustc-version.sh b/scripts/rustc-version.sh
new file mode 100755
index 000000000000..62ea510be71b
--- /dev/null
+++ b/scripts/rustc-version.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Usage: $ ./rustc-version.sh rustc
+#
+# Print the Rust compiler name and its version in a 6 or 7-digit form.
+
+# Convert the version string x.y.z to a canonical up-to-7-digits form.
+#
+# Note that this function uses one more digit (compared to other
+# instances in other version scripts) to give a bit more space to
+# `rustc` since it will reach 1.100.0 in late 2026.
+get_canonical_version()
+{
+ IFS=.
+ set -- $1
+ echo $((100000 * $1 + 100 * $2 + $3))
+}
+
+if output=$("$@" --version 2>/dev/null); then
+ set -- $output
+ get_canonical_version $2
+else
+ echo 0
+ exit 1
+fi
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 2/6] kbuild: rust: re-run Kconfig if the version text changes
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION` Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 3/6] kbuild: rust: rebuild " Miguel Ojeda
` (6 subsequent siblings)
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
Re-run Kconfig if we detect the Rust compiler has changed via the version
text, like it is done for C.
Unlike C, and unlike `RUSTC_VERSION`, the `RUSTC_VERSION_TEXT` is kept
under `depends on RUST`, since it should not be needed unless `RUST`
is enabled.
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
Makefile | 8 +++++---
init/Kconfig | 4 +++-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
index 68ebd6d6b444..1883aeb919a4 100644
--- a/Makefile
+++ b/Makefile
@@ -648,9 +648,11 @@ endif
# The expansion should be delayed until arch/$(SRCARCH)/Makefile is included.
# Some architectures define CROSS_COMPILE in arch/$(SRCARCH)/Makefile.
-# CC_VERSION_TEXT is referenced from Kconfig (so it needs export),
-# and from include/config/auto.conf.cmd to detect the compiler upgrade.
+# CC_VERSION_TEXT and RUSTC_VERSION_TEXT are referenced from Kconfig (so they
+# need export), and from include/config/auto.conf.cmd to detect the compiler
+# upgrade.
CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
+RUSTC_VERSION_TEXT = $(subst $(pound),,$(shell $(RUSTC) --version 2>/dev/null))
ifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
include $(srctree)/scripts/Makefile.clang
@@ -671,7 +673,7 @@ ifdef config-build
# KBUILD_DEFCONFIG may point out an alternative default configuration
# used for 'make defconfig'
include $(srctree)/arch/$(SRCARCH)/Makefile
-export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT
+export KBUILD_DEFCONFIG KBUILD_KCONFIG CC_VERSION_TEXT RUSTC_VERSION_TEXT
config: outputmakefile scripts_basic FORCE
$(Q)$(MAKE) $(build)=scripts/kconfig $@
diff --git a/init/Kconfig b/init/Kconfig
index 38c1cfcce821..c6b744a48dcc 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1927,7 +1927,9 @@ config RUST
config RUSTC_VERSION_TEXT
string
depends on RUST
- default "$(shell,$(RUSTC) --version 2>/dev/null)"
+ default "$(RUSTC_VERSION_TEXT)"
+ help
+ See `CC_VERSION_TEXT`.
config BINDGEN_VERSION_TEXT
string
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 3/6] kbuild: rust: rebuild if the version text changes
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 2/6] kbuild: rust: re-run Kconfig if the version text changes Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 4/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text Miguel Ojeda
` (5 subsequent siblings)
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
Now that `RUSTC_VERSION_TEXT` exists, use it to rebuild `core` when the
version text changes (which in turn will trigger a rebuild of all the
kernel Rust code).
This also applies to proc macros (which only work with the `rustc` that
compiled them), via the already existing dependency on `core.o`. That
is cleaned up in the next commit.
However, this does not cover host programs written in Rust, which is
the same case in the C side.
This is accomplished by referencing directly the generated file, instead
of using the `fixdep` header trick, since we cannot change the Rust
standard library sources. This is not too much of a burden, since it
only needs to be done for `core`.
Tested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/Makefile | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/rust/Makefile b/rust/Makefile
index e13d14ec5fe7..bb57a7c30f1a 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -389,7 +389,8 @@ $(obj)/core.o: private skip_clippy = 1
$(obj)/core.o: private skip_flags = -Wunreachable_pub
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
-$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
+$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
+ $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
+$(call if_changed_rule,rustc_library)
ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
$(obj)/core.o: scripts/target.json
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 4/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (2 preceding siblings ...)
2024-09-02 16:55 ` [PATCH v2 3/6] kbuild: rust: rebuild " Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 5/6] kbuild: rust: warn if the out-of-tree compiler differs from the kernel one Miguel Ojeda
` (4 subsequent siblings)
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
With the `RUSTC_VERSION_TEXT` rebuild support in place, now proc macros
can depend on that instead of `core.o`.
This means that both the `core` and `macros` crates can be built in
parallel, and that touching `core.o` does not trigger a rebuild of the
proc macros.
This could be accomplished using the same approach as for `core`
(i.e. depending directly on `include/config/RUSTC_VERSION_TEXT`). However,
that is considered an implementation detail [1], and thus it is best
to avoid it. Instead, let fixdep find a string that we explicitly
write down in the source code for this purpose (like it is done for
`include/linux/compiler-version.h`), which we can easily do (unlike for
`core`) since this is our own source code.
Suggested-by: Masahiro Yamada <masahiroy@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/CAK7LNAQBG0nDupXSgAAk-6nOqeqGVkr3H1RjYaqRJ1OxmLm6xA@mail.gmail.com/ [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/Makefile | 4 +---
rust/macros/lib.rs | 4 ++++
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/rust/Makefile b/rust/Makefile
index bb57a7c30f1a..4eae318f36ff 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -342,9 +342,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
# Procedural macros can only be used with the `rustc` that compiled it.
-# Therefore, to get `libmacros.so` automatically recompiled when the compiler
-# version changes, we add `core.o` as a dependency (even if it is not needed).
-$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
+$(obj)/libmacros.so: $(src)/macros/lib.rs FORCE
+$(call if_changed_dep,rustc_procmacro)
quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index 5be0cb9db3ee..a626b1145e5c 100644
--- a/rust/macros/lib.rs
+++ b/rust/macros/lib.rs
@@ -2,6 +2,10 @@
//! Crate for all kernel procedural macros.
+// When fixdep scans this, it will find this string `CONFIG_RUSTC_VERSION_TEXT`
+// and thus add a dependency on `include/config/RUSTC_VERSION_TEXT`, which is
+// touched by Kconfig when the version string from the compiler changes.
+
#[macro_use]
mod quote;
mod concat_idents;
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 5/6] kbuild: rust: warn if the out-of-tree compiler differs from the kernel one
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (3 preceding siblings ...)
2024-09-02 16:55 ` [PATCH v2 4/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 6/6] docs: rust: include other expressions in conditional compilation section Miguel Ojeda
` (3 subsequent siblings)
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
For out-of-tree modules, in the C side, Kbuild warns if the C compiler
differs from the one used to build the kernel.
Do the same for Rust, except that we add the extra condition that Rust
support was enabled in the kernel.
For instance, the output will look like:
warning: the Rust compiler differs from the one used to build the kernel
The kernel was built by: rustc 1.78.0 (9b00956e5 2024-04-29)
You are using: rustc 1.79.0 (129f3b996 2024-06-10)
Note that the build will fail later anyway in such a case, since the
Rust compiler checks for incompatible Rust versions, e.g.:
RUSTC [M] rust-out-of-tree-module/rust_out_of_tree.o
error[E0514]: found crate `core` compiled by an incompatible version of rustc
|
= note: the following crate versions were found:
crate `core` compiled by rustc 1.78.0 (9b00956e5 2024-04-29): rust/libcore.rmeta
= help: please recompile that crate using this compiler (rustc 1.79.0 (129f3b996 2024-06-10)) (consider running `cargo clean` first)
Nevertheless, the added warning at the top should hopefully make the
situation clearer.
The extra condition on `CONFIG_RUST=y` is added since otherwise users
would get warnings when a Rust compiler is reachable, e.g. the most
likely case today is that they are building a C module for a kernel that
does not have Rust enabled.
However, if Rust was indeed enabled, then we do print the warning in
all cases, e.g. even if they are building a C module. While it may not
matter in some cases, it may still be a sign that something unexpectedly
changed in the environment they may want to be aware of:
warning: the Rust compiler differs from the one used to build the kernel
The kernel was built by: rustc 1.78.0 (9b00956e5 2024-04-29)
You are using: rustc 1.79.0 (129f3b996 2024-06-10)
CC [M] c-out-of-tree-module/c_out_of_tree.o
Note that the check will also trigger for cases where the user does not
have the Rust compiler installed, including when building a C module:
warning: the Rust compiler differs from the one used to build the kernel
The kernel was built by: rustc 1.78.0 (9b00956e5 2024-04-29)
You are using:
CC [M] c-out-of-tree-module/c_out_of_tree.o
Suggested-by: Nicolas Schier <nicolas@fjasle.eu>
Link: https://lore.kernel.org/rust-for-linux/20240817-heavy-dancing-whale-6ae13d@lindesnes/
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
Makefile | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
index 1883aeb919a4..e3fcc7028c5f 100644
--- a/Makefile
+++ b/Makefile
@@ -1794,14 +1794,20 @@ clean: private rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modu
$(KBUILD_EXTMOD)/compile_commands.json
PHONY += prepare
-# now expand this into a simple variable to reduce the cost of shell evaluations
+# now expand these into simple variables to reduce the cost of shell evaluations
prepare: CC_VERSION_TEXT := $(CC_VERSION_TEXT)
+prepare: RUSTC_VERSION_TEXT := $(RUSTC_VERSION_TEXT)
prepare:
@if [ "$(CC_VERSION_TEXT)" != "$(CONFIG_CC_VERSION_TEXT)" ]; then \
- echo >&2 "warning: the compiler differs from the one used to build the kernel"; \
+ echo >&2 "warning: the C compiler differs from the one used to build the kernel"; \
echo >&2 " The kernel was built by: $(CONFIG_CC_VERSION_TEXT)"; \
echo >&2 " You are using: $(CC_VERSION_TEXT)"; \
fi
+ @if [ "$(CONFIG_RUST)" = "y" -a "$(RUSTC_VERSION_TEXT)" != "$(CONFIG_RUSTC_VERSION_TEXT)" ]; then \
+ echo >&2 "warning: the Rust compiler differs from the one used to build the kernel"; \
+ echo >&2 " The kernel was built by: $(CONFIG_RUSTC_VERSION_TEXT)"; \
+ echo >&2 " You are using: $(RUSTC_VERSION_TEXT)"; \
+ fi
PHONY += help
help:
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v2 6/6] docs: rust: include other expressions in conditional compilation section
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (4 preceding siblings ...)
2024-09-02 16:55 ` [PATCH v2 5/6] kbuild: rust: warn if the out-of-tree compiler differs from the kernel one Miguel Ojeda
@ 2024-09-02 16:55 ` Miguel Ojeda
2024-09-03 11:35 ` [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Alice Ryhl
` (2 subsequent siblings)
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-02 16:55 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
Expand the conditional compilation section to explain how to support
other expressions, such as testing whether `RUSTC_VERSION` is at least
a given version, which requires a numerical comparison that Rust's `cfg`
predicates do not support (yet?).
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
Documentation/rust/general-information.rst | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/rust/general-information.rst b/Documentation/rust/general-information.rst
index 2d76e09da121..6146b49b6a98 100644
--- a/Documentation/rust/general-information.rst
+++ b/Documentation/rust/general-information.rst
@@ -151,3 +151,11 @@ configuration:
#[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
#[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
#[cfg(not(CONFIG_X))] // Disabled
+
+For other predicates that Rust's ``cfg`` does not support, e.g. expressions with
+numerical comparisons, one may define a new Kconfig symbol:
+
+.. code-block:: kconfig
+
+ config RUSTC_VERSION_MIN_107900
+ def_bool y if RUSTC_VERSION >= 107900
--
2.46.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (5 preceding siblings ...)
2024-09-02 16:55 ` [PATCH v2 6/6] docs: rust: include other expressions in conditional compilation section Miguel Ojeda
@ 2024-09-03 11:35 ` Alice Ryhl
2024-09-03 11:36 ` Miguel Ojeda
2024-09-04 19:32 ` Nicolas Schier
2024-09-04 21:15 ` Miguel Ojeda
8 siblings, 1 reply; 14+ messages in thread
From: Alice Ryhl @ 2024-09-03 11:35 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
rust-for-linux, Nathan Chancellor, Nicolas Schier, linux-kbuild,
linux-kernel, patches
On Mon, Sep 2, 2024 at 6:55 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> This series mainly adds support for `CONFIG_RUSTC_VERSION` (which is needed for
> other upcoming series) as well as support for rebuilding the kernel when the
> Rust compiler version text changes, plus other secondary improvements.
>
> v1: https://lore.kernel.org/rust-for-linux/20240808221138.873750-1-ojeda@kernel.org/
> v2:
>
> - Dropped patch #2 "kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer
> to the `CC` one" (Masahiro, Björn).
>
> In other words, now the `RUSTC_VERSION_TEXT` command is kept as it
> was, without `LC_ALL=1` nor `| head -n1`.
>
> - Replaced `macros` crate dependency with a comment in the code that `fixdep`
> will find, since we can do it for that one, unlike `core` (Masahiro,
> Nicolas).
>
> - Added patch (here #5) to add a warning when the Rust compiler used to build
> the kernel differs from the one used to build an out-of-tree module, like
> the C side does (Nicolas).
>
> If the patch is not wanted, then it can be skipped without much loss, since
> anyway Rust will fail to compile in that case. However, it would be nice to
> have to prevent potentially unexpected situations and to clarify the errors
> that `rustc` would emit later. See the commit message for more details.
>
> - Rewrapped comment to stay under 80 lines (Nicolas).
>
> - Picked up a couple tags that could more or less be reasonably taken given
> the changes to v2. Re-runs of tests welcome!
>
> Miguel Ojeda (6):
> kbuild: rust: add `CONFIG_RUSTC_VERSION`
> kbuild: rust: re-run Kconfig if the version text changes
> kbuild: rust: rebuild if the version text changes
> kbuild: rust: replace proc macros dependency on `core.o` with the
> version text
> kbuild: rust: warn if the out-of-tree compiler differs from the kernel
> one
> docs: rust: include other expressions in conditional compilation
> section
I re-ran the same tests as for v1.
Tested-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
2024-09-03 11:35 ` [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Alice Ryhl
@ 2024-09-03 11:36 ` Miguel Ojeda
0 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-03 11:36 UTC (permalink / raw)
To: Alice Ryhl
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, rust-for-linux, Nathan Chancellor,
Nicolas Schier, linux-kbuild, linux-kernel, patches
On Tue, Sep 3, 2024 at 1:35 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> I re-ran the same tests as for v1.
>
> Tested-by: Alice Ryhl <aliceryhl@google.com>
Thanks a lot!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (6 preceding siblings ...)
2024-09-03 11:35 ` [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Alice Ryhl
@ 2024-09-04 19:32 ` Nicolas Schier
2024-09-04 21:15 ` Miguel Ojeda
2024-09-04 21:15 ` Miguel Ojeda
8 siblings, 1 reply; 14+ messages in thread
From: Nicolas Schier @ 2024-09-04 19:32 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux, Nathan Chancellor, linux-kbuild,
linux-kernel, patches
On Mon, Sep 02, 2024 at 06:55:27PM +0200, Miguel Ojeda wrote:
> This series mainly adds support for `CONFIG_RUSTC_VERSION` (which is needed for
> other upcoming series) as well as support for rebuilding the kernel when the
> Rust compiler version text changes, plus other secondary improvements.
>
> v1: https://lore.kernel.org/rust-for-linux/20240808221138.873750-1-ojeda@kernel.org/
> v2:
>
> - Dropped patch #2 "kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer
> to the `CC` one" (Masahiro, Björn).
>
> In other words, now the `RUSTC_VERSION_TEXT` command is kept as it
> was, without `LC_ALL=1` nor `| head -n1`.
>
> - Replaced `macros` crate dependency with a comment in the code that `fixdep`
> will find, since we can do it for that one, unlike `core` (Masahiro,
> Nicolas).
>
> - Added patch (here #5) to add a warning when the Rust compiler used to build
> the kernel differs from the one used to build an out-of-tree module, like
> the C side does (Nicolas).
>
> If the patch is not wanted, then it can be skipped without much loss, since
> anyway Rust will fail to compile in that case. However, it would be nice to
> have to prevent potentially unexpected situations and to clarify the errors
> that `rustc` would emit later. See the commit message for more details.
>
> - Rewrapped comment to stay under 80 lines (Nicolas).
>
> - Picked up a couple tags that could more or less be reasonably taken given
> the changes to v2. Re-runs of tests welcome!
>
> Miguel Ojeda (6):
> kbuild: rust: add `CONFIG_RUSTC_VERSION`
> kbuild: rust: re-run Kconfig if the version text changes
> kbuild: rust: rebuild if the version text changes
> kbuild: rust: replace proc macros dependency on `core.o` with the
> version text
> kbuild: rust: warn if the out-of-tree compiler differs from the kernel
> one
> docs: rust: include other expressions in conditional compilation
> section
>
> Documentation/rust/general-information.rst | 8 +++++++
> Makefile | 18 ++++++++++-----
> init/Kconfig | 11 ++++++++-
> rust/Makefile | 7 +++---
> rust/macros/lib.rs | 4 ++++
> scripts/rustc-version.sh | 26 ++++++++++++++++++++++
> 6 files changed, 64 insertions(+), 10 deletions(-)
> create mode 100755 scripts/rustc-version.sh
>
>
> base-commit: a335e95914046c6bed45c0d17cabcd483682cf5e
> --
> 2.46.0
>
the whole series looks good to me. Thanks for the good explanations.
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
Kind regards,
Nicolas
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
2024-09-04 19:32 ` Nicolas Schier
@ 2024-09-04 21:15 ` Miguel Ojeda
0 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-04 21:15 UTC (permalink / raw)
To: Nicolas Schier
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, rust-for-linux, Nathan Chancellor,
linux-kbuild, linux-kernel, patches
On Wed, Sep 4, 2024 at 9:32 PM Nicolas Schier <nicolas@fjasle.eu> wrote:
>
> the whole series looks good to me. Thanks for the good explanations.
>
> Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
Thanks a lot Nicolas, I appreciate the quick review and the kind words.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
` (7 preceding siblings ...)
2024-09-04 19:32 ` Nicolas Schier
@ 2024-09-04 21:15 ` Miguel Ojeda
8 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-04 21:15 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux, Nathan Chancellor, Nicolas Schier,
linux-kbuild, linux-kernel, patches
On Mon, Sep 2, 2024 at 6:55 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> This series mainly adds support for `CONFIG_RUSTC_VERSION` (which is needed for
> other upcoming series) as well as support for rebuilding the kernel when the
> Rust compiler version text changes, plus other secondary improvements.
Applied to `rust-next` -- thanks everyone!
Just to be extra safe, I did not add patch #5 ("kbuild: rust: warn if
the out-of-tree compiler differs from the kernel one") for the moment
to give people more time to look at it, but we can add it later if
nobody shouts.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION`
2024-09-02 16:55 ` [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION` Miguel Ojeda
@ 2024-09-05 1:08 ` Masahiro Yamada
2024-09-05 20:46 ` Miguel Ojeda
0 siblings, 1 reply; 14+ messages in thread
From: Masahiro Yamada @ 2024-09-05 1:08 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
rust-for-linux, Nathan Chancellor, Nicolas Schier, linux-kbuild,
linux-kernel, patches
On Tue, Sep 3, 2024 at 1:56 AM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Now that we support several Rust versions, introduce
> `CONFIG_RUSTC_VERSION` so that it can be used in Kconfig to enable and
> disable configuration options based on the `rustc` version.
>
> The approach taken resembles `pahole`'s -- see commit 613fe1692377
> ("kbuild: Add CONFIG_PAHOLE_VERSION"), i.e. a simple version parsing
> without trying to identify several kinds of compilers, since so far
> there is only one (`rustc`).
>
> However, unlike `pahole`'s, we also print a zero if executing failed for
> any reason, rather than checking if the command is found and executable
> (which still leaves things like a file that exists and is executable,
> but e.g. is built for another platform [1]). An equivalent approach to
> the one here was also submitted for `pahole` [2].
>
> Link: https://lore.kernel.org/rust-for-linux/CANiq72=4vX_tJMJLE6e+bg7ZECHkS-AQpm8GBzuK75G1EB7+Nw@mail.gmail.com/ [1]
> Link: https://lore.kernel.org/linux-kbuild/20240728125527.690726-1-ojeda@kernel.org/ [2]
> Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
> Tested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
> init/Kconfig | 7 +++++++
> scripts/rustc-version.sh | 26 ++++++++++++++++++++++++++
> 2 files changed, 33 insertions(+)
> create mode 100755 scripts/rustc-version.sh
>
> diff --git a/init/Kconfig b/init/Kconfig
> index 839c83034006..38c1cfcce821 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -60,6 +60,13 @@ config LLD_VERSION
> default $(ld-version) if LD_IS_LLD
> default 0
>
> +config RUSTC_VERSION
> + int
> + default $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
> + help
> + It does not depend on `RUST` since that one may need to use the version
> + in a `depends on`.
> +
> config RUST_IS_AVAILABLE
> def_bool $(success,$(srctree)/scripts/rust_is_available.sh)
> help
> diff --git a/scripts/rustc-version.sh b/scripts/rustc-version.sh
> new file mode 100755
> index 000000000000..62ea510be71b
> --- /dev/null
> +++ b/scripts/rustc-version.sh
> @@ -0,0 +1,26 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# Usage: $ ./rustc-version.sh rustc
> +#
> +# Print the Rust compiler name and its version in a 6 or 7-digit form.
This script does not print the name.
Only the version.
> +
> +# Convert the version string x.y.z to a canonical up-to-7-digits form.
> +#
> +# Note that this function uses one more digit (compared to other
> +# instances in other version scripts) to give a bit more space to
> +# `rustc` since it will reach 1.100.0 in late 2026.
> +get_canonical_version()
> +{
> + IFS=.
> + set -- $1
> + echo $((100000 * $1 + 100 * $2 + $3))
> +}
> +
> +if output=$("$@" --version 2>/dev/null); then
> + set -- $output
> + get_canonical_version $2
> +else
> + echo 0
> + exit 1
> +fi
> --
> 2.46.0
>
--
Best Regards
Masahiro Yamada
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION`
2024-09-05 1:08 ` Masahiro Yamada
@ 2024-09-05 20:46 ` Miguel Ojeda
0 siblings, 0 replies; 14+ messages in thread
From: Miguel Ojeda @ 2024-09-05 20:46 UTC (permalink / raw)
To: Masahiro Yamada
Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, rust-for-linux, Nathan Chancellor, Nicolas Schier,
linux-kbuild, linux-kernel, patches
On Thu, Sep 5, 2024 at 3:08 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> This script does not print the name.
>
> Only the version.
Good catch -- fixed!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2024-09-05 20:47 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-02 16:55 [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION` Miguel Ojeda
2024-09-05 1:08 ` Masahiro Yamada
2024-09-05 20:46 ` Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 2/6] kbuild: rust: re-run Kconfig if the version text changes Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 3/6] kbuild: rust: rebuild " Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 4/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 5/6] kbuild: rust: warn if the out-of-tree compiler differs from the kernel one Miguel Ojeda
2024-09-02 16:55 ` [PATCH v2 6/6] docs: rust: include other expressions in conditional compilation section Miguel Ojeda
2024-09-03 11:35 ` [PATCH v2 0/6] `RUSTC_VERSION` and re-config/re-build support on compiler change Alice Ryhl
2024-09-03 11:36 ` Miguel Ojeda
2024-09-04 19:32 ` Nicolas Schier
2024-09-04 21:15 ` Miguel Ojeda
2024-09-04 21:15 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).