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