1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::collections::{BTreeMap, HashMap};
use std::default::Default;
use std::str::FromStr;
use std::{fmt, fs, path::Path as StdPath};
use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
use serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use crate::bindgen::ir::annotation::AnnotationSet;
use crate::bindgen::ir::path::Path;
use crate::bindgen::ir::repr::ReprAlign;
pub use crate::bindgen::rename::RenameRule;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// A language type to generate bindings for.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Language {
Cxx,
C,
Cython,
}
impl FromStr for Language {
type Err = String;
fn from_str(s: &str) -> Result<Language, Self::Err> {
match s {
"cxx" => Ok(Language::Cxx),
"Cxx" => Ok(Language::Cxx),
"CXX" => Ok(Language::Cxx),
"cpp" => Ok(Language::Cxx),
"Cpp" => Ok(Language::Cxx),
"CPP" => Ok(Language::Cxx),
"c++" => Ok(Language::Cxx),
"C++" => Ok(Language::Cxx),
"c" => Ok(Language::C),
"C" => Ok(Language::C),
"cython" => Ok(Language::Cython),
"Cython" => Ok(Language::Cython),
_ => Err(format!("Unrecognized Language: '{}'.", s)),
}
}
}
deserialize_enum_str!(Language);
impl Language {
pub(crate) fn typedef(self) -> &'static str {
match self {
Language::Cxx | Language::C => "typedef",
Language::Cython => "ctypedef",
}
}
}
/// Controls what type of line endings are used in the generated code.
#[derive(Debug, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum LineEndingStyle {
/// Use Unix-style linefeed characters
LF,
/// Use classic Mac-style carriage-return characters
CR,
/// Use Windows-style carriage-return and linefeed characters
CRLF,
/// Use the native mode for the platform: CRLF on Windows, LF everywhere else.
Native,
}
impl Default for LineEndingStyle {
fn default() -> Self {
LineEndingStyle::LF
}
}
impl LineEndingStyle {
pub fn as_str(&self) -> &'static str {
match self {
Self::LF => "\n",
Self::CR => "\r",
Self::CRLF => "\r\n",
Self::Native => {
#[cfg(target_os = "windows")]
{
Self::CRLF.as_str()
}
#[cfg(not(target_os = "windows"))]
{
Self::LF.as_str()
}
}
}
}
}
impl FromStr for LineEndingStyle {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"native" => Ok(Self::Native),
"lf" => Ok(Self::LF),
"crlf" => Ok(Self::CRLF),
"cr" => Ok(Self::CR),
_ => Err(format!("Unrecognized line ending style: '{}'.", s)),
}
}
}
deserialize_enum_str!(LineEndingStyle);
/// A style of braces to use for generating code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Braces {
SameLine,
NextLine,
}
impl FromStr for Braces {
type Err = String;
fn from_str(s: &str) -> Result<Braces, Self::Err> {
match s {
"SameLine" => Ok(Braces::SameLine),
"same_line" => Ok(Braces::SameLine),
"NextLine" => Ok(Braces::NextLine),
"next_line" => Ok(Braces::NextLine),
_ => Err(format!("Unrecognized Braces: '{}'.", s)),
}
}
}
deserialize_enum_str!(Braces);
/// A type of layout to use when generating long lines of code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Layout {
Horizontal,
Vertical,
Auto,
}
impl FromStr for Layout {
type Err = String;
fn from_str(s: &str) -> Result<Layout, Self::Err> {
match s {
"Horizontal" => Ok(Layout::Horizontal),
"horizontal" => Ok(Layout::Horizontal),
"Vertical" => Ok(Layout::Vertical),
"vertical" => Ok(Layout::Vertical),
"Auto" => Ok(Layout::Auto),
"auto" => Ok(Layout::Auto),
_ => Err(format!("Unrecognized Layout: '{}'.", s)),
}
}
}
deserialize_enum_str!(Layout);
/// How the comments containing documentation should be styled.
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum DocumentationStyle {
C,
C99,
Doxy,
Cxx,
Auto,
}
impl FromStr for DocumentationStyle {
type Err = String;
fn from_str(s: &str) -> Result<DocumentationStyle, Self::Err> {
match s.to_lowercase().as_ref() {
"c" => Ok(DocumentationStyle::C),
"c99" => Ok(DocumentationStyle::C99),
"cxx" => Ok(DocumentationStyle::Cxx),
"c++" => Ok(DocumentationStyle::Cxx),
"doxy" => Ok(DocumentationStyle::Doxy),
"auto" => Ok(DocumentationStyle::Auto),
_ => Err(format!("Unrecognized documentation style: '{}'.", s)),
}
}
}
deserialize_enum_str!(DocumentationStyle);
/// How much of the documentation to include in the header file.
#[derive(Debug, Clone, Copy)]
pub enum DocumentationLength {
Short,
Full,
}
impl FromStr for DocumentationLength {
type Err = String;
fn from_str(s: &str) -> Result<DocumentationLength, Self::Err> {
match s.to_lowercase().as_ref() {
"short" => Ok(DocumentationLength::Short),
"full" => Ok(DocumentationLength::Full),
_ => Err(format!("Unrecognized documentation style: '{}'.", s)),
}
}
}
deserialize_enum_str!(DocumentationLength);
/// A style of Style to use when generating structs and enums.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Style {
Both,
Tag,
Type,
}
impl Style {
pub fn generate_tag(self) -> bool {
match self {
Style::Both | Style::Tag => true,
Style::Type => false,
}
}
pub fn generate_typedef(self) -> bool {
match self {
Style::Both | Style::Type => true,
Style::Tag => false,
}
}
// https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#styles-of-struct-union-and-enum-declaration
pub fn cython_def(self) -> &'static str {
if self.generate_tag() {
"cdef "
} else {
"ctypedef "
}
}
}
impl Default for Style {
fn default() -> Self {
Style::Both
}
}
impl FromStr for Style {
type Err = String;
fn from_str(s: &str) -> Result<Style, Self::Err> {
match s {
"Both" => Ok(Style::Both),
"both" => Ok(Style::Both),
"Tag" => Ok(Style::Tag),
"tag" => Ok(Style::Tag),
"Type" => Ok(Style::Type),
"type" => Ok(Style::Type),
_ => Err(format!("Unrecognized Style: '{}'.", s)),
}
}
}
deserialize_enum_str!(Style);
/// Different item types that we can generate and filter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ItemType {
Constants,
Globals,
Enums,
Structs,
Unions,
Typedefs,
OpaqueItems,
Functions,
}
impl FromStr for ItemType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use self::ItemType::*;
Ok(match &*s.to_lowercase() {
"constants" => Constants,
"globals" => Globals,
"enums" => Enums,
"structs" => Structs,
"unions" => Unions,
"typedefs" => Typedefs,
"opaque" => OpaqueItems,
"functions" => Functions,
_ => return Err(format!("Unrecognized Style: '{}'.", s)),
})
}
}
deserialize_enum_str!(ItemType);
/// Type which specifies the sort order of functions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortKey {
Name,
None,
}
impl FromStr for SortKey {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use self::SortKey::*;
Ok(match &*s.to_lowercase() {
"name" => Name,
"none" => None,
_ => return Err(format!("Unrecognized sort option: '{}'.", s)),
})
}
}
deserialize_enum_str!(SortKey);
/// Settings to apply when exporting items.
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct ExportConfig {
/// A list of additional items not used by exported functions to include in
/// the generated bindings
pub include: Vec<String>,
/// A list of items to not include in the generated bindings
pub exclude: Vec<String>,
/// Table of name conversions to apply to item names
pub rename: HashMap<String, String>,
/// Table of raw strings to prepend to the body of items.
pub pre_body: HashMap<String, String>,
/// Table of raw strings to append to the body of items.
pub body: HashMap<String, String>,
/// A prefix to add before the name of every item
pub prefix: Option<String>,
/// Types of items to generate.
pub item_types: Vec<ItemType>,
/// Whether renaming overrides or extends prefixing.
pub renaming_overrides_prefixing: bool,
/// Mangling configuration.
pub mangle: MangleConfig,
}
/// Mangling-specific configuration.
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct MangleConfig {
/// The rename rule to apply to the type names mangled.
pub rename_types: RenameRule,
/// Remove the underscores used for name mangling.
pub remove_underscores: bool,
}
impl ExportConfig {
pub(crate) fn should_generate(&self, item_type: ItemType) -> bool {
self.item_types.is_empty() || self.item_types.contains(&item_type)
}
pub(crate) fn pre_body(&self, path: &Path) -> Option<&str> {
self.pre_body.get(path.name()).map(|s| s.trim_matches('\n'))
}
pub(crate) fn post_body(&self, path: &Path) -> Option<&str> {
self.body.get(path.name()).map(|s| s.trim_matches('\n'))
}
pub(crate) fn rename(&self, item_name: &mut String) {
if let Some(name) = self.rename.get(item_name) {
*item_name = name.clone();
if self.renaming_overrides_prefixing {
return;
}
}
if let Some(ref prefix) = self.prefix {
item_name.insert_str(0, prefix);
}
}
}
/// Settings to apply to generated types with layout modifiers.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct LayoutConfig {
/// The way to annotate C types as #[repr(packed)].
pub packed: Option<String>,
/// The way to annotate C types as #[repr(align(...))]. This is assumed to be a functional
/// macro which takes a single argument (the alignment).
pub aligned_n: Option<String>,
}
impl LayoutConfig {
pub(crate) fn ensure_safe_to_represent(&self, align: &ReprAlign) -> Result<(), String> {
match (align, &self.packed, &self.aligned_n) {
(ReprAlign::Packed, None, _) => Err("Cannot safely represent #[repr(packed)] type without configured 'packed' annotation.".to_string()),
(ReprAlign::Align(_), _, None) => Err("Cannot safely represent #[repr(aligned(...))] type without configured 'aligned_n' annotation.".to_string()),
_ => Ok(()),
}
}
}
/// Settings to apply to generated functions.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct FunctionConfig {
/// Optional text to output before each function declaration
pub prefix: Option<String>,
/// Optional text to output after each function declaration
pub postfix: Option<String>,
/// The way to annotation this function as #[must_use]
pub must_use: Option<String>,
/// The style to layout the args
pub args: Layout,
/// The rename rule to apply to function args
pub rename_args: RenameRule,
/// An optional macro to use when generating Swift function name attributes
pub swift_name_macro: Option<String>,
/// Sort key for functions
pub sort_by: Option<SortKey>,
/// Optional text to output after functions which return `!`.
pub no_return: Option<String>,
}
impl Default for FunctionConfig {
fn default() -> FunctionConfig {
FunctionConfig {
prefix: None,
postfix: None,
must_use: None,
args: Layout::Auto,
rename_args: RenameRule::None,
swift_name_macro: None,
sort_by: None,
no_return: None,
}
}
}
impl FunctionConfig {
pub(crate) fn prefix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("prefix") {
return x;
}
self.prefix.clone()
}
pub(crate) fn postfix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("postfix") {
return x;
}
self.postfix.clone()
}
}
/// Settings to apply to generated structs.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct StructConfig {
/// The rename rule to apply to the name of struct fields
pub rename_fields: RenameRule,
/// Whether to generate a constructor for the struct (which takes
/// arguments to initialize all the members)
pub derive_constructor: bool,
/// Whether to generate a piecewise equality operator
pub derive_eq: bool,
/// Whether to generate a piecewise inequality operator
pub derive_neq: bool,
/// Whether to generate a less than operator on structs with one field
pub derive_lt: bool,
/// Whether to generate a less than or equal to operator on structs with one field
pub derive_lte: bool,
/// Whether to generate a greater than operator on structs with one field
pub derive_gt: bool,
/// Whether to generate a greater than or equal to operator on structs with one field
pub derive_gte: bool,
/// Whether to generate a ostream serializer for the struct
pub derive_ostream: bool,
/// Whether associated constants should be in the body. Only applicable to
/// non-transparent structs, and in C++-only.
pub associated_constants_in_body: bool,
/// The way to annotate this struct as #[must_use].
pub must_use: Option<String>,
}
impl StructConfig {
pub(crate) fn derive_constructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-constructor") {
return x;
}
self.derive_constructor
}
pub(crate) fn derive_eq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-eq") {
return x;
}
self.derive_eq
}
pub(crate) fn derive_neq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-neq") {
return x;
}
self.derive_neq
}
pub(crate) fn derive_lt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lt") {
return x;
}
self.derive_lt
}
pub(crate) fn derive_lte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lte") {
return x;
}
self.derive_lte
}
pub(crate) fn derive_gt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gt") {
return x;
}
self.derive_gt
}
pub(crate) fn derive_gte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gte") {
return x;
}
self.derive_gte
}
pub(crate) fn derive_ostream(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-ostream") {
return x;
}
self.derive_ostream
}
}
/// Settings to apply to generated enums.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct EnumConfig {
/// The rename rule to apply to the name of enum variants
pub rename_variants: RenameRule,
/// The rename rule to apply to the names of the union fields in C/C++
/// generated from the Rust enum. Applied before rename_variants
/// rename rule. Defaults to SnakeCase.
pub rename_variant_name_fields: RenameRule,
/// Whether to add a `Sentinel` value at the end of every enum
/// This is useful in Gecko for IPC serialization
pub add_sentinel: bool,
/// Whether the enum variants should be prefixed with the enum name
pub prefix_with_name: bool,
/// Whether to generate static `::X(..)` constructors and `IsX()`
/// methods for tagged enums.
pub derive_helper_methods: bool,
/// Whether to generate `AsX() const` methods for tagged enums.
pub derive_const_casts: bool,
/// Whether to generate `AsX()` methods for tagged enums.
pub derive_mut_casts: bool,
/// The name of the macro to use for `derive_{const,mut}casts`. If custom, you're
/// responsible to provide the necessary header, otherwise `assert` will be
/// used, and `<cassert>` will be included.
pub cast_assert_name: Option<String>,
/// The way to annotation this enum as #[must_use].
pub must_use: Option<String>,
/// Whether to generate destructors of tagged enums.
pub derive_tagged_enum_destructor: bool,
/// Whether to generate copy-constructors of tagged enums.
pub derive_tagged_enum_copy_constructor: bool,
/// Whether to generate copy-assignment operators of tagged enums.
///
/// This is only generated if a copy constructor for the same tagged enum is
/// generated as well.
pub derive_tagged_enum_copy_assignment: bool,
/// Whether to generate a ostream serializer for the struct
pub derive_ostream: bool,
/// Declare the enum as an enum class.
/// Only relevant when targeting C++.
pub enum_class: bool,
/// Whether to generate empty, private default-constructors for tagged
/// enums.
pub private_default_tagged_enum_constructor: bool,
}
impl Default for EnumConfig {
fn default() -> EnumConfig {
EnumConfig {
rename_variants: RenameRule::None,
rename_variant_name_fields: RenameRule::SnakeCase,
add_sentinel: false,
prefix_with_name: false,
derive_helper_methods: false,
derive_const_casts: false,
derive_mut_casts: false,
cast_assert_name: None,
must_use: None,
derive_tagged_enum_destructor: false,
derive_tagged_enum_copy_constructor: false,
derive_tagged_enum_copy_assignment: false,
derive_ostream: false,
enum_class: true,
private_default_tagged_enum_constructor: false,
}
}
}
impl EnumConfig {
pub(crate) fn add_sentinel(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("add-sentinel") {
return x;
}
self.add_sentinel
}
pub(crate) fn derive_helper_methods(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-helper-methods") {
return x;
}
self.derive_helper_methods
}
pub(crate) fn derive_const_casts(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-const-casts") {
return x;
}
self.derive_const_casts
}
pub(crate) fn derive_mut_casts(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-mut-casts") {
return x;
}
self.derive_mut_casts
}
pub(crate) fn derive_tagged_enum_destructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-destructor") {
return x;
}
self.derive_tagged_enum_destructor
}
pub(crate) fn derive_tagged_enum_copy_constructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-copy-constructor") {
return x;
}
self.derive_tagged_enum_copy_constructor
}
pub(crate) fn derive_tagged_enum_copy_assignment(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-copy-assignment") {
return x;
}
self.derive_tagged_enum_copy_assignment
}
pub(crate) fn derive_ostream(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-ostream") {
return x;
}
self.derive_ostream
}
pub(crate) fn enum_class(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("enum-class") {
return x;
}
self.enum_class
}
pub(crate) fn private_default_tagged_enum_constructor(
&self,
annotations: &AnnotationSet,
) -> bool {
if let Some(x) = annotations.bool("private-default-tagged-enum-constructor") {
return x;
}
self.private_default_tagged_enum_constructor
}
}
/// Settings to apply to generated constants.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct ConstantConfig {
/// Whether a generated constant can be a static const in C++ mode.
pub allow_static_const: bool,
/// Whether a generated constant should be constexpr in C++ mode.
pub allow_constexpr: bool,
/// Sort key for constants
pub sort_by: Option<SortKey>,
}
impl Default for ConstantConfig {
fn default() -> ConstantConfig {
ConstantConfig {
allow_static_const: true,
allow_constexpr: true,
sort_by: None,
}
}
}
/// Settings for custom macro expansion.
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct MacroExpansionConfig {
/// Whether the `bitflags` macro should be expanded.
pub bitflags: bool,
}
/// Controls which Cargo profile is used for macro expansion.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Profile {
Debug,
Release,
}
impl FromStr for Profile {
type Err = String;
fn from_str(s: &str) -> Result<Profile, Self::Err> {
match s {
"debug" | "Debug" => Ok(Profile::Debug),
"release" | "Release" => Ok(Profile::Release),
_ => Err(format!("Unrecognized Profile: '{}'.", s)),
}
}
}
deserialize_enum_str!(Profile);
/// Settings to apply when running `rustc -Zunpretty=expanded`
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct ParseExpandConfig {
/// The names of crates to parse with `rustc -Zunpretty=expanded`
pub crates: Vec<String>,
/// Whether to enable all the features when expanding.
pub all_features: bool,
/// Whether to use the default feature set when expanding.
pub default_features: bool,
/// List of features to use when expanding. Combines with `default_features` like in
/// `Cargo.toml`.
pub features: Option<Vec<String>>,
/// Controls whether or not to pass `--release` when expanding.
pub profile: Profile,
}
impl Default for ParseExpandConfig {
fn default() -> ParseExpandConfig {
ParseExpandConfig {
crates: Vec::new(),
all_features: false,
default_features: true,
features: None,
profile: Profile::Debug,
}
}
}
// Backwards-compatibility deserializer for ParseExpandConfig. This allows accepting both the
// simple `expand = ["crate"]` and the more complex `expand = {"crates": ["crate"],
// "default_features": false}` format for the `expand` key.
//
// Note that one (major) difference between the two forms is that, for backwards-compatibility
// reasons, the `expand = ["crate"]` form will enable the `--all-features` flag by default while
// the `expand = {"crates": ["crate"]}` form will use the default feature set by default.
fn retrocomp_parse_expand_config_deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<ParseExpandConfig, D::Error> {
struct ParseExpandVisitor;
impl<'de> Visitor<'de> for ParseExpandVisitor {
type Value = ParseExpandConfig;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a map or sequence of string")
}
fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
let crates =
<Vec<String> as Deserialize>::deserialize(SeqAccessDeserializer::new(seq))?;
Ok(ParseExpandConfig {
crates,
all_features: true,
default_features: true,
features: None,
profile: Profile::Debug,
})
}
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
<ParseExpandConfig as Deserialize>::deserialize(MapAccessDeserializer::new(map))
}
}
deserializer.deserialize_any(ParseExpandVisitor)
}
/// Settings to apply when parsing.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct ParseConfig {
/// Whether to parse dependencies when generating bindings. When this is true,
/// each dependent crate is found using a combination of `cargo metadata` and
/// `Cargo.lock`. To further control this behavior, crates can be whitelisted or
/// blacklisted using `include` and `exclude` respectively. Additionally in cases
/// where crates have types to expose in bindings hidden in macros, a crate can
/// be marked in `expand` and `cargo expand` will be used to expand the macros
/// before parsing. A crate marked in `expand` doesn't need to be added to any
/// whitelist.
pub parse_deps: bool,
/// An optional whitelist of names of crates to parse
pub include: Option<Vec<String>>,
/// The names of crates to not parse
pub exclude: Vec<String>,
/// The configuration options for `rustc -Zunpretty=expanded`
#[serde(deserialize_with = "retrocomp_parse_expand_config_deserialize")]
pub expand: ParseExpandConfig,
/// Whether to use a new temporary target directory when running `rustc -Zunpretty=expanded`.
/// This may be required for some build processes.
pub clean: bool,
/// List of crate names which generate consts, statics, and fns. By default
/// no dependent crates generate them.
pub extra_bindings: Vec<String>,
}
impl ParseConfig {
pub(crate) fn should_generate_top_level_item(
&self,
crate_name: &str,
binding_crate_name: &str,
) -> bool {
if crate_name == binding_crate_name {
// Always generate items for the binding crate.
return true;
}
self.extra_bindings.iter().any(|dep| dep == crate_name)
}
}
/// Settings to apply to pointers
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct PtrConfig {
/// Optional attribute to apply to pointers that are required to not be null
pub non_null_attribute: Option<String>,
}
/// Settings specific to Cython bindings.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct CythonConfig {
/// Header specified in the top level `cdef extern from header:` declaration.
pub header: Option<String>,
/// `from module cimport name1, name2, ...` declarations added in the same place
/// where you'd get includes in C.
pub cimports: BTreeMap<String, Vec<String>>,
}
/// A collection of settings to customize the generated bindings.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct Config {
/// Optional text to output at the beginning of the file
pub header: Option<String>,
/// A list of additional includes to put at the beginning of the generated header
pub includes: Vec<String>,
/// A list of additional system includes to put at the beginning of the generated header
pub sys_includes: Vec<String>,
/// Optional verbatim code added after the include blocks
pub after_includes: Option<String>,
/// Optional text to output at the end of the file
pub trailer: Option<String>,
/// Optional name to use for an include guard
pub include_guard: Option<String>,
/// Add a `#pragma once` guard
pub pragma_once: bool,
/// Generates no includes at all. Overrides all other include options
///
/// This option is useful when using cbindgen with tools such as python's cffi which
/// doesn't understand include directives
pub no_includes: bool,
/// Optional text to output at major sections to deter manual editing
pub autogen_warning: Option<String>,
/// Include a comment with the version of cbindgen used to generate the file
pub include_version: bool,
/// An optional name for the root namespace. Only applicable when language="C++"
pub namespace: Option<String>,
/// An optional list of namespaces. Only applicable when language="C++"
pub namespaces: Option<Vec<String>>,
/// An optional list of namespaces to declare as using. Only applicable when language="C++"
pub using_namespaces: Option<Vec<String>>,
/// The style to use for braces
pub braces: Braces,
/// The preferred length of a line, used for auto breaking function arguments
pub line_length: usize,
/// The amount of spaces in a tab
pub tab_width: usize,
/// The type of line endings to generate
pub line_endings: LineEndingStyle,
/// The language to output bindings for
pub language: Language,
/// Include preprocessor defines in C bindings to ensure C++ compatibility
pub cpp_compat: bool,
/// The style to declare structs, enums and unions in for C
pub style: Style,
/// Default sort key for functions and constants.
pub sort_by: SortKey,
/// If this option is true `usize` and `isize` will be converted into `size_t` and `ptrdiff_t`
/// instead of `uintptr_t` and `intptr_t` respectively.
pub usize_is_size_t: bool,
/// The configuration options for parsing
pub parse: ParseConfig,
/// The configuration options for exporting
pub export: ExportConfig,
/// The configuration options for macros.
pub macro_expansion: MacroExpansionConfig,
/// The configuration options for type layouts.
pub layout: LayoutConfig,
/// The configuration options for functions
#[serde(rename = "fn")]
pub function: FunctionConfig,
/// The configuration options for structs
#[serde(rename = "struct")]
pub structure: StructConfig,
/// The configuration options for enums
#[serde(rename = "enum")]
pub enumeration: EnumConfig,
/// The configuration options for constants
#[serde(rename = "const")]
pub constant: ConstantConfig,
/// Preprocessor defines to use when generating #ifdef's for #[cfg]
pub defines: HashMap<String, String>,
/// Include doc comments from Rust as documentation
pub documentation: bool,
/// How documentation comments should be styled.
pub documentation_style: DocumentationStyle,
/// How much of the documentation should be output for each item.
pub documentation_length: DocumentationLength,
/// Configuration options for pointers
#[serde(rename = "ptr")]
pub pointer: PtrConfig,
/// Only download sources for dependencies needed for the target platform.
///
/// By default, cbindgen will fetch sources for dependencies used on any platform so that if a
/// type is defined in terms of a type from a dependency on another target (probably behind a
/// `#[cfg]`), cbindgen will be able to generate the appropriate binding as it can see the
/// nested type's definition. However, this makes calling cbindgen slower, as it may have to
/// download a number of additional dependencies.
///
/// As an example, consider this Cargo.toml:
///
/// ```toml
/// [target.'cfg(windows)'.dependencies]
/// windows = "0.7"
/// ```
///
/// with this declaration in one of the `.rs` files that cbindgen is asked to generate bindings
/// for:
///
/// ```rust,ignore
/// #[cfg(windows)]
/// pub struct Error(windows::ErrorCode);
/// ```
///
/// With the default value (`false`), cbindgen will download the `windows` dependency even when
/// not compiling for Windows, and will thus be able to generate the binding for `Error`
/// (behind a `#define`).
///
/// If this value is instead to `true`, cbindgen will _not_ download the `windows` dependency
/// if it's not compiling for Windows, but will also fail to generate a Windows binding for
/// `Error` as it does not know the definition for `ErrorCode`.
///
/// The target can be chosen via the `TARGET` environment variable (if used
/// via the CLI, when ran from a build script cargo sets this variable
/// appropriately).
pub only_target_dependencies: bool,
/// Configuration options specific to Cython.
pub cython: CythonConfig,
}
impl Default for Config {
fn default() -> Config {
Config {
header: None,
includes: Vec::new(),
sys_includes: Vec::new(),
after_includes: None,
trailer: None,
include_guard: None,
pragma_once: false,
autogen_warning: None,
include_version: false,
no_includes: false,
namespace: None,
namespaces: None,
using_namespaces: None,
braces: Braces::SameLine,
line_length: 100,
tab_width: 2,
line_endings: LineEndingStyle::default(),
language: Language::Cxx,
cpp_compat: false,
style: Style::default(),
usize_is_size_t: false,
sort_by: SortKey::None,
macro_expansion: Default::default(),
parse: ParseConfig::default(),
export: ExportConfig::default(),
layout: LayoutConfig::default(),
function: FunctionConfig::default(),
structure: StructConfig::default(),
enumeration: EnumConfig::default(),
constant: ConstantConfig::default(),
defines: HashMap::new(),
documentation: true,
documentation_style: DocumentationStyle::Auto,
documentation_length: DocumentationLength::Full,
pointer: PtrConfig::default(),
only_target_dependencies: false,
cython: CythonConfig::default(),
}
}
}
impl Config {
pub(crate) fn cpp_compatible_c(&self) -> bool {
self.language == Language::C && self.cpp_compat
}
pub(crate) fn include_guard(&self) -> Option<&str> {
if self.language == Language::Cython {
None
} else {
self.include_guard.as_deref()
}
}
pub(crate) fn includes(&self) -> &[String] {
if self.language == Language::Cython {
&[]
} else {
&self.includes
}
}
pub(crate) fn sys_includes(&self) -> &[String] {
if self.language == Language::Cython {
&[]
} else {
&self.sys_includes
}
}
pub fn from_file<P: AsRef<StdPath>>(file_name: P) -> Result<Config, String> {
let config_text = fs::read_to_string(file_name.as_ref()).map_err(|_| {
format!(
"Couldn't open config file: {}.",
file_name.as_ref().display()
)
})?;
match toml::from_str::<Config>(&config_text) {
Ok(x) => Ok(x),
Err(e) => Err(format!("Couldn't parse config file: {}.", e)),
}
}
pub fn from_root_or_default<P: AsRef<StdPath>>(root: P) -> Config {
let c = root.as_ref().join("cbindgen.toml");
if c.exists() {
Config::from_file(c).unwrap()
} else {
Config::default()
}
}
}