Skip to content

Commit ff21137

Browse files
committed
Set target
1 parent e1d688d commit ff21137

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎Modules/cpython-sys/build.rs‎

Lines changed: 26 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,21 @@ 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
68+
if target.contains("wasi") {
69+
if let Ok(wasi_sdk) = env::var("WASI_SDK_PATH") {
70+
let sysroot = PathBuf::from(&wasi_sdk).join("share/wasi-sysroot");
71+
if sysroot.exists() {
72+
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
73+
}
74+
}
75+
}
76+
5377
let bindings = builder
5478
.allowlist_function("_?Py.*")
5579
.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"

0 commit comments

Comments
 (0)