Skip to content

Commit c98910d

Browse files
committed
Set target
1 parent e1d688d commit c98910d

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

Modules/cpython-sys/build.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ fn main() {
1212
if gil_disabled(srcdir, builddir.as_deref()) {
1313
println!("cargo:rustc-cfg=py_gil_disabled");
1414
}
15-
generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path());
15+
let target = env::var("TARGET").unwrap_or_default();
16+
generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path(), &target);
1617
// TODO(emmatyping): generate bindings to the internal parser API
1718
// The parser includes things slightly differently, so we should generate
1819
// it's bindings independently
@@ -36,9 +37,17 @@ fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
3637
false
3738
}
3839

39-
fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path) {
40+
fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path, target: &str) {
4041
let mut builder = bindgen::Builder::default().header("wrapper.h");
4142

43+
let host = env::var("HOST").unwrap_or_default();
44+
let is_cross_compiling = !target.is_empty() && target != host;
45+
46+
// For cross-compilation, don't auto-detect host system include paths
47+
if is_cross_compiling {
48+
builder = builder.detect_include_paths(false);
49+
}
50+
4251
// Always search the source dir and the public headers.
4352
let mut include_dirs = vec![srcdir.to_path_buf(), srcdir.join("Include")];
4453
// Include the build directory if provided; out-of-tree builds place
@@ -50,6 +59,32 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
5059
builder = builder.clang_arg(format!("-I{}", dir.display()));
5160
}
5261

62+
// Set target triple for cross-compilation
63+
if !target.is_empty() {
64+
builder = builder.clang_arg(format!("--target={}", target));
65+
}
66+
67+
// Handle WASI SDK sysroot and clang includes
68+
if target.contains("wasi") {
69+
if let Ok(wasi_sdk) = env::var("WASI_SDK_PATH") {
70+
let wasi_sdk_path = PathBuf::from(&wasi_sdk);
71+
let sysroot = wasi_sdk_path.join("share/wasi-sysroot");
72+
if sysroot.exists() {
73+
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
74+
}
75+
// Add clang's built-in headers (stddef.h, etc.) from WASI SDK
76+
if let Ok(entries) = std::fs::read_dir(wasi_sdk_path.join("lib/clang")) {
77+
for entry in entries.flatten() {
78+
let clang_include = entry.path().join("include");
79+
if clang_include.exists() {
80+
builder = builder.clang_arg(format!("-I{}", clang_include.display()));
81+
break;
82+
}
83+
}
84+
}
85+
}
86+
}
87+
5388
let bindings = builder
5489
.allowlist_function("_?Py.*")
5590
.allowlist_type("_?Py.*")

configure

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4347,7 +4347,6 @@ else
43474347
;;
43484348
wasm32-unknown-wasip1)
43494349
CARGO_TARGET=wasm32-wasip1
4350-
export BINDGEN_EXTRA_CLANG_ARGS_wasm32_wasip1="--sysroot=/opt/wasi-sdk/share/wasi-sysroot"
43514350
;;
43524351
*)
43534352
CARGO_TARGET="$host"

rust-toolchain.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[toolchain]
22
channel = "1.91.1"
33
components = ["rustfmt", "clippy"]
4+
targets = ["wasm32-wasip1"]

0 commit comments

Comments
 (0)