Re: [PATCH v2] Kbuild: fix issues with rustc-option

From: Miguel Ojeda
Date: Wed Oct 09 2024 - 06:01:28 EST


On Wed, Oct 9, 2024 at 11:23 AM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
> Miguel, can you link this issue? I don't think I saw it.

https://github.com/Rust-for-Linux/linux/pull/1087#issuecomment-2218445303

(It was in the Lore message I linked yesterday, sorry, I should have
been more explicit)

> Masahiro, are you able to clarify how to pass TMPOUT to rustc?
>
> __rustc-option = $(call try-run2,\
> $(1) $(2) $(3) --crate-type=rlib $(srctree)/rust/probe.rs
> --out-dir=$(TMPOUT),$(3),$(4))
>
> Should I use $(TMPOUT) or $$TMPOUT for this case? Right now, only TMP
> is defined inside try-run. I am assuming that there is a reason for
> having TMP be defined in try-run, rather than just using $(TMP)
> everywhere. Does the same reason apply to TMPOUT? Should I add a
> TMPOUT=$(TMPOUT) inside try-run?

`TMPOUT` is defined already in that `Makefile`, thus you can directly
expand it. However, `TMP` is defined inside the `shell` function, and
thus `$$TMP` is used so that that script (inside the `shell`) expands
it instead.

This is why Masahiro was saying that the `TMPOUT=$(TMPOUT)` was
unnecessary, i.e. it would work, but we can just expand it directly.

Something like this, combining everything [1] seems to work for me.

i.e. passing the file inline, `RUSTC_BOOTSTRAP=1`, avoiding an output
file, keeping `--out-dir` for intermediates files. I added using a
null sysroot too (and skipping if already given, since that is an
error).

I will test it a bit more with KASAN etc.

Cheers,
Miguel

[1]

diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
index 057305eae85c..3ce6a808764a 100644
--- a/scripts/Makefile.compiler
+++ b/scripts/Makefile.compiler
@@ -76,7 +76,9 @@ ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS)
$(1) -v,$(1),$(2),$(3))
# __rustc-option
# Usage: MY_RUSTFLAGS += $(call
__rustc-option,$(RUSTC),$(MY_RUSTFLAGS),-Cinstrument-coverage,-Zinstrument-coverage)
__rustc-option = $(call try-run,\
- $(1) $(2) $(3) --crate-type=rlib /dev/null --out-dir=$$TMPOUT
-o "$$TMP",$(3),$(4))
+ echo '//!\n#![feature(no_core)]#![no_core]' | RUSTC_BOOTSTRAP=1\
+ $(1) --sysroot=/dev/null $(filter-out
--sysroot=/dev/null,$(2)) $(3)\
+ --crate-type=rlib --out-dir=$(TMPOUT) --emit=obj=- -
>/dev/null,$(3),$(4))

# rustc-option
# Usage: rustflags-y += $(call
rustc-option,-Cinstrument-coverage,-Zinstrument-coverage)