@@ -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.*" )
0 commit comments