@@ -33,6 +33,32 @@ fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
3333 false
3434}
3535
36+ /// Map a Rust target triple to the corresponding LLVM/clang target triple.
37+ /// Most are identical, but some platforms differ:
38+ /// - Apple: aarch64 → arm64, darwin → macosx, ios-sim → ios-simulator
39+ /// - RISC-V: riscv64gc → riscv64, riscv32gc → riscv32
40+ /// - WASI threads: wasip1-threads → wasi
41+ fn rust_target_to_llvm ( rust_target : & str ) -> String {
42+ match rust_target {
43+ "aarch64-apple-darwin" => "arm64-apple-macosx" . into ( ) ,
44+ "x86_64-apple-darwin" => "x86_64-apple-macosx" . into ( ) ,
45+ "aarch64-apple-ios" => "arm64-apple-ios" . into ( ) ,
46+ "aarch64-apple-ios-sim" => "arm64-apple-ios-simulator" . into ( ) ,
47+ "x86_64-apple-ios" => "x86_64-apple-ios-simulator" . into ( ) ,
48+ "wasm32-wasip1-threads" => "wasm32-wasi" . into ( ) ,
49+ other => {
50+ // riscv64gc-* → riscv64-*, riscv32gc-* → riscv32-*
51+ if let Some ( rest) = other. strip_prefix ( "riscv64gc-" ) {
52+ return format ! ( "riscv64-{rest}" ) ;
53+ }
54+ if let Some ( rest) = other. strip_prefix ( "riscv32gc-" ) {
55+ return format ! ( "riscv32-{rest}" ) ;
56+ }
57+ other. into ( )
58+ }
59+ }
60+ }
61+
3662fn generate_c_api_bindings ( srcdir : & Path , builddir : Option < & str > , out_path : & Path ) {
3763 let mut builder = bindgen:: Builder :: default ( ) . header ( "wrapper.h" ) ;
3864
@@ -43,9 +69,13 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
4369 // LLVM_TARGET is the clang/LLVM triple which may differ from the Rust
4470 // target (e.g. arm64-apple-macosx vs aarch64-apple-darwin, or
4571 // riscv64-unknown-linux-gnu vs riscv64gc-unknown-linux-gnu).
46- // Falls back to Cargo's TARGET if LLVM_TARGET is not set .
72+ // Falls back to Cargo's TARGET with known Rust→LLVM mappings .
4773 let target = env:: var ( "LLVM_TARGET" )
48- . or_else ( |_| env:: var ( "TARGET" ) )
74+ . ok ( )
75+ . filter ( |s| !s. is_empty ( ) )
76+ . or_else ( || {
77+ env:: var ( "TARGET" ) . ok ( ) . map ( |t| rust_target_to_llvm ( & t) )
78+ } )
4979 . unwrap_or_default ( ) ;
5080 if !target. is_empty ( ) {
5181 builder = builder. clang_arg ( format ! ( "--target={}" , target) ) ;
0 commit comments