Skip to content

Commit e8cba65

Browse files
committed
Set target
1 parent e1d688d commit e8cba65

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Modules/cpython-sys/build.rs

Lines changed: 17 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,7 +37,7 @@ 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

4243
// Always search the source dir and the public headers.
@@ -50,6 +51,20 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
5051
builder = builder.clang_arg(format!("-I{}", dir.display()));
5152
}
5253

54+
// Set target triple for cross-compilation
55+
if !target.is_empty() {
56+
builder = builder.clang_arg(format!("--target={}", target));
57+
}
58+
59+
if target.contains("wasip1") {
60+
if let Ok(ndk_home) = env::var("WASI_SDK_PATH") {
61+
let sysroot = PathBuf::from(&ndk_home).join("toolchains/llvm/prebuilt/linux-x86_64/sysroot");
62+
if sysroot.exists() {
63+
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
64+
}
65+
}
66+
}
67+
5368
let bindings = builder
5469
.allowlist_function("_?Py.*")
5570
.allowlist_type("_?Py.*")

0 commit comments

Comments
 (0)