@@ -19,103 +19,119 @@ impl ParseCallbacks for UppercaseCallbacks {
1919pub struct Options {
2020 pub out_dir : PathBuf ,
2121 pub sources_dir : PathBuf ,
22+ }
23+
24+ pub struct Library {
25+ pub sources_dir : PathBuf ,
2226 pub target_triple : String ,
27+ pub header : & ' static [ u8 ] ,
28+ pub module : & ' static str ,
29+ pub includes : Vec < PathBuf > ,
30+ pub library : PathBuf ,
2331}
2432
2533pub struct Gen {
2634 opts : Options ,
35+ libs : Vec < Library > ,
2736}
2837
2938impl Gen {
30- pub fn new ( opts : Options ) -> Self {
31- Self { opts }
39+ pub fn new ( opts : Options , libs : Vec < Library > ) -> Self {
40+ Self { opts, libs }
3241 }
3342
3443 pub fn run_gen ( & mut self ) {
3544 let _ = fs:: remove_dir_all ( self . opts . out_dir . clone ( ) ) ;
3645 fs:: create_dir_all ( self . opts . out_dir . join ( "src/bindings" ) ) . unwrap ( ) ;
3746 fs:: create_dir_all ( self . opts . out_dir . join ( "src/lib" ) ) . unwrap ( ) ;
3847
39- // Create a named temporary file
40- let mut header = NamedTempFile :: new ( ) . unwrap ( ) ;
41-
42- // Write some data to the first handle
43- header
44- . write_all ( include_bytes ! ( "../inc/wpan-wba.h" ) )
45- . unwrap ( ) ;
46-
47- header. reopen ( ) . unwrap ( ) ;
48-
49- // The bindgen::Builder is the main entry point
50- // to bindgen, and lets you build up options for
51- // the resulting bindings.
52- let target_flag = format ! ( "--target={}" , self . opts. target_triple) ;
53- let include_arg = format ! (
54- "-I{}/Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc" ,
55- self . opts. sources_dir. to_str( ) . unwrap( )
56- ) ;
57- let mut builder = bindgen:: Builder :: default ( )
58- . parse_callbacks ( Box :: new ( UppercaseCallbacks ) )
59- // Force Clang to use the same layout as the selected target.
60- . clang_arg ( & target_flag)
61- . clang_arg ( & include_arg) ;
62- if self
63- . opts
64- . target_triple
65- . to_ascii_lowercase ( )
66- . starts_with ( "thumb" )
67- {
68- builder = builder. clang_arg ( "-mthumb" ) ;
69- }
70- let bindings = builder
71- // The input header we would like to generate
72- // bindings for.
73- . header ( "stm32-bindings-gen/inc/wpan-wba.h" )
74- // Finish the builder and generate the bindings.
75- . generate ( )
76- // Unwrap the Result and panic on failure.
77- . expect ( "Unable to generate bindings" ) ;
78-
79- let out_path = self . opts . out_dir . join ( "src/bindings/wpan_wba.rs" ) ;
80-
81- bindings
82- . write_to_file ( & out_path)
83- . expect ( "Couldn't write bindings!" ) ;
84-
85- let mut file_contents = fs:: read_to_string ( & out_path) . unwrap ( ) ;
86- file_contents = file_contents
87- . replace ( "::std::mem::" , "::core::mem::" )
88- . replace ( "::std::os::raw::" , "::core::ffi::" )
89- . replace ( "::std::option::" , "::core::option::" ) ;
90-
91- file_contents = file_contents
92- . lines ( )
93- . map ( |line| {
94- if let Some ( rest) = line. strip_prefix ( "pub const " ) {
95- if let Some ( ( name, tail) ) = rest. split_once ( ':' ) {
96- let upper = name. trim ( ) . to_ascii_uppercase ( ) ;
97- return format ! ( "pub const {}:{}" , upper, tail) ;
48+ for lib in & self . libs {
49+ let sources_dir = self . opts . sources_dir . join ( & lib. sources_dir ) ;
50+
51+ // Create a named temporary file
52+ let mut header = NamedTempFile :: with_suffix ( ".h" ) . unwrap ( ) ;
53+
54+ // Write some data to the first handle
55+ header. write_all ( lib. header ) . unwrap ( ) ;
56+
57+ // The bindgen::Builder is the main entry point
58+ // to bindgen, and lets you build up options for
59+ // the resulting bindings.
60+ let target_flag = format ! ( "--target={}" , lib. target_triple) ;
61+
62+ let mut builder = bindgen:: Builder :: default ( )
63+ . parse_callbacks ( Box :: new ( UppercaseCallbacks ) )
64+ // Force Clang to use the same layout as the selected target.
65+ . clang_arg ( & target_flag) ;
66+
67+ for include_arg in & lib. includes {
68+ builder = builder. clang_arg ( & format ! (
69+ "-I{}" ,
70+ sources_dir. join( include_arg) . to_str( ) . unwrap( )
71+ ) ) ;
72+ }
73+
74+ if lib. target_triple . to_ascii_lowercase ( ) . starts_with ( "thumb" ) {
75+ builder = builder. clang_arg ( "-mthumb" ) ;
76+ }
77+ let bindings = builder
78+ // The input header we would like to generate
79+ // bindings for.
80+ . header ( header. path ( ) . to_str ( ) . unwrap ( ) )
81+ // Finish the builder and generate the bindings.
82+ . generate ( )
83+ // Unwrap the Result and panic on failure.
84+ . expect ( "Unable to generate bindings" ) ;
85+
86+ let out_path = self
87+ . opts
88+ . out_dir
89+ . join ( "src" )
90+ . join ( "bindings" )
91+ . join ( format ! ( "{}.rs" , lib. module) ) ;
92+
93+ bindings
94+ . write_to_file ( & out_path)
95+ . expect ( "Couldn't write bindings!" ) ;
96+
97+ let mut file_contents = fs:: read_to_string ( & out_path) . unwrap ( ) ;
98+ file_contents = file_contents
99+ . replace ( "::std::mem::" , "::core::mem::" )
100+ . replace ( "::std::os::raw::" , "::core::ffi::" )
101+ . replace ( "::std::option::" , "::core::option::" ) ;
102+
103+ file_contents = file_contents
104+ . lines ( )
105+ . map ( |line| {
106+ if let Some ( rest) = line. strip_prefix ( "pub const " ) {
107+ if let Some ( ( name, tail) ) = rest. split_once ( ':' ) {
108+ let upper = name. trim ( ) . to_ascii_uppercase ( ) ;
109+ return format ! ( "pub const {}:{}" , upper, tail) ;
110+ }
98111 }
99- }
100- line. to_owned ( )
101- } )
102- . collect :: < Vec < _ > > ( )
103- . join ( "\n " ) ;
104-
105- if !file_contents. ends_with ( '\n' ) {
106- file_contents. push ( '\n' ) ;
112+ line. to_owned ( )
113+ } )
114+ . collect :: < Vec < _ > > ( )
115+ . join ( "\n " ) ;
116+
117+ if !file_contents. ends_with ( '\n' ) {
118+ file_contents. push ( '\n' ) ;
119+ }
120+
121+ fs:: write ( & out_path, file_contents) . unwrap ( ) ;
122+
123+ // copy misc files
124+ fs:: copy (
125+ sources_dir. join ( & lib. library ) ,
126+ self . opts
127+ . out_dir
128+ . join ( "src" )
129+ . join ( "lib" )
130+ . join ( format ! ( "{}.a" , lib. module) ) ,
131+ )
132+ . unwrap ( ) ;
107133 }
108134
109- fs:: write ( & out_path, file_contents) . unwrap ( ) ;
110-
111- // copy misc files
112- fs:: copy (
113- self . opts
114- . sources_dir
115- . join ( "Middlewares/ST/STM32_WPAN/mac_802_15_4/lib/wba_mac_lib.a" ) ,
116- self . opts . out_dir . join ( "src/lib/wba_mac_lib.a" ) ,
117- )
118- . unwrap ( ) ;
119135 fs:: write (
120136 self . opts . out_dir . join ( "README.md" ) ,
121137 include_bytes ! ( "../res/README.md" ) ,
0 commit comments