Skip to content

Commit 205c8a2

Browse files
committed
Set target
1 parent 327944d commit 205c8a2

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Modules/cpython-sys/build.rs

Lines changed: 18 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,21 @@ 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+
// For Android targets, use the NDK sysroot if available
60+
if target.contains("android") {
61+
if let Ok(ndk_home) = env::var("ANDROID_NDK_HOME") {
62+
let sysroot = PathBuf::from(&ndk_home).join("toolchains/llvm/prebuilt/linux-x86_64/sysroot");
63+
if sysroot.exists() {
64+
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
65+
}
66+
}
67+
}
68+
5369
let bindings = builder
5470
.allowlist_function("_?Py.*")
5571
.allowlist_type("_?Py.*")

0 commit comments

Comments
 (0)