1 | //! Declarations and setter methods for `bindgen` options. |
2 | //! |
3 | //! The main entry point of this module is the `options` macro. |
4 | #[macro_use ] |
5 | mod helpers; |
6 | mod as_args; |
7 | |
8 | use crate::callbacks::ParseCallbacks; |
9 | use crate::codegen::{ |
10 | AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle, |
11 | }; |
12 | use crate::deps::DepfileSpec; |
13 | use crate::features::{RustFeatures, RustTarget}; |
14 | use crate::regex_set::RegexSet; |
15 | use crate::Abi; |
16 | use crate::Builder; |
17 | use crate::CodegenConfig; |
18 | use crate::FieldVisibilityKind; |
19 | use crate::Formatter; |
20 | use crate::HashMap; |
21 | use crate::DEFAULT_ANON_FIELDS_PREFIX; |
22 | |
23 | use std::env; |
24 | use std::path::{Path, PathBuf}; |
25 | use std::rc::Rc; |
26 | |
27 | use as_args::AsArgs; |
28 | use helpers::ignore; |
29 | |
30 | /// Macro used to generate the [`BindgenOptions`] type and the [`Builder`] setter methods for each |
31 | /// one of the fields of `BindgenOptions`. |
32 | /// |
33 | /// The input format of this macro resembles a `struct` pattern. Each field of the `BindgenOptions` |
34 | /// type is declared by adding the name of the field and its type using the `name: type` syntax and |
35 | /// a block of code with the following items: |
36 | /// |
37 | /// - `default`: The default value for the field. If this item is omitted, `Default::default()` is |
38 | /// used instead, meaning that the type of the field must implement `Default`. |
39 | /// - `methods`: A block of code containing methods for the `Builder` type. These methods should be |
40 | /// related to the field being declared. |
41 | /// - `as_args`: This item declares how the field should be converted into a valid CLI argument for |
42 | /// `bindgen` and is used in the [`Builder::command_line_flags`] method which is used to do a |
43 | /// roundtrip test of the CLI args in the `bindgen-test` crate. This item can take one of the |
44 | /// following: |
45 | /// - A string literal with the flag if the type of the field implements the [`AsArgs`] trait. |
46 | /// - A closure with the signature `|field, args: &mut Vec<String>| -> ()` that pushes arguments |
47 | /// into the `args` buffer based on the value of the field. This is used if the field does not |
48 | /// implement `AsArgs` or if the implementation of the trait is not logically correct for the |
49 | /// option and a custom behavior must be taken into account. |
50 | /// - The `ignore` literal, which does not emit any CLI arguments for this field. This is useful |
51 | /// if the field cannot be used from the `bindgen` CLI. |
52 | /// |
53 | /// As an example, this would be the declaration of a `bool` field called `be_fun` whose default |
54 | /// value is `false` (the `Default` value for `bool`): |
55 | /// ```rust,ignore |
56 | /// be_fun: bool { |
57 | /// methods: { |
58 | /// /// Ask `bindgen` to be fun. This option is disabled by default. |
59 | /// fn be_fun(mut self) -> Self { |
60 | /// self.options.be_fun = true; |
61 | /// self |
62 | /// } |
63 | /// }, |
64 | /// as_args: "--be-fun" , |
65 | /// } |
66 | /// ``` |
67 | /// |
68 | /// However, we could also set the `be_fun` field to `true` by default and use a `--not-fun` flag |
69 | /// instead. This means that we have to add the `default` item and use a closure in the `as_args` |
70 | /// item: |
71 | /// ```rust,ignore |
72 | /// be_fun: bool { |
73 | /// default: true, |
74 | /// methods: { |
75 | /// /// Ask `bindgen` to not be fun. `bindgen` is fun by default. |
76 | /// fn not_fun(mut self) -> Self { |
77 | /// self.options.be_fun = false; |
78 | /// self |
79 | /// } |
80 | /// }, |
81 | /// as_args: |be_fun, args| (!be_fun).as_args(args, "--not-fun" ), |
82 | /// } |
83 | /// ``` |
84 | /// More complex examples can be found in the sole invocation of this macro. |
85 | macro_rules! options { |
86 | ($( |
87 | $(#[doc = $docs:literal])+ |
88 | $field:ident: $ty:ty { |
89 | $(default: $default:expr,)? |
90 | methods: {$($methods_tokens:tt)*}$(,)? |
91 | as_args: $as_args:expr$(,)? |
92 | }$(,)? |
93 | )*) => { |
94 | #[derive(Debug, Clone)] |
95 | pub(crate) struct BindgenOptions { |
96 | $($(#[doc = $docs])* pub(crate) $field: $ty,)* |
97 | } |
98 | |
99 | impl Default for BindgenOptions { |
100 | fn default() -> Self { |
101 | Self { |
102 | $($field: default!($($default)*),)* |
103 | } |
104 | } |
105 | } |
106 | |
107 | impl Builder { |
108 | /// Generates the command line flags used to create this [`Builder`]. |
109 | pub fn command_line_flags(&self) -> Vec<String> { |
110 | let mut args = vec![]; |
111 | |
112 | let headers = match self.options.input_headers.split_last() { |
113 | Some((header, headers)) => { |
114 | // The last input header is passed as an argument in the first position. |
115 | args.push(header.clone().into()); |
116 | headers |
117 | }, |
118 | None => &[] |
119 | }; |
120 | |
121 | $({ |
122 | let func: fn(&$ty, &mut Vec<String>) = as_args!($as_args); |
123 | func(&self.options.$field, &mut args); |
124 | })* |
125 | |
126 | // Add the `--experimental` flag if `bindgen` is built with the `experimental` |
127 | // feature. |
128 | if cfg!(feature = "experimental" ) { |
129 | args.push("--experimental" .to_owned()); |
130 | } |
131 | |
132 | // Add all the clang arguments. |
133 | args.push("--" .to_owned()); |
134 | |
135 | if !self.options.clang_args.is_empty() { |
136 | args.extend(self.options.clang_args.iter().map(|s| s.clone().into())); |
137 | } |
138 | |
139 | // We need to pass all but the last header via the `-include` clang argument. |
140 | for header in headers { |
141 | args.push("-include" .to_owned()); |
142 | args.push(header.clone().into()); |
143 | } |
144 | |
145 | args |
146 | } |
147 | |
148 | $($($methods_tokens)*)* |
149 | } |
150 | }; |
151 | } |
152 | |
153 | options! { |
154 | /// Types that have been blocklisted and should not appear anywhere in the generated code. |
155 | blocklisted_types: RegexSet { |
156 | methods: { |
157 | regex_option! { |
158 | /// Do not generate any bindings for the given type. |
159 | /// |
160 | /// This option is not recursive, meaning that it will only block types whose names |
161 | /// explicitly match the argument of this method. |
162 | pub fn blocklist_type<T: AsRef<str>>(mut self, arg: T) -> Builder { |
163 | self.options.blocklisted_types.insert(arg); |
164 | self |
165 | } |
166 | } |
167 | }, |
168 | as_args: "--blocklist-type" , |
169 | }, |
170 | /// Functions that have been blocklisted and should not appear in the generated code. |
171 | blocklisted_functions: RegexSet { |
172 | methods: { |
173 | regex_option! { |
174 | /// Do not generate any bindings for the given function. |
175 | /// |
176 | /// This option is not recursive, meaning that it will only block functions whose |
177 | /// names explicitly match the argument of this method. |
178 | pub fn blocklist_function<T: AsRef<str>>(mut self, arg: T) -> Builder { |
179 | self.options.blocklisted_functions.insert(arg); |
180 | self |
181 | } |
182 | } |
183 | }, |
184 | as_args: "--blocklist-function" , |
185 | }, |
186 | /// Items that have been blocklisted and should not appear in the generated code. |
187 | blocklisted_items: RegexSet { |
188 | methods: { |
189 | regex_option! { |
190 | /// Do not generate any bindings for the given item, regardless of whether it is a |
191 | /// type, function, module, etc. |
192 | /// |
193 | /// This option is not recursive, meaning that it will only block items whose names |
194 | /// explicitly match the argument of this method. |
195 | pub fn blocklist_item<T: AsRef<str>>(mut self, arg: T) -> Builder { |
196 | self.options.blocklisted_items.insert(arg); |
197 | self |
198 | } |
199 | } |
200 | }, |
201 | as_args: "--blocklist-item" , |
202 | }, |
203 | /// Files whose contents should be blocklisted and should not appear in the generated code. |
204 | blocklisted_files: RegexSet { |
205 | methods: { |
206 | regex_option! { |
207 | /// Do not generate any bindings for the contents of the given file, regardless of |
208 | /// whether the contents of the file are types, functions, modules, etc. |
209 | /// |
210 | /// This option is not recursive, meaning that it will only block files whose names |
211 | /// explicitly match the argument of this method. |
212 | /// |
213 | /// This method will use the argument to match the complete path of the file |
214 | /// instead of a section of it. |
215 | pub fn blocklist_file<T: AsRef<str>>(mut self, arg: T) -> Builder { |
216 | self.options.blocklisted_files.insert(arg); |
217 | self |
218 | } |
219 | } |
220 | }, |
221 | as_args: "--blocklist-file" , |
222 | }, |
223 | /// Variables that have been blocklisted and should not appear in the generated code. |
224 | blocklisted_vars: RegexSet { |
225 | methods: { |
226 | regex_option! { |
227 | /// Do not generate any bindings for the given variable. |
228 | /// |
229 | /// This option is not recursive, meaning that it will only block variables whose |
230 | /// names explicitly match the argument of this method. |
231 | pub fn blocklist_var<T: AsRef<str>>(mut self, arg: T) -> Builder { |
232 | self.options.blocklisted_vars.insert(arg); |
233 | self |
234 | } |
235 | } |
236 | }, |
237 | as_args: "--blocklist-var" , |
238 | }, |
239 | /// Types that should be treated as opaque structures in the generated code. |
240 | opaque_types: RegexSet { |
241 | methods: { |
242 | regex_option! { |
243 | /// Treat the given type as opaque in the generated bindings. |
244 | /// |
245 | /// Opaque in this context means that none of the generated bindings will contain |
246 | /// information about the inner representation of the type and the type itself will |
247 | /// be represented as a chunk of bytes with the alignment and size of the type. |
248 | pub fn opaque_type<T: AsRef<str>>(mut self, arg: T) -> Builder { |
249 | self.options.opaque_types.insert(arg); |
250 | self |
251 | } |
252 | } |
253 | }, |
254 | as_args: "--opaque-type" , |
255 | }, |
256 | /// The explicit `rustfmt` path. |
257 | rustfmt_path: Option<PathBuf> { |
258 | methods: { |
259 | /// Set an explicit path to the `rustfmt` binary. |
260 | /// |
261 | /// This option only comes into effect if `rustfmt` is set to be the formatter used by |
262 | /// `bindgen`. Check the documentation of the [`Builder::formatter`] method for more |
263 | /// information. |
264 | pub fn with_rustfmt<P: Into<PathBuf>>(mut self, path: P) -> Self { |
265 | self.options.rustfmt_path = Some(path.into()); |
266 | self |
267 | } |
268 | }, |
269 | // This option cannot be set from the CLI. |
270 | as_args: ignore, |
271 | }, |
272 | /// The path to which we should write a Makefile-syntax depfile (if any). |
273 | depfile: Option<DepfileSpec> { |
274 | methods: { |
275 | /// Add a depfile output which will be written alongside the generated bindings. |
276 | pub fn depfile<H: Into<String>, D: Into<PathBuf>>( |
277 | mut self, |
278 | output_module: H, |
279 | depfile: D, |
280 | ) -> Builder { |
281 | self.options.depfile = Some(DepfileSpec { |
282 | output_module: output_module.into(), |
283 | depfile_path: depfile.into(), |
284 | }); |
285 | self |
286 | } |
287 | }, |
288 | as_args: |depfile, args| { |
289 | if let Some(depfile) = depfile { |
290 | args.push("--depfile" .into()); |
291 | args.push(depfile.depfile_path.display().to_string()); |
292 | } |
293 | }, |
294 | }, |
295 | /// Types that have been allowlisted and should appear in the generated code. |
296 | allowlisted_types: RegexSet { |
297 | methods: { |
298 | regex_option! { |
299 | /// Generate bindings for the given type. |
300 | /// |
301 | /// This option is transitive by default. Check the documentation of the |
302 | /// [`Builder::allowlist_recursively`] method for further information. |
303 | pub fn allowlist_type<T: AsRef<str>>(mut self, arg: T) -> Builder { |
304 | self.options.allowlisted_types.insert(arg); |
305 | self |
306 | } |
307 | } |
308 | }, |
309 | as_args: "--allowlist-type" , |
310 | }, |
311 | /// Functions that have been allowlisted and should appear in the generated code. |
312 | allowlisted_functions: RegexSet { |
313 | methods: { |
314 | regex_option! { |
315 | /// Generate bindings for the given function. |
316 | /// |
317 | /// This option is transitive by default. Check the documentation of the |
318 | /// [`Builder::allowlist_recursively`] method for further information. |
319 | pub fn allowlist_function<T: AsRef<str>>(mut self, arg: T) -> Builder { |
320 | self.options.allowlisted_functions.insert(arg); |
321 | self |
322 | } |
323 | } |
324 | }, |
325 | as_args: "--allowlist-function" , |
326 | }, |
327 | /// Variables that have been allowlisted and should appear in the generated code. |
328 | allowlisted_vars: RegexSet { |
329 | methods: { |
330 | regex_option! { |
331 | /// Generate bindings for the given variable. |
332 | /// |
333 | /// This option is transitive by default. Check the documentation of the |
334 | /// [`Builder::allowlist_recursively`] method for further information. |
335 | pub fn allowlist_var<T: AsRef<str>>(mut self, arg: T) -> Builder { |
336 | self.options.allowlisted_vars.insert(arg); |
337 | self |
338 | } |
339 | } |
340 | }, |
341 | as_args: "--allowlist-var" , |
342 | }, |
343 | /// Files whose contents have been allowlisted and should appear in the generated code. |
344 | allowlisted_files: RegexSet { |
345 | methods: { |
346 | regex_option! { |
347 | /// Generate bindings for the content of the given file. |
348 | /// |
349 | /// This option is transitive by default. Check the documentation of the |
350 | /// [`Builder::allowlist_recursively`] method for further information. |
351 | /// |
352 | /// This method will use the argument to match the complete path of the file |
353 | /// instead of a section of it. |
354 | pub fn allowlist_file<T: AsRef<str>>(mut self, arg: T) -> Builder { |
355 | self.options.allowlisted_files.insert(arg); |
356 | self |
357 | } |
358 | } |
359 | }, |
360 | as_args: "--allowlist-file" , |
361 | }, |
362 | /// Items that have been allowlisted and should appear in the generated code. |
363 | allowlisted_items: RegexSet { |
364 | methods: { |
365 | regex_option! { |
366 | /// Generate bindings for the given item, regardless of whether it is a type, |
367 | /// function, module, etc. |
368 | /// |
369 | /// This option is transitive by default. Check the documentation of the |
370 | /// [`Builder::allowlist_recursively`] method for further information. |
371 | pub fn allowlist_item<T: AsRef<str>>(mut self, arg: T) -> Builder { |
372 | self.options.allowlisted_items.insert(arg); |
373 | self |
374 | } |
375 | } |
376 | }, |
377 | as_args: "--allowlist-item" , |
378 | }, |
379 | /// The default style of for generated `enum`s. |
380 | default_enum_style: EnumVariation { |
381 | methods: { |
382 | /// Set the default style for generated `enum`s. |
383 | /// |
384 | /// If this method is not called, the [`EnumVariation::Consts`] style will be used by |
385 | /// default. |
386 | /// |
387 | /// To set the style for individual `enum`s, use [`Builder::bitfield_enum`], |
388 | /// [`Builder::newtype_enum`], [`Builder::newtype_global_enum`], |
389 | /// [`Builder::rustified_enum`], [`Builder::rustified_non_exhaustive_enum`], |
390 | /// [`Builder::constified_enum_module`] or [`Builder::constified_enum`]. |
391 | pub fn default_enum_style( |
392 | mut self, |
393 | arg: EnumVariation, |
394 | ) -> Builder { |
395 | self.options.default_enum_style = arg; |
396 | self |
397 | } |
398 | }, |
399 | as_args: |variation, args| { |
400 | if *variation != Default::default() { |
401 | args.push("--default-enum-style" .to_owned()); |
402 | args.push(variation.to_string()); |
403 | } |
404 | }, |
405 | }, |
406 | /// `enum`s marked as bitfield-like. This is, newtypes with bitwise operations. |
407 | bitfield_enums: RegexSet { |
408 | methods: { |
409 | regex_option! { |
410 | /// Mark the given `enum` as being bitfield-like. |
411 | /// |
412 | /// This is similar to the [`Builder::newtype_enum`] style, but with the bitwise |
413 | /// operators implemented. |
414 | pub fn bitfield_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
415 | self.options.bitfield_enums.insert(arg); |
416 | self |
417 | } |
418 | } |
419 | }, |
420 | as_args: "--bitfield-enum" , |
421 | }, |
422 | /// `enum`s marked as newtypes. |
423 | newtype_enums: RegexSet { |
424 | methods: { |
425 | regex_option! { |
426 | /// Mark the given `enum` as a newtype. |
427 | /// |
428 | /// This means that an integer newtype will be declared to represent the `enum` |
429 | /// type and its variants will be represented as constants inside of this type's |
430 | /// `impl` block. |
431 | pub fn newtype_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
432 | self.options.newtype_enums.insert(arg); |
433 | self |
434 | } |
435 | } |
436 | }, |
437 | as_args: "--newtype-enum" , |
438 | }, |
439 | /// `enum`s marked as global newtypes . |
440 | newtype_global_enums: RegexSet { |
441 | methods: { |
442 | regex_option! { |
443 | /// Mark the given `enum` as a global newtype. |
444 | /// |
445 | /// This is similar to the [`Builder::newtype_enum`] style, but the constants for |
446 | /// each variant are free constants instead of being declared inside an `impl` |
447 | /// block for the newtype. |
448 | pub fn newtype_global_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
449 | self.options.newtype_global_enums.insert(arg); |
450 | self |
451 | } |
452 | } |
453 | }, |
454 | as_args: "--newtype-global-enum" , |
455 | }, |
456 | /// `enum`s marked as Rust `enum`s. |
457 | rustified_enums: RegexSet { |
458 | methods: { |
459 | regex_option! { |
460 | /// Mark the given `enum` as a Rust `enum`. |
461 | /// |
462 | /// This means that each variant of the `enum` will be represented as a Rust `enum` |
463 | /// variant. |
464 | /// |
465 | /// **Use this with caution**, creating an instance of a Rust `enum` with an |
466 | /// invalid value will cause undefined behaviour. To avoid this, use the |
467 | /// [`Builder::newtype_enum`] style instead. |
468 | pub fn rustified_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
469 | self.options.rustified_enums.insert(arg); |
470 | self |
471 | } |
472 | } |
473 | }, |
474 | as_args: "--rustified-enum" , |
475 | }, |
476 | /// `enum`s marked as non-exhaustive Rust `enum`s. |
477 | rustified_non_exhaustive_enums: RegexSet { |
478 | methods: { |
479 | regex_option! { |
480 | /// Mark the given `enum` as a non-exhaustive Rust `enum`. |
481 | /// |
482 | /// This is similar to the [`Builder::rustified_enum`] style, but the `enum` is |
483 | /// tagged with the `#[non_exhaustive]` attribute. |
484 | pub fn rustified_non_exhaustive_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
485 | self.options.rustified_non_exhaustive_enums.insert(arg); |
486 | self |
487 | } |
488 | } |
489 | }, |
490 | as_args: "--rustified-non-exhaustive-enums" , |
491 | }, |
492 | /// `enum`s marked as modules of constants. |
493 | constified_enum_modules: RegexSet { |
494 | methods: { |
495 | regex_option! { |
496 | /// Mark the given `enum` as a module with a set of integer constants. |
497 | pub fn constified_enum_module<T: AsRef<str>>(mut self, arg: T) -> Builder { |
498 | self.options.constified_enum_modules.insert(arg); |
499 | self |
500 | } |
501 | } |
502 | }, |
503 | as_args: "--constified-enum-module" , |
504 | }, |
505 | /// `enum`s marked as a set of constants. |
506 | constified_enums: RegexSet { |
507 | methods: { |
508 | regex_option! { |
509 | /// Mark the given `enum` as a set of integer constants. |
510 | /// |
511 | /// This is similar to the [`Builder::constified_enum_module`] style, but the |
512 | /// constants are generated in the current module instead of in a new module. |
513 | pub fn constified_enum<T: AsRef<str>>(mut self, arg: T) -> Builder { |
514 | self.options.constified_enums.insert(arg); |
515 | self |
516 | } |
517 | } |
518 | }, |
519 | as_args: "--constified-enum" , |
520 | }, |
521 | /// The default type signedness for C macro constants. |
522 | default_macro_constant_type: MacroTypeVariation { |
523 | methods: { |
524 | /// Set the default type signedness to be used for macro constants. |
525 | /// |
526 | /// If this method is not called, [`MacroTypeVariation::Unsigned`] is used by default. |
527 | /// |
528 | /// To set the type for individual macro constants, use the |
529 | /// [`ParseCallbacks::int_macro`] method. |
530 | pub fn default_macro_constant_type(mut self, arg: MacroTypeVariation) -> Builder { |
531 | self.options.default_macro_constant_type = arg; |
532 | self |
533 | } |
534 | |
535 | }, |
536 | as_args: |variation, args| { |
537 | if *variation != Default::default() { |
538 | args.push("--default-macro-constant-type" .to_owned()); |
539 | args.push(variation.to_string()); |
540 | } |
541 | }, |
542 | }, |
543 | /// The default style of code generation for `typedef`s. |
544 | default_alias_style: AliasVariation { |
545 | methods: { |
546 | /// Set the default style of code generation for `typedef`s. |
547 | /// |
548 | /// If this method is not called, the [`AliasVariation::TypeAlias`] style is used by |
549 | /// default. |
550 | /// |
551 | /// To set the style for individual `typedefs`s, use [`Builder::type_alias`], |
552 | /// [`Builder::new_type_alias`] or [`Builder::new_type_alias_deref`]. |
553 | pub fn default_alias_style( |
554 | mut self, |
555 | arg: AliasVariation, |
556 | ) -> Builder { |
557 | self.options.default_alias_style = arg; |
558 | self |
559 | } |
560 | }, |
561 | as_args: |variation, args| { |
562 | if *variation != Default::default() { |
563 | args.push("--default-alias-style" .to_owned()); |
564 | args.push(variation.to_string()); |
565 | } |
566 | }, |
567 | }, |
568 | /// `typedef` patterns that will use regular type aliasing. |
569 | type_alias: RegexSet { |
570 | methods: { |
571 | regex_option! { |
572 | /// Mark the given `typedef` as a regular Rust `type` alias. |
573 | /// |
574 | /// This is the default behavior, meaning that this method only comes into effect |
575 | /// if a style different from [`AliasVariation::TypeAlias`] was passed to the |
576 | /// [`Builder::default_alias_style`] method. |
577 | pub fn type_alias<T: AsRef<str>>(mut self, arg: T) -> Builder { |
578 | self.options.type_alias.insert(arg); |
579 | self |
580 | } |
581 | } |
582 | }, |
583 | as_args: "--type-alias" , |
584 | }, |
585 | /// `typedef` patterns that will be aliased by creating a newtype. |
586 | new_type_alias: RegexSet { |
587 | methods: { |
588 | regex_option! { |
589 | /// Mark the given `typedef` as a Rust newtype by having the aliased |
590 | /// type be wrapped in a `struct` with `#[repr(transparent)]`. |
591 | /// |
592 | /// This method can be used to enforce stricter type checking. |
593 | pub fn new_type_alias<T: AsRef<str>>(mut self, arg: T) -> Builder { |
594 | self.options.new_type_alias.insert(arg); |
595 | self |
596 | } |
597 | } |
598 | }, |
599 | as_args: "--new-type-alias" , |
600 | }, |
601 | /// `typedef` patterns that will be wrapped in a newtype implementing `Deref` and `DerefMut`. |
602 | new_type_alias_deref: RegexSet { |
603 | methods: { |
604 | regex_option! { |
605 | /// Mark the given `typedef` to be generated as a newtype that can be dereferenced. |
606 | /// |
607 | /// This is similar to the [`Builder::new_type_alias`] style, but the newtype |
608 | /// implements `Deref` and `DerefMut` with the aliased type as a target. |
609 | pub fn new_type_alias_deref<T: AsRef<str>>(mut self, arg: T) -> Builder { |
610 | self.options.new_type_alias_deref.insert(arg); |
611 | self |
612 | } |
613 | } |
614 | }, |
615 | as_args: "--new-type-alias-deref" , |
616 | }, |
617 | /// The default style of code to generate for `union`s containing non-`Copy` members. |
618 | default_non_copy_union_style: NonCopyUnionStyle { |
619 | methods: { |
620 | /// Set the default style of code to generate for `union`s with non-`Copy` members. |
621 | /// |
622 | /// If this method is not called, the [`NonCopyUnionStyle::BindgenWrapper`] style is |
623 | /// used by default. |
624 | /// |
625 | /// To set the style for individual `union`s, use [`Builder::bindgen_wrapper_union`] or |
626 | /// [`Builder::manually_drop_union`]. |
627 | pub fn default_non_copy_union_style(mut self, arg: NonCopyUnionStyle) -> Self { |
628 | self.options.default_non_copy_union_style = arg; |
629 | self |
630 | } |
631 | }, |
632 | as_args: |style, args| { |
633 | if *style != Default::default() { |
634 | args.push("--default-non-copy-union-style" .to_owned()); |
635 | args.push(style.to_string()); |
636 | } |
637 | }, |
638 | }, |
639 | /// The patterns marking non-`Copy` `union`s as using the `bindgen` generated wrapper. |
640 | bindgen_wrapper_union: RegexSet { |
641 | methods: { |
642 | regex_option! { |
643 | /// Mark the given `union` to use a `bindgen`-generated wrapper for its members if at |
644 | /// least one them is not `Copy`. |
645 | /// |
646 | /// This is the default behavior, meaning that this method only comes into effect |
647 | /// if a style different from [`NonCopyUnionStyle::BindgenWrapper`] was passed to |
648 | /// the [`Builder::default_non_copy_union_style`] method. |
649 | pub fn bindgen_wrapper_union<T: AsRef<str>>(mut self, arg: T) -> Self { |
650 | self.options.bindgen_wrapper_union.insert(arg); |
651 | self |
652 | } |
653 | } |
654 | }, |
655 | as_args: "--bindgen-wrapper-union" , |
656 | }, |
657 | /// The patterns marking non-`Copy` `union`s as using the `ManuallyDrop` wrapper. |
658 | manually_drop_union: RegexSet { |
659 | methods: { |
660 | regex_option! { |
661 | /// Mark the given `union` to use [`::core::mem::ManuallyDrop`] for its members if |
662 | /// at least one of them is not `Copy`. |
663 | /// |
664 | /// The `ManuallyDrop` type was stabilized in Rust 1.20.0, do not use this option |
665 | /// if your target version is lower than this. |
666 | pub fn manually_drop_union<T: AsRef<str>>(mut self, arg: T) -> Self { |
667 | self.options.manually_drop_union.insert(arg); |
668 | self |
669 | } |
670 | } |
671 | |
672 | }, |
673 | as_args: "--manually-drop-union" , |
674 | }, |
675 | |
676 | |
677 | /// Whether we should generate built-in definitions. |
678 | builtins: bool { |
679 | methods: { |
680 | /// Generate Rust bindings for built-in definitions (for example `__builtin_va_list`). |
681 | /// |
682 | /// Bindings for built-in definitions are not emitted by default. |
683 | pub fn emit_builtins(mut self) -> Builder { |
684 | self.options.builtins = true; |
685 | self |
686 | } |
687 | }, |
688 | as_args: "--builtins" , |
689 | }, |
690 | /// Whether we should dump the Clang AST for debugging purposes. |
691 | emit_ast: bool { |
692 | methods: { |
693 | /// Emit the Clang AST to `stdout` for debugging purposes. |
694 | /// |
695 | /// The Clang AST is not emitted by default. |
696 | pub fn emit_clang_ast(mut self) -> Builder { |
697 | self.options.emit_ast = true; |
698 | self |
699 | } |
700 | }, |
701 | as_args: "--emit-clang-ast" , |
702 | }, |
703 | /// Whether we should dump our IR for debugging purposes. |
704 | emit_ir: bool { |
705 | methods: { |
706 | /// Emit the `bindgen` internal representation to `stdout` for debugging purposes. |
707 | /// |
708 | /// This internal representation is not emitted by default. |
709 | pub fn emit_ir(mut self) -> Builder { |
710 | self.options.emit_ir = true; |
711 | self |
712 | } |
713 | }, |
714 | as_args: "--emit-ir" , |
715 | }, |
716 | /// Output path for the `graphviz` DOT file. |
717 | emit_ir_graphviz: Option<String> { |
718 | methods: { |
719 | /// Set the path for the file where the`bindgen` internal representation will be |
720 | /// emitted as a graph using the `graphviz` DOT language. |
721 | /// |
722 | /// This graph representation is not emitted by default. |
723 | pub fn emit_ir_graphviz<T: Into<String>>(mut self, path: T) -> Builder { |
724 | let path = path.into(); |
725 | self.options.emit_ir_graphviz = Some(path); |
726 | self |
727 | } |
728 | }, |
729 | as_args: "--emit-ir-graphviz" , |
730 | }, |
731 | |
732 | /// Whether we should emulate C++ namespaces with Rust modules. |
733 | enable_cxx_namespaces: bool { |
734 | methods: { |
735 | /// Emulate C++ namespaces using Rust modules in the generated bindings. |
736 | /// |
737 | /// C++ namespaces are not emulated by default. |
738 | pub fn enable_cxx_namespaces(mut self) -> Builder { |
739 | self.options.enable_cxx_namespaces = true; |
740 | self |
741 | } |
742 | }, |
743 | as_args: "--enable-cxx-namespaces" , |
744 | }, |
745 | /// Whether we should try to find unexposed attributes in functions. |
746 | enable_function_attribute_detection: bool { |
747 | methods: { |
748 | /// Enable detecting function attributes on C functions. |
749 | /// |
750 | /// This enables the following features: |
751 | /// - Add `#[must_use]` attributes to Rust items whose C counterparts are marked as so. |
752 | /// This feature also requires that the Rust target version supports the attribute. |
753 | /// - Set `!` as the return type for Rust functions whose C counterparts are marked as |
754 | /// diverging. |
755 | /// |
756 | /// This option can be quite slow in some cases (check [#1465]), so it is disabled by |
757 | /// default. |
758 | /// |
759 | /// [#1465]: https://github.com/rust-lang/rust-bindgen/issues/1465 |
760 | pub fn enable_function_attribute_detection(mut self) -> Self { |
761 | self.options.enable_function_attribute_detection = true; |
762 | self |
763 | } |
764 | |
765 | }, |
766 | as_args: "--enable-function-attribute-detection" , |
767 | }, |
768 | /// Whether we should avoid mangling names with namespaces. |
769 | disable_name_namespacing: bool { |
770 | methods: { |
771 | /// Disable name auto-namespacing. |
772 | /// |
773 | /// By default, `bindgen` mangles names like `foo::bar::Baz` to look like `foo_bar_Baz` |
774 | /// instead of just `Baz`. This method disables that behavior. |
775 | /// |
776 | /// Note that this does not change the names used for allowlisting and blocklisting, |
777 | /// which should still be mangled with the namespaces. Additionally, this option may |
778 | /// cause `bindgen` to generate duplicate names. |
779 | pub fn disable_name_namespacing(mut self) -> Builder { |
780 | self.options.disable_name_namespacing = true; |
781 | self |
782 | } |
783 | }, |
784 | as_args: "--disable-name-namespacing" , |
785 | }, |
786 | /// Whether we should avoid generating nested `struct` names. |
787 | disable_nested_struct_naming: bool { |
788 | methods: { |
789 | /// Disable nested `struct` naming. |
790 | /// |
791 | /// The following `struct`s have different names for C and C++. In C, they are visible |
792 | /// as `foo` and `bar`. In C++, they are visible as `foo` and `foo::bar`. |
793 | /// |
794 | /// ```c |
795 | /// struct foo { |
796 | /// struct bar { |
797 | /// } b; |
798 | /// }; |
799 | /// ``` |
800 | /// |
801 | /// `bindgen` tries to avoid duplicate names by default, so it follows the C++ naming |
802 | /// convention and it generates `foo` and `foo_bar` instead of just `foo` and `bar`. |
803 | /// |
804 | /// This method disables this behavior and it is indented to be used only for headers |
805 | /// that were written in C. |
806 | pub fn disable_nested_struct_naming(mut self) -> Builder { |
807 | self.options.disable_nested_struct_naming = true; |
808 | self |
809 | } |
810 | }, |
811 | as_args: "--disable-nested-struct-naming" , |
812 | }, |
813 | /// Whether we should avoid embedding version identifiers into source code. |
814 | disable_header_comment: bool { |
815 | methods: { |
816 | /// Do not insert the `bindgen` version identifier into the generated bindings. |
817 | /// |
818 | /// This identifier is inserted by default. |
819 | pub fn disable_header_comment(mut self) -> Self { |
820 | self.options.disable_header_comment = true; |
821 | self |
822 | } |
823 | |
824 | }, |
825 | as_args: "--disable-header-comment" , |
826 | }, |
827 | /// Whether we should generate layout tests for generated `struct`s. |
828 | layout_tests: bool { |
829 | default: true, |
830 | methods: { |
831 | /// Set whether layout tests should be generated. |
832 | /// |
833 | /// Layout tests are generated by default. |
834 | pub fn layout_tests(mut self, doit: bool) -> Self { |
835 | self.options.layout_tests = doit; |
836 | self |
837 | } |
838 | }, |
839 | as_args: |value, args| (!value).as_args(args, "--no-layout-tests" ), |
840 | }, |
841 | /// Whether we should implement `Debug` for types that cannot derive it. |
842 | impl_debug: bool { |
843 | methods: { |
844 | /// Set whether `Debug` should be implemented for types that cannot derive it. |
845 | /// |
846 | /// This option is disabled by default. |
847 | pub fn impl_debug(mut self, doit: bool) -> Self { |
848 | self.options.impl_debug = doit; |
849 | self |
850 | } |
851 | |
852 | }, |
853 | as_args: "--impl-debug" , |
854 | }, |
855 | /// Whether we should implement `PartialEq` types that cannot derive it. |
856 | impl_partialeq: bool { |
857 | methods: { |
858 | /// Set whether `PartialEq` should be implemented for types that cannot derive it. |
859 | /// |
860 | /// This option is disabled by default. |
861 | pub fn impl_partialeq(mut self, doit: bool) -> Self { |
862 | self.options.impl_partialeq = doit; |
863 | self |
864 | } |
865 | }, |
866 | as_args: "--impl-partialeq" , |
867 | }, |
868 | /// Whether we should derive `Copy` when possible. |
869 | derive_copy: bool { |
870 | default: true, |
871 | methods: { |
872 | /// Set whether the `Copy` trait should be derived when possible. |
873 | /// |
874 | /// `Copy` is derived by default. |
875 | pub fn derive_copy(mut self, doit: bool) -> Self { |
876 | self.options.derive_copy = doit; |
877 | self |
878 | } |
879 | }, |
880 | as_args: |value, args| (!value).as_args(args, "--no-derive-copy" ), |
881 | }, |
882 | |
883 | /// Whether we should derive `Debug` when possible. |
884 | derive_debug: bool { |
885 | default: true, |
886 | methods: { |
887 | /// Set whether the `Debug` trait should be derived when possible. |
888 | /// |
889 | /// The [`Builder::impl_debug`] method can be used to implement `Debug` for types that |
890 | /// cannot derive it. |
891 | /// |
892 | /// `Debug` is derived by default. |
893 | pub fn derive_debug(mut self, doit: bool) -> Self { |
894 | self.options.derive_debug = doit; |
895 | self |
896 | } |
897 | }, |
898 | as_args: |value, args| (!value).as_args(args, "--no-derive-debug" ), |
899 | }, |
900 | |
901 | /// Whether we should derive `Default` when possible. |
902 | derive_default: bool { |
903 | methods: { |
904 | /// Set whether the `Default` trait should be derived when possible. |
905 | /// |
906 | /// `Default` is not derived by default. |
907 | pub fn derive_default(mut self, doit: bool) -> Self { |
908 | self.options.derive_default = doit; |
909 | self |
910 | } |
911 | }, |
912 | as_args: |&value, args| { |
913 | let arg = if value { |
914 | "--with-derive-default" |
915 | } else { |
916 | "--no-derive-default" |
917 | }; |
918 | |
919 | args.push(arg.to_owned()); |
920 | }, |
921 | }, |
922 | /// Whether we should derive `Hash` when possible. |
923 | derive_hash: bool { |
924 | methods: { |
925 | /// Set whether the `Hash` trait should be derived when possible. |
926 | /// |
927 | /// `Hash` is not derived by default. |
928 | pub fn derive_hash(mut self, doit: bool) -> Self { |
929 | self.options.derive_hash = doit; |
930 | self |
931 | } |
932 | }, |
933 | as_args: "--with-derive-hash" , |
934 | }, |
935 | /// Whether we should derive `PartialOrd` when possible. |
936 | derive_partialord: bool { |
937 | methods: { |
938 | /// Set whether the `PartialOrd` trait should be derived when possible. |
939 | /// |
940 | /// Take into account that `Ord` cannot be derived for a type that does not implement |
941 | /// `PartialOrd`. For this reason, setting this method to `false` also sets |
942 | /// automatically [`Builder::derive_ord`] to `false`. |
943 | /// |
944 | /// `PartialOrd` is not derived by default. |
945 | pub fn derive_partialord(mut self, doit: bool) -> Self { |
946 | self.options.derive_partialord = doit; |
947 | if !doit { |
948 | self.options.derive_ord = false; |
949 | } |
950 | self |
951 | } |
952 | }, |
953 | as_args: "--with-derive-partialord" , |
954 | }, |
955 | /// Whether we should derive `Ord` when possible. |
956 | derive_ord: bool { |
957 | methods: { |
958 | /// Set whether the `Ord` trait should be derived when possible. |
959 | /// |
960 | /// Take into account that `Ord` cannot be derived for a type that does not implement |
961 | /// `PartialOrd`. For this reason, the value set with this method will also be set |
962 | /// automatically for [`Builder::derive_partialord`]. |
963 | /// |
964 | /// `Ord` is not derived by default. |
965 | pub fn derive_ord(mut self, doit: bool) -> Self { |
966 | self.options.derive_ord = doit; |
967 | self.options.derive_partialord = doit; |
968 | self |
969 | } |
970 | }, |
971 | as_args: "--with-derive-ord" , |
972 | }, |
973 | /// Whether we should derive `PartialEq` when possible. |
974 | derive_partialeq: bool { |
975 | methods: { |
976 | /// Set whether the `PartialEq` trait should be derived when possible. |
977 | /// |
978 | /// Take into account that `Eq` cannot be derived for a type that does not implement |
979 | /// `PartialEq`. For this reason, setting this method to `false` also sets |
980 | /// automatically [`Builder::derive_eq`] to `false`. |
981 | /// |
982 | /// The [`Builder::impl_partialeq`] method can be used to implement `PartialEq` for |
983 | /// types that cannot derive it. |
984 | /// |
985 | /// `PartialEq` is not derived by default. |
986 | pub fn derive_partialeq(mut self, doit: bool) -> Self { |
987 | self.options.derive_partialeq = doit; |
988 | if !doit { |
989 | self.options.derive_eq = false; |
990 | } |
991 | self |
992 | } |
993 | }, |
994 | as_args: "--with-derive-partialeq" , |
995 | }, |
996 | /// Whether we should derive `Eq` when possible. |
997 | derive_eq: bool { |
998 | methods: { |
999 | /// Set whether the `Eq` trait should be derived when possible. |
1000 | /// |
1001 | /// Take into account that `Eq` cannot be derived for a type that does not implement |
1002 | /// `PartialEq`. For this reason, the value set with this method will also be set |
1003 | /// automatically for [`Builder::derive_partialeq`]. |
1004 | /// |
1005 | /// `Eq` is not derived by default. |
1006 | pub fn derive_eq(mut self, doit: bool) -> Self { |
1007 | self.options.derive_eq = doit; |
1008 | if doit { |
1009 | self.options.derive_partialeq = doit; |
1010 | } |
1011 | self |
1012 | } |
1013 | }, |
1014 | as_args: "--with-derive-eq" , |
1015 | }, |
1016 | /// Whether we should use `core` instead of `std`. |
1017 | /// |
1018 | /// If this option is enabled and the Rust target version is greater than 1.64, the prefix for |
1019 | /// C platform-specific types will be `::core::ffi` instead of `::core::os::raw`. |
1020 | use_core: bool { |
1021 | methods: { |
1022 | /// Use `core` instead of `std` in the generated bindings. |
1023 | /// |
1024 | /// `std` is used by default. |
1025 | pub fn use_core(mut self) -> Builder { |
1026 | self.options.use_core = true; |
1027 | self |
1028 | } |
1029 | |
1030 | }, |
1031 | as_args: "--use-core" , |
1032 | }, |
1033 | /// An optional prefix for the C platform-specific types. |
1034 | ctypes_prefix: Option<String> { |
1035 | methods: { |
1036 | /// Use the given prefix for the C platform-specific types instead of `::std::os::raw`. |
1037 | /// |
1038 | /// Alternatively, the [`Builder::use_core`] method can be used to set the prefix to |
1039 | /// `::core::ffi` or `::core::os::raw`. |
1040 | pub fn ctypes_prefix<T: Into<String>>(mut self, prefix: T) -> Builder { |
1041 | self.options.ctypes_prefix = Some(prefix.into()); |
1042 | self |
1043 | } |
1044 | }, |
1045 | as_args: "--ctypes-prefix" , |
1046 | }, |
1047 | /// The prefix for anonymous fields. |
1048 | anon_fields_prefix: String { |
1049 | default: DEFAULT_ANON_FIELDS_PREFIX.into(), |
1050 | methods: { |
1051 | /// Use the given prefix for the anonymous fields. |
1052 | /// |
1053 | /// An anonymous field, is a field of a C/C++ type that does not have a name. For |
1054 | /// example, in the following C code: |
1055 | /// ```c |
1056 | /// struct integer { |
1057 | /// struct { |
1058 | /// int inner; |
1059 | /// }; |
1060 | /// } |
1061 | /// ``` |
1062 | /// |
1063 | /// The only field of the `integer` `struct` is an anonymous field and its Rust |
1064 | /// representation will be named using this prefix followed by an integer identifier. |
1065 | /// |
1066 | /// The default prefix is `__bindgen_anon_`. |
1067 | pub fn anon_fields_prefix<T: Into<String>>(mut self, prefix: T) -> Builder { |
1068 | self.options.anon_fields_prefix = prefix.into(); |
1069 | self |
1070 | } |
1071 | }, |
1072 | as_args: |prefix, args| { |
1073 | if prefix != DEFAULT_ANON_FIELDS_PREFIX { |
1074 | args.push("--anon-fields-prefix" .to_owned()); |
1075 | args.push(prefix.clone()); |
1076 | } |
1077 | }, |
1078 | }, |
1079 | /// Whether to measure the time for each one of the `bindgen` phases. |
1080 | time_phases: bool { |
1081 | methods: { |
1082 | /// Set whether to measure the elapsed time for each one of the `bindgen` phases. This |
1083 | /// information is printed to `stderr`. |
1084 | /// |
1085 | /// The elapsed time is not measured by default. |
1086 | pub fn time_phases(mut self, doit: bool) -> Self { |
1087 | self.options.time_phases = doit; |
1088 | self |
1089 | } |
1090 | }, |
1091 | as_args: "--time-phases" , |
1092 | }, |
1093 | /// Whether to convert C float types to `f32` and `f64`. |
1094 | convert_floats: bool { |
1095 | default: true, |
1096 | methods: { |
1097 | /// Avoid converting C float types to `f32` and `f64`. |
1098 | pub fn no_convert_floats(mut self) -> Self { |
1099 | self.options.convert_floats = false; |
1100 | self |
1101 | } |
1102 | }, |
1103 | as_args: |value, args| (!value).as_args(args, "--no-convert-floats" ), |
1104 | }, |
1105 | /// The set of raw lines to be prepended to the top-level module of the generated Rust code. |
1106 | raw_lines: Vec<Box<str>> { |
1107 | methods: { |
1108 | /// Add a line of Rust code at the beginning of the generated bindings. The string is |
1109 | /// passed through without any modification. |
1110 | pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Self { |
1111 | self.options.raw_lines.push(arg.into().into_boxed_str()); |
1112 | self |
1113 | } |
1114 | }, |
1115 | as_args: |raw_lines, args| { |
1116 | for line in raw_lines { |
1117 | args.push("--raw-line" .to_owned()); |
1118 | args.push(line.clone().into()); |
1119 | } |
1120 | }, |
1121 | }, |
1122 | /// The set of raw lines to prepend to different modules. |
1123 | module_lines: HashMap<Box<str>, Vec<Box<str>>> { |
1124 | methods: { |
1125 | /// Add a given line to the beginning of a given module. |
1126 | /// |
1127 | /// This option only comes into effect if the [`Builder::enable_cxx_namespaces`] method |
1128 | /// is also being called. |
1129 | pub fn module_raw_line<T, U>(mut self, module: T, line: U) -> Self |
1130 | where |
1131 | T: Into<String>, |
1132 | U: Into<String>, |
1133 | { |
1134 | self.options |
1135 | .module_lines |
1136 | .entry(module.into().into_boxed_str()) |
1137 | .or_default() |
1138 | .push(line.into().into_boxed_str()); |
1139 | self |
1140 | } |
1141 | }, |
1142 | as_args: |module_lines, args| { |
1143 | for (module, lines) in module_lines { |
1144 | for line in lines.iter() { |
1145 | args.push("--module-raw-line" .to_owned()); |
1146 | args.push(module.clone().into()); |
1147 | args.push(line.clone().into()); |
1148 | } |
1149 | } |
1150 | }, |
1151 | }, |
1152 | /// The input header files. |
1153 | input_headers: Vec<Box<str>> { |
1154 | methods: { |
1155 | /// Add an input C/C++ header to generate bindings for. |
1156 | /// |
1157 | /// This can be used to generate bindings for a single header: |
1158 | /// |
1159 | /// ```ignore |
1160 | /// let bindings = bindgen::Builder::default() |
1161 | /// .header("input.h") |
1162 | /// .generate() |
1163 | /// .unwrap(); |
1164 | /// ``` |
1165 | /// |
1166 | /// Or for multiple headers: |
1167 | /// |
1168 | /// ```ignore |
1169 | /// let bindings = bindgen::Builder::default() |
1170 | /// .header("first.h") |
1171 | /// .header("second.h") |
1172 | /// .header("third.h") |
1173 | /// .generate() |
1174 | /// .unwrap(); |
1175 | /// ``` |
1176 | pub fn header<T: Into<String>>(mut self, header: T) -> Builder { |
1177 | self.options.input_headers.push(header.into().into_boxed_str()); |
1178 | self |
1179 | } |
1180 | |
1181 | /// Add input C/C++ header(s) to generate bindings for. |
1182 | /// |
1183 | /// This can be used to generate bindings for a single header: |
1184 | /// |
1185 | /// ```ignore |
1186 | /// let bindings = bindgen::Builder::default() |
1187 | /// .headers(["input.h"]) |
1188 | /// .generate() |
1189 | /// .unwrap(); |
1190 | /// ``` |
1191 | /// |
1192 | /// Or for multiple headers: |
1193 | /// |
1194 | /// ```ignore |
1195 | /// let bindings = bindgen::Builder::default() |
1196 | /// .headers(["first.h", "second.h", "third.h"]) |
1197 | /// .generate() |
1198 | /// .unwrap(); |
1199 | /// ``` |
1200 | pub fn headers<I: IntoIterator>(mut self, headers: I) -> Builder |
1201 | where |
1202 | I::Item: Into<String>, |
1203 | { |
1204 | self.options |
1205 | .input_headers |
1206 | .extend(headers.into_iter().map(Into::into).map(Into::into)); |
1207 | self |
1208 | } |
1209 | }, |
1210 | // This field is handled specially inside the macro. |
1211 | as_args: ignore, |
1212 | }, |
1213 | /// The set of arguments to be passed straight through to Clang. |
1214 | clang_args: Vec<Box<str>> { |
1215 | methods: { |
1216 | /// Add an argument to be passed straight through to Clang. |
1217 | pub fn clang_arg<T: Into<String>>(self, arg: T) -> Builder { |
1218 | self.clang_args([arg.into().into_boxed_str()]) |
1219 | } |
1220 | |
1221 | /// Add several arguments to be passed straight through to Clang. |
1222 | pub fn clang_args<I: IntoIterator>(mut self, args: I) -> Builder |
1223 | where |
1224 | I::Item: AsRef<str>, |
1225 | { |
1226 | for arg in args { |
1227 | self.options.clang_args.push(arg.as_ref().to_owned().into_boxed_str()); |
1228 | } |
1229 | self |
1230 | } |
1231 | }, |
1232 | // This field is handled specially inside the macro. |
1233 | as_args: ignore, |
1234 | }, |
1235 | /// Tuples of unsaved file contents of the form (name, contents). |
1236 | input_header_contents: Vec<(Box<str>, Box<str>)> { |
1237 | methods: { |
1238 | /// Add `contents` as an input C/C++ header named `name`. |
1239 | /// |
1240 | /// This can be used to inject additional C/C++ code as an input without having to |
1241 | /// create additional header files. |
1242 | pub fn header_contents(mut self, name: &str, contents: &str) -> Builder { |
1243 | // Apparently clang relies on having virtual FS correspondent to |
1244 | // the real one, so we need absolute paths here |
1245 | let absolute_path = env::current_dir() |
1246 | .expect("Cannot retrieve current directory" ) |
1247 | .join(name) |
1248 | .to_str() |
1249 | .expect("Cannot convert current directory name to string" ) |
1250 | .into(); |
1251 | self.options |
1252 | .input_header_contents |
1253 | .push((absolute_path, contents.into())); |
1254 | self |
1255 | } |
1256 | }, |
1257 | // Header contents cannot be added from the CLI. |
1258 | as_args: ignore, |
1259 | }, |
1260 | /// A user-provided visitor to allow customizing different kinds of situations. |
1261 | parse_callbacks: Vec<Rc<dyn ParseCallbacks>> { |
1262 | methods: { |
1263 | /// Add a new [`ParseCallbacks`] instance to configure types in different situations. |
1264 | /// |
1265 | /// This can also be used with [`CargoCallbacks`](struct@crate::CargoCallbacks) to emit |
1266 | /// `cargo:rerun-if-changed=...` for all `#include`d header files. |
1267 | pub fn parse_callbacks(mut self, cb: Box<dyn ParseCallbacks>) -> Self { |
1268 | self.options.parse_callbacks.push(Rc::from(cb)); |
1269 | self |
1270 | } |
1271 | }, |
1272 | as_args: |_callbacks, _args| { |
1273 | #[cfg (feature = "__cli" )] |
1274 | for cb in _callbacks { |
1275 | _args.extend(cb.cli_args()); |
1276 | } |
1277 | }, |
1278 | }, |
1279 | /// Which kind of items should we generate. We generate all of them by default. |
1280 | codegen_config: CodegenConfig { |
1281 | default: CodegenConfig::all(), |
1282 | methods: { |
1283 | /// Do not generate any functions. |
1284 | /// |
1285 | /// Functions are generated by default. |
1286 | pub fn ignore_functions(mut self) -> Builder { |
1287 | self.options.codegen_config.remove(CodegenConfig::FUNCTIONS); |
1288 | self |
1289 | } |
1290 | |
1291 | /// Do not generate any methods. |
1292 | /// |
1293 | /// Methods are generated by default. |
1294 | pub fn ignore_methods(mut self) -> Builder { |
1295 | self.options.codegen_config.remove(CodegenConfig::METHODS); |
1296 | self |
1297 | } |
1298 | |
1299 | /// Choose what to generate using a [`CodegenConfig`]. |
1300 | /// |
1301 | /// This option overlaps with [`Builder::ignore_functions`] and |
1302 | /// [`Builder::ignore_methods`]. |
1303 | /// |
1304 | /// All the items in `CodegenConfig` are generated by default. |
1305 | pub fn with_codegen_config(mut self, config: CodegenConfig) -> Self { |
1306 | self.options.codegen_config = config; |
1307 | self |
1308 | } |
1309 | }, |
1310 | as_args: |codegen_config, args| { |
1311 | if !codegen_config.functions() { |
1312 | args.push("--ignore-functions" .to_owned()); |
1313 | } |
1314 | |
1315 | args.push("--generate" .to_owned()); |
1316 | |
1317 | //Temporary placeholder for the 4 options below. |
1318 | let mut options: Vec<String> = Vec::new(); |
1319 | if codegen_config.functions() { |
1320 | options.push("functions" .to_owned()); |
1321 | } |
1322 | |
1323 | if codegen_config.types() { |
1324 | options.push("types" .to_owned()); |
1325 | } |
1326 | |
1327 | if codegen_config.vars() { |
1328 | options.push("vars" .to_owned()); |
1329 | } |
1330 | |
1331 | if codegen_config.methods() { |
1332 | options.push("methods" .to_owned()); |
1333 | } |
1334 | |
1335 | if codegen_config.constructors() { |
1336 | options.push("constructors" .to_owned()); |
1337 | } |
1338 | |
1339 | if codegen_config.destructors() { |
1340 | options.push("destructors" .to_owned()); |
1341 | } |
1342 | |
1343 | args.push(options.join("," )); |
1344 | |
1345 | if !codegen_config.methods() { |
1346 | args.push("--ignore-methods" .to_owned()); |
1347 | } |
1348 | }, |
1349 | }, |
1350 | /// Whether to treat inline namespaces conservatively. |
1351 | conservative_inline_namespaces: bool { |
1352 | methods: { |
1353 | /// Treat inline namespaces conservatively. |
1354 | /// |
1355 | /// This is tricky, because in C++ is technically legal to override an item |
1356 | /// defined in an inline namespace: |
1357 | /// |
1358 | /// ```cpp |
1359 | /// inline namespace foo { |
1360 | /// using Bar = int; |
1361 | /// } |
1362 | /// using Bar = long; |
1363 | /// ``` |
1364 | /// |
1365 | /// Even though referencing `Bar` is a compiler error. |
1366 | /// |
1367 | /// We want to support this (arguably esoteric) use case, but we do not want to make |
1368 | /// the rest of `bindgen` users pay an usability penalty for that. |
1369 | /// |
1370 | /// To support this, we need to keep all the inline namespaces around, but then using |
1371 | /// `bindgen` becomes a bit more difficult, because you cannot reference paths like |
1372 | /// `std::string` (you'd need to use the proper inline namespace). |
1373 | /// |
1374 | /// We could complicate a lot of the logic to detect name collisions and, in the |
1375 | /// absence of collisions, generate a `pub use inline_ns::*` or something like that. |
1376 | /// |
1377 | /// That is probably something we can do to improve the usability of this option if we |
1378 | /// realize it is needed way more often. Our guess is that this extra logic is not |
1379 | /// going to be very useful. |
1380 | /// |
1381 | /// This option is disabled by default. |
1382 | pub fn conservative_inline_namespaces(mut self) -> Builder { |
1383 | self.options.conservative_inline_namespaces = true; |
1384 | self |
1385 | } |
1386 | }, |
1387 | as_args: "--conservative-inline-namespaces" , |
1388 | }, |
1389 | /// Whether to keep documentation comments in the generated output. |
1390 | generate_comments: bool { |
1391 | default: true, |
1392 | methods: { |
1393 | /// Set whether the generated bindings should contain documentation comments. |
1394 | /// |
1395 | /// Documentation comments are included by default. |
1396 | /// |
1397 | /// Note that clang excludes comments from system headers by default, pass |
1398 | /// `"-fretain-comments-from-system-headers"` to the [`Builder::clang_arg`] method to |
1399 | /// include them. |
1400 | /// |
1401 | /// It is also possible to process all comments and not just documentation using the |
1402 | /// `"-fparse-all-comments"` flag. Check [these slides on clang comment parsing]( |
1403 | /// https://llvm.org/devmtg/2012-11/Gribenko_CommentParsing.pdf) for more information |
1404 | /// and examples. |
1405 | pub fn generate_comments(mut self, doit: bool) -> Self { |
1406 | self.options.generate_comments = doit; |
1407 | self |
1408 | } |
1409 | }, |
1410 | as_args: |value, args| (!value).as_args(args, "--no-doc-comments" ), |
1411 | }, |
1412 | /// Whether to generate inline functions. |
1413 | generate_inline_functions: bool { |
1414 | methods: { |
1415 | /// Set whether to generate inline functions. |
1416 | /// |
1417 | /// This option is disabled by default. |
1418 | /// |
1419 | /// Note that they will usually not work. However you can use `-fkeep-inline-functions` |
1420 | /// or `-fno-inline-functions` if you are responsible of compiling the library to make |
1421 | /// them callable. |
1422 | #[cfg_attr ( |
1423 | feature = "experimental" , |
1424 | doc = " \nCheck the [`Builder::wrap_static_fns`] method for an alternative." |
1425 | )] |
1426 | pub fn generate_inline_functions(mut self, doit: bool) -> Self { |
1427 | self.options.generate_inline_functions = doit; |
1428 | self |
1429 | } |
1430 | }, |
1431 | as_args: "--generate-inline-functions" , |
1432 | }, |
1433 | /// Whether to allowlist types recursively. |
1434 | allowlist_recursively: bool { |
1435 | default: true, |
1436 | methods: { |
1437 | /// Set whether to recursively allowlist items. |
1438 | /// |
1439 | /// Items are allowlisted recursively by default. |
1440 | /// |
1441 | /// Given that we have explicitly allowlisted the `initiate_dance_party` function in |
1442 | /// this C header: |
1443 | /// |
1444 | /// ```c |
1445 | /// typedef struct MoonBoots { |
1446 | /// int bouncy_level; |
1447 | /// } MoonBoots; |
1448 | /// |
1449 | /// void initiate_dance_party(MoonBoots* boots); |
1450 | /// ``` |
1451 | /// |
1452 | /// We would normally generate bindings to both the `initiate_dance_party` function and |
1453 | /// the `MoonBoots` type that it transitively references. If `false` is passed to this |
1454 | /// method, `bindgen` will not emit bindings for anything except the explicitly |
1455 | /// allowlisted items, meaning that the definition for `MoonBoots` would not be |
1456 | /// generated. However, the `initiate_dance_party` function would still reference |
1457 | /// `MoonBoots`! |
1458 | /// |
1459 | /// **Disabling this feature will almost certainly cause `bindgen` to emit bindings |
1460 | /// that will not compile!** If you disable this feature, then it is *your* |
1461 | /// responsibility to provide definitions for every type that is referenced from an |
1462 | /// explicitly allowlisted item. One way to provide the missing definitions is by using |
1463 | /// the [`Builder::raw_line`] method, another would be to define them in Rust and then |
1464 | /// `include!(...)` the bindings immediately afterwards. |
1465 | pub fn allowlist_recursively(mut self, doit: bool) -> Self { |
1466 | self.options.allowlist_recursively = doit; |
1467 | self |
1468 | } |
1469 | }, |
1470 | as_args: |value, args| (!value).as_args(args, "--no-recursive-allowlist" ), |
1471 | }, |
1472 | /// Whether to emit `#[macro_use] extern crate objc;` instead of `use objc;` in the prologue of |
1473 | /// the files generated from objective-c files. |
1474 | objc_extern_crate: bool { |
1475 | methods: { |
1476 | /// Emit `#[macro_use] extern crate objc;` instead of `use objc;` in the prologue of |
1477 | /// the files generated from objective-c files. |
1478 | /// |
1479 | /// `use objc;` is emitted by default. |
1480 | pub fn objc_extern_crate(mut self, doit: bool) -> Self { |
1481 | self.options.objc_extern_crate = doit; |
1482 | self |
1483 | } |
1484 | }, |
1485 | as_args: "--objc-extern-crate" , |
1486 | }, |
1487 | /// Whether to generate proper block signatures instead of `void` pointers. |
1488 | generate_block: bool { |
1489 | methods: { |
1490 | /// Generate proper block signatures instead of `void` pointers. |
1491 | /// |
1492 | /// `void` pointers are used by default. |
1493 | pub fn generate_block(mut self, doit: bool) -> Self { |
1494 | self.options.generate_block = doit; |
1495 | self |
1496 | } |
1497 | }, |
1498 | as_args: "--generate-block" , |
1499 | }, |
1500 | /// Whether to generate strings as `CStr`. |
1501 | generate_cstr: bool { |
1502 | methods: { |
1503 | /// Set whether string constants should be generated as `&CStr` instead of `&[u8]`. |
1504 | /// |
1505 | /// A minimum Rust target of 1.59 is required for this to have any effect as support |
1506 | /// for `CStr::from_bytes_with_nul_unchecked` in `const` contexts is needed. |
1507 | /// |
1508 | /// This option is disabled by default but will become enabled by default in a future |
1509 | /// release, so enabling this is recommended. |
1510 | pub fn generate_cstr(mut self, doit: bool) -> Self { |
1511 | self.options.generate_cstr = doit; |
1512 | self |
1513 | } |
1514 | }, |
1515 | as_args: "--generate-cstr" , |
1516 | }, |
1517 | /// Whether to emit `#[macro_use] extern crate block;` instead of `use block;` in the prologue |
1518 | /// of the files generated from apple block files. |
1519 | block_extern_crate: bool { |
1520 | methods: { |
1521 | /// Emit `#[macro_use] extern crate block;` instead of `use block;` in the prologue of |
1522 | /// the files generated from apple block files. |
1523 | /// |
1524 | /// `use block;` is emitted by default. |
1525 | pub fn block_extern_crate(mut self, doit: bool) -> Self { |
1526 | self.options.block_extern_crate = doit; |
1527 | self |
1528 | } |
1529 | }, |
1530 | as_args: "--block-extern-crate" , |
1531 | }, |
1532 | /// Whether to use the clang-provided name mangling. |
1533 | enable_mangling: bool { |
1534 | default: true, |
1535 | methods: { |
1536 | /// Set whether to use the clang-provided name mangling. This is probably needed for |
1537 | /// C++ features. |
1538 | /// |
1539 | /// The mangling provided by clang is used by default. |
1540 | /// |
1541 | /// We allow disabling this option because some old `libclang` versions seem to return |
1542 | /// incorrect results in some cases for non-mangled functions, check [#528] for more |
1543 | /// information. |
1544 | /// |
1545 | /// [#528]: https://github.com/rust-lang/rust-bindgen/issues/528 |
1546 | pub fn trust_clang_mangling(mut self, doit: bool) -> Self { |
1547 | self.options.enable_mangling = doit; |
1548 | self |
1549 | } |
1550 | |
1551 | }, |
1552 | as_args: |value, args| (!value).as_args(args, "--distrust-clang-mangling" ), |
1553 | }, |
1554 | /// Whether to detect include paths using `clang_sys`. |
1555 | detect_include_paths: bool { |
1556 | default: true, |
1557 | methods: { |
1558 | /// Set whether to detect include paths using `clang_sys`. |
1559 | /// |
1560 | /// `clang_sys` is used to detect include paths by default. |
1561 | pub fn detect_include_paths(mut self, doit: bool) -> Self { |
1562 | self.options.detect_include_paths = doit; |
1563 | self |
1564 | } |
1565 | }, |
1566 | as_args: |value, args| (!value).as_args(args, "--no-include-path-detection" ), |
1567 | }, |
1568 | /// Whether we should try to fit macro constants into types smaller than `u32` and `i32`. |
1569 | fit_macro_constants: bool { |
1570 | methods: { |
1571 | /// Set whether `bindgen` should try to fit macro constants into types smaller than `u32` |
1572 | /// and `i32`. |
1573 | /// |
1574 | /// This option is disabled by default. |
1575 | pub fn fit_macro_constants(mut self, doit: bool) -> Self { |
1576 | self.options.fit_macro_constants = doit; |
1577 | self |
1578 | } |
1579 | }, |
1580 | as_args: "--fit-macro-constant-types" , |
1581 | }, |
1582 | /// Whether to prepend the `enum` name to constant or newtype variants. |
1583 | prepend_enum_name: bool { |
1584 | default: true, |
1585 | methods: { |
1586 | /// Set whether to prepend the `enum` name to constant or newtype variants. |
1587 | /// |
1588 | /// The `enum` name is prepended by default. |
1589 | pub fn prepend_enum_name(mut self, doit: bool) -> Self { |
1590 | self.options.prepend_enum_name = doit; |
1591 | self |
1592 | } |
1593 | }, |
1594 | as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name" ), |
1595 | }, |
1596 | /// Version of the Rust compiler to target. |
1597 | rust_target: RustTarget { |
1598 | methods: { |
1599 | /// Specify the Rust target version. |
1600 | /// |
1601 | /// The default target is the latest stable Rust version. |
1602 | pub fn rust_target(mut self, rust_target: RustTarget) -> Self { |
1603 | self.options.set_rust_target(rust_target); |
1604 | self |
1605 | } |
1606 | }, |
1607 | as_args: |rust_target, args| { |
1608 | args.push("--rust-target" .to_owned()); |
1609 | args.push(rust_target.to_string()); |
1610 | }, |
1611 | }, |
1612 | /// Features to be enabled. They are derived from `rust_target`. |
1613 | rust_features: RustFeatures { |
1614 | default: RustTarget::default().into(), |
1615 | methods: {}, |
1616 | // This field cannot be set from the CLI, |
1617 | as_args: ignore, |
1618 | }, |
1619 | /// Enable support for native Rust unions if they are supported. |
1620 | untagged_union: bool { |
1621 | default: true, |
1622 | methods: { |
1623 | /// Disable support for native Rust unions, if supported. |
1624 | /// |
1625 | /// The default value of this option is set based on the value passed to |
1626 | /// [`Builder::rust_target`]. |
1627 | pub fn disable_untagged_union(mut self) -> Self { |
1628 | self.options.untagged_union = false; |
1629 | self |
1630 | } |
1631 | } |
1632 | as_args: |value, args| (!value).as_args(args, "--disable-untagged-union" ), |
1633 | }, |
1634 | /// Whether we should record which items in the regex sets did match any C items. |
1635 | record_matches: bool { |
1636 | default: true, |
1637 | methods: { |
1638 | /// Set whether we should record which items in our regex sets did match any C items. |
1639 | /// |
1640 | /// Matches are recorded by default. |
1641 | pub fn record_matches(mut self, doit: bool) -> Self { |
1642 | self.options.record_matches = doit; |
1643 | self |
1644 | } |
1645 | |
1646 | }, |
1647 | as_args: |value, args| (!value).as_args(args, "--no-record-matches" ), |
1648 | }, |
1649 | /// Whether `size_t` should be translated to `usize` automatically. |
1650 | size_t_is_usize: bool { |
1651 | default: true, |
1652 | methods: { |
1653 | /// Set whether `size_t` should be translated to `usize`. |
1654 | /// |
1655 | /// If `size_t` is translated to `usize`, type definitions for `size_t` will not be |
1656 | /// emitted. |
1657 | /// |
1658 | /// `size_t` is translated to `usize` by default. |
1659 | pub fn size_t_is_usize(mut self, is: bool) -> Self { |
1660 | self.options.size_t_is_usize = is; |
1661 | self |
1662 | } |
1663 | }, |
1664 | as_args: |value, args| (!value).as_args(args, "--no-size_t-is-usize" ), |
1665 | }, |
1666 | /// The tool that should be used to format the generated bindings. |
1667 | formatter: Formatter { |
1668 | methods: { |
1669 | /// Set whether `rustfmt` should be used to format the generated bindings. |
1670 | /// |
1671 | /// `rustfmt` is used by default. |
1672 | /// |
1673 | /// This method overlaps in functionality with the more general [`Builder::formatter`]. |
1674 | /// Thus, the latter should be preferred. |
1675 | #[deprecated ] |
1676 | pub fn rustfmt_bindings(mut self, doit: bool) -> Self { |
1677 | self.options.formatter = if doit { |
1678 | Formatter::Rustfmt |
1679 | } else { |
1680 | Formatter::None |
1681 | }; |
1682 | self |
1683 | } |
1684 | |
1685 | /// Set which tool should be used to format the generated bindings. |
1686 | /// |
1687 | /// The default formatter is [`Formatter::Rustfmt`]. |
1688 | /// |
1689 | /// To be able to use `prettyplease` as a formatter, the `"prettyplease"` feature for |
1690 | /// `bindgen` must be enabled in the Cargo manifest. |
1691 | pub fn formatter(mut self, formatter: Formatter) -> Self { |
1692 | self.options.formatter = formatter; |
1693 | self |
1694 | } |
1695 | }, |
1696 | as_args: |formatter, args| { |
1697 | if *formatter != Default::default() { |
1698 | args.push("--formatter" .to_owned()); |
1699 | args.push(formatter.to_string()); |
1700 | } |
1701 | }, |
1702 | }, |
1703 | /// The absolute path to the `rustfmt` configuration file. |
1704 | rustfmt_configuration_file: Option<PathBuf> { |
1705 | methods: { |
1706 | /// Set the absolute path to the `rustfmt` configuration file. |
1707 | /// |
1708 | /// The default `rustfmt` options are used if `None` is passed to this method or if |
1709 | /// this method is not called at all. |
1710 | /// |
1711 | /// Calling this method will set the [`Builder::rustfmt_bindings`] option to `true` |
1712 | /// and the [`Builder::formatter`] option to [`Formatter::Rustfmt`]. |
1713 | pub fn rustfmt_configuration_file(mut self, path: Option<PathBuf>) -> Self { |
1714 | self = self.formatter(Formatter::Rustfmt); |
1715 | self.options.rustfmt_configuration_file = path; |
1716 | self |
1717 | } |
1718 | }, |
1719 | as_args: "--rustfmt-configuration-file" , |
1720 | }, |
1721 | /// Types that should not derive `PartialEq`. |
1722 | no_partialeq_types: RegexSet { |
1723 | methods: { |
1724 | regex_option! { |
1725 | /// Do not derive `PartialEq` for a given type. |
1726 | pub fn no_partialeq<T: Into<String>>(mut self, arg: T) -> Builder { |
1727 | self.options.no_partialeq_types.insert(arg.into()); |
1728 | self |
1729 | } |
1730 | } |
1731 | }, |
1732 | as_args: "--no-partialeq" , |
1733 | }, |
1734 | /// Types that should not derive `Copy`. |
1735 | no_copy_types: RegexSet { |
1736 | methods: { |
1737 | regex_option! { |
1738 | /// Do not derive `Copy` and `Clone` for a given type. |
1739 | pub fn no_copy<T: Into<String>>(mut self, arg: T) -> Self { |
1740 | self.options.no_copy_types.insert(arg.into()); |
1741 | self |
1742 | } |
1743 | } |
1744 | }, |
1745 | as_args: "--no-copy" , |
1746 | }, |
1747 | /// Types that should not derive `Debug`. |
1748 | no_debug_types: RegexSet { |
1749 | methods: { |
1750 | regex_option! { |
1751 | /// Do not derive `Debug` for a given type. |
1752 | pub fn no_debug<T: Into<String>>(mut self, arg: T) -> Self { |
1753 | self.options.no_debug_types.insert(arg.into()); |
1754 | self |
1755 | } |
1756 | } |
1757 | }, |
1758 | as_args: "--no-debug" , |
1759 | }, |
1760 | /// Types that should not derive or implement `Default`. |
1761 | no_default_types: RegexSet { |
1762 | methods: { |
1763 | regex_option! { |
1764 | /// Do not derive or implement `Default` for a given type. |
1765 | pub fn no_default<T: Into<String>>(mut self, arg: T) -> Self { |
1766 | self.options.no_default_types.insert(arg.into()); |
1767 | self |
1768 | } |
1769 | } |
1770 | }, |
1771 | as_args: "--no-default" , |
1772 | }, |
1773 | /// Types that should not derive `Hash`. |
1774 | no_hash_types: RegexSet { |
1775 | methods: { |
1776 | regex_option! { |
1777 | /// Do not derive `Hash` for a given type. |
1778 | pub fn no_hash<T: Into<String>>(mut self, arg: T) -> Builder { |
1779 | self.options.no_hash_types.insert(arg.into()); |
1780 | self |
1781 | } |
1782 | } |
1783 | }, |
1784 | as_args: "--no-hash" , |
1785 | }, |
1786 | /// Types that should be annotated with `#[must_use]`. |
1787 | must_use_types: RegexSet { |
1788 | methods: { |
1789 | regex_option! { |
1790 | /// Annotate the given type with the `#[must_use]` attribute. |
1791 | pub fn must_use_type<T: Into<String>>(mut self, arg: T) -> Builder { |
1792 | self.options.must_use_types.insert(arg.into()); |
1793 | self |
1794 | } |
1795 | } |
1796 | }, |
1797 | as_args: "--must-use-type" , |
1798 | }, |
1799 | /// Whether C arrays should be regular pointers in rust or array pointers |
1800 | array_pointers_in_arguments: bool { |
1801 | methods: { |
1802 | /// Translate arrays `T arr[size]` into array pointers `*mut [T; size]` instead of |
1803 | /// translating them as `*mut T` which is the default. |
1804 | /// |
1805 | /// The same is done for `*const` pointers. |
1806 | pub fn array_pointers_in_arguments(mut self, doit: bool) -> Self { |
1807 | self.options.array_pointers_in_arguments = doit; |
1808 | self |
1809 | } |
1810 | |
1811 | }, |
1812 | as_args: "--use-array-pointers-in-arguments" , |
1813 | }, |
1814 | /// The name of the `wasm_import_module`. |
1815 | wasm_import_module_name: Option<String> { |
1816 | methods: { |
1817 | /// Adds the `#[link(wasm_import_module = import_name)]` attribute to all the `extern` |
1818 | /// blocks generated by `bindgen`. |
1819 | /// |
1820 | /// This attribute is not added by default. |
1821 | pub fn wasm_import_module_name<T: Into<String>>( |
1822 | mut self, |
1823 | import_name: T, |
1824 | ) -> Self { |
1825 | self.options.wasm_import_module_name = Some(import_name.into()); |
1826 | self |
1827 | } |
1828 | }, |
1829 | as_args: "--wasm-import-module-name" , |
1830 | }, |
1831 | /// The name of the dynamic library (if we are generating bindings for a shared library). |
1832 | dynamic_library_name: Option<String> { |
1833 | methods: { |
1834 | /// Generate bindings for a shared library with the given name. |
1835 | /// |
1836 | /// This option is disabled by default. |
1837 | pub fn dynamic_library_name<T: Into<String>>( |
1838 | mut self, |
1839 | dynamic_library_name: T, |
1840 | ) -> Self { |
1841 | self.options.dynamic_library_name = Some(dynamic_library_name.into()); |
1842 | self |
1843 | } |
1844 | }, |
1845 | as_args: "--dynamic-loading" , |
1846 | }, |
1847 | /// Whether to require successful linkage for all routines in a shared library. |
1848 | dynamic_link_require_all: bool { |
1849 | methods: { |
1850 | /// Set whether to require successful linkage for all routines in a shared library. |
1851 | /// This allows us to optimize function calls by being able to safely assume function |
1852 | /// pointers are valid. |
1853 | /// |
1854 | /// This option only comes into effect if the [`Builder::dynamic_library_name`] option |
1855 | /// is set. |
1856 | /// |
1857 | /// This option is disabled by default. |
1858 | pub fn dynamic_link_require_all(mut self, req: bool) -> Self { |
1859 | self.options.dynamic_link_require_all = req; |
1860 | self |
1861 | } |
1862 | }, |
1863 | as_args: "--dynamic-link-require-all" , |
1864 | }, |
1865 | /// Whether to only make generated bindings `pub` if the items would be publicly accessible by |
1866 | /// C++. |
1867 | respect_cxx_access_specs: bool { |
1868 | methods: { |
1869 | /// Set whether to respect the C++ access specifications. |
1870 | /// |
1871 | /// Passing `true` to this method will set the visibility of the generated Rust items |
1872 | /// as `pub` only if the corresponding C++ items are publicly accessible instead of |
1873 | /// marking all the items as public, which is the default. |
1874 | pub fn respect_cxx_access_specs(mut self, doit: bool) -> Self { |
1875 | self.options.respect_cxx_access_specs = doit; |
1876 | self |
1877 | } |
1878 | |
1879 | }, |
1880 | as_args: "--respect-cxx-access-specs" , |
1881 | }, |
1882 | /// Whether to translate `enum` integer types to native Rust integer types. |
1883 | translate_enum_integer_types: bool { |
1884 | methods: { |
1885 | /// Set whether to always translate `enum` integer types to native Rust integer types. |
1886 | /// |
1887 | /// Passing `true` to this method will result in `enum`s having types such as `u32` and |
1888 | /// `i16` instead of `c_uint` and `c_short` which is the default. The `#[repr]` types |
1889 | /// of Rust `enum`s are always translated to Rust integer types. |
1890 | pub fn translate_enum_integer_types(mut self, doit: bool) -> Self { |
1891 | self.options.translate_enum_integer_types = doit; |
1892 | self |
1893 | } |
1894 | }, |
1895 | as_args: "--translate-enum-integer-types" , |
1896 | }, |
1897 | /// Whether to generate types with C style naming. |
1898 | c_naming: bool { |
1899 | methods: { |
1900 | /// Set whether to generate types with C style naming. |
1901 | /// |
1902 | /// Passing `true` to this method will add prefixes to the generated type names. For |
1903 | /// example, instead of a `struct` with name `A` we will generate a `struct` with |
1904 | /// `struct_A`. Currently applies to `struct`s, `union`s, and `enum`s. |
1905 | pub fn c_naming(mut self, doit: bool) -> Self { |
1906 | self.options.c_naming = doit; |
1907 | self |
1908 | } |
1909 | }, |
1910 | as_args: "--c-naming" , |
1911 | }, |
1912 | /// Whether to always emit explicit padding fields. |
1913 | force_explicit_padding: bool { |
1914 | methods: { |
1915 | /// Set whether to always emit explicit padding fields. |
1916 | /// |
1917 | /// This option should be enabled if a `struct` needs to be serialized in its native |
1918 | /// format (padding bytes and all). This could be required if such `struct` will be |
1919 | /// written to a file or sent over the network, as anything reading the padding bytes |
1920 | /// of a struct may cause undefined behavior. |
1921 | /// |
1922 | /// Padding fields are not emitted by default. |
1923 | pub fn explicit_padding(mut self, doit: bool) -> Self { |
1924 | self.options.force_explicit_padding = doit; |
1925 | self |
1926 | } |
1927 | }, |
1928 | as_args: "--explicit-padding" , |
1929 | }, |
1930 | /// Whether to emit vtable functions. |
1931 | vtable_generation: bool { |
1932 | methods: { |
1933 | /// Set whether to enable experimental support to generate virtual table functions. |
1934 | /// |
1935 | /// This option should mostly work, though some edge cases are likely to be broken. |
1936 | /// |
1937 | /// Virtual table generation is disabled by default. |
1938 | pub fn vtable_generation(mut self, doit: bool) -> Self { |
1939 | self.options.vtable_generation = doit; |
1940 | self |
1941 | } |
1942 | }, |
1943 | as_args: "--vtable-generation" , |
1944 | }, |
1945 | /// Whether to sort the generated Rust items. |
1946 | sort_semantically: bool { |
1947 | methods: { |
1948 | /// Set whether to sort the generated Rust items in a predefined manner. |
1949 | /// |
1950 | /// Items are not ordered by default. |
1951 | pub fn sort_semantically(mut self, doit: bool) -> Self { |
1952 | self.options.sort_semantically = doit; |
1953 | self |
1954 | } |
1955 | }, |
1956 | as_args: "--sort-semantically" , |
1957 | }, |
1958 | /// Whether to deduplicate `extern` blocks. |
1959 | merge_extern_blocks: bool { |
1960 | methods: { |
1961 | /// Merge all extern blocks under the same module into a single one. |
1962 | /// |
1963 | /// Extern blocks are not merged by default. |
1964 | pub fn merge_extern_blocks(mut self, doit: bool) -> Self { |
1965 | self.options.merge_extern_blocks = doit; |
1966 | self |
1967 | } |
1968 | }, |
1969 | as_args: "--merge-extern-blocks" , |
1970 | }, |
1971 | /// Whether to wrap unsafe operations in unsafe blocks. |
1972 | wrap_unsafe_ops: bool { |
1973 | methods: { |
1974 | /// Wrap all unsafe operations in unsafe blocks. |
1975 | /// |
1976 | /// Unsafe operations are not wrapped by default. |
1977 | pub fn wrap_unsafe_ops(mut self, doit: bool) -> Self { |
1978 | self.options.wrap_unsafe_ops = doit; |
1979 | self |
1980 | } |
1981 | }, |
1982 | as_args: "--wrap-unsafe-ops" , |
1983 | }, |
1984 | /// Use DSTs to represent structures with flexible array members. |
1985 | flexarray_dst: bool { |
1986 | methods: { |
1987 | /// Use DSTs to represent structures with flexible array members. |
1988 | /// |
1989 | /// This option is disabled by default. |
1990 | pub fn flexarray_dst(mut self, doit: bool) -> Self { |
1991 | self.options.flexarray_dst = doit; |
1992 | self |
1993 | } |
1994 | }, |
1995 | as_args: "--flexarray-dst" , |
1996 | }, |
1997 | /// Patterns for functions whose ABI should be overridden. |
1998 | abi_overrides: HashMap<Abi, RegexSet> { |
1999 | methods: { |
2000 | regex_option! { |
2001 | /// Override the ABI of a given function. |
2002 | pub fn override_abi<T: Into<String>>(mut self, abi: Abi, arg: T) -> Self { |
2003 | self.options |
2004 | .abi_overrides |
2005 | .entry(abi) |
2006 | .or_default() |
2007 | .insert(arg.into()); |
2008 | self |
2009 | } |
2010 | } |
2011 | }, |
2012 | as_args: |overrides, args| { |
2013 | for (abi, set) in overrides { |
2014 | for item in set.get_items() { |
2015 | args.push("--override-abi" .to_owned()); |
2016 | args.push(format!(" {}= {}" , item, abi)); |
2017 | } |
2018 | } |
2019 | }, |
2020 | }, |
2021 | /// Whether to generate wrappers for `static` functions. |
2022 | wrap_static_fns: bool { |
2023 | methods: { |
2024 | #[cfg (feature = "experimental" )] |
2025 | /// Set whether to generate wrappers for `static`` functions. |
2026 | /// |
2027 | /// Passing `true` to this method will generate a C source file with non-`static` |
2028 | /// functions that call the `static` functions found in the input headers and can be |
2029 | /// called from Rust once the source file is compiled. |
2030 | /// |
2031 | /// The path of this source file can be set using the [`Builder::wrap_static_fns_path`] |
2032 | /// method. |
2033 | pub fn wrap_static_fns(mut self, doit: bool) -> Self { |
2034 | self.options.wrap_static_fns = doit; |
2035 | self |
2036 | } |
2037 | }, |
2038 | as_args: "--wrap-static-fns" , |
2039 | }, |
2040 | /// The suffix to be added to the function wrappers for `static` functions. |
2041 | wrap_static_fns_suffix: Option<String> { |
2042 | methods: { |
2043 | #[cfg (feature = "experimental" )] |
2044 | /// Set the suffix added to the wrappers for `static` functions. |
2045 | /// |
2046 | /// This option only comes into effect if `true` is passed to the |
2047 | /// [`Builder::wrap_static_fns`] method. |
2048 | /// |
2049 | /// The default suffix is `__extern`. |
2050 | pub fn wrap_static_fns_suffix<T: AsRef<str>>(mut self, suffix: T) -> Self { |
2051 | self.options.wrap_static_fns_suffix = Some(suffix.as_ref().to_owned()); |
2052 | self |
2053 | } |
2054 | }, |
2055 | as_args: "--wrap-static-fns-suffix" , |
2056 | }, |
2057 | /// The path of the file where the wrappers for `static` functions will be emitted. |
2058 | wrap_static_fns_path: Option<PathBuf> { |
2059 | methods: { |
2060 | #[cfg (feature = "experimental" )] |
2061 | /// Set the path for the source code file that would be created if any wrapper |
2062 | /// functions must be generated due to the presence of `static` functions. |
2063 | /// |
2064 | /// `bindgen` will automatically add the right extension to the header and source code |
2065 | /// files. |
2066 | /// |
2067 | /// This option only comes into effect if `true` is passed to the |
2068 | /// [`Builder::wrap_static_fns`] method. |
2069 | /// |
2070 | /// The default path is `temp_dir/bindgen/extern`, where `temp_dir` is the path |
2071 | /// returned by [`std::env::temp_dir`] . |
2072 | pub fn wrap_static_fns_path<T: AsRef<Path>>(mut self, path: T) -> Self { |
2073 | self.options.wrap_static_fns_path = Some(path.as_ref().to_owned()); |
2074 | self |
2075 | } |
2076 | }, |
2077 | as_args: "--wrap-static-fns-path" , |
2078 | }, |
2079 | /// Default visibility of fields. |
2080 | default_visibility: FieldVisibilityKind { |
2081 | methods: { |
2082 | /// Set the default visibility of fields, including bitfields and accessor methods for |
2083 | /// bitfields. |
2084 | /// |
2085 | /// This option only comes into effect if the [`Builder::respect_cxx_access_specs`] |
2086 | /// option is disabled. |
2087 | pub fn default_visibility( |
2088 | mut self, |
2089 | visibility: FieldVisibilityKind, |
2090 | ) -> Self { |
2091 | self.options.default_visibility = visibility; |
2092 | self |
2093 | } |
2094 | }, |
2095 | as_args: |visibility, args| { |
2096 | if *visibility != Default::default() { |
2097 | args.push("--default-visibility" .to_owned()); |
2098 | args.push(visibility.to_string()); |
2099 | } |
2100 | }, |
2101 | }, |
2102 | /// Whether to emit diagnostics or not. |
2103 | emit_diagnostics: bool { |
2104 | methods: { |
2105 | #[cfg (feature = "experimental" )] |
2106 | /// Emit diagnostics. |
2107 | /// |
2108 | /// These diagnostics are emitted to `stderr` if you are using `bindgen-cli` or printed |
2109 | /// using `cargo:warning=` if you are using `bindgen` as a `build-dependency`. |
2110 | /// |
2111 | /// Diagnostics are not emitted by default. |
2112 | /// |
2113 | /// The layout and contents of these diagnostic messages are not covered by versioning |
2114 | /// and can change without notice. |
2115 | pub fn emit_diagnostics(mut self) -> Self { |
2116 | self.options.emit_diagnostics = true; |
2117 | self |
2118 | } |
2119 | }, |
2120 | as_args: "--emit-diagnostics" , |
2121 | }, |
2122 | /// Whether to use Clang evaluation on temporary files as a fallback for macros that fail to |
2123 | /// parse. |
2124 | clang_macro_fallback: bool { |
2125 | methods: { |
2126 | /// Use Clang as a fallback for macros that fail to parse using `CExpr`. |
2127 | /// |
2128 | /// This uses a workaround to evaluate each macro in a temporary file. Because this |
2129 | /// results in slower compilation, this option is opt-in. |
2130 | pub fn clang_macro_fallback(mut self) -> Self { |
2131 | self.options.clang_macro_fallback = true; |
2132 | self |
2133 | } |
2134 | }, |
2135 | as_args: "--clang-macro-fallback" , |
2136 | } |
2137 | /// Path to use for temporary files created by clang macro fallback code like precompiled |
2138 | /// headers. |
2139 | clang_macro_fallback_build_dir: Option<PathBuf> { |
2140 | methods: { |
2141 | /// Set a path to a directory to which `.c` and `.h.pch` files should be written for the |
2142 | /// purpose of using clang to evaluate macros that can't be easily parsed. |
2143 | /// |
2144 | /// The default location for `.h.pch` files is the directory that the corresponding |
2145 | /// `.h` file is located in. The default for the temporary `.c` file used for clang |
2146 | /// parsing is the current working directory. Both of these defaults are overridden |
2147 | /// by this option. |
2148 | pub fn clang_macro_fallback_build_dir<P: AsRef<Path>>(mut self, path: P) -> Self { |
2149 | self.options.clang_macro_fallback_build_dir = Some(path.as_ref().to_owned()); |
2150 | self |
2151 | } |
2152 | }, |
2153 | as_args: "--clang-macro-fallback-build-dir" , |
2154 | } |
2155 | } |
2156 | |