1# cbindgen User Guide 2 3cbindgen creates C/C++11 headers for Rust libraries which expose a public C API. 4 5While you could do this by hand, it's not a particularly good use of your time. It's also much more likely to be error-prone than machine-generated headers that are based on your actual Rust code. The cbindgen developers have also worked closely with the developers of Rust to ensure that the headers we generate reflect actual guarantees about Rust's type layout and ABI. 6 7C++ headers are nice because we can use operator overloads, constructors, enum classes, and templates to make the API more ergonomic and Rust-like. C headers are nice because you can be more confident that whoever you're interoperating with can handle them. With cbindgen *you don't need to choose*! You can just tell it to emit both from the same Rust library. 8 9There are two ways to use cbindgen: as a standalone program, or as a library (presumably in your build.rs). 10There isn't really much practical difference, because cbindgen is a simple rust library with no interesting dependencies. Using it as a program means people building your software will need it installed. Using it in your library means people may have to build cbindgen more frequently (e.g. every time they update their rust compiler). 11 12It's worth noting that the development of cbindgen has been largely adhoc, as features have been added to support the usecases of the maintainers. This means cbindgen may randomly fail to support some particular situation simply because no one has put in the effort to handle it yet. [Please file an issue if you run into such a situation][file-it]. Although since we all have other jobs, you might need to do the implementation work too :) 13 14 15 16 17 18# Quick Start 19 20To install cbindgen, you just need to run 21 22```text 23cargo install --force cbindgen 24``` 25 26(--force just makes it update to the latest cbindgen if it's already installed) 27 28To use cbindgen you need two things: 29 30* A configuration (cbindgen.toml, which can be empty to start) 31* A Rust crate with a public C API 32 33Then all you need to do is run it: 34 35```text 36cbindgen --config cbindgen.toml --crate my_rust_library --output my_header.h 37``` 38 39See `cbindgen --help` for more options. 40 41[Get a template cbindgen.toml here.](template.toml) 42 43 44 45## build.rs 46 47If you don't want to use cbindgen as an application, here's an example build.rs script: 48 49```rust 50extern crate cbindgen; 51 52use std::env; 53 54fn main() { 55 let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 56 57 cbindgen::Builder::new() 58 .with_crate(crate_dir) 59 .generate() 60 .expect("Unable to generate bindings") 61 .write_to_file("bindings.h"); 62} 63``` 64 65You can add configuration options using the [`Builder`](https://docs.rs/cbindgen/*/cbindgen/struct.Builder.html#methods) interface. 66 67If you'd like to use a `build.rs` script with a `cbindgen.toml`, consider using [`cbindgen::generate()`](https://docs.rs/cbindgen/*/cbindgen/fn.generate.html) instead. 68 69 70 71 72 73# Writing Your C API 74 75cbindgen has a simple but effective strategy. It walks through your crate looking for: 76 77* `#[no_mangle] pub extern fn` ("functions") 78* `#[no_mangle] pub static` ("globals") 79* `pub const` ("constants") 80 81and generates a header declaring those items. But to declare those items, it needs to also be able to describe the layout and ABI of the types that appear in their signatures. So it will also spider through your crate (and optionally its dependencies) to try to find the definitions of every type used in your public API. 82 83> NOTE: A major limitation of cbindgen is that it does not understand Rust's module system or namespacing. This means that if cbindgen sees that it needs the definition for `MyType` and there exists two things in your project with the type name `MyType`, it won't know what to do. Currently, cbindgen's behaviour is unspecified if this happens. However this may be ok if they have [different cfgs][section-cfgs]. 84 85If a type is determined to have a guaranteed layout, a full definition will be emitted in the header. If the type doesn't have a guaranteed layout, only a forward declaration will be emitted. This may be fine if the type is intended to be passed around opaquely and by reference. 86 87 88 89 90# Examples 91 92 93 94It would be really nice to have some curated and clean examples, but we don't have those yet. 95 96[The README has some useful links though](README.md#examples). 97 98 99 100# Supported Types 101 102Most things in Rust don't have a guaranteed layout by default. In most cases this is nice because it enables layout to be optimized in the majority of cases where type layout isn't that interesting. However this is problematic for our purposes. Thankfully Rust lets us opt into guaranteed layouts with the `repr` attribute. 103 104You can learn about all of the different repr attributes [by reading Rust's reference][reference], but here's a quick summary: 105 106* `#[repr(C)]`: give this struct/union/enum the same layout and ABI C would 107* `#[repr(u8, u16, ... etc)]`: give this enum the same layout and ABI as the given integer type 108* `#[repr(transparent)]`: give this single-field struct the same ABI as its field (useful for newtyping integers but keeping the integer ABI) 109 110cbindgen supports the `#[repr(align(N))]` and `#[repr(packed)]` attributes, but currently does not support `#[repr(packed(N))]`. 111 112cbindgen also supports using `repr(C)`/`repr(u8)` on non-C-like enums (enums with fields). This gives a C-compatible tagged union layout, as [defined by this RFC 2195][really-tagged-unions]. `repr(C)` will give a simpler layout that is perhaps more intuitive, while `repr(u8)` will produce a more compact layout. 113 114If you ensure everything has a guaranteed repr, then cbindgen will generate definitions for: 115 116* struct (named-style or tuple-style) 117* enum (fieldless or with fields) 118* union 119* type 120* `[T; n]` (arrays always have a guaranteed C-compatible layout) 121* `&T`, `&mut T`, `*const T`, `*mut T`, `Option<&T>`, `Option<&mut T>` (all have the same pointer ABI) 122* `fn()` (as an actual function pointer) 123* `bitflags! { ... }` (if macro_expansion.bitflags is enabled) 124 125structs, enums, unions, and type aliases may be generic, although certain generic substitutions may fail to resolve under certain configurations. In C mode generics are resolved through monomorphization and mangling, while in C++ mode generics are resolved with templates. cbindgen cannot support generic functions, as they do not actually have a single defined symbol. 126 127cbindgen sadly cannot ever support anonymous tuples `(A, B, ...)`, as there is no way to guarantee their layout. You must use a tuple struct. 128 129cbindgen also cannot support wide pointers like `&dyn Trait` or `&[T]`, as their layout and ABI is not guaranteed. In the case of slices you can at least decompose them into a pointer and length, and reconstruct them with `slice::from_raw_parts`. 130 131If cbindgen determines that a type is zero-sized, it will erase all references to that type (so fields of that type simply won't be emitted). This won't work if that type appears as a function argument because C, C++, and Rust all have different definitions of what it means for a type to be empty. 132 133Don't use the `[u64; 0]` trick to over-align a struct, we don't support this. 134 135cbindgen contains the following hardcoded mappings (again completely ignoring namespacing, literally just looking at the name of the type): 136 137 138 139 140## std types 141 142* bool => bool 143* char => uint32_t 144* u8 => uint8_t 145* u16 => uint16_t 146* u32 => uint32_t 147* u64 => uint64_t 148* usize => uintptr_t 149* i8 => int8_t 150* i16 => int16_t 151* i32 => int32_t 152* i64 => int64_t 153* isize => intptr_t 154* f32 => float 155* f64 => double 156* VaList => va_list 157* PhantomData => *evaporates*, can only appear as the field of a type 158* () => *evaporates*, can only appear as the field of a type 159 160 161 162## libc types 163 164* c_void => void 165* c_char => char 166* c_schar => signed char 167* c_uchar => unsigned char 168* c_float => float 169* c_double => double 170* c_short => short 171* c_int => int 172* c_long => long 173* c_longlong => long long 174* c_ushort => unsigned short 175* c_uint => unsigned int 176* c_ulong => unsigned long 177* c_ulonglong => unsigned long long 178 179 180 181## stdint types 182 183* uint8_t => uint8_t 184* uint16_t => uint16_t 185* uint32_t => uint32_t 186* uint64_t => uint64_t 187* uintptr_t => uintptr_t 188* size_t => size_t 189* int8_t => int8_t 190* int16_t => int16_t 191* int32_t => int32_t 192* int64_t => int64_t 193* intptr_t => intptr_t 194* ssize_t => ssize_t 195* ptrdiff_t => ptrdiff_t 196 197 198 199 200 201 202# Configuring Your Header 203 204cbindgen supports several different options for configuring the output of your header, including target language, styling, mangling, prefixing, includes, and defines. 205 206 207 208 209 210## Defines and Cfgs 211 212As cbindgen spiders through your crate, it will make note of all the cfgs it found on the path to every item. If it finds multiple declarations that share a single name but have different cfgs, it will then try to emit every version it found wrapped in defines that correspond to those cfgs. In this way platform-specific APIs or representations can be properly supported. 213 214However cbindgen has no way of knowing how you want to map those cfgs to defines. You will need to use the `[defines]` section in your cbindgen.toml to specify all the different mappings. It natively understands concepts like any() and all(), so you only need to tell it how you want to translate base concepts like `target_os = "freebsd"` or `feature = "serde"`. 215 216Note that because cbindgen just parses the source of your crate, you mostly don't need to worry about what crate features or what platform you're targetting. Every possible configuration should be visible to the parser. Our primitive mappings should also be completely platform agnostic (i32 is int32_t regardless of your target). 217 218While modules within a crate form a tree with uniquely defined paths to each item, and therefore uniquely defined cfgs for those items, dependencies do not. If you depend on a crate in multiple ways, and those ways produce different cfgs, one of them will be arbitrarily chosen for any types found in that crate. 219 220 221 222 223## Annotations 224 225While output configuration is primarily done through the cbindgen.toml, in some cases you need to manually override your global settings. In those cases you can add inline annotations to your types, which are doc comments that start with `cbindgen:`. Here's an example of using annotations to rename a struct's fields and opt into overloading `operator==`: 226 227```rust 228/// cbindgen:field-names=[x, y] 229/// cbindgen:derive-eq 230#[repr(C)] 231pub struct Point(pub f32, pub f32); 232``` 233 234An annotation may be a bool, string (no quotes), or list of strings. If just the annotation's name is provided, `=true` is assumed. The annotation parser is currently fairly naive and lacks any capacity for escaping, so don't try to make any strings with `=`, `,`, `[` or `]`. 235 236Most annotations are just local overrides for identical settings in the cbindgen.toml, but a few are unique because they don't make sense in a global context. The set of supported annotation are as follows: 237 238 239 240### Struct Annotations 241 242* field-names=\[field1, field2, ...\] -- sets the names of all the fields in the output struct. These names will be output verbatim, and are not eligible for renaming. 243 244The rest are just local overrides for the same options found in the cbindgen.toml: 245 246* rename-all=RenameRule 247* derive-constructor 248* derive-eq 249* derive-neq 250* derive-lt 251* derive-lte 252* derive-gt 253* derive-gte 254 255 256 257### Enum Annotations 258 259* enum-trailing-values=\[variant1, variant2, ...\] -- add the following fieldless enum variants to the end of the enum's definition. These variant names *will* have the enum's renaming rules applied. 260 261WARNING: if any of these values are ever passed into Rust, behaviour will be Undefined. Rust does not know about them, and will assume they cannot happen. 262 263The rest are just local overrides for the same options found in the cbindgen.toml: 264 265* rename-all=RenameRule 266* add-sentinel 267* derive-helper-methods 268* derive-const-casts 269* derive-mut-casts 270* derive-tagged-enum-destructor 271* derive-tagged-enum-copy-constructor 272* enum-class 273* prefix-with-name 274* private-default-tagged-enum-constructor 275 276 277 278### Union Annotations 279 280* field-names=\[field1, field2, ...\] -- sets the names of all the fields in the output union. These names will be output verbatim, and are not eligible for renaming. 281 282The rest are just local overrides for the same options found in the cbindgen.toml: 283 284* rename-all=RenameRule 285 286 287 288### Function Annotations 289 290All function attributes are just local overrides for the same options found in the cbindgen.toml: 291 292* rename-all=RenameRule 293* prefix 294* postfix 295 296## Generating Swift Bindings 297 298In addition to parsing function names in C/C++ header files, the Swift compiler can make use of the `swift_name` attribute on functions to generate more idiomatic names for imported functions and methods. 299 300This attribute is commonly used in Objective-C/C/C++ via the `NS_SWIFT_NAME` and `CF_SWIFT_NAME` macros. 301 302Given configuration in the cbindgen.toml, `cbindgen` can generate these attributes for you by guessing an appropriate method signature based on the existing function name (and type, if it is a method in an `impl` block). 303 304This is controlled by the `swift_name_macro` option in the cbindgen.toml. 305 306 307## cbindgen.toml 308 309Most configuration happens through your cbindgen.toml file. Every value has a default (that is usually reasonable), so you can start with an empty cbindgen.toml and tweak it until you like the output you're getting. 310 311Note that many options defined here only apply for one of C or C++. Usually it's an option specifying whether we should try to make use of a feature in C++'s type system or generate a helper method. 312 313```toml 314# The language to output bindings in 315# 316# possible values: "C", "C++" 317# 318# default: "C++" 319language = "C" 320 321 322 323 324# Options for wrapping the contents of the header: 325 326# An optional string of text to output at the beginning of the generated file 327# default: doesn't emit anything 328header = "/* Text to put at the beginning of the generated file. Probably a license. */" 329 330# An optional string of text to output at the end of the generated file 331# default: doesn't emit anything 332trailer = "/* Text to put at the end of the generated file */" 333 334# An optional name to use as an include guard 335# default: doesn't emit an include guard 336include_guard = "mozilla_wr_bindings_h" 337 338# An optional string of text to output between major sections of the generated 339# file as a warning against manual editing 340# 341# default: doesn't emit anything 342autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" 343 344# Whether to include a comment with the version of cbindgen used to generate the file 345# default: false 346include_version = true 347 348# An optional namespace to output around the generated bindings 349# default: doesn't emit a namespace 350namespace = "ffi" 351 352# An optional list of namespaces to output around the generated bindings 353# default: [] 354namespaces = ["mozilla", "wr"] 355 356# An optional list of namespaces to declare as using with "using namespace" 357# default: [] 358using_namespaces = ["mozilla", "wr"] 359 360# A list of sys headers to #include (with angle brackets) 361# default: [] 362sys_includes = ["stdio", "string"] 363 364# A list of headers to #include (with quotes) 365# default: [] 366includes = ["my_great_lib.h"] 367 368# Whether cbindgen's default C/C++ standard imports should be suppressed. These 369# imports are included by default because our generated headers tend to require 370# them (e.g. for uint32_t). Currently, the generated imports are: 371# 372# * for C: <stdarg.h>, <stdbool.h>, <stdint.h>, <stdlib.h>, <uchar.h> 373# 374# * for C++: <cstdarg>, <cstdint>, <cstdlib>, <new>, <cassert> (depending on config) 375# 376# default: false 377no_includes = false 378 379 380 381 382 383# Code Style Options 384 385# The style to use for curly braces 386# 387# possible values: "SameLine", "NextLine" 388# 389# default: "SameLine" 390braces = "SameLine" 391 392# The desired length of a line to use when formatting lines 393# default: 100 394line_length = 80 395 396# The amount of spaces to indent by 397# default: 2 398tab_width = 3 399 400# How the generated documentation should be commented. 401# 402# possible values: 403# * "c": /* like this */ 404# * "c99": // like this 405# * "c++": /// like this 406# * "doxy": like C, but with leading *'s on each line 407# * "auto": "c++" if that's the language, "doxy" otherwise 408# 409# default: "auto" 410documentation_style = "doxy" 411 412 413 414 415 416# Codegen Options 417 418# When generating a C header, the kind of declaration style to use for structs 419# or enums. 420# 421# possible values: 422# * "type": typedef struct { ... } MyType; 423# * "tag": struct MyType { ... }; 424# * "both": typedef struct MyType { ... } MyType; 425# 426# default: "both" 427style = "both" 428 429# A list of substitutions for converting cfg's to ifdefs. cfgs which aren't 430# defined here will just be discarded. 431# 432# e.g. 433# `#[cfg(target = "freebsd")] ...` 434# becomes 435# `#if defined(DEFINE_FREEBSD) ... #endif` 436[defines] 437"target_os = freebsd" = "DEFINE_FREEBSD" 438"feature = serde" = "DEFINE_SERDE" 439 440 441 442 443 444[export] 445# A list of additional items to always include in the generated bindings if they're 446# found but otherwise don't appear to be used by the public API. 447# 448# default: [] 449include = ["MyOrphanStruct", "MyGreatTypeRename"] 450 451# A list of items to not include in the generated bindings 452# default: [] 453exclude = ["Bad"] 454 455# A prefix to add before the name of every item 456# default: no prefix is added 457prefix = "CAPI_" 458 459# Types of items that we'll generate. If empty, then all types of item are emitted. 460# 461# possible items: (TODO: explain these in detail) 462# * "constants": 463# * "globals": 464# * "enums": 465# * "structs": 466# * "unions": 467# * "typedefs": 468# * "opaque": 469# * "functions": 470# 471# default: [] 472item_types = ["enums", "structs", "opaque", "functions"] 473 474# Whether applying rules in export.rename prevents export.prefix from applying. 475# 476# e.g. given this toml: 477# 478# [export] 479# prefix = "capi_" 480# [export.rename] 481# "MyType" = "my_cool_type" 482# 483# You get the following results: 484# 485# renaming_overrides_prefixing = true: 486# "MyType" => "my_cool_type" 487# 488# renaming_overrides_prefixing = false: 489# "MyType => capi_my_cool_type" 490# 491# default: false 492renaming_overrides_prefixing = true 493 494# Table of name conversions to apply to item names (lhs becomes rhs) 495[export.rename] 496"MyType" = "my_cool_type" 497"my_function" = "BetterFunctionName" 498 499# Table of things to prepend to the body of any struct, union, or enum that has the 500# given name. This can be used to add things like methods which don't change ABI, 501# mark fields private, etc 502[export.pre_body] 503"MyType" = """ 504 MyType() = delete; 505private: 506""" 507 508# Table of things to append to the body of any struct, union, or enum that has the 509# given name. This can be used to add things like methods which don't change ABI. 510[export.body] 511"MyType" = """ 512 void cppMethod() const; 513""" 514 515[layout] 516# A string that should come before the name of any type which has been marked 517# as `#[repr(packed)]`. For instance, "__attribute__((packed))" would be a 518# reasonable value if targeting gcc/clang. A more portable solution would 519# involve emitting the name of a macro which you define in a platform-specific 520# way. e.g. "PACKED" 521# 522# default: `#[repr(packed)]` types will be treated as opaque, since it would 523# be unsafe for C callers to use a incorrectly laid-out union. 524packed = "PACKED" 525 526# A string that should come before the name of any type which has been marked 527# as `#[repr(align(n))]`. This string must be a function-like macro which takes 528# a single argument (the requested alignment, `n`). For instance, a macro 529# `#define`d as `ALIGNED(n)` in `header` which translates to 530# `__attribute__((aligned(n)))` would be a reasonable value if targeting 531# gcc/clang. 532# 533# default: `#[repr(align(n))]` types will be treated as opaque, since it 534# could be unsafe for C callers to use a incorrectly-aligned union. 535aligned_n = "ALIGNED" 536 537 538[fn] 539# An optional prefix to put before every function declaration 540# default: no prefix added 541prefix = "WR_START_FUNC" 542 543# An optional postfix to put after any function declaration 544# default: no postix added 545postfix = "WR_END_FUNC" 546 547# How to format function arguments 548# 549# possible values: 550# * "horizontal": place all arguments on the same line 551# * "vertical": place each argument on its own line 552# * "auto": only use vertical if horizontal would exceed line_length 553# 554# default: "auto" 555args = "horizontal" 556 557# An optional string that should prefix function declarations which have been 558# marked as `#[must_use]`. For instance, "__attribute__((warn_unused_result))" 559# would be a reasonable value if targeting gcc/clang. A more portable solution 560# would involve emitting the name of a macro which you define in a 561# platform-specific way. e.g. "MUST_USE_FUNC" 562# default: nothing is emitted for must_use functions 563must_use = "MUST_USE_FUNC" 564 565# An optional string that, if present, will be used to generate Swift function 566# and method signatures for generated functions, for example "CF_SWIFT_NAME". 567# If no such macro is available in your toolchain, you can define one using the 568# `header` option in cbindgen.toml 569# default: no swift_name function attributes are generated 570swift_name_macro = "CF_SWIFT_NAME" 571 572# A rule to use to rename function argument names. The renaming assumes the input 573# is the Rust standard snake_case, however it accepts all the different rename_args 574# inputs. This means many options here are no-ops or redundant. 575# 576# possible values (that actually do something): 577# * "CamelCase": my_arg => myArg 578# * "PascalCase": my_arg => MyArg 579# * "GeckoCase": my_arg => aMyArg 580# * "ScreamingSnakeCase": my_arg => MY_ARG 581# * "None": apply no renaming 582# 583# technically possible values (that shouldn't have a purpose here): 584# * "SnakeCase": apply no renaming 585# * "LowerCase": apply no renaming (actually applies to_lowercase, is this bug?) 586# * "UpperCase": same as ScreamingSnakeCase in this context 587# * "QualifiedScreamingSnakeCase" => same as ScreamingSnakeCase in this context 588# 589# default: "None" 590rename_args = "PascalCase" 591 592# This rule specifies if the order of functions will be sorted in some way. 593# 594# "Name": sort by the name of the function 595# "None": keep order in which the functions have been parsed 596# 597# default: "Name" 598sort_by = "None" 599 600[struct] 601# A rule to use to rename struct field names. The renaming assumes the input is 602# the Rust standard snake_case, however it acccepts all the different rename_args 603# inputs. This means many options here are no-ops or redundant. 604# 605# possible values (that actually do something): 606# * "CamelCase": my_arg => myArg 607# * "PascalCase": my_arg => MyArg 608# * "GeckoCase": my_arg => mMyArg 609# * "ScreamingSnakeCase": my_arg => MY_ARG 610# * "None": apply no renaming 611# 612# technically possible values (that shouldn't have a purpose here): 613# * "SnakeCase": apply no renaming 614# * "LowerCase": apply no renaming (actually applies to_lowercase, is this bug?) 615# * "UpperCase": same as ScreamingSnakeCase in this context 616# * "QualifiedScreamingSnakeCase" => same as ScreamingSnakeCase in this context 617# 618# default: "None" 619rename_fields = "PascalCase" 620 621# An optional string that should come before the name of any struct which has been 622# marked as `#[must_use]`. For instance, "__attribute__((warn_unused))" 623# would be a reasonable value if targeting gcc/clang. A more portable solution 624# would involve emitting the name of a macro which you define in a 625# platform-specific way. e.g. "MUST_USE_STRUCT" 626# 627# default: nothing is emitted for must_use structs 628must_use = "MUST_USE_STRUCT" 629 630# Whether a Rust type with associated consts should emit those consts inside the 631# type's body. Otherwise they will be emitted trailing and with the type's name 632# prefixed. This does nothing if the target is C, or if 633# [const]allow_static_const = false 634# 635# default: false 636# associated_constants_in_body: false 637 638# Whether to derive a simple constructor that takes a value for every field. 639# default: false 640derive_constructor = true 641 642# Whether to derive an operator== for all structs 643# default: false 644derive_eq = false 645 646# Whether to derive an operator!= for all structs 647# default: false 648derive_neq = false 649 650# Whether to derive an operator< for all structs 651# default: false 652derive_lt = false 653 654# Whether to derive an operator<= for all structs 655# default: false 656derive_lte = false 657 658# Whether to derive an operator> for all structs 659# default: false 660derive_gt = false 661 662# Whether to derive an operator>= for all structs 663# default: false 664derive_gte = false 665 666 667 668 669 670[enum] 671# A rule to use to rename enum variants, and the names of any fields those 672# variants have. This should probably be split up into two separate options, but 673# for now, they're the same! See the documentation for `[struct]rename_fields` 674# for how this applies to fields. Renaming of the variant assumes that the input 675# is the Rust standard PascalCase. In the case of QualifiedScreamingSnakeCase, 676# it also assumed that the enum's name is PascalCase. 677# 678# possible values (that actually do something): 679# * "CamelCase": MyVariant => myVariant 680# * "SnakeCase": MyVariant => my_variant 681# * "ScreamingSnakeCase": MyVariant => MY_VARIANT 682# * "QualifiedScreamingSnakeCase": MyVariant => ENUM_NAME_MY_VARIANT 683# * "LowerCase": MyVariant => myvariant 684# * "UpperCase": MyVariant => MYVARIANT 685# * "None": apply no renaming 686# 687# technically possible values (that shouldn't have a purpose for the variants): 688# * "PascalCase": apply no renaming 689# * "GeckoCase": apply no renaming 690# 691# default: "None" 692rename_variants = "None" 693 694# Whether an extra "sentinel" enum variant should be added to all generated enums. 695# Firefox uses this for their IPC serialization library. 696# 697# WARNING: if the sentinel is ever passed into Rust, behaviour will be Undefined. 698# Rust does not know about this value, and will assume it cannot happen. 699# 700# default: false 701add_sentinel = false 702 703# Whether enum variant names should be prefixed with the name of the enum. 704# default: false 705prefix_with_name = false 706 707# Whether to emit enums using "enum class" when targeting C++. 708# default: true 709enum_class = true 710 711# Whether to generate static `::MyVariant(..)` constructors and `bool IsMyVariant()` 712# methods for enums with fields. 713# 714# default: false 715derive_helper_methods = false 716 717# Whether to generate `const MyVariant& AsMyVariant() const` methods for enums with fields. 718# default: false 719derive_const_casts = false 720 721# Whether to generate `MyVariant& AsMyVariant()` methods for enums with fields 722# default: false 723derive_mut_casts = false 724 725# The name of the macro/function to use for asserting `IsMyVariant()` in the body of 726# derived `AsMyVariant()` cast methods. 727# 728# default: "assert" (but also causes `<cassert>` to be included by default) 729cast_assert_name = "MOZ_RELEASE_ASSERT" 730 731# An optional string that should come before the name of any enum which has been 732# marked as `#[must_use]`. For instance, "__attribute__((warn_unused))" 733# would be a reasonable value if targeting gcc/clang. A more portable solution 734# would involve emitting the name of a macro which you define in a 735# platform-specific way. e.g. "MUST_USE_ENUM" 736# 737# Note that this refers to the *output* type. That means this will not apply to an enum 738# with fields, as it will be emitted as a struct. `[struct]must_use` will apply there. 739# 740# default: nothing is emitted for must_use enums 741must_use = "MUST_USE_ENUM" 742 743# Whether enums with fields should generate destructors. This exists so that generic 744# enums can be properly instantiated with payloads that are C++ types with 745# destructors. This isn't necessary for structs because C++ has rules to 746# automatically derive the correct constructors and destructors for those types. 747# 748# Care should be taken with this option, as Rust and C++ cannot 749# properly interoperate with eachother's notions of destructors. Also, this may 750# change the ABI for the type. Either your destructor-full enums must live 751# exclusively within C++, or they must only be passed by-reference between 752# C++ and Rust. 753# 754# default: false 755derive_tagged_enum_destructor = false 756 757# Whether enums with fields should generate copy-constructor. See the discussion on 758# derive_tagged_enum_destructor for why this is both useful and very dangerous. 759# 760# default: false 761derive_tagged_enum_copy_constructor = false 762# Whether enums with fields should generate copy-assignment operators. 763# 764# This depends on also deriving copy-constructors, and it is highly encouraged 765# for this to be set to true. 766# 767# default: false 768derive_tagged_enum_copy_assignment = false 769 770# Whether enums with fields should generate an empty, private destructor. 771# This allows the auto-generated constructor functions to compile, if there are 772# non-trivially constructible members. This falls in the same family of 773# dangerousness as `derive_tagged_enum_copy_constructor` and co. 774# 775# default: false 776private_default_tagged_enum_constructor = false 777 778 779 780 781 782[const] 783# Whether a generated constant can be a static const in C++ mode. I have no 784# idea why you would turn this off. 785# 786# default: true 787allow_static_const = true 788 789# Whether a generated constant can be constexpr in C++ mode. 790# 791# default: false 792allow_constexpr = false 793 794 795 796 797 798[macro_expansion] 799# Whether bindings should be generated for instances of the bitflags! macro. 800# default: false 801bitflags = true 802 803 804 805 806 807 808# Options for how your Rust library should be parsed 809 810[parse] 811# Whether to parse dependent crates and include their types in the output 812# default: false 813parse_deps = true 814 815# A white list of crate names that are allowed to be parsed. If this is defined, 816# only crates found in this list will ever be parsed. 817# 818# default: there is no whitelist (NOTE: this is the opposite of []) 819include = ["webrender", "webrender_traits"] 820 821# A black list of crate names that are not allowed to be parsed. 822# default: [] 823exclude = ["libc"] 824 825# Whether to use a new temporary target directory when running `rustc --pretty=expanded`. 826# This may be required for some build processes. 827# 828# default: false 829clean = false 830 831# Which crates other than the top-level binding crate we should generate 832# bindings for. 833# 834# default: [] 835extra_bindings = ["my_awesome_dep"] 836 837[parse.expand] 838# A list of crate names that should be run through `cargo expand` before 839# parsing to expand any macros. Note that if a crate is named here, it 840# will always be parsed, even if the blacklist/whitelist says it shouldn't be. 841# 842# default: [] 843crates = ["euclid"] 844 845# If enabled, use the `--all-features` option when expanding. Ignored when 846# `features` is set. For backwards-compatibility, this is forced on if 847# `expand = ["euclid"]` shorthand is used. 848# 849# default: false 850all_features = false 851 852# When `all_features` is disabled and this is also disabled, use the 853# `--no-default-features` option when expanding. 854# 855# default: true 856default_features = true 857 858# A list of feature names that should be used when running `cargo expand`. This 859# combines with `default_features` like in your `Cargo.toml`. Note that the features 860# listed here are features for the current crate being built, *not* the crates 861# being expanded. The crate's `Cargo.toml` must take care of enabling the 862# appropriate features in its dependencies 863# 864# default: [] 865features = ["cbindgen"] 866 867``` 868 869 870 871 872 873[reference]: https://doc.rust-lang.org/nightly/reference/type-layout.html#representations 874[really-tagged-unions]: https://github.com/rust-lang/rfcs/blob/master/text/2195-really-tagged-unions.md 875[section-cfgs]: #defines-and-cfgs 876[file-it]: https://github.com/eqrion/cbindgen/issues/new 877