@@ -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
2219fn gil_disabled ( srcdir : & Path , builddir : Option < & str > ) -> bool {
@@ -39,6 +36,36 @@ fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
3936fn 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