1
+ //! Generate Rust bindings for C and C++ libraries.
2
+ //!
3
+ //! Provide a C/C++ header file, receive Rust FFI code to call into C/C++
4
+ //! functions and use types defined in the header.
5
+ //!
6
+ //! See the [Builder](./struct.Builder.html) struct for usage.
7
+
1
8
#![ crate_name = "bindgen" ]
2
9
#![ crate_type = "dylib" ]
3
10
4
11
#![ cfg_attr( feature = "clippy" , feature( plugin) ) ]
5
12
#![ cfg_attr( feature = "clippy" , plugin( clippy) ) ]
6
13
14
+ #![ deny( missing_docs) ]
15
+
16
+ // We internally use the deprecated BindgenOptions all over the place. Once we
17
+ // remove its `pub` declaration, we can un-deprecate it and remove this pragma.
18
+ #![ allow( deprecated) ]
19
+
7
20
// To avoid rather annoying warnings when matching with CXCursor_xxx as a
8
21
// constant.
9
22
#![ allow( non_upper_case_globals) ]
@@ -45,107 +58,189 @@ use ir::item::{Item, ItemId};
45
58
use parse:: { ClangItemParser , ParseError } ;
46
59
use regex_set:: RegexSet ;
47
60
61
+ /// Configure and generate Rust bindings for a C/C++ header.
62
+ ///
63
+ /// This is the main entry point to the library.
64
+ ///
65
+ /// ```ignore
66
+ /// use bindgen::builder;
67
+ ///
68
+ /// // Configure and generate bindings.
69
+ /// let bindings = try!(builder().header("path/to/input/header")
70
+ /// .whitelisted_type("SomeCoolClass")
71
+ /// .whitelisted_function("do_some_cool_thing")
72
+ /// .generate());
73
+ ///
74
+ /// // Write the generated bindings to an output file.
75
+ /// try!(bindings.write_to_file("path/to/output.rs"));
76
+ /// ```
48
77
#[ derive( Debug , Default ) ]
49
78
pub struct Builder {
50
79
options : BindgenOptions ,
51
80
}
52
81
82
+ /// Construct a new [`Builder`](./struct.Builder.html).
53
83
pub fn builder ( ) -> Builder {
54
84
Default :: default ( )
55
85
}
56
86
57
87
impl Builder {
88
+ /// Set the input C/C++ header.
58
89
pub fn header < T : Into < String > > ( self , header : T ) -> Builder {
59
90
self . clang_arg ( header)
60
91
}
61
92
93
+ /// Hide the given type from the generated bindings.
62
94
pub fn hide_type < T : Into < String > > ( mut self , arg : T ) -> Builder {
63
95
self . options . hidden_types . insert ( arg. into ( ) ) ;
64
96
self
65
97
}
66
98
99
+ /// Treat the given type as opaque in the generated bindings.
67
100
pub fn opaque_type < T : Into < String > > ( mut self , arg : T ) -> Builder {
68
101
self . options . opaque_types . insert ( arg. into ( ) ) ;
69
102
self
70
103
}
71
104
105
+ /// Whitelist the given type so that it (and all types that it transitively
106
+ /// refers to) appears in the generated bindings.
72
107
pub fn whitelisted_type < T : Borrow < str > > ( mut self , arg : T ) -> Builder {
73
108
self . options . whitelisted_types . insert ( & arg) ;
74
109
self
75
110
}
76
111
112
+ /// Whitelist the given function so that it (and all types that it
113
+ /// transitively refers to) appears in the generated bindings.
77
114
pub fn whitelisted_function < T : Borrow < str > > ( mut self , arg : T ) -> Builder {
78
115
self . options . whitelisted_functions . insert ( & arg) ;
79
116
self
80
117
}
81
118
119
+ /// Whitelist the given variable so that it (and all types that it
120
+ /// transitively refers to) appears in the generated bindings.
82
121
pub fn whitelisted_var < T : Borrow < str > > ( mut self , arg : T ) -> Builder {
83
122
self . options . whitelisted_vars . insert ( & arg) ;
84
123
self
85
124
}
86
125
126
+ /// Add a string to prepend to the generated bindings. The string is passed
127
+ /// through without any modification.
87
128
pub fn raw_line < T : Into < String > > ( mut self , arg : T ) -> Builder {
88
129
self . options . raw_lines . push ( arg. into ( ) ) ;
89
130
self
90
131
}
91
132
133
+ /// Add an argument to be passed straight through to clang.
92
134
pub fn clang_arg < T : Into < String > > ( mut self , arg : T ) -> Builder {
93
135
self . options . clang_args . push ( arg. into ( ) ) ;
94
136
self
95
137
}
96
138
139
+ /// Make the generated bindings link the given shared library.
97
140
pub fn link < T : Into < String > > ( mut self , library : T ) -> Builder {
98
141
self . options . links . push ( ( library. into ( ) , LinkType :: Default ) ) ;
99
142
self
100
143
}
101
144
145
+ /// Make the generated bindings link the given static library.
102
146
pub fn link_static < T : Into < String > > ( mut self , library : T ) -> Builder {
103
147
self . options . links . push ( ( library. into ( ) , LinkType :: Static ) ) ;
104
148
self
105
149
}
106
150
151
+ /// Make the generated bindings link the given framework.
107
152
pub fn link_framework < T : Into < String > > ( mut self , library : T ) -> Builder {
108
153
self . options . links . push ( ( library. into ( ) , LinkType :: Framework ) ) ;
109
154
self
110
155
}
111
156
157
+ /// Emit bindings for builtin definitions (for example `__builtin_va_list`)
158
+ /// in the generated Rust.
112
159
pub fn emit_builtins ( mut self ) -> Builder {
113
160
self . options . builtins = true ;
114
161
self
115
162
}
116
163
164
+ /// Avoid generating any unstable Rust in the generated bindings.
117
165
pub fn no_unstable_rust ( mut self ) -> Builder {
118
166
self . options . unstable_rust = false ;
119
167
self
120
168
}
121
169
170
+ /// Generate the Rust bindings using the options built up thus far.
122
171
pub fn generate ( self ) -> Result < Bindings , ( ) > {
123
172
Bindings :: generate ( self . options , None )
124
173
}
125
174
}
126
175
127
- /// Deprecated - use a `Builder` instead
176
+ /// Configuration options for generated bindings.
177
+ ///
178
+ /// Deprecated: use a `Builder` instead.
128
179
#[ derive( Debug ) ]
180
+ #[ deprecated]
129
181
pub struct BindgenOptions {
182
+ /// The set of types that have been blacklisted and should not appear
183
+ /// anywhere in the generated code.
130
184
pub hidden_types : HashSet < String > ,
185
+
186
+ /// The set of types that should be treated as opaque structures in the
187
+ /// generated code.
131
188
pub opaque_types : HashSet < String > ,
189
+
190
+ /// The set of types that we should have bindings for in the generated
191
+ /// code.
192
+ ///
193
+ /// This includes all types transitively reachable from any type in this
194
+ /// set. One might think of whitelisted types/vars/functions as GC roots,
195
+ /// and the generated Rust code as including everything that gets marked.
132
196
pub whitelisted_types : RegexSet ,
197
+
198
+ /// Whitelisted functions. See docs for `whitelisted_types` for more.
133
199
pub whitelisted_functions : RegexSet ,
200
+
201
+ /// Whitelisted variables. See docs for `whitelisted_types` for more.
134
202
pub whitelisted_vars : RegexSet ,
203
+
204
+ /// Whether we should generate builtins or not.
135
205
pub builtins : bool ,
206
+
207
+ /// The set of libraries we should link in the generated Rust code.
136
208
pub links : Vec < ( String , LinkType ) > ,
209
+
210
+ /// True if we should dump the Clang AST for debugging purposes.
137
211
pub emit_ast : bool ,
212
+
213
+ /// True if we should ignore functions and only generate bindings for
214
+ /// structures, types, and methods.
138
215
pub ignore_functions : bool ,
216
+
217
+ /// True if we should avoid generating bindings for methods, and instead
218
+ /// just generate code for structures and types.
139
219
pub ignore_methods : bool ,
220
+
221
+ /// True if we should emulate C++ namespaces with Rust modules in the
222
+ /// generated bindings.
140
223
pub enable_cxx_namespaces : bool ,
224
+
225
+ /// True if we shold derive Debug trait implementations for C/C++ structures
226
+ /// and types.
141
227
pub derive_debug : bool ,
142
- /// Generate or not only stable rust.
228
+
229
+ /// True if we can use unstable Rust code in the bindings, false if we
230
+ /// cannot.
143
231
pub unstable_rust : bool ,
144
- /// Wether to generate names that are **directly** under namespaces.
232
+
233
+ /// True if we should generate constant names that are **directly** under
234
+ /// namespaces.
145
235
pub namespaced_constants : bool ,
146
- /// Whether to use msvc mangling rules
236
+
237
+ /// True if we should use MSVC name mangling rules.
147
238
pub msvc_mangling : bool ,
239
+
240
+ /// The set of raw lines to prepend to the generated Rust code.
148
241
pub raw_lines : Vec < String > ,
242
+
243
+ /// The set of arguments to pass straight through to Clang.
149
244
pub clang_args : Vec < String > ,
150
245
}
151
246
@@ -173,21 +268,31 @@ impl Default for BindgenOptions {
173
268
}
174
269
}
175
270
271
+ /// The linking type to use with a given library.
272
+ ///
273
+ /// TODO: #104: This is ignored at the moment, but shouldn't be.
176
274
#[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
177
275
pub enum LinkType {
276
+ /// Use shared library linking. This is the default.
178
277
Default ,
278
+ /// Use static linking.
179
279
Static ,
280
+ /// The library is an OSX framework.
180
281
Framework
181
282
}
182
283
284
+ /// Generated Rust bindings.
183
285
#[ derive( Debug , Clone ) ]
184
286
pub struct Bindings {
185
287
module : ast:: Mod ,
186
288
raw_lines : Vec < String > ,
187
289
}
188
290
189
291
impl Bindings {
292
+ /// Generate bindings for the given options.
293
+ ///
190
294
/// Deprecated - use a `Builder` instead
295
+ #[ deprecated]
191
296
pub fn generate ( options : BindgenOptions , span : Option < Span > ) -> Result < Bindings , ( ) > {
192
297
let span = span. unwrap_or ( DUMMY_SP ) ;
193
298
@@ -205,10 +310,12 @@ impl Bindings {
205
310
} )
206
311
}
207
312
313
+ /// Convert these bindings into a Rust AST.
208
314
pub fn into_ast ( self ) -> Vec < P < ast:: Item > > {
209
315
self . module . items
210
316
}
211
317
318
+ /// Convert these bindings into source text (with raw lines prepended).
212
319
pub fn to_string ( & self ) -> String {
213
320
let mut mod_str = vec ! [ ] ;
214
321
{
@@ -218,11 +325,13 @@ impl Bindings {
218
325
String :: from_utf8 ( mod_str) . unwrap ( )
219
326
}
220
327
328
+ /// Write these bindings as source text to a file.
221
329
pub fn write_to_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
222
330
let file = try!( OpenOptions :: new ( ) . write ( true ) . truncate ( true ) . create ( true ) . open ( path) ) ;
223
331
self . write ( Box :: new ( file) )
224
332
}
225
333
334
+ /// Write these bindings as source text to the given `Write`able.
226
335
// https://github.com/Manishearth/rust-clippy/issues/740
227
336
#[ cfg_attr( feature = "clippy" , allow( needless_lifetimes) ) ]
228
337
pub fn write < ' a > ( & self , mut writer : Box < Write + ' a > ) -> io:: Result < ( ) > {
@@ -255,6 +364,7 @@ fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool {
255
364
}
256
365
}
257
366
367
+ /// Parse one `Item` from the Clang cursor.
258
368
pub fn parse_one ( ctx : & mut BindgenContext ,
259
369
cursor : clang:: Cursor ,
260
370
parent : Option < ItemId > ,
@@ -274,6 +384,7 @@ pub fn parse_one(ctx: &mut BindgenContext,
274
384
CXChildVisit_Continue
275
385
}
276
386
387
+ /// Parse the Clang AST into our `Item` internal representation.
277
388
fn parse ( context : & mut BindgenContext ) {
278
389
use clang:: Diagnostic ;
279
390
use clangll:: * ;
0 commit comments