From: Nicolas Schier <nsc@kernel.org>
To: Mohamad Alsadhan <mo@sdhn.cc>
Cc: nathan@kernel.org, ojeda@kernel.org, gary@garyguo.net,
miguel.ojeda.sandonis@gmail.com, linux-kbuild@vger.kernel.org,
rust-for-linux@vger.kernel.org,
Yoann Congal <yoann.congal@smile.fr>
Subject: Re: [PATCH v4] kbuild: host: use single executable for rustc -C linker
Date: Fri, 20 Mar 2026 07:51:44 +0100 [thread overview]
Message-ID: <abzuCFiO3mB3nUM_@derry.ads.avm.de> (raw)
In-Reply-To: <20260317112021.14353-1-mo@sdhn.cc>
On Tue, Mar 17, 2026 at 02:20:21PM +0300, Mohamad Alsadhan wrote:
> rustc's -C linker= option expects a single executable path. When
> HOSTCC contains a wrapper (e.g. "ccache gcc"), passing
> `-Clinker=$(HOSTCC)` results in the shell splitting the value into
> multiple words, and rustc interprets the additional word as an
> input filename:
>
> error: multiple input filenames provided ...
>
> Generate a small wrapper script and pass it to -Clinker e.g.
> ```
> exec sh -c 'exec "$0" "$@"' ccache gcc "$@"
> ```
>
> This fix should be general enough to address most if not all cases
> (incl. wrappers or subcommands) and avoids surprises of simpler fixes
> like just defaulting to gcc.
>
> This avoids passing the user command as an environment variable as
> that would be more challenging to trace and debug shell expansions.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1224
> Suggested-by: Yoann Congal <yoann.congal@smile.fr>
> Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
> ---
> v3 -> v4:
> - Use filechk instead of if_changed macro to regenerate script
> - Remove trailing space at EOL
>
> v2 -> v3:
> - Scrap previous hacky approaches (e.g. using lastword) and go with
> a proper fix which turned out not that complex to implement.
> Apologies Gary, I should have listened to you earlier :/
>
> v1 -> v2:
> - Rename HOSTRUSTC_LINKER to HOSTRUSTC_LD for consistency
> - Introduce explicit HOSTRUSTC_LD override
> - Warn when falling back due to multi-argument HOSTCC
> - Error out if a user-specified HOSTRUSTC_LD is not an executable
>
> v1: https://lore.kernel.org/all/20260225102819.16553-1-mo@sdhn.cc/
> v2: https://lore.kernel.org/all/20260227132713.23106-1-mo@sdhn.cc/
> v3: https://lore.kernel.org/all/20260312002852.11292-1-mo@sdhn.cc/
> ---
> scripts/Makefile.host | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index c1dedf646..e41753828 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -87,11 +87,29 @@ hostcxx_flags = -Wp,-MMD,$(depfile) \
> $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
> $(HOSTCXXFLAGS_$(target-stem).o)
>
> +# rustc's `-Clinker=` expects a single executable path, not a command line.
> +# `HOSTCC` may be a multi-word command when wrapped (e.g. "ccache gcc"), which
> +# would otherwise be split by the shell and mis-parsed by rustc.
> +# To work around this, we generate a wrapper script that forwards arguments to
> +# `HOSTRUSTC_LD` so that such commands can be used safely.
> +#
> +# Set `HOSTRUSTC_LD` for a different rustc linker command than `HOSTCC`
> +HOSTRUSTC_LD ?= $(HOSTCC)
> +define filechk_rustc-wrapper
> + printf "%s\n" \
> + '#!/bin/sh' \
> + 'exec sh -c '\''exec "$$0" "$$@"'\'' $(HOSTRUSTC_LD) "$$@"'
> +endef
This printf-based solution needs some more tweaking to also support
complex commands in HOSTRUSTC_LD with quoted spaces. A straight forward
way could be:
'exec sh -c '\''exec "$$0" "$$@"'\'' $(call escsq,$(HOSTRUSTC_LD)) "$$@"'
to escape single quotes within HOSTRUSTC_LD. Then the complex example
from v3 works again (make HOSTRUSTC_LD="env 'CCACHE_DIR=/tmp/my cache' ccache gcc").
With the quoting fixed:
Reviewed-by: Nicolas Schier <nsc@kernel.org>
Tested-by: Nicolas Schier <nsc@kernel.org>
I am still not seeing a benefit in calling usage of 'exec' and 'sh -c'.
If we'd skip both, there would be no need to use 'env' for environment
updates:
'$(call escsq,$(HOSTRUSTC_LD)) "$$@"'
But I may be missing something here.
Kind regards,
Nicolas
next prev parent reply other threads:[~2026-03-20 6:52 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260227132713.23106-1-mo@sdhn.cc/>
2026-03-12 0:28 ` [PATCH v3] kbuild: host: use single executable for rustc -C linker Mohamad Alsadhan
2026-03-12 13:50 ` Nicolas Schier
2026-03-17 11:10 ` Mohamad Alsadhan
2026-03-20 6:50 ` Nicolas Schier
2026-03-17 11:20 ` [PATCH v4] " Mohamad Alsadhan
2026-03-20 6:51 ` Nicolas Schier [this message]
2026-03-21 14:57 ` Mohamad Alsadhan
2026-03-21 15:00 ` [PATCH v5] " Mohamad Alsadhan
2026-03-25 8:45 ` Nicolas Schier
2026-03-29 2:12 ` Mohamad Alsadhan
2026-03-29 19:46 ` Nicolas Schier
2026-03-25 14:25 ` Yoann Congal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=abzuCFiO3mB3nUM_@derry.ads.avm.de \
--to=nsc@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kbuild@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=mo@sdhn.cc \
--cc=nathan@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=yoann.congal@smile.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox