1+ use bindgen:: callbacks:: { ItemInfo , ItemKind , ParseCallbacks } ;
12use std:: io:: Write ;
23use std:: { fs, path:: PathBuf } ;
34use tempfile:: NamedTempFile ;
45
6+ #[ derive( Debug ) ]
7+ struct UppercaseCallbacks ;
8+
9+ impl ParseCallbacks for UppercaseCallbacks {
10+ fn item_name ( & self , item : ItemInfo < ' _ > ) -> Option < String > {
11+ if matches ! ( item. kind, ItemKind :: Var ) {
12+ Some ( item. name . to_ascii_uppercase ( ) )
13+ } else {
14+ None
15+ }
16+ }
17+ }
18+
519pub struct Options {
620 pub out_dir : PathBuf ,
721 pub sources_dir : PathBuf ,
22+ pub target_triple : String ,
823}
924
1025pub struct Gen {
@@ -34,11 +49,25 @@ impl Gen {
3449 // The bindgen::Builder is the main entry point
3550 // to bindgen, and lets you build up options for
3651 // the resulting bindings.
37- let bindings = bindgen:: Builder :: default ( )
38- . clang_arg ( format ! (
39- "-I{}/Middlewares/ST/STM32_WPAN/mac_802_15_4/core/inc" ,
40- self . opts. sources_dir. to_str( ) . unwrap( )
41- ) )
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
4271 // The input header we would like to generate
4372 // bindings for.
4473 . header ( "stm32-bindings-gen/inc/wpan-wba.h" )
@@ -53,12 +82,30 @@ impl Gen {
5382 . write_to_file ( & out_path)
5483 . expect ( "Couldn't write bindings!" ) ;
5584
56- let file_contents = fs:: read_to_string ( & out_path) . unwrap ( ) ;
57- let file_contents = file_contents
85+ let mut file_contents = fs:: read_to_string ( & out_path) . unwrap ( ) ;
86+ file_contents = file_contents
5887 . replace ( "::std::mem::" , "::core::mem::" )
5988 . replace ( "::std::os::raw::" , "::core::ffi::" )
6089 . replace ( "::std::option::" , "::core::option::" ) ;
6190
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) ;
98+ }
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' ) ;
107+ }
108+
62109 fs:: write ( & out_path, file_contents) . unwrap ( ) ;
63110
64111 // copy misc files
0 commit comments