diff mbox series

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

Message ID 20240808221138.873750-6-ojeda@kernel.org (mailing list archive)
State New
Headers show
Series kbuild: rust: add `RUSTC_VERSION` and reconfig/rebuild support | expand

Commit Message

Miguel Ojeda Aug. 8, 2024, 10:11 p.m. UTC
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.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Masahiro Yamada Aug. 9, 2024, 5:31 p.m. UTC | #1
On Fri, Aug 9, 2024 at 7:12 AM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> 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.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
>  rust/Makefile | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/rust/Makefile b/rust/Makefile
> index 966743a9ee25..40c8d2c57024 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -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



diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index 5be0cb9db3ee..91be2112ceee 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 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?








>
>  quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
> --
> 2.46.0
>
>
Miguel Ojeda Aug. 10, 2024, 11:36 a.m. UTC | #2
On Fri, Aug 9, 2024 at 7:31 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> 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.

> 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
diff mbox series

Patch

diff --git a/rust/Makefile b/rust/Makefile
index 966743a9ee25..40c8d2c57024 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -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 $@