Skip to contentSkip to conversation

rust: Respect HOSTCC when linking for host

PATCH Superseded Matthew Maurer 4 participants 5 messages Sep 28, 2023 – Oct 5, 2023

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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.

---
 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)
rust/Makefile +2 -0
index 87958e864be0..da664d7aed51 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='$(subst ','\'',$(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.582.g8ccd20d70d-goog
Reviewed-by on v3 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 2 lines

---

Psst, next time I'd recommend putting patch version to version changes here. Still, LGTM.

Quoted 1 line

[...]

#2 lore raw
In reply to Matthew Maurer
Quoted 24 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.

---
 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)
rust/Makefile +0 -0
index 87958e864be0..da664d7aed51 100644

rust/Makefile $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE · line 387

Quoted 5 lines
rust/Makefile +2 -0
@@ -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='$(subst ','\'',$(KBUILD_HOSTLDFLAGS))' \

Why not consistently use the escsq macro here too ?

Quoted 17 lines
rust/Makefile +0 -0
                --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))
 
--
Quoted 2 lines

2.42.0.582.g8ccd20d70d-goog

--
Best Regards
Masahiro Yamada

#3 lore raw
Quoted 21 lines · Masahiro Yamada
Quoted 19 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.

---
 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)
Quoted 16 lines

rust/Makefile $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE · line 387

Quoted 10 lines
rust/Makefile +2 -0
index 87958e864be0..da664d7aed51 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='$(subst ','\'',$(KBUILD_HOSTLDFLAGS))' \

Why not consistently use the escsq macro here too ?

I didn't use it in rust/Makefile because that makefile doesn't directly import scripts/Kbuild.include, and I was under the mistaken impression that scripts/ was supposed to be its own namespace, even if Make wasn't enforcing it. Inspecting other functions in scripts/Kbuild.include though, it's clear they're called elsewhere, so I'll upload another version of the patch using escsq here as well.

Quoted 28 lines
Quoted 21 lines
rust/Makefile +0 -0
                --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))
 
--
Quoted 2 lines

2.42.0.582.g8ccd20d70d-goog

--
Best Regards
Masahiro Yamada

reviewer Tested-by on v3 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 1 line