6
6
* option. This file may not be copied, modified, or distributed except
7
7
* according to those terms. */
8
8
9
- use bindgen;
10
-
11
9
use std:: fs:: File ;
12
- use std:: io:: { stderr, Write } ;
10
+ use std:: io:: Write ;
11
+
12
+ use bindgen;
13
+ use bindgen:: callbacks:: IntKind ;
13
14
14
15
use crate :: headers;
15
16
16
17
#[ derive( Debug ) ]
17
- struct StderrLogger ;
18
+ struct ParseCallback ;
18
19
19
- impl bindgen:: Logger for StderrLogger {
20
- fn error ( & self , msg : & str ) {
21
- let _ = writeln ! ( stderr( ) , "Bindgen ERROR: {}" , msg) ;
20
+ impl bindgen:: callbacks:: ParseCallbacks for ParseCallback {
21
+ fn int_macro ( & self , name : & str , _value : i64 ) -> Option < IntKind > {
22
+ if name. starts_with ( "MBEDTLS_" ) {
23
+ Some ( IntKind :: Int )
24
+ } else {
25
+ None
26
+ }
27
+ }
28
+ fn enum_variant_name (
29
+ & self ,
30
+ _enum_name : Option < & str > ,
31
+ original_variant_name : & str ,
32
+ _variant_value : bindgen:: callbacks:: EnumVariantValue ,
33
+ ) -> Option < String > {
34
+ if original_variant_name. starts_with ( "MBEDTLS_" ) {
35
+ Some (
36
+ original_variant_name
37
+ . trim_start_matches ( "MBEDTLS_" )
38
+ . to_string ( ) ,
39
+ )
40
+ } else {
41
+ None
42
+ }
22
43
}
23
- fn warn ( & self , msg : & str ) {
24
- let _ = writeln ! ( stderr( ) , "Bindgen WARNING: {}" , msg) ;
44
+
45
+ fn item_name ( & self , original_item_name : & str ) -> Option < String > {
46
+ if original_item_name. eq ( "mbedtls_time_t" ) {
47
+ None
48
+ } else if original_item_name. starts_with ( "mbedtls_" ) {
49
+ Some (
50
+ original_item_name
51
+ . trim_start_matches ( "mbedtls_" )
52
+ . to_string ( ) ,
53
+ )
54
+ } else if original_item_name. starts_with ( "MBEDTLS_" ) {
55
+ Some (
56
+ original_item_name
57
+ . trim_start_matches ( "MBEDTLS_" )
58
+ . to_string ( ) ,
59
+ )
60
+ } else {
61
+ None
62
+ }
25
63
}
26
64
}
27
65
@@ -33,49 +71,55 @@ impl super::BuildConfig {
33
71
Ok ( for h in headers:: enabled_ordered ( ) {
34
72
writeln ! ( f, "#include <mbedtls/{}>" , h) ?;
35
73
} )
36
- } ) . expect ( "bindgen-input.h I/O error" ) ;
74
+ } )
75
+ . expect ( "bindgen-input.h I/O error" ) ;
37
76
38
77
let include = self . mbedtls_src . join ( "include" ) ;
39
78
40
- let logger = StderrLogger ;
41
- let mut bindgen = bindgen:: Builder :: new ( header. into_os_string ( ) . into_string ( ) . unwrap ( ) ) ;
42
- let bindings = bindgen
43
- . log ( & logger)
44
- . clang_arg ( "-Dmbedtls_t_udbl=mbedtls_t_udbl;" ) // bindgen can't handle unused uint128
79
+ let bindings = bindgen:: Builder :: default ( )
80
+ . header ( header. into_os_string ( ) . into_string ( ) . unwrap ( ) )
45
81
. clang_arg ( format ! (
46
82
"-DMBEDTLS_CONFIG_FILE=<{}>" ,
47
83
self . config_h. to_str( ) . expect( "config.h UTF-8 error" )
48
- ) ) . clang_arg ( format ! (
84
+ ) )
85
+ . clang_arg ( format ! (
49
86
"-I{}" ,
50
87
include. to_str( ) . expect( "include/ UTF-8 error" )
51
- ) ) . match_pat ( include. to_str ( ) . expect ( "include/ UTF-8 error" ) )
52
- . match_pat ( self . config_h . to_str ( ) . expect ( "config.h UTF-8 error" ) )
53
- . use_core ( true )
88
+ ) )
89
+ . use_core ( )
54
90
. derive_debug ( false ) // buggy :(
55
- . ctypes_prefix ( vec ! [ "types" . to_owned( ) , "raw_types" . to_owned( ) ] )
56
- . remove_prefix ( "mbedtls_" )
57
- . rust_enums ( false )
58
- . convert_macros ( true )
59
- . macro_int_types (
60
- vec ! [
61
- "sint" ,
62
- "sint" ,
63
- "sint" ,
64
- "slonglong" ,
65
- "sint" ,
66
- "sint" ,
67
- "sint" ,
68
- "slonglong" ,
69
- ] . into_iter ( ) ,
70
- ) . generate ( )
91
+ . parse_callbacks ( Box :: new ( ParseCallback ) )
92
+ . ctypes_prefix ( "crate::types::raw_types" )
93
+ . blacklist_function ( "strtold" )
94
+ . blacklist_function ( "qecvt_r" )
95
+ . blacklist_function ( "qecvt" )
96
+ . blacklist_function ( "qfcvt_r" )
97
+ . blacklist_function ( "qgcvt" )
98
+ . blacklist_function ( "qfcvt" )
99
+ . opaque_type ( "std::*" )
100
+ . opaque_type ( "time_t" )
101
+ . generate_comments ( false )
102
+ . prepend_enum_name ( false )
103
+ . generate ( )
71
104
. expect ( "bindgen error" ) ;
72
105
73
106
let bindings_rs = self . out_dir . join ( "bindings.rs" ) ;
74
107
File :: create ( & bindings_rs)
75
108
. and_then ( |mut f| {
109
+ f. write_all ( br#"
110
+ #![allow(nonstandard_style)]
111
+ #![allow(unused_imports)]
112
+ "# ) ?;
113
+
76
114
bindings. write ( Box :: new ( & mut f) ) ?;
77
- f. write_all ( b"use crate::types::*;\n " ) // for FILE, time_t, etc.
78
- } ) . expect ( "bindings.rs I/O error" ) ;
115
+
116
+ f. write_all ( br#"
117
+ // for FILE, time_t, etc.
118
+ use crate::types::*;
119
+ "# )
120
+
121
+ } )
122
+ . expect ( "bindings.rs I/O error" ) ;
79
123
80
124
let mod_bindings = self . out_dir . join ( "mod-bindings.rs" ) ;
81
125
File :: create ( & mod_bindings)
0 commit comments