Skip to content

Commit a87ab23

Browse files
committed
Set target
1 parent e1d688d commit a87ab23

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

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.*")

Modules/cpython-sys/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub const _Py_STATIC_FLAG_BITS: Py_ssize_t =
5555
pub const _Py_STATIC_IMMORTAL_INITIAL_REFCNT: Py_ssize_t =
5656
(_Py_IMMORTAL_INITIAL_REFCNT as Py_ssize_t) | (_Py_STATIC_FLAG_BITS << 48);
5757
#[cfg(not(target_pointer_width = "64"))]
58-
pub const _Py_STATIC_IMMORTAL_INITIAL_REFCNT: Py_ssize_t = 7u32 << 28;
58+
pub const _Py_STATIC_IMMORTAL_INITIAL_REFCNT: Py_ssize_t = (7u32 << 28) as Py_ssize_t;
5959

6060
#[repr(transparent)]
6161
pub struct PyObject(std::cell::UnsafeCell<_object>);
@@ -129,14 +129,22 @@ pub const PyObject_HEAD_INIT: PyObject = {
129129
PyObject(std::cell::UnsafeCell::new(obj))
130130
};
131131

132-
#[cfg(not(py_gil_disabled))]
132+
#[cfg(all(not(py_gil_disabled), target_pointer_width = "64"))]
133133
pub const PyObject_HEAD_INIT: PyObject = PyObject(std::cell::UnsafeCell::new(_object {
134134
__bindgen_anon_1: _object__bindgen_ty_1 {
135135
ob_refcnt_full: _Py_STATIC_IMMORTAL_INITIAL_REFCNT as i64,
136136
},
137137
ob_type: std::ptr::null_mut(),
138138
}));
139139

140+
#[cfg(all(not(py_gil_disabled), not(target_pointer_width = "64")))]
141+
pub const PyObject_HEAD_INIT: PyObject = PyObject(std::cell::UnsafeCell::new(_object {
142+
__bindgen_anon_1: _object__bindgen_ty_1 {
143+
ob_refcnt: _Py_STATIC_IMMORTAL_INITIAL_REFCNT,
144+
},
145+
ob_type: std::ptr::null_mut(),
146+
}));
147+
140148
pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base {
141149
ob_base: PyObject_HEAD_INIT,
142150
m_init: None,

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)