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

Asuna Yang posted 4 patches 2 months ago
[PATCH v5 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Asuna Yang 2 months ago
From: Asuna Yang <spriteovo@gmail.com>

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.

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 <SpriteOvO@gmail.com>
---
 scripts/Makefile.rust | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/scripts/Makefile.rust b/scripts/Makefile.rust
index 5c12b4b8c8b6..b219244cd051 100644
--- a/scripts/Makefile.rust
+++ b/scripts/Makefile.rust
@@ -6,4 +6,8 @@ BINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
 BINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
 BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
 
+ifeq ($(BINDGEN_TARGET),)
+$(error add '--target=' option to scripts/Makefile.rust)
+else
 export BINDGEN_TARGET
+endif

-- 
2.51.1
Re: [PATCH v5 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Mingcong Bai 1 month, 2 weeks ago
Hi Asuna,

在 2025/12/4 15:54, Asuna Yang 写道:
> From: Asuna Yang <spriteovo@gmail.com>
> 
> 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.
> 
> 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 <SpriteOvO@gmail.com>

Unfortunately, this broke build for MIPS and PowerPC, neither of which 
has support for Rust. I also feel as though it might be pointless to ask 
all architecture to define BINDGEN_TARGET, especially when such a target 
specification may not even exist upstream (in Rust or even in LLVM).

As shown in the first patch, the original Makefile did not require this 
definition either (please do enlighten me).

Best Regards,
Mingcong Bai
Re: [PATCH v5 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Vivian Wang 1 month, 2 weeks ago
On 12/23/25 20:09, Mingcong Bai wrote:
> Hi Asuna,
>
> 在 2025/12/4 15:54, Asuna Yang 写道:
>> From: Asuna Yang <spriteovo@gmail.com>
>>
>> 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.
>>
>> 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 <SpriteOvO@gmail.com>
>
> Unfortunately, this broke build for MIPS and PowerPC, neither of which
> has support for Rust. I also feel as though it might be pointless to
> ask all architecture to define BINDGEN_TARGET, especially when such a
> target specification may not even exist upstream (in Rust or even in
> LLVM).
>
> As shown in the first patch, the original Makefile did not require
> this definition either (please do enlighten me). 

Looks like the rust/ subdir is conditional on CONFIG_RUST, so the
original rust/Makefile didn't have this problem.

Fixing this should be as simple as moving the check into rust/Makefile
instead.

Vivian "dramforever" Wang

Re: [PATCH v5 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
Posted by Asuna Yang 2 months ago
I noticed that RISC-V CI bjorn/dtb-warn-rv64 [^1] is failing somehow due 
to the error here. I'll look into it tomorrow.

[^1]: 
https://patchwork.kernel.org/project/linux-riscv/patch/20251204-gcc-rust-v5-v5-4-2d4f20d86c24@gmail.com/