rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] rust: Respect HOSTCC when linking for host
@ 2023-09-28 19:48 Matthew Maurer
  2023-09-28 21:02 ` Martin Rodriguez Reboredo
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Matthew Maurer @ 2023-09-28 19:48 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada,
	Nathan Chancellor, Nick Desaulniers
  Cc: Matthew Maurer, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Nicolas Schier,
	Tom Rix, rust-for-linux, linux-kernel, linux-kbuild, llvm

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`.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
 rust/Makefile         | 2 ++
 scripts/Makefile.host | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/rust/Makefile b/rust/Makefile
index 87958e864be0..da664d7aed51 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -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 $@)) $<
diff --git a/scripts/Makefile.host b/scripts/Makefile.host
index 8f7f842b54f9..08d83d9db31a 100644
--- a/scripts/Makefile.host
+++ b/scripts/Makefile.host
@@ -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.582.g8ccd20d70d-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] rust: Respect HOSTCC when linking for host
  2023-09-28 19:48 [PATCH v3] rust: Respect HOSTCC when linking for host Matthew Maurer
@ 2023-09-28 21:02 ` Martin Rodriguez Reboredo
  2023-09-30  5:58 ` Masahiro Yamada
  2023-10-02  7:42 ` Alice Ryhl
  2 siblings, 0 replies; 5+ messages in thread
From: Martin Rodriguez Reboredo @ 2023-09-28 21:02 UTC (permalink / raw)
  To: Matthew Maurer, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Masahiro Yamada, Nathan Chancellor, Nick Desaulniers
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Nicolas Schier, Tom Rix,
	rust-for-linux, linux-kernel, linux-kbuild, llvm

On 9/28/23 16:48, Matthew Maurer wrote:
> 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`.
> 
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> ---

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

> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] rust: Respect HOSTCC when linking for host
  2023-09-28 19:48 [PATCH v3] rust: Respect HOSTCC when linking for host Matthew Maurer
  2023-09-28 21:02 ` Martin Rodriguez Reboredo
@ 2023-09-30  5:58 ` Masahiro Yamada
  2023-10-05 21:36   ` Matthew Maurer
  2023-10-02  7:42 ` Alice Ryhl
  2 siblings, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2023-09-30  5:58 UTC (permalink / raw)
  To: Matthew Maurer
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Nathan Chancellor, Nick Desaulniers, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Nicolas Schier, Tom Rix, rust-for-linux, linux-kernel,
	linux-kbuild, llvm

On Fri, Sep 29, 2023 at 4:48 AM Matthew Maurer <mmaurer@google.com> wrote:
>
> 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`.
>
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> ---
>  rust/Makefile         | 2 ++
>  scripts/Makefile.host | 2 ++
>  2 files changed, 4 insertions(+)
>
> diff --git a/rust/Makefile b/rust/Makefile
> index 87958e864be0..da664d7aed51 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -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 ?






>                 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
>                 --crate-type proc-macro \
>                 --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index 8f7f842b54f9..08d83d9db31a 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -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.582.g8ccd20d70d-goog
>


--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] rust: Respect HOSTCC when linking for host
  2023-09-28 19:48 [PATCH v3] rust: Respect HOSTCC when linking for host Matthew Maurer
  2023-09-28 21:02 ` Martin Rodriguez Reboredo
  2023-09-30  5:58 ` Masahiro Yamada
@ 2023-10-02  7:42 ` Alice Ryhl
  2 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2023-10-02  7:42 UTC (permalink / raw)
  To: Matthew Maurer
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Masahiro Yamada,
	Nathan Chancellor, Nick Desaulniers, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Nicolas Schier, Tom Rix, rust-for-linux, linux-kernel,
	linux-kbuild, llvm

On Thu, Sep 28, 2023 at 9:48 PM Matthew Maurer <mmaurer@google.com> wrote:
>
> 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`.
>
> Signed-off-by: Matthew Maurer <mmaurer@google.com>

Tested-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] rust: Respect HOSTCC when linking for host
  2023-09-30  5:58 ` Masahiro Yamada
@ 2023-10-05 21:36   ` Matthew Maurer
  0 siblings, 0 replies; 5+ messages in thread
From: Matthew Maurer @ 2023-10-05 21:36 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Nathan Chancellor, Nick Desaulniers, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Nicolas Schier, Tom Rix, rust-for-linux, linux-kernel,
	linux-kbuild, llvm

On Fri, Sep 29, 2023 at 10:59 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> On Fri, Sep 29, 2023 at 4:48 AM Matthew Maurer <mmaurer@google.com> wrote:
> >
> > 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`.
> >
> > Signed-off-by: Matthew Maurer <mmaurer@google.com>
> > ---
> >  rust/Makefile         | 2 ++
> >  scripts/Makefile.host | 2 ++
> >  2 files changed, 4 insertions(+)
> >
> > diff --git a/rust/Makefile b/rust/Makefile
> > index 87958e864be0..da664d7aed51 100644
> > --- a/rust/Makefile
> > +++ b/rust/Makefile
> > @@ -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.
>
>
>
>
> >                 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
> >                 --crate-type proc-macro \
> >                 --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
> > diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> > index 8f7f842b54f9..08d83d9db31a 100644
> > --- a/scripts/Makefile.host
> > +++ b/scripts/Makefile.host
> > @@ -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.582.g8ccd20d70d-goog
> >
>
>
> --
> Best Regards
> Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-10-05 21:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-28 19:48 [PATCH v3] rust: Respect HOSTCC when linking for host Matthew Maurer
2023-09-28 21:02 ` Martin Rodriguez Reboredo
2023-09-30  5:58 ` Masahiro Yamada
2023-10-05 21:36   ` Matthew Maurer
2023-10-02  7:42 ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).