1- use std:: { env, fs, io, process} ;
1+ use std:: { env, fs, io, path :: PathBuf , process} ;
22
33use camino:: { Utf8Path , Utf8PathBuf } ;
44use quote:: ToTokens ;
@@ -97,7 +97,7 @@ fn generate_preprocessed_bindings(include: &Utf8Path) -> String {
9797 bindgen:: Builder :: default ( )
9898 . header ( include. join ( "all.h" ) )
9999 . clang_args ( & [ "-target" , "armv7a-none-eabihf" ] )
100- . parse_callbacks ( Box :: new ( bindgen :: CargoCallbacks ) )
100+ . parse_callbacks ( Box :: new ( BindgenCallbacks :: new ( ) ) )
101101 . use_core ( )
102102 . ctypes_prefix ( "crate::ctypes" )
103103 . generate_comments ( false )
@@ -158,6 +158,7 @@ fn localize_bindings(original_include: &Utf8Path, localized_include: &Utf8Path)
158158 let new_include = self . include_regex . replace_all (
159159 & original_include,
160160 |captures : & regex:: Captures < ' _ > | {
161+ // Do not replace if it's one of these include paths
161162 if let "stddef.h" | "stdint.h" | "stdarg.h" = & captures[ 1 ] {
162163 return captures[ 0 ] . to_owned ( ) ;
163164 }
@@ -166,17 +167,40 @@ fn localize_bindings(original_include: &Utf8Path, localized_include: &Utf8Path)
166167 } ,
167168 ) ;
168169
169- let changed = match fs:: read_to_string ( local_include) {
170- Ok ( old_include) => old_include != new_include,
171- Err ( e) if e. kind ( ) == io:: ErrorKind :: NotFound => true ,
172- Err ( e) => panic ! ( "Failed to read old local include `{local_include}`: {e:?}" ) ,
173- } ;
174-
175- if changed {
176- fs:: write ( local_include, new_include. as_ref ( ) ) . unwrap ( ) ;
177- }
170+ fs:: write ( local_include, new_include. as_ref ( ) ) . unwrap ( ) ;
178171 }
179172 }
180173
181174 Localizer :: new ( localized_include) . localize_dir ( original_include, localized_include) ;
182175}
176+
177+ #[ derive( Debug ) ]
178+ struct BindgenCallbacks {
179+ out_dir : PathBuf ,
180+ }
181+
182+ impl BindgenCallbacks {
183+ fn new ( ) -> Self {
184+ BindgenCallbacks {
185+ out_dir : PathBuf :: from ( env:: var_os ( "OUT_DIR" ) . unwrap ( ) )
186+ . canonicalize ( )
187+ . unwrap ( ) ,
188+ }
189+ }
190+ }
191+
192+ impl bindgen:: callbacks:: ParseCallbacks for BindgenCallbacks {
193+ fn include_file ( & self , filename : & str ) {
194+ if !Utf8Path :: new ( filename)
195+ . canonicalize ( )
196+ . unwrap ( )
197+ . starts_with ( & self . out_dir )
198+ {
199+ println ! ( "cargo:rerun-if-changed={filename}" )
200+ }
201+ }
202+
203+ fn read_env_var ( & self , key : & str ) {
204+ println ! ( "cargo:rerun-if-env-changed={key}" ) ;
205+ }
206+ }
0 commit comments