Skip to contentSkip to conversation

rust: Respect HOSTCC when linking for host

PATCH In review Matthew Maurer 6 participants 7 messages Oct 5, 2023 – Oct 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 rust: Respect HOSTCC when linking for host

Currently, rustc defaults to invoking cc, even if HOSTCC is defined, resulting in build failures in hermetic environments where cc does not exist. This includes both hostprogs and proc-macros.

Since we are setting the linker to HOSTCC, we set the linker flavor to gcc explicitly. The linker-flavor selects both which linker to search for if the linker is unset, and which kind of linker flags to pass. Without this flag, rustc would attempt to determine which flags to pass based on the name of the binary passed as HOSTCC. gcc is the name of the linker-flavor used by rustc for all C compilers, including both gcc and clang.

---

Edited to use escsq in both Makefiles, as per Masahiro Yamada's suggestion.

 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)
rust/Makefile +2 -0
index 87958e864be0..2ddd821d9435 100644
@@ -383,6 +383,8 @@ $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
 quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
       cmd_rustc_procmacro = \
 	$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
+		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
+		-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
 		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
 		--crate-type proc-macro \
 		--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
scripts/Makefile.host +2 -0
index 8f7f842b54f9..08d83d9db31a 100644
@@ -91,6 +91,8 @@ hostcxx_flags  = -Wp,-MMD,$(depfile) \
 # current working directory, which may be not accessible in the out-of-tree
 # modules case.
 hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
+		 -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
+		 -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
                  $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
                  $(HOSTRUSTFLAGS_$(target-stem))
 
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Reviewed-by on v4 1/1
#1 lore raw
Quoted 12 lines · Matthew Maurer

Currently, rustc defaults to invoking cc, even if HOSTCC is defined, resulting in build failures in hermetic environments where cc does not exist. This includes both hostprogs and proc-macros.

Since we are setting the linker to HOSTCC, we set the linker flavor to gcc explicitly. The linker-flavor selects both which linker to search for if the linker is unset, and which kind of linker flags to pass. Without this flag, rustc would attempt to determine which flags to pass based on the name of the binary passed as HOSTCC. gcc is the name of the linker-flavor used by rustc for all C compilers, including both gcc and clang.

Quoted 3 lines

---
[...]

reviewer
#2 lore raw

PATCH v4 1/1 rust: Respect HOSTCC when linking for host

In reply to Matthew Maurer
Quoted 12 lines · Matthew Maurer

Currently, rustc defaults to invoking cc, even if HOSTCC is defined, resulting in build failures in hermetic environments where cc does not exist. This includes both hostprogs and proc-macros.

Since we are setting the linker to HOSTCC, we set the linker flavor to gcc explicitly. The linker-flavor selects both which linker to search for if the linker is unset, and which kind of linker flags to pass. Without this flag, rustc would attempt to determine which flags to pass based on the name of the binary passed as HOSTCC. gcc is the name of the linker-flavor used by rustc for all C compilers, including both gcc and clang.

Quoted 1 line
Reviewed-by on v4 1/1
#3 lore raw
In reply to Matthew Maurer
Quoted 13 lines · Matthew Maurer

Currently, rustc defaults to invoking cc, even if HOSTCC is defined, resulting in build failures in hermetic environments where cc does not exist. This includes both hostprogs and proc-macros.

Since we are setting the linker to HOSTCC, we set the linker flavor to gcc explicitly. The linker-flavor selects both which linker to search for if the linker is unset, and which kind of linker flags to pass. Without this flag, rustc would attempt to determine which flags to pass based on the name of the binary passed as HOSTCC. gcc is the name of the linker-flavor used by rustc for all C compilers, including both gcc and clang.

Quoted 5 lines

---

Edited to use escsq in both Makefiles, as per Masahiro Yamada's suggestion.

That looks better; thanks Matthew!

Quoted 34 lines
 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)
rust/Makefile +2 -0
index 87958e864be0..2ddd821d9435 100644
@@ -383,6 +383,8 @@ $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
 quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
       cmd_rustc_procmacro = \
        $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
+               -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
+               -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
                --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
                --crate-type proc-macro \
                --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
scripts/Makefile.host +2 -1
index 8f7f842b54f9..08d83d9db31a 100644
@@ -91,6 +91,8 @@ hostcxx_flags  = -Wp,-MMD,$(depfile) \
 # current working directory, which may be not accessible in the out-of-tree
 # modules case.
 hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
+                -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
+                -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
                  $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
                  $(HOSTRUSTFLAGS_$(target-stem))
 
--

2.42.0.609.gbb76f46606-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
Acked-by on v4 1/1
#4 lore raw
In reply to Matthew Maurer
Quoted 13 lines · Matthew Maurer

Currently, rustc defaults to invoking cc, even if HOSTCC is defined, resulting in build failures in hermetic environments where cc does not exist. This includes both hostprogs and proc-macros.

Since we are setting the linker to HOSTCC, we set the linker flavor to gcc explicitly. The linker-flavor selects both which linker to search for if the linker is unset, and which kind of linker flags to pass. Without this flag, rustc would attempt to determine which flags to pass based on the name of the binary passed as HOSTCC. gcc is the name of the linker-flavor used by rustc for all C compilers, including both gcc and clang.

Quoted 5 lines

---

Edited to use escsq in both Makefiles, as per Masahiro Yamada's suggestion.

if Miguel picked this up.

Please let me know if I should pick this up.

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

if Miguel picked this up.

Please let me know if I should pick this up.

Thanks a lot for taking a look Masahiro -- either way is fine for me. If you want to take it, please feel free to add my Acked-by.

Cheers,
Miguel

#6 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Masahiro Yamada

if Miguel picked this up.

Quoted 7 lines
Quoted 1 line

Please let me know if I should pick this up.

Thanks a lot for taking a look Masahiro -- either way is fine for me. If you want to take it, please feel free to add my Acked-by.

Cheers,
Miguel

I do not see this yet in linux-next.

So, I applied to linux-kbuild with Miguel's Ack now.

Signature · 2 lines
Best Regards
Masahiro Yamada