Skip to content

Commit cb43e5b

Browse files
committed
Pass CPPFLAGS to cpython-sys and set target
1 parent 5ce8a5f commit cb43e5b

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h
33783378
# Module dependencies and platform-specific files
33793379

33803380
cpython-sys: Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h
3381-
CARGO_TARGET_DIR=$(abs_builddir)/target PYTHON_BUILD_DIR=$(abs_builddir) $(CARGO_HOME)/bin/cargo build --lib --locked --package cpython-sys --profile $(CARGO_PROFILE) $(if $(CARGO_TARGET),--target=$(CARGO_TARGET)) --manifest-path $(srcdir)/Cargo.toml
3381+
CARGO_TARGET_DIR=$(abs_builddir)/target PYTHON_BUILD_DIR=$(abs_builddir) PY_CPPFLAGS="$(CPPFLAGS)" $(CARGO_HOME)/bin/cargo build --lib --locked --package cpython-sys --profile $(CARGO_PROFILE) $(if $(CARGO_TARGET),--target=$(CARGO_TARGET)) --manifest-path $(srcdir)/Cargo.toml
33823382

33833383
RUST_STATICLIB_A= target/$(if $(CARGO_TARGET),$(CARGO_TARGET)/$(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR))/libcpython_rust_staticlib.a
33843384

Modules/cpython-sys/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2024"
66
[dependencies]
77

88
[build-dependencies]
9-
bindgen = "0.72.1"
9+
bindgen = "0.72.1"
10+
shlex = "1.3"

Modules/cpython-sys/build.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ fn main() {
1212
if gil_disabled(srcdir, builddir.as_deref()) {
1313
println!("cargo:rustc-cfg=py_gil_disabled");
1414
}
15+
println!("cargo::rustc-check-cfg=cfg(py_gil_disabled)");
1516
generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path());
16-
// TODO(emmatyping): generate bindings to the internal parser API
17-
// The parser includes things slightly differently, so we should generate
18-
// it's bindings independently
19-
//generate_parser_bindings(srcdir, &out_path.as_path());
2017
}
2118

2219
fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
@@ -39,6 +36,36 @@ fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
3936
fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path) {
4037
let mut builder = bindgen::Builder::default().header("wrapper.h");
4138

39+
// Suppress all clang warnings (deprecation warnings, etc.)
40+
builder = builder.clang_arg("-w");
41+
42+
// Tell clang the correct target triple for cross-compilation.
43+
// Without this, bindgen uses the host target which causes errors like
44+
// "thread-local storage is not supported" on iOS or missing headers
45+
// on Android/WASI.
46+
if let Ok(target) = env::var("TARGET") {
47+
builder = builder.clang_arg(format!("--target={}", target));
48+
}
49+
50+
// Forward cross-compilation flags (include paths, defines, sysroot)
51+
// from CPython's CPPFLAGS. These are needed so bindgen's clang can
52+
// find system headers (e.g. assert.h) when cross-compiling for
53+
// Android NDK, WASI, etc.
54+
if let Ok(cppflags) = env::var("PY_CPPFLAGS") {
55+
if let Some(flags) = shlex::split(&cppflags) {
56+
for flag in &flags {
57+
if flag.starts_with("-I")
58+
|| flag.starts_with("-D")
59+
|| flag.starts_with("--sysroot")
60+
|| flag.starts_with("-isysroot")
61+
|| flag.starts_with("-isystem")
62+
{
63+
builder = builder.clang_arg(flag);
64+
}
65+
}
66+
}
67+
}
68+
4269
// Always search the source dir and the public headers.
4370
let mut include_dirs = vec![srcdir.to_path_buf(), srcdir.join("Include")];
4471
// Include the build directory if provided; out-of-tree builds place

0 commit comments

Comments
 (0)