1+ use std:: io:: Write ;
12use std:: { fs, path:: PathBuf } ;
3+ use tempfile:: NamedTempFile ;
24
35pub struct Options {
46 pub out_dir : PathBuf ,
@@ -15,9 +17,20 @@ impl Gen {
1517 }
1618
1719 pub fn run_gen ( & mut self ) {
20+ let _ = fs:: remove_dir_all ( self . opts . out_dir . clone ( ) ) ;
1821 fs:: create_dir_all ( self . opts . out_dir . join ( "src/bindings" ) ) . unwrap ( ) ;
1922 fs:: create_dir_all ( self . opts . out_dir . join ( "src/lib" ) ) . unwrap ( ) ;
2023
24+ // Create a named temporary file
25+ let mut header = NamedTempFile :: new ( ) . unwrap ( ) ;
26+
27+ // Write some data to the first handle
28+ header
29+ . write_all ( include_bytes ! ( "../inc/wpan-wba.h" ) )
30+ . unwrap ( ) ;
31+
32+ header. reopen ( ) . unwrap ( ) ;
33+
2134 // The bindgen::Builder is the main entry point
2235 // to bindgen, and lets you build up options for
2336 // the resulting bindings.
@@ -34,10 +47,20 @@ impl Gen {
3447 // Unwrap the Result and panic on failure.
3548 . expect ( "Unable to generate bindings" ) ;
3649
50+ let out_path = self . opts . out_dir . join ( "src/bindings/wpan_wba.rs" ) ;
51+
3752 bindings
38- . write_to_file ( self . opts . out_dir . join ( "src/bindings/wpan-wba.rs" ) )
53+ . write_to_file ( & out_path )
3954 . expect ( "Couldn't write bindings!" ) ;
4055
56+ let file_contents = fs:: read_to_string ( & out_path) . unwrap ( ) ;
57+ let file_contents = file_contents
58+ . replace ( "::std::mem::" , "::core::mem::" )
59+ . replace ( "::std::os::raw::" , "::core::ffi::" )
60+ . replace ( "::std::option::" , "::core::option::" ) ;
61+
62+ fs:: write ( & out_path, file_contents) . unwrap ( ) ;
63+
4164 // copy misc files
4265 fs:: copy (
4366 self . opts
@@ -51,6 +74,11 @@ impl Gen {
5174 include_bytes ! ( "../res/README.md" ) ,
5275 )
5376 . unwrap ( ) ;
77+ fs:: write (
78+ self . opts . out_dir . join ( "Cargo.toml" ) ,
79+ include_bytes ! ( "../res/Cargo.toml" ) ,
80+ )
81+ . unwrap ( ) ;
5482 fs:: write (
5583 self . opts . out_dir . join ( "build.rs" ) ,
5684 include_bytes ! ( "../res/build.rs" ) ,
@@ -61,5 +89,11 @@ impl Gen {
6189 include_bytes ! ( "../res/src/lib.rs" ) ,
6290 )
6391 . unwrap ( ) ;
92+
93+ fs:: write (
94+ self . opts . out_dir . join ( "src/bindings/mod.rs" ) ,
95+ include_bytes ! ( "../res/src/bindings/mod.rs" ) ,
96+ )
97+ . unwrap ( ) ;
6498 }
6599}
0 commit comments