Skip to contentSkip to conversation

kbuild: rust: avoid creating temporary files

PATCH Superseded Miguel Ojeda 5 participants 10 messages Jul 18, 2023 – Jul 23, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 kbuild: rust: avoid creating temporary files

rustc outputs by default the temporary files (i.e. the ones saved by -Csave-temps, such as *.rcgu* files) in the current working directory when -o and --out-dir are not given (even if --emit=x=path is given, i.e. it does not use those for temporaries).

Since out-of-tree modules are compiled from the linux tree, rustc then tries to create them there, which may not be accessible.

Thus pass --out-dir explicitly, even if it is just for the temporary files.

---
 scripts/Makefile.build | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.build +4 -1
index 6413342a03f4..82e3fb19fdaf 100644
@@ -264,6 +264,9 @@ $(obj)/%.lst: $(src)/%.c FORCE
 
 rust_allowed_features := new_uninit
 
+# `--out-dir` is required to avoid temporaries being created by `rustc` in the
+# current working directory, which may be not accessible in the out-of-tree
+# modules case.
 rust_common_cmd = \
 	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
 	-Zallow-features=$(rust_allowed_features) \
@@ -272,7 +275,7 @@ rust_common_cmd = \
 	--extern alloc --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
-	--emit=dep-info=$(depfile)
+	--out-dir $(dir $@) --emit=dep-info=$(depfile)
 
 # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
 # will be used. We explicitly request `-Ccodegen-units=1` in any case, and

base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5

Signature · 1 line
2.41.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Miguel Ojeda

rustc outputs by default the temporary files (i.e. the ones saved by -Csave-temps, such as *.rcgu* files) in the current working directory when -o and --out-dir are not given (even if --emit=x=path is given, i.e. it does not use those for temporaries).

Since out-of-tree modules are compiled from the linux tree, rustc then tries to create them there, which may not be accessible.

Thus pass --out-dir explicitly, even if it is just for the temporary files.

Quoted 7 lines

Seems reasonable to me.

Quoted 32 lines
---
 scripts/Makefile.build | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.build +4 -1
index 6413342a03f4..82e3fb19fdaf 100644
@@ -264,6 +264,9 @@ $(obj)/%.lst: $(src)/%.c FORCE
 
 rust_allowed_features := new_uninit
 
+# `--out-dir` is required to avoid temporaries being created by `rustc` in the
+# current working directory, which may be not accessible in the out-of-tree
+# modules case.
 rust_common_cmd = \
 	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
 	-Zallow-features=$(rust_allowed_features) \
@@ -272,7 +275,7 @@ rust_common_cmd = \
 	--extern alloc --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
-	--emit=dep-info=$(depfile)
+	--out-dir $(dir $@) --emit=dep-info=$(depfile)
 
 # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
 # will be used. We explicitly request `-Ccodegen-units=1` in any case, and

base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5

Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Miguel Ojeda
Quoted 18 lines · Miguel Ojeda

rustc outputs by default the temporary files (i.e. the ones saved by -Csave-temps, such as *.rcgu* files) in the current working directory when -o and --out-dir are not given (even if --emit=x=path is given, i.e. it does not use those for temporaries).

Since out-of-tree modules are compiled from the linux tree, rustc then tries to create them there, which may not be accessible.

Thus pass --out-dir explicitly, even if it is just for the temporary files.

Quoted 2 lines

---
[...]

#3 lore raw
In reply to Miguel Ojeda
Quoted 12 lines · Miguel Ojeda

rustc outputs by default the temporary files (i.e. the ones saved by -Csave-temps, such as *.rcgu* files) in the current working directory when -o and --out-dir are not given (even if --emit=x=path is given, i.e. it does not use those for temporaries).

Since out-of-tree modules are compiled from the linux tree, rustc then tries to create them there, which may not be accessible.

Thus pass --out-dir explicitly, even if it is just for the temporary files.

Quoted 8 lines

---

Applied to linux-kbuild/fixes. Thanks.

I believe this is a fix, so I will include it
in my next pull request.

Quoted 31 lines
 scripts/Makefile.build | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.build +4 -1
index 6413342a03f4..82e3fb19fdaf 100644
@@ -264,6 +264,9 @@ $(obj)/%.lst: $(src)/%.c FORCE
 
 rust_allowed_features := new_uninit
 
+# `--out-dir` is required to avoid temporaries being created by `rustc` in the
+# current working directory, which may be not accessible in the out-of-tree
+# modules case.
 rust_common_cmd = \
        RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
        -Zallow-features=$(rust_allowed_features) \
@@ -272,7 +275,7 @@ rust_common_cmd = \
        --extern alloc --extern kernel \
        --crate-type rlib -L $(objtree)/rust/ \
        --crate-name $(basename $(notdir $@)) \
-       --emit=dep-info=$(depfile)
+       --out-dir $(dir $@) --emit=dep-info=$(depfile)
 
 # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
 # will be used. We explicitly request `-Ccodegen-units=1` in any case, and

base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5 --
2.41.0

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

Applied to linux-kbuild/fixes. Thanks.

I believe this is a fix, so I will include it
in my next pull request.

Thanks Masahiro! Yeah, it can be considered a fix. By the way, in case you want:

    Fixes: 2f7ab1267dc9 ("Kbuild: add Rust support")

I guess it can be also considered a feature (e.g. "supporting more setups"), but having the temporaries created where they were was unintentional.

Cheers,
Miguel

#5 lore raw
Quoted 11 lines · Miguel Ojeda
Quoted 5 lines · Masahiro Yamada

Applied to linux-kbuild/fixes. Thanks.

I believe this is a fix, so I will include it
in my next pull request.

Thanks Masahiro! Yeah, it can be considered a fix. By the way, in case you want:

    Fixes: 2f7ab1267dc9 ("Kbuild: add Rust support")

Maybe, the following was a breakage.

commit 295d8398c67e314d99bb070f38883f83fe94a97a

Author: Masahiro Yamada <[email protected]>
Date:   Sat Jan 7 18:18:15 2023 +0900

kbuild: specify output names separately for each emission type from rustc

Before that, rust_common_cmd had --out-dir.

BTW, do we also need to fix scripts/Makefile.host in case the external module Makefile creates host programs?

Quoted 3 lines

I guess it can be also considered a feature (e.g. "supporting more setups"), but having the temporaries created where they were was unintentional.

Signature · 2 lines
Best Regards
Masahiro Yamada
#6 lore raw
Quoted 10 lines · Masahiro Yamada

Maybe, the following was a breakage.

commit 295d8398c67e314d99bb070f38883f83fe94a97a

Author: Masahiro Yamada <[email protected]>
Date:   Sat Jan 7 18:18:15 2023 +0900

kbuild: specify output names separately for each emission type from rustc

Before that, rust_common_cmd had --out-dir.

That's right, good catch!

Quoted 2 lines

BTW, do we also need to fix scripts/Makefile.host in case the external module Makefile creates host programs?

Indeed, we need it too. [1] would fix it (tested it with a trivial out-of-tree Rust hostprog).

Do you want me to send it separately? i.e. to avoid losing the Tested-bys etc. that we already got for this one, which is the important one since that actually has users.

The hostprogs fix is not really critical for stable, since it is unlikely there are users at all (we just got the first in-tree real user in the Rust+KUnit integration coming into 6.6), but I guess it does not hurt for the same reason.

Thanks!

Cheers,
Miguel

[1]

scripts/Makefile.host +1 -1
index 7aea9005e497..54adf2e2ec51 100644
@@ -86,7 +86,7 @@ hostc_flags    = -Wp,-MMD,$(depfile) \
 hostcxx_flags  = -Wp,-MMD,$(depfile) \
                  $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
                  $(HOSTCXXFLAGS_$(target-stem).o)
-hostrust_flags = --emit=dep-info=$(depfile) \
+hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
                  $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
                  $(HOSTRUSTFLAGS_$(target-stem))
#7 lore raw
Quoted 21 lines · Miguel Ojeda
Quoted 10 lines · Masahiro Yamada

Maybe, the following was a breakage.

commit 295d8398c67e314d99bb070f38883f83fe94a97a

Author: Masahiro Yamada <[email protected]>
Date:   Sat Jan 7 18:18:15 2023 +0900

kbuild: specify output names separately for each emission type from rustc

Before that, rust_common_cmd had --out-dir.

That's right, good catch!

Quoted 2 lines

BTW, do we also need to fix scripts/Makefile.host in case the external module Makefile creates host programs?

Indeed, we need it too. [1] would fix it (tested it with a trivial out-of-tree Rust hostprog).

Quoted 8 lines

Do you want me to send it separately? i.e. to avoid losing the Tested-bys etc. that we already got for this one, which is the important one since that actually has users.

The hostprogs fix is not really critical for stable, since it is unlikely there are users at all (we just got the first in-tree real user in the Rust+KUnit integration coming into 6.6), but I guess it does not hurt for the same reason.

Can you send v2 with the following squashed?

I think it makes sense to fix both if we add

Fixes 295d8398c67e ("kbuild: specify output names separately for each

emission type from rustc")

Quoted 19 lines

Thanks!

Cheers,
Miguel

[1]

scripts/Makefile.host +1 -1
index 7aea9005e497..54adf2e2ec51 100644
@@ -86,7 +86,7 @@ hostc_flags    = -Wp,-MMD,$(depfile) \
 hostcxx_flags  = -Wp,-MMD,$(depfile) \
                  $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
                  $(HOSTCXXFLAGS_$(target-stem).o)
-hostrust_flags = --emit=dep-info=$(depfile) \
+hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
                  $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
                  $(HOSTRUSTFLAGS_$(target-stem))
Signature · 2 lines
Best Regards
Masahiro Yamada
#8 lore raw
Quoted 34 lines · Masahiro Yamada
Quoted 21 lines · Miguel Ojeda
Quoted 10 lines · Masahiro Yamada

Maybe, the following was a breakage.

commit 295d8398c67e314d99bb070f38883f83fe94a97a

Author: Masahiro Yamada <[email protected]>
Date:   Sat Jan 7 18:18:15 2023 +0900

kbuild: specify output names separately for each emission type from rustc

Before that, rust_common_cmd had --out-dir.

That's right, good catch!

Quoted 2 lines

BTW, do we also need to fix scripts/Makefile.host in case the external module Makefile creates host programs?

Indeed, we need it too. [1] would fix it (tested it with a trivial out-of-tree Rust hostprog).

Quoted 8 lines

Do you want me to send it separately? i.e. to avoid losing the Tested-bys etc. that we already got for this one, which is the important one since that actually has users.

The hostprogs fix is not really critical for stable, since it is unlikely there are users at all (we just got the first in-tree real user in the Rust+KUnit integration coming into 6.6), but I guess it does not hurt for the same reason.

Quoted 6 lines

Can you send v2 with the following squashed?

I think it makes sense to fix both if we add

Fixes 295d8398c67e ("kbuild: specify output names separately for each

emission type from rustc")

I dropped v1 for now,
expecting to get v2 soon.

Signature · 2 lines
Best Regards
Masahiro Yamada