@@ -63,16 +63,13 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
6363 // Suppress all clang warnings (deprecation warnings, etc.)
6464 builder = builder. clang_arg ( "-w" ) ;
6565
66- // Tell clang the correct target triple for cross-compilation.
67- // LLVM_TARGET is the clang/LLVM triple which may differ from the Rust
68- // target (e.g. arm64-apple-macosx vs aarch64-apple-darwin, or
69- // riscv64-unknown-linux-gnu vs riscv64gc-unknown-linux-gnu).
70- // Falls back to Cargo's TARGET if LLVM_TARGET is not set.
71- let target = env:: var ( "LLVM_TARGET" )
72- . or_else ( |_| env:: var ( "TARGET" ) )
73- . unwrap_or_default ( ) ;
74- if !target. is_empty ( ) {
75- builder = builder. clang_arg ( format ! ( "--target={}" , target) ) ;
66+ // Tell clang the correct target triple for cross-compilation when we have
67+ // an LLVM-specific triple. Otherwise let bindgen translate Cargo's TARGET
68+ // itself (e.g. aarch64-apple-ios-sim -> arm64-apple-ios-simulator).
69+ let cargo_target = env:: var ( "TARGET" ) . unwrap_or_default ( ) ;
70+ let llvm_target = env:: var ( "LLVM_TARGET" ) . unwrap_or_default ( ) ;
71+ if !llvm_target. is_empty ( ) && llvm_target != cargo_target {
72+ builder = builder. clang_arg ( format ! ( "--target={llvm_target}" ) ) ;
7673 }
7774
7875 // Extract cross-compilation flags from the C compiler command (PY_CC),
@@ -112,7 +109,7 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
112109 // WASI SDK: WASI_SDK_PATH is set by Tools/wasm/wasi/__main__.py.
113110 // The sysroot is at $WASI_SDK_PATH/share/wasi-sysroot.
114111 if !have_sysroot
115- && target . contains ( "wasi" )
112+ && cargo_target . contains ( "wasi" )
116113 && let Ok ( sdk_path) = env:: var ( "WASI_SDK_PATH" )
117114 {
118115 let sysroot = PathBuf :: from ( & sdk_path) . join ( "share" ) . join ( "wasi-sysroot" ) ;
@@ -128,7 +125,7 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
128125 // The sysroot is a sibling of bin/:
129126 // .../toolchains/llvm/prebuilt/<host>/sysroot
130127 if !have_sysroot
131- && target . contains ( "android" )
128+ && cargo_target . contains ( "android" )
132129 && let Ok ( cc) = env:: var ( "PY_CC" )
133130 && let Some ( parts) = shlex:: split ( & cc)
134131 && let Some ( binary) = parts. first ( )
0 commit comments