Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Modules/cpython-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +37,7 @@ 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");

// Always search the source dir and the public headers.
Expand All @@ -50,6 +51,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));
}

// For Android targets, use the NDK sysroot if available
if target.contains("android") {
if let Ok(ndk_home) = env::var("ANDROID_NDK_HOME") {
let sysroot = PathBuf::from(&ndk_home).join("toolchains/llvm/prebuilt/linux-x86_64/sysroot");
if sysroot.exists() {
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
}
}
}

let bindings = builder
.allowlist_function("_?Py.*")
.allowlist_type("_?Py.*")
Expand Down
3 changes: 3 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4342,6 +4342,9 @@ else
esac
CARGO_TARGET="${cargo_host%%-apple-darwin*}-apple-darwin"
;;
*-linux-android*)
CARGO_TARGET=$(echo "$host" | sed 's/^\([^-]*\)-[^-]*-linux-android/\1-linux-android/')
;;
*-pc-linux-*)
CARGO_TARGET=$(echo "$host" | sed 's/-pc-linux-/-unknown-linux-/')
;;
Expand Down
4 changes: 4 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[toolchain]
channel = "1.91.1"
components = ["rustfmt", "clippy"]
targets = [
"aarch64-linux-android",
"x86_64-linux-android",
]
Loading