Skip to contentSkip to conversation

kbuild: rust: add `RUSTC_VERSION` and reconfig/rebuild support

PATCH Superseded Miguel Ojeda 6 participants 25 messages Aug 8, 2024 – Sep 5, 2024

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

cover kbuild: rust: add `RUSTC_VERSION` and reconfig/rebuild support

Hi all,

This series introduces RUSTC_VERSION support, which is needed for several patch series getting upstreamed, and it will be increasingly useful as we now support several Rust compiler (and bindgen) versions. Later on, we will likely introduce other macros, such as rustc-min-version.

In addition, the series introduces automatic reconfiguration and rebuild based on RUSTC_VERSION_TEXT, to mimic the C side, which is also good to have now that we support several versions.

It is based on top of the current rust-fixes, since there are a couple pending changes to the version text Kconfig symbols that will be sent to Linus soon for an -rc.

Cheers,
Miguel

Miguel Ojeda (6):
kbuild: rust: add CONFIG_RUSTC_VERSION
kbuild: rust: make command for RUSTC_VERSION_TEXT closer to the CC
one
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
docs: rust: include other expressions in conditional compilation
section

 Documentation/rust/general-information.rst |  8 +++++++
 Makefile                                   |  5 +++--
 init/Kconfig                               | 11 ++++++++-
 rust/Makefile                              |  8 +++----
 scripts/rustc-version.sh                   | 26 ++++++++++++++++++++++
 5 files changed, 51 insertions(+), 7 deletions(-)
 create mode 100755 scripts/rustc-version.sh

base-commit: fe992163575b187405899c5abaad8ef6fb828ff6 --
2.46.0

author maintainer
#1 lore raw

PATCH 1/6 kbuild: rust: add `CONFIG_RUSTC_VERSION`

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 has also been submitted for pahole [2].

---
 init/Kconfig             |  7 +++++++
 scripts/rustc-version.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100755 scripts/rustc-version.sh
init/Kconfig +7 -0
index 3ada33b1d681..47e2c3227b99 100644
@@ -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
scripts/rustc-version.sh new file +26 -0
new file mode 100755
index 000000000000..62ea510be71b
@@ -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
Signature · 1 line
2.46.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] kbuild: rust: add `CONFIG_RUSTC_VERSION`

Quoted 77 lines · Miguel Ojeda

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 has also been submitted for pahole [2].

---
 init/Kconfig             |  7 +++++++
 scripts/rustc-version.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100755 scripts/rustc-version.sh
init/Kconfig +7 -0
index 3ada33b1d681..47e2c3227b99 100644
@@ -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
scripts/rustc-version.sh new file +26 -0
new file mode 100755
index 000000000000..62ea510be71b
@@ -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
Signature · 1 line
2.46.0
author maintainer
#3 lore raw

PATCH 2/6 kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer to the `CC` one

In reply to Miguel Ojeda

CC_VERSION_TEXT is defined as:

    CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))

Make RUSTC_VERSION_TEXT closer to that, i.e. add LC_ALL=C and | head -n 1 in case it matters in the future, and for consistency.

This reduces the difference in the next commit.

---
 init/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/Kconfig +1 -1
index 47e2c3227b99..2f974f412374 100644
@@ -1926,7 +1926,7 @@ config RUST
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
-	default "$(shell,$(RUSTC) --version 2>/dev/null)"
+	default "$(shell,LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1)"
 
 config BINDGEN_VERSION_TEXT
 	string
Signature · 1 line
2.46.0
#4 lore raw

Re: [PATCH 2/6] kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer to the `CC` one

Quoted 7 lines · Miguel Ojeda

CC_VERSION_TEXT is defined as:

    CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))

Make RUSTC_VERSION_TEXT closer to that, i.e. add LC_ALL=C and | head -n 1 in case it matters in the future, and for consistency.

Even if "rustc --version" starts to print multiple lines, it will not cause an immediate problem.

$(shell ... ) (both Makefile and Kconfig) replaces a new line with a space.

CONFIG_RUSTC_VERSION_TEXT is not broken in any way. It would be just longer than we would expect.

Quoted 24 lines

This reduces the difference in the next commit.

---
 init/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/Kconfig +1 -2
index 47e2c3227b99..2f974f412374 100644
@@ -1926,7 +1926,7 @@ config RUST
 config RUSTC_VERSION_TEXT
        string
        depends on RUST
-       default "$(shell,$(RUSTC) --version 2>/dev/null)"
+       default "$(shell,LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1)"
 
 config BINDGEN_VERSION_TEXT
        string
--

2.46.0

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw

Re: [PATCH 2/6] kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer to the `CC` one

Quoted 9 lines · Masahiro Yamada

Even if "rustc --version" starts to print multiple lines, it will not cause an immediate problem.

$(shell ... ) (both Makefile and Kconfig) replaces a new line with a space.

CONFIG_RUSTC_VERSION_TEXT is not broken in any way. It would be just longer than we would expect.

+1, I will update the commit message when I apply it (or if you prefer a v2 if you are taking it, or if you want to drop this commit, please let me know!).

Thanks for taking a look, Masahiro.

Cheers,
Miguel

#6 lore raw

Re: [PATCH 2/6] kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer to the `CC` one

In reply to Miguel Ojeda
Quoted 6 lines · Miguel Ojeda

CC_VERSION_TEXT is defined as:

    CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))

Make RUSTC_VERSION_TEXT closer to that, i.e. add LC_ALL=C and | head -n 1 in case it matters in the future, and for consistency.

Cargo depends on the rustc version string not getting localized. Or to be precise it depends on the version string being fixed for a given rustc version, which would not be the case if the value of LC_ALL could change the version string. If the version string changes, cargo will rebuild everything from scratch. There is also not really anything to localize in the non-verbose version string. I guess setting LC_ALL doesn't hurt either though.

Quoted 23 lines

This reduces the difference in the next commit.

---
 init/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/Kconfig +1 -2
index 47e2c3227b99..2f974f412374 100644
@@ -1926,7 +1926,7 @@ config RUST
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
-	default "$(shell,$(RUSTC) --version 2>/dev/null)"
+	default "$(shell,LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1)"
 
 config BINDGEN_VERSION_TEXT
 	string
--

2.46.0

#7 lore raw

Re: [PATCH 2/6] kbuild: rust: make command for `RUSTC_VERSION_TEXT` closer to the `CC` one

Quoted 2 lines · Björn Roy Baron

Cargo depends on the rustc version string not getting localized. Or to be precise it depends on the version string being fixed for a given rustc version, which would not be the case if the value of LC_ALL could change the version string. If the version string changes, cargo will rebuild everything from scratch. There is also not really anything to localize in the non-verbose version string. I guess setting LC_ALL doesn't hurt either though.

Thanks Björn -- yeah, I agree it works either way (also for head as Masahiro mentioned).

I made this commit to make it as close as possible to the C one, since in the next patch it gets moved to another place where both commands go together.

It is reasonable to argue that it makes it more complex and slower, so I am happy dropping it.

Cheers,
Miguel

author maintainer
#8 lore raw

PATCH 3/6 kbuild: rust: re-run Kconfig if the version text changes

In reply to Miguel Ojeda

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.

---
Masahiro: I think leaving the depends on RUST in RUSTC_VERSION is OK, but since this is different from the C side, please let me know if you prefer otherwise. Thanks!

 Makefile     | 5 +++--
 init/Kconfig | 4 +++-
 2 files changed, 6 insertions(+), 3 deletions(-)
Makefile +3 -2
index 8ad55d6e7b60..2b5f9f098b6f 100644
@@ -648,9 +648,10 @@ 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),
+# 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 LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1))
 
 ifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
 include $(srctree)/scripts/Makefile.clang
@@ -671,7 +672,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 $@
init/Kconfig +3 -1
index 2f974f412374..b0238c4b6e79 100644
@@ -1926,7 +1926,9 @@ config RUST
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
-	default "$(shell,LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1)"
+	default "$(RUSTC_VERSION_TEXT)"
+	help
+	  See `CC_VERSION_TEXT`.
 
 config BINDGEN_VERSION_TEXT
 	string

--
2.46.0

Reviewed-by on v1 3/6
#9 lore raw

Re: [PATCH 3/6] kbuild: rust: re-run Kconfig if the version text changes

Quoted 22 lines · Miguel Ojeda

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.

---
Masahiro: I think leaving the depends on RUST in RUSTC_VERSION is OK, but since this is different from the C side, please let me know if you prefer otherwise. Thanks!

 Makefile     | 5 +++--
 init/Kconfig | 4 +++-
 2 files changed, 6 insertions(+), 3 deletions(-)
Makefile +0 -0
index 8ad55d6e7b60..2b5f9f098b6f 100644

Makefile endif · line 652

Quoted 6 lines
Makefile +1 -1
@@ -648,9 +648,10 @@ 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),
+# 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.

If you send a v2, mind you consider reformatting so that this comment block stays <= 80 chars?

Quoted 30 lines
Makefile +2 -1
 CC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(CC) --version 2>/dev/null | head -n 1))
+RUSTC_VERSION_TEXT = $(subst $(pound),,$(shell LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1))
 
 ifneq ($(findstring clang,$(CC_VERSION_TEXT)),)
 include $(srctree)/scripts/Makefile.clang
@@ -671,7 +672,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 $@
init/Kconfig +3 -2
index 2f974f412374..b0238c4b6e79 100644
@@ -1926,7 +1926,9 @@ config RUST
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
-	default "$(shell,LC_ALL=C $(RUSTC) --version 2>/dev/null | head -n 1)"
+	default "$(RUSTC_VERSION_TEXT)"
+	help
+	  See `CC_VERSION_TEXT`.
 
 config BINDGEN_VERSION_TEXT
 	string
--
Quoted 1 line

2.46.0

Do we already support rust in external kernel modules? In top-level Makefile's oot-kmod 'prepare' target we check that the compiler (version) is the same as when the kernel itself was built. If rust modules are supported, adding a similar check might be helpful.

Nevertheless,

#10 lore raw

Re: [PATCH 3/6] kbuild: rust: re-run Kconfig if the version text changes

Quoted 3 lines · Nicolas Schier

If you send a v2, mind you consider reformatting so that this comment block stays <= 80 chars?

Sure.

Quoted 1 line

Do we already support rust in external kernel modules? In top-level

If you mean out-of-tree modules, then yeah, they should generally work (at least I test the build with a trivial one regularly, but perhaps it is missing something), although they are not the highest priority.

Quoted 3 lines

Makefile's oot-kmod 'prepare' target we check that the compiler (version) is the same as when the kernel itself was built. If rust modules are supported, adding a similar check might be helpful.

That is a good point, I will take a look.

Quoted 1 line

Thanks!

Cheers,
Miguel

author maintainer
#11 lore raw

PATCH 4/6 kbuild: rust: rebuild if the version text changes

In reply to Miguel Ojeda

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.

---
Masahiro: I used $(objtree) here since we still use it in the rest of this Makefile, but please let me know if you prefer otherwise. Thanks!

 rust/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/Makefile +2 -1
index 6c0644b6090c..966743a9ee25 100644
@@ -370,7 +370,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_dep,rustc_library)
 ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
 $(obj)/core.o: scripts/target.json

--
2.46.0

#12 lore raw

Re: [PATCH 4/6] kbuild: rust: rebuild if the version text changes

Quoted 39 lines · Miguel Ojeda

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.

---
Masahiro: I used $(objtree) here since we still use it in the rest of this Makefile, but please let me know if you prefer otherwise. Thanks!

 rust/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/Makefile +2 -2
index 6c0644b6090c..966743a9ee25 100644
@@ -370,7 +370,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_dep,rustc_library)
 ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
 $(obj)/core.o: scripts/target.json
--
Quoted 1 line

2.46.0

Looks good to me, but I'd prefer something like the suggestion from Masahiro:
https://lore.kernel.org/linux-kbuild/CAK7LNAQBG0nDupXSgAAk-6nOqeqGVkr3H1RjYaqRJ1OxmLm6xA@mail.gmail.com/

#14 lore raw

Re: [PATCH 4/6] kbuild: rust: rebuild if the version text changes

Quoted 8 lines · Miguel Ojeda
Quoted 4 lines · Nicolas Schier

Looks good to me, but I'd prefer something like the suggestion from Masahiro:
https://lore.kernel.org/linux-kbuild/CAK7LNAQBG0nDupXSgAAk-6nOqeqGVkr3H1RjYaqRJ1OxmLm6xA@mail.gmail.com/

Do you mean for core (here), or for macros (in the next commit)? (if you mean for core, please see my reply to Masahiro there).

oops, I missed that 'core' is delivered external. Sorry for the noise.

Kind regards,
Nicolas

#15 lore raw

Re: [PATCH 4/6] kbuild: rust: rebuild if the version text changes

Quoted 2 lines · Nicolas Schier

oops, I missed that 'core' is delivered external. Sorry for the noise.

No worries at all, thanks for taking a look! I appreciate the reviews.

Cheers,
Miguel

author maintainer
#16 lore raw

PATCH 5/6 kbuild: rust: replace proc macros dependency on `core.o` with the version text

In reply to Miguel Ojeda

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.

---
 rust/Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
rust/Makefile +2 -3
index 966743a9ee25..40c8d2c57024 100644
@@ -329,9 +329,8 @@ 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 \
+    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
 	+$(call if_changed_dep,rustc_procmacro)
 
 quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
Signature · 1 line
2.46.0
#17 lore raw

Re: [PATCH 5/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text

Quoted 18 lines · Miguel Ojeda

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.

---
 rust/Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
rust/Makefile +0 -0
index 966743a9ee25..40c8d2c57024 100644

rust/Makefile quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ · line 334

Quoted 9 lines
rust/Makefile +2 -3
@@ -329,9 +329,8 @@ 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 \
+    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
        +$(call if_changed_dep,rustc_procmacro)

The touched file, include/config/*, is an implementation detail in Kconfig and fixdep.

Rather, I'd like to put the string "CONFIG_RUST_VERSION_TEXT" in the comment of the source file.

This is the idea adopted in include/linux/compiler-version.h

rust/macros/lib.rs +4 -0
index 5be0cb9db3ee..91be2112ceee 100644
@@ -2,6 +2,10 @@
 
 //! Crate for all kernel procedural macros.
 
+// When fixdep scans this, it will find this string "CONFIG_RUSTC_VERSION_TEXT"
+// and add 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;

I do not know how to do it for rust/core.o because there is no in-tree source file.

But, can we add rust/core.rs, from which rustlib/src/rust/library/core/src/macros/mod.rs is imported?

Quoted 6 lines
rust/Makefile +0 -1
 quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
--

2.46.0

Signature · 2 lines
Best Regards
Masahiro Yamada
#18 lore raw

Re: [PATCH 5/6] kbuild: rust: replace proc macros dependency on `core.o` with the version text

Quoted 8 lines · Masahiro Yamada

The touched file, include/config/*, is an implementation detail in Kconfig and fixdep.

Rather, I'd like to put the string "CONFIG_RUST_VERSION_TEXT" in the comment of the source file.

This is the idea adopted in include/linux/compiler-version.h

+1, I did it this way to follow the same pattern to the previous patch on core.o, since that one needed another approach, but I am happy to change it.

Quoted 5 lines

I do not know how to do it for rust/core.o because there is no in-tree source file.

But, can we add rust/core.rs, from which rustlib/src/rust/library/core/src/macros/mod.rs is imported?

That is an interesting idea... :)

Hmm... I think core is a bit too special for that, since we need attributes on the crate root and it is the one defining things like include!. Even if we generated the file on the fly to mimic the original, we would still need to handle the paths for each module or recreate a directory hierarchy or similar.

We are also in talks with the Rust project to figure out building core in a stable way, so the details around it may change too, e.g. probably something like passing a flag like -Cbuild-std=core. They may also want to figure out the right input file path themselves, i.e. rather than having to provide one from our side.

So I think a hack may not be worth it compared to depending on the implementation detail, and may be a bit brittle until we know the final details of that.

Thanks!

Cheers,
Miguel

author maintainer
#19 lore raw

PATCH 6/6 docs: rust: include other expressions in conditional compilation section

In reply to Miguel Ojeda

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?).

---
 Documentation/rust/general-information.rst | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/rust/general-information.rst +8 -0
index e3f388ef4ee4..071c5bafd668 100644
@@ -142,3 +142,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
Signature · 1 line
2.46.0
reviewer Tested-by on the v1 series
#20 lore raw
In reply to Miguel Ojeda
Quoted 13 lines · Miguel Ojeda

Hi all,

This series introduces RUSTC_VERSION support, which is needed for several patch series getting upstreamed, and it will be increasingly useful as we now support several Rust compiler (and bindgen) versions. Later on, we will likely introduce other macros, such as rustc-min-version.

In addition, the series introduces automatic reconfiguration and rebuild based on RUSTC_VERSION_TEXT, to mimic the C side, which is also good to have now that we support several versions.

Quoted 6 lines

It is based on top of the current rust-fixes, since there are a couple pending changes to the version text Kconfig symbols that will be sent to Linus soon for an -rc.

Cheers,
Miguel

I tested this together with Rust Binder. I also ran this patchset through Android's CI setup to verify that it works in builds with CONFIG_RUST disabled. It passes both with and without a Rust compiler available to the build system.

I'm happy to rerun these tests for new versions.

Alice

#21 lore raw
Quoted 9 lines · Alice Ryhl

I tested this together with Rust Binder. I also ran this patchset through Android's CI setup to verify that it works in builds with CONFIG_RUST disabled. It passes both with and without a Rust compiler available to the build system.

I'm happy to rerun these tests for new versions.

Thanks a lot Alice, that is very useful.

I sent v2, in case you have time to give it another go.

Cheers,
Miguel

#22 lore raw
Quoted 10 lines · Miguel Ojeda
Quoted 8 lines · Alice Ryhl

I tested this together with Rust Binder. I also ran this patchset through Android's CI setup to verify that it works in builds with CONFIG_RUST disabled. It passes both with and without a Rust compiler available to the build system.

Quoted 8 lines
Quoted 1 line

I'm happy to rerun these tests for new versions.

Thanks a lot Alice, that is very useful.

I sent v2, in case you have time to give it another go.

Cheers,
Miguel

With the nit in 1/6 fixed (No need to resend if you fix it locally)

Signature · 2 lines
Best Regards
Masahiro Yamada
#23 lore raw
Quoted 4 lines · Masahiro Yamada

With the nit in 1/6 fixed (No need to resend if you fix it locally)

Thanks a lot Masahiro (nit fixed and tag applied). I guess you meant to reply to v2?

Cheers,
Miguel

#24 lore raw
Quoted 9 lines · Miguel Ojeda
Quoted 4 lines · Masahiro Yamada

With the nit in 1/6 fixed (No need to resend if you fix it locally)

Thanks a lot Masahiro (nit fixed and tag applied). I guess you meant to reply to v2?

Yes, I meant Ack to v2.

Sorry for confusion.

Signature · 2 lines
Best Regards
Masahiro Yamada