@@ -51,15 +51,15 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
5151 builder = builder. clang_arg ( format ! ( "--target={}" , target) ) ;
5252 }
5353
54- // Extract cross-compilation flags from the C compiler command (PY_CC)
55- // and preprocessor flags (PY_CPPFLAGS). These provide the sysroot and
56- // include paths that bindgen's clang needs to find system headers when
57- // cross-compiling.
54+ // Extract cross-compilation flags from the C compiler command (PY_CC),
55+ // preprocessor flags (PY_CPPFLAGS), and compiler flags (PY_CFLAGS).
56+ // These provide the sysroot, include paths, and defines that bindgen's
57+ // clang needs when cross-compiling.
5858 //
59- // - WASI: the sysroot is embedded in CC ("clang --sysroot=...")
60- // - iOS: -isysroot in CPPFLAGS points to the SDK
59+ // - WASI: sysroot in CC, -D_WASI_EMULATED_SIGNAL in CFLAGS
60+ // - iOS: -isysroot in CPPFLAGS
6161 let mut have_sysroot = false ;
62- for env_name in [ "PY_CC" , "PY_CPPFLAGS" ] {
62+ for env_name in [ "PY_CC" , "PY_CPPFLAGS" , "PY_CFLAGS" ] {
6363 if let Ok ( value) = env:: var ( env_name) {
6464 if let Some ( flags) = shlex:: split ( & value) {
6565 let mut iter = flags. iter ( ) . peekable ( ) ;
@@ -126,13 +126,15 @@ fn generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Pat
126126 }
127127 }
128128
129- // Always search the source dir and the public headers.
130- let mut include_dirs = vec ! [ srcdir . to_path_buf ( ) , srcdir . join ( "Include" ) ] ;
131- // Include the build directory if provided; out-of-tree builds place
132- // the generated pyconfig.h there.
129+ // Include the build directory first so that cross-build pyconfig.h
130+ // takes precedence over any pyconfig.h in the source tree (which may
131+ // be from a native build with different settings like LONG_BIT).
132+ let mut include_dirs = Vec :: new ( ) ;
133133 if let Some ( build) = builddir {
134134 include_dirs. push ( PathBuf :: from ( build) ) ;
135135 }
136+ include_dirs. push ( srcdir. to_path_buf ( ) ) ;
137+ include_dirs. push ( srcdir. join ( "Include" ) ) ;
136138
137139 for dir in include_dirs {
138140 builder = builder. clang_arg ( format ! ( "-I{}" , dir. display( ) ) ) ;
0 commit comments