[PATCH v6 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined

Asuna Yang posted 4 patches 1 month, 1 week ago
[PATCH v6 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Asuna Yang 1 month, 1 week ago
Generate a friendly fatal error if the target triplet is undefined for
bindgen, rather than having the compiler generate obscure error messages
during the build stage.

`BINDGEN_TARGET` is actually defined in `scripts/Makefile.rust`, but the
file is included regardless of whether Rust is enabled, so perform this
check in `rust/Makefile` to avoid breaking targets that do not yet
support Rust builds.

This piece of code is copied from `scripts/Makefile.clang`.

Before this commit, error messages might look like:

error: unknown argument: '-mno-riscv-attribute'
error: unsupported argument 'medany' to option '-mcmodel=' for target
'unknown'
error: unsupported option '-march=' for target ''
error: unsupported option '-mno-save-restore' for target ''
error: unknown target triple 'unknown'
panicked at bindgen/ir/context.rs:562:15:
libclang error; possible causes include:
- Invalid flag syntax
- Unrecognized flags
- Invalid flag arguments
- File I/O errors
- Host vs. target architecture mismatch

Acked-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Asuna Yang <xinrui.riscv@isrc.iscas.ac.cn>
---
 rust/Makefile | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/rust/Makefile b/rust/Makefile
index 2603b34f9833..37b4205afb70 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -385,6 +385,12 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
 	-fzero-init-padding-bits=% -mno-fdpic \
 	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
 
+# Because scripts/Makefile.rust is included regardless of whether Rust is enabled,
+# we perform this check here to avoid breaking targets that do not yet support Rust builds.
+ifeq ($(BINDGEN_TARGET),)
+$(error add '--target=' option to scripts/Makefile.rust)
+endif
+
 # All warnings are inhibited since GCC builds are very experimental,
 # many GCC warnings are not supported by Clang, they may only appear in
 # some configurations, with new GCC versions, etc.

-- 
2.51.1
Re: [PATCH v6 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Nathan Chancellor 1 week, 3 days ago
On Tue, Dec 30, 2025 at 05:47:55PM +0100, Asuna Yang wrote:
> Generate a friendly fatal error if the target triplet is undefined for
> bindgen, rather than having the compiler generate obscure error messages
> during the build stage.
> 
> `BINDGEN_TARGET` is actually defined in `scripts/Makefile.rust`, but the
> file is included regardless of whether Rust is enabled, so perform this
> check in `rust/Makefile` to avoid breaking targets that do not yet
> support Rust builds.
> 
> This piece of code is copied from `scripts/Makefile.clang`.
> 
> Before this commit, error messages might look like:
> 
> error: unknown argument: '-mno-riscv-attribute'
> error: unsupported argument 'medany' to option '-mcmodel=' for target
> 'unknown'
> error: unsupported option '-march=' for target ''
> error: unsupported option '-mno-save-restore' for target ''
> error: unknown target triple 'unknown'
> panicked at bindgen/ir/context.rs:562:15:
> libclang error; possible causes include:
> - Invalid flag syntax
> - Unrecognized flags
> - Invalid flag arguments
> - File I/O errors
> - Host vs. target architecture mismatch
> 
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
> Signed-off-by: Asuna Yang <xinrui.riscv@isrc.iscas.ac.cn>
> ---
>  rust/Makefile | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/Makefile b/rust/Makefile
> index 2603b34f9833..37b4205afb70 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -385,6 +385,12 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
>  	-fzero-init-padding-bits=% -mno-fdpic \
>  	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
>  
> +# Because scripts/Makefile.rust is included regardless of whether Rust is enabled,
> +# we perform this check here to avoid breaking targets that do not yet support Rust builds.

This might read a little better if it were

  scripts/Makefile.rust is included ..., so we perform ...

instead of

  Because ..., we perform ...

or at the very least reversing the phrases

  We perform ... because ...

But that could just be personal preference.

> +ifeq ($(BINDGEN_TARGET),)
> +$(error add '--target=' option to scripts/Makefile.rust)
> +endif
> +
>  # All warnings are inhibited since GCC builds are very experimental,
>  # many GCC warnings are not supported by Clang, they may only appear in
>  # some configurations, with new GCC versions, etc.
> 
> -- 
> 2.51.1
>
Re: [PATCH v6 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Asuna Yang 3 days, 6 hours ago
On 1/30/26 06:40, Nathan Chancellor wrote:

> This might read a little better if it were
> 
>    scripts/Makefile.rust is included ..., so we perform ...
> 
> instead of
> 
>    Because ..., we perform ...
> 
> or at the very least reversing the phrases
> 
>    We perform ... because ...
> 
> But that could just be personal preference.

I think you're right. Since I'm not a native English speaker, some of my 
phrasing might not sound quite natural.

However, considering this isn't really a big deal, I'd prefer to skip 
submitting a new patch to fix it. Unlike GitHub, I'm concerned that a 
new patch might make too much noise on the mailing list and interrupt 
the thread.

Or if someone could help modify it when merging this patch, that would 
be better! I've read the docs, and the kernel tree seems to allow this 
practice.

Best regards,
Asuna