From e1d688d8536c842654a04802c2621ecf45cac32c Mon Sep 17 00:00:00 2001 From: Aliaksei Semianiuk Date: Sat, 27 Dec 2025 23:24:18 +0500 Subject: [PATCH 1/2] Fix WASI build --- configure | 4 ++++ configure.ac | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/configure b/configure index 5b09de36f9a0b8..5e36f79bf931b2 100755 --- a/configure +++ b/configure @@ -16122,6 +16122,10 @@ else *-pc-linux-*) CARGO_TARGET=$(echo "$host" | sed 's/-pc-linux-/-unknown-linux-/') ;; + wasm32-unknown-wasip1) + CARGO_TARGET=wasm32-wasip1 + export BINDGEN_EXTRA_CLANG_ARGS_wasm32_wasip1="--sysroot=/opt/wasi-sdk/share/wasi-sysroot" + ;; *) CARGO_TARGET="$host" ;; diff --git a/configure.ac b/configure.ac index 1561c6f9b2e99f..db87af119cc4da 100644 --- a/configure.ac +++ b/configure.ac @@ -4345,6 +4345,10 @@ else *-pc-linux-*) CARGO_TARGET=$(echo "$host" | sed 's/-pc-linux-/-unknown-linux-/') ;; + wasm32-unknown-wasip1) + CARGO_TARGET=wasm32-wasip1 + export BINDGEN_EXTRA_CLANG_ARGS_wasm32_wasip1="--sysroot=/opt/wasi-sdk/share/wasi-sysroot" + ;; *) CARGO_TARGET="$host" ;; From ff21137ba5a65f8babf189419590d480ef94370f Mon Sep 17 00:00:00 2001 From: Aliaksei Semianiuk Date: Fri, 2 Jan 2026 14:23:21 +0500 Subject: [PATCH 2/2] Set target --- Modules/cpython-sys/build.rs | 28 ++++++++++++++++++++++++++-- configure | 1 - configure.ac | 1 - 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Modules/cpython-sys/build.rs b/Modules/cpython-sys/build.rs index 248b141bfd6d99..1a6d5f6e2e361b 100644 --- a/Modules/cpython-sys/build.rs +++ b/Modules/cpython-sys/build.rs @@ -12,7 +12,8 @@ fn main() { if gil_disabled(srcdir, builddir.as_deref()) { println!("cargo:rustc-cfg=py_gil_disabled"); } - generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path()); + let target = env::var("TARGET").unwrap_or_default(); + generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path(), &target); // TODO(emmatyping): generate bindings to the internal parser API // The parser includes things slightly differently, so we should generate // it's bindings independently @@ -36,9 +37,17 @@ fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool { false } -fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path) { +fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path, target: &str) { let mut builder = bindgen::Builder::default().header("wrapper.h"); + let host = env::var("HOST").unwrap_or_default(); + let is_cross_compiling = !target.is_empty() && target != host; + + // For cross-compilation, don't auto-detect host system include paths + if is_cross_compiling { + builder = builder.detect_include_paths(false); + } + // Always search the source dir and the public headers. let mut include_dirs = vec![srcdir.to_path_buf(), srcdir.join("Include")]; // Include the build directory if provided; out-of-tree builds place @@ -50,6 +59,21 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat builder = builder.clang_arg(format!("-I{}", dir.display())); } + // Set target triple for cross-compilation + if !target.is_empty() { + builder = builder.clang_arg(format!("--target={}", target)); + } + + // Handle WASI SDK sysroot + if target.contains("wasi") { + if let Ok(wasi_sdk) = env::var("WASI_SDK_PATH") { + let sysroot = PathBuf::from(&wasi_sdk).join("share/wasi-sysroot"); + if sysroot.exists() { + builder = builder.clang_arg(format!("--sysroot={}", sysroot.display())); + } + } + } + let bindings = builder .allowlist_function("_?Py.*") .allowlist_type("_?Py.*") diff --git a/configure b/configure index 5e36f79bf931b2..73d13789252293 100755 --- a/configure +++ b/configure @@ -16124,7 +16124,6 @@ else ;; wasm32-unknown-wasip1) CARGO_TARGET=wasm32-wasip1 - export BINDGEN_EXTRA_CLANG_ARGS_wasm32_wasip1="--sysroot=/opt/wasi-sdk/share/wasi-sysroot" ;; *) CARGO_TARGET="$host" diff --git a/configure.ac b/configure.ac index db87af119cc4da..bd8f5eca68a8c3 100644 --- a/configure.ac +++ b/configure.ac @@ -4347,7 +4347,6 @@ else ;; wasm32-unknown-wasip1) CARGO_TARGET=wasm32-wasip1 - export BINDGEN_EXTRA_CLANG_ARGS_wasm32_wasip1="--sysroot=/opt/wasi-sdk/share/wasi-sysroot" ;; *) CARGO_TARGET="$host"