From 0aa6ece5fd75ecd6a7880eedeea21cf68894fbf5 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 8 Jan 2025 14:21:27 -0600 Subject: [PATCH] config --- crates/libs/bindgen/src/config.rs | 38 +++++++++++++++++++ crates/libs/bindgen/src/derive_writer.rs | 4 +- crates/libs/bindgen/src/lib.rs | 31 +++------------ crates/libs/bindgen/src/type_map.rs | 6 +-- crates/libs/bindgen/src/type_tree.rs | 4 +- crates/libs/bindgen/src/types/class.rs | 6 +-- crates/libs/bindgen/src/types/cpp_const.rs | 6 +-- crates/libs/bindgen/src/types/cpp_delegate.rs | 2 +- crates/libs/bindgen/src/types/cpp_enum.rs | 8 ++-- crates/libs/bindgen/src/types/cpp_fn.rs | 16 ++++---- .../libs/bindgen/src/types/cpp_interface.rs | 28 +++++++------- crates/libs/bindgen/src/types/cpp_struct.rs | 14 +++---- crates/libs/bindgen/src/types/delegate.rs | 2 +- crates/libs/bindgen/src/types/enum.rs | 8 ++-- crates/libs/bindgen/src/types/interface.rs | 34 ++++++++--------- crates/libs/bindgen/src/types/mod.rs | 16 ++++---- crates/libs/bindgen/src/types/struct.rs | 6 +-- crates/libs/bindgen/src/writer/cpp_handle.rs | 2 +- crates/libs/bindgen/src/writer/format.rs | 8 ++-- crates/libs/bindgen/src/writer/mod.rs | 15 ++++---- crates/libs/bindgen/src/writer/names.rs | 12 +++--- 21 files changed, 142 insertions(+), 124 deletions(-) create mode 100644 crates/libs/bindgen/src/config.rs diff --git a/crates/libs/bindgen/src/config.rs b/crates/libs/bindgen/src/config.rs new file mode 100644 index 0000000000..b910095954 --- /dev/null +++ b/crates/libs/bindgen/src/config.rs @@ -0,0 +1,38 @@ +use super::*; + +use std::sync::atomic::{AtomicPtr, Ordering}; + +static CONFIG: AtomicPtr = AtomicPtr::new(std::ptr::null_mut()); + +pub fn config() -> &'static Config { + let ptr = CONFIG.load(Ordering::Relaxed); + + if ptr.is_null() { + panic!(); + } else { + unsafe { &*ptr } + } +} + +pub struct Config { + pub types: TypeMap, + pub references: References, + pub output: String, + pub flat: bool, + pub no_allow: bool, + pub no_comment: bool, + pub no_core: bool, + pub no_toml: bool, + pub package: bool, + pub rustfmt: String, + pub sys: bool, + pub implement: bool, + pub derive: Derive, +} + +impl Config { + pub fn init(value: Self) { + let config = Box::leak(Box::new(value)); + CONFIG.store(config, Ordering::Relaxed); + } +} diff --git a/crates/libs/bindgen/src/derive_writer.rs b/crates/libs/bindgen/src/derive_writer.rs index 63118f7b05..bc15f6884d 100644 --- a/crates/libs/bindgen/src/derive_writer.rs +++ b/crates/libs/bindgen/src/derive_writer.rs @@ -3,9 +3,9 @@ use super::*; pub struct DeriveWriter(BTreeSet); impl DeriveWriter { - pub fn new(writer: &Writer, type_name: TypeName) -> Self { + pub fn new(type_name: TypeName) -> Self { let mut derive = BTreeSet::new(); - derive.extend(writer.config.derive.get(type_name)); + derive.extend(config().derive.get(type_name)); Self(derive) } diff --git a/crates/libs/bindgen/src/lib.rs b/crates/libs/bindgen/src/lib.rs index 1c65fa3602..ffeb934477 100644 --- a/crates/libs/bindgen/src/lib.rs +++ b/crates/libs/bindgen/src/lib.rs @@ -6,6 +6,7 @@ clippy::needless_doctest_main )] +mod config; mod derive; mod derive_writer; mod filter; @@ -24,6 +25,7 @@ mod value; mod winmd; mod writer; +use config::*; use derive::*; use derive_writer::*; use filter::*; @@ -47,22 +49,6 @@ use writer::*; mod method_names; use method_names::*; -struct Config { - pub types: TypeMap, - pub references: References, - pub output: String, - pub flat: bool, - pub no_allow: bool, - pub no_comment: bool, - pub no_core: bool, - pub no_toml: bool, - pub package: bool, - pub rustfmt: String, - pub sys: bool, - pub implement: bool, - pub derive: Derive, -} - /// The Windows code generator. #[track_caller] pub fn bindgen(args: I) @@ -165,7 +151,7 @@ where let types = TypeMap::filter(&filter, &references); let derive = Derive::new(&types, &derive); - let config = Box::leak(Box::new(Config { + Config::init(Config { types, flat, references, @@ -179,16 +165,11 @@ where output, sys, implement, - })); + }); - let tree = TypeTree::new(&config.types); - - let writer = Writer { - config, - namespace: "", - }; + let writer = Writer { namespace: "" }; - writer.write(tree) + writer.write(TypeTree::new()) } enum ArgKind { diff --git a/crates/libs/bindgen/src/type_map.rs b/crates/libs/bindgen/src/type_map.rs index c2c8b279ac..87279d4c92 100644 --- a/crates/libs/bindgen/src/type_map.rs +++ b/crates/libs/bindgen/src/type_map.rs @@ -82,7 +82,7 @@ impl TypeMap { ) } - pub fn included(&self, config: &Config) -> bool { + pub fn included(&self) -> bool { self.0.iter().all(|(tn, _)| { // An empty namespace covers core types like `HRESULT`. This way we don't exclude methods // that depend on core types that aren't explicitly included in the filter. @@ -90,11 +90,11 @@ impl TypeMap { return true; } - if config.types.contains_key(tn) { + if config().types.contains_key(tn) { return true; } - if config.references.contains(*tn).is_some() { + if config().references.contains(*tn).is_some() { return true; } diff --git a/crates/libs/bindgen/src/type_tree.rs b/crates/libs/bindgen/src/type_tree.rs index 4d7b6f2812..18b552cf6d 100644 --- a/crates/libs/bindgen/src/type_tree.rs +++ b/crates/libs/bindgen/src/type_tree.rs @@ -9,10 +9,10 @@ pub struct TypeTree { } impl TypeTree { - pub fn new(dependencies: &TypeMap) -> Self { + pub fn new() -> Self { let mut tree = Self::with_namespace(""); - for (tn, types) in dependencies.iter() { + for (tn, types) in config().types.iter() { let tree = tree.insert_namespace(tn.namespace()); types.iter().for_each(|ty| { tree.types.insert(ty.clone()); diff --git a/crates/libs/bindgen/src/types/class.rs b/crates/libs/bindgen/src/types/class.rs index fa9ce56303..94a03fbb0c 100644 --- a/crates/libs/bindgen/src/types/class.rs +++ b/crates/libs/bindgen/src/types/class.rs @@ -17,7 +17,7 @@ impl Class { let name = to_ident(type_name.name()); let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } @@ -38,7 +38,7 @@ impl Class { let mut virtual_names = MethodNames::new(); for method in interface - .get_methods(writer) + .get_methods() .iter() .filter_map(|method| match &method { MethodOrName::Method(method) => Some(method), @@ -47,7 +47,7 @@ impl Class { { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } diff --git a/crates/libs/bindgen/src/types/cpp_const.rs b/crates/libs/bindgen/src/types/cpp_const.rs index 39782aa1f3..4ad6de2f0f 100644 --- a/crates/libs/bindgen/src/types/cpp_const.rs +++ b/crates/libs/bindgen/src/types/cpp_const.rs @@ -38,7 +38,7 @@ impl CppConst { let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } @@ -52,7 +52,7 @@ impl CppConst { let crate_name = writer.write_core(); let value = constant.value().write(); - // TODO: if writer.config.no_core then write these literals out as byte strings? + // TODO: if config().no_core then write these literals out as byte strings? if is_ansi_encoding(self.field) { quote! { #cfg @@ -94,7 +94,7 @@ impl CppConst { value = quote! { #value as _ }; } - if writer.config.sys || field_ty == Type::Bool { + if config().sys || field_ty == Type::Bool { quote! { #cfg pub const #name: #ty = #value; diff --git a/crates/libs/bindgen/src/types/cpp_delegate.rs b/crates/libs/bindgen/src/types/cpp_delegate.rs index c2f4cd6a89..f89e2f68c4 100644 --- a/crates/libs/bindgen/src/types/cpp_delegate.rs +++ b/crates/libs/bindgen/src/types/cpp_delegate.rs @@ -49,7 +49,7 @@ impl CppDelegate { let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } diff --git a/crates/libs/bindgen/src/types/cpp_enum.rs b/crates/libs/bindgen/src/types/cpp_enum.rs index 59f1fcdc33..25c35ebbfc 100644 --- a/crates/libs/bindgen/src/types/cpp_enum.rs +++ b/crates/libs/bindgen/src/types/cpp_enum.rs @@ -30,17 +30,17 @@ impl CppEnum { let tn = self.def.type_name(); let is_scoped = self.def.has_attribute("ScopedEnumAttribute"); - if !is_scoped && writer.config.sys { + if !is_scoped && config().sys { return writer.write_cpp_handle(self.def); } let name = to_ident(tn.name()); let underlying_type = self.def.underlying_type().write_name(writer); - let mut derive = DeriveWriter::new(writer, tn); + let mut derive = DeriveWriter::new(tn); derive.extend(["Copy", "Clone"]); - if !writer.config.sys { + if !config().sys { derive.extend(["Default", "Debug", "PartialEq", "Eq"]); } @@ -67,7 +67,7 @@ impl CppEnum { quote! {} }; - let flags = if writer.config.sys || !self.def.has_attribute("FlagsAttribute") { + let flags = if config().sys || !self.def.has_attribute("FlagsAttribute") { quote! {} } else { quote! { diff --git a/crates/libs/bindgen/src/types/cpp_fn.rs b/crates/libs/bindgen/src/types/cpp_fn.rs index bad1cfad33..a4d7ceeb8c 100644 --- a/crates/libs/bindgen/src/types/cpp_fn.rs +++ b/crates/libs/bindgen/src/types/cpp_fn.rs @@ -62,12 +62,12 @@ impl CppFn { let return_sig = writer.write_return_sig(self.method, &signature, underlying_types); - let vararg = - if writer.config.sys && signature.call_flags.contains(MethodCallAttributes::VARARG) { - quote! { , ... } - } else { - quote! {} - }; + let vararg = if config().sys && signature.call_flags.contains(MethodCallAttributes::VARARG) + { + quote! { , ... } + } else { + quote! {} + }; link_fmt(quote! { windows_targets::link!(#library #abi #symbol fn #name(#(#params),* #vararg) #return_sig); @@ -79,14 +79,14 @@ impl CppFn { let signature = self.method.signature(self.namespace, &[]); let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } let link = self.write_link(writer, false); let cfg = writer.write_cfg(self.method, self.namespace, &dependencies, false); let window_long = self.write_window_long(); - if writer.config.sys { + if config().sys { return quote! { #cfg #link diff --git a/crates/libs/bindgen/src/types/cpp_interface.rs b/crates/libs/bindgen/src/types/cpp_interface.rs index 8a4427df04..1d2672d718 100644 --- a/crates/libs/bindgen/src/types/cpp_interface.rs +++ b/crates/libs/bindgen/src/types/cpp_interface.rs @@ -28,14 +28,14 @@ impl CppInterface { self.def.type_name() } - pub fn get_methods(&self, writer: &Writer) -> Vec { + pub fn get_methods(&self) -> Vec { let namespace = self.def.namespace(); self.def .methods() .map(|def| { let method = CppMethod::new(def, namespace); - if method.dependencies.included(writer.config) { + if method.dependencies.included() { CppMethodOrName::Method(method) } else { CppMethodOrName::Name(method.def.name()) @@ -45,14 +45,14 @@ impl CppInterface { } pub fn write(&self, writer: &Writer) -> TokenStream { - let methods = self.get_methods(writer); + let methods = self.get_methods(); let base_interfaces = self.base_interfaces(); let has_unknown_base = matches!(base_interfaces.first(), Some(Type::IUnknown)); let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } @@ -82,7 +82,7 @@ impl CppInterface { CppMethodOrName::Method(method) => { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } @@ -122,10 +122,10 @@ impl CppInterface { } }; - if writer.config.sys { + if config().sys { let mut result = quote! {}; - if !writer.config.package { + if !config().package { if has_unknown_base { if let Some(guid) = self.def.guid_attribute() { let name: TokenStream = format!("IID_{}", self.def.name()).into(); @@ -193,7 +193,7 @@ impl CppInterface { }) { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } @@ -220,19 +220,19 @@ impl CppInterface { let impl_name: TokenStream = format!("{}_Impl", self.def.name()).into(); - if writer.config.package { - fn collect(interface: &CppInterface, dependencies: &mut TypeMap, writer: &Writer) { - for method in interface.get_methods(writer).iter() { + if config().package { + fn collect(interface: &CppInterface, dependencies: &mut TypeMap) { + for method in interface.get_methods().iter() { if let CppMethodOrName::Method(method) = method { dependencies.combine(&method.dependencies); } } } - collect(self, &mut dependencies, writer); + collect(self, &mut dependencies); base_interfaces.iter().for_each(|interface| { if let Type::CppInterface(ty) = interface { - collect(ty, &mut dependencies, writer); + collect(ty, &mut dependencies); } }); } @@ -404,7 +404,7 @@ impl CppInterface { } pub fn write_name(&self, writer: &Writer) -> TokenStream { - if writer.config.sys { + if config().sys { quote! { *mut core::ffi::c_void } } else { self.type_name().write(writer, &[]) diff --git a/crates/libs/bindgen/src/types/cpp_struct.rs b/crates/libs/bindgen/src/types/cpp_struct.rs index 3939fe3630..fbaa061463 100644 --- a/crates/libs/bindgen/src/types/cpp_struct.rs +++ b/crates/libs/bindgen/src/types/cpp_struct.rs @@ -59,7 +59,7 @@ impl CppStruct { let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } @@ -87,10 +87,10 @@ impl CppStruct { let fields = fields.iter().map(|(name, ty)| { let name = to_ident(name); - let ty = if !writer.config.sys && is_union && !ty.is_copyable() { + let ty = if !config().sys && is_union && !ty.is_copyable() { let ty = ty.write_default(writer); quote! { core::mem::ManuallyDrop<#ty> } - } else if !writer.config.sys && ty.is_dropped() { + } else if !config().sys && ty.is_dropped() { if let Type::ArrayFixed(ty, len) = ty { let ty = ty.write_default(writer); let len = Literal::usize_unsuffixed(*len); @@ -119,10 +119,10 @@ impl CppStruct { } }; - let mut derive = DeriveWriter::new(writer, self.type_name()); + let mut derive = DeriveWriter::new(self.type_name()); let mut manual_clone = None; - if writer.config.sys || is_copyable { + if config().sys || is_copyable { derive.extend(["Clone", "Copy"]); } else if !matches!( TypeName(self.def.namespace(), self.def.name()), @@ -142,11 +142,11 @@ impl CppStruct { } } - if !writer.config.sys && !has_explicit_layout && !has_packing { + if !config().sys && !has_explicit_layout && !has_packing { derive.extend(["Debug", "PartialEq"]); } - let default = if writer.config.sys { + let default = if config().sys { quote! {} } else { quote! { diff --git a/crates/libs/bindgen/src/types/delegate.rs b/crates/libs/bindgen/src/types/delegate.rs index ebf6c3929a..8639c4d470 100644 --- a/crates/libs/bindgen/src/types/delegate.rs +++ b/crates/libs/bindgen/src/types/delegate.rs @@ -25,7 +25,7 @@ impl Delegate { let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } diff --git a/crates/libs/bindgen/src/types/enum.rs b/crates/libs/bindgen/src/types/enum.rs index 423931d163..ab02dc9af5 100644 --- a/crates/libs/bindgen/src/types/enum.rs +++ b/crates/libs/bindgen/src/types/enum.rs @@ -18,10 +18,10 @@ impl Enum { let name = to_ident(self.def.name()); let underlying_type = self.def.underlying_type(); - let mut derive = DeriveWriter::new(writer, self.type_name()); + let mut derive = DeriveWriter::new(self.type_name()); derive.extend(["Copy", "Clone"]); - if !writer.config.sys { + if !config().sys { derive.extend(["Default", "Debug", "PartialEq", "Eq"]); } @@ -38,7 +38,7 @@ impl Enum { } }); - let flags = if writer.config.sys || underlying_type != Type::U32 { + let flags = if config().sys || underlying_type != Type::U32 { quote! {} } else { quote! { @@ -81,7 +81,7 @@ impl Enum { let underlying_type = underlying_type.write_name(writer); - let win_traits = if writer.config.sys { + let win_traits = if config().sys { quote! {} } else { let signature = Literal::byte_string(&self.runtime_signature()); diff --git a/crates/libs/bindgen/src/types/interface.rs b/crates/libs/bindgen/src/types/interface.rs index c5243cdea5..133c2e624d 100644 --- a/crates/libs/bindgen/src/types/interface.rs +++ b/crates/libs/bindgen/src/types/interface.rs @@ -53,12 +53,12 @@ impl Interface { self.def.type_name() } - pub fn get_methods(&self, writer: &Writer) -> Vec { + pub fn get_methods(&self) -> Vec { self.def .methods() .map(|def| { let method = Method::new(def, &self.generics); - if method.dependencies.included(writer.config) { + if method.dependencies.included() { MethodOrName::Method(method) } else { MethodOrName::Name(method.def.name()) @@ -69,7 +69,7 @@ impl Interface { pub fn write(&self, writer: &Writer) -> TokenStream { let type_name = self.def.type_name(); - let methods = self.get_methods(writer); + let methods = self.get_methods(); let mut required_interfaces = self.required_interfaces(); required_interfaces.sort(); @@ -84,7 +84,7 @@ impl Interface { let mut dependencies = TypeMap::new(); - if writer.config.package { + if config().package { self.dependencies(&mut dependencies); } @@ -98,7 +98,7 @@ impl Interface { MethodOrName::Method(method) => { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } @@ -139,10 +139,10 @@ impl Interface { } }; - if writer.config.sys { + if config().sys { let mut result = quote! {}; - if !writer.config.package { + if !config().package { if let Some(guid) = self.def.guid_attribute() { let name: TokenStream = format!("IID_{}", self.def.name()).into(); result.combine(writer.write_cpp_const_guid(name, &guid)); @@ -232,7 +232,7 @@ impl Interface { }) { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } @@ -257,7 +257,7 @@ impl Interface { for method in interface - .get_methods(writer) + .get_methods() .iter() .filter_map(|method| match &method { MethodOrName::Method(method) => Some(method), @@ -266,7 +266,7 @@ impl Interface { { let mut difference = TypeMap::new(); - if writer.config.package { + if config().package { difference = method.dependencies.difference(&dependencies); } @@ -340,7 +340,7 @@ impl Interface { } } - if writer.config.implement || !is_exclusive { + if config().implement || !is_exclusive { let impl_name: TokenStream = format!("{}_Impl", self.def.name()).into(); let generics: Vec<_> = self @@ -351,19 +351,19 @@ impl Interface { let runtime_name = format!("{type_name}"); - if writer.config.package { - fn collect(interface: &Interface, dependencies: &mut TypeMap, writer: &Writer) { - for method in interface.get_methods(writer).iter() { + if config().package { + fn collect(interface: &Interface, dependencies: &mut TypeMap) { + for method in interface.get_methods().iter() { if let MethodOrName::Method(method) = method { dependencies.combine(&method.dependencies); } } } - collect(self, &mut dependencies, writer); + collect(self, &mut dependencies); required_interfaces .iter() - .for_each(|interface| collect(interface, &mut dependencies, writer)); + .for_each(|interface| collect(interface, &mut dependencies)); } let cfg = writer.write_cfg(self.def, self.def.namespace(), &dependencies, false); @@ -509,7 +509,7 @@ impl Interface { } pub fn write_name(&self, writer: &Writer) -> TokenStream { - if writer.config.sys { + if config().sys { quote! { *mut core::ffi::c_void } } else { self.type_name().write(writer, &self.generics) diff --git a/crates/libs/bindgen/src/types/mod.rs b/crates/libs/bindgen/src/types/mod.rs index 01856b1b7c..3928604e8c 100644 --- a/crates/libs/bindgen/src/types/mod.rs +++ b/crates/libs/bindgen/src/types/mod.rs @@ -380,7 +380,7 @@ impl Type { quote! { #name BSTR } } Self::IUnknown => { - if writer.config.sys { + if config().sys { quote! { *mut core::ffi::c_void } } else { let name = writer.write_core(); @@ -400,7 +400,7 @@ impl Type { quote! { #name HSTRING } } Self::Object => { - if writer.config.sys { + if config().sys { quote! { *mut core::ffi::c_void } } else { let name = writer.write_core(); @@ -454,7 +454,7 @@ impl Type { Self::ArrayRef(ty) => ty.write_name(writer), Self::ConstRef(ty) => ty.write_name(writer), Self::PrimitiveOrEnum(primitive, ty) => { - if writer.config.sys { + if config().sys { primitive.write_name(writer) } else { ty.write_name(writer) @@ -472,7 +472,7 @@ impl Type { if matches!(self, Self::Param(_)) { quote! { <#tokens as windows_core::Type<#tokens>>::Default } - } else if self.is_interface() && !writer.config.sys { + } else if self.is_interface() && !config().sys { quote! { Option<#tokens> } } else { tokens @@ -493,7 +493,7 @@ impl Type { } pub fn write_abi(&self, writer: &Writer) -> TokenStream { - if writer.config.sys { + if config().sys { return self.write_default(writer); } @@ -837,8 +837,8 @@ impl Type { } impl Type { - fn write_no_deps(&self, writer: &Writer) -> TokenStream { - if !writer.config.no_core { + fn write_no_deps(&self) -> TokenStream { + if !config().no_core { return quote! {}; } @@ -906,7 +906,7 @@ impl Type { Self::Class(ty) => ty.write(writer), Self::CppInterface(ty) => ty.write(writer), - _ => self.write_no_deps(writer), + _ => self.write_no_deps(), } } diff --git a/crates/libs/bindgen/src/types/struct.rs b/crates/libs/bindgen/src/types/struct.rs index 157ab87e08..735c9663e4 100644 --- a/crates/libs/bindgen/src/types/struct.rs +++ b/crates/libs/bindgen/src/types/struct.rs @@ -25,14 +25,14 @@ impl Struct { let is_copyable = fields.iter().all(|(_, ty)| ty.is_copyable()); - let mut derive = DeriveWriter::new(writer, self.type_name()); + let mut derive = DeriveWriter::new(self.type_name()); derive.extend(["Clone"]); if is_copyable { derive.extend(["Copy"]); } - if !writer.config.sys { + if !config().sys { derive.extend(["Default", "Debug", "PartialEq"]); } @@ -42,7 +42,7 @@ impl Struct { quote! { pub #name: #ty, } }); - let win_traits = if writer.config.sys { + let win_traits = if config().sys { quote! {} } else { let type_kind = if is_copyable { diff --git a/crates/libs/bindgen/src/writer/cpp_handle.rs b/crates/libs/bindgen/src/writer/cpp_handle.rs index b5bd35af2c..509eaccbcb 100644 --- a/crates/libs/bindgen/src/writer/cpp_handle.rs +++ b/crates/libs/bindgen/src/writer/cpp_handle.rs @@ -7,7 +7,7 @@ impl Writer { let ty = def.underlying_type(); let ty_name = ty.write_name(self); - if self.config.sys { + if config().sys { quote! { pub type #name = #ty_name; } diff --git a/crates/libs/bindgen/src/writer/format.rs b/crates/libs/bindgen/src/writer/format.rs index 65bfa5a14e..402411845a 100644 --- a/crates/libs/bindgen/src/writer/format.rs +++ b/crates/libs/bindgen/src/writer/format.rs @@ -3,7 +3,7 @@ use std::io::Write; impl Writer { pub fn format(&self, tokens: &str) -> String { - let preamble = if self.config.no_comment { + let preamble = if config().no_comment { String::new() } else { let version = std::env!("CARGO_PKG_VERSION"); @@ -16,7 +16,7 @@ impl Writer { ) }; - let allow = if self.config.no_allow { + let allow = if config().no_allow { "" } else { "#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]\n\n" @@ -28,9 +28,9 @@ impl Writer { cmd.stdout(std::process::Stdio::piped()); cmd.stderr(std::process::Stdio::null()); - if !self.config.rustfmt.is_empty() { + if !config().rustfmt.is_empty() { cmd.arg("--config"); - cmd.arg(&self.config.rustfmt); + cmd.arg(&config().rustfmt); } let Ok(mut child) = cmd.spawn() else { diff --git a/crates/libs/bindgen/src/writer/mod.rs b/crates/libs/bindgen/src/writer/mod.rs index cb0de64b03..e146df308c 100644 --- a/crates/libs/bindgen/src/writer/mod.rs +++ b/crates/libs/bindgen/src/writer/mod.rs @@ -9,7 +9,6 @@ use rayon::prelude::*; #[derive(Clone)] pub struct Writer { - pub config: &'static Config, pub namespace: &'static str, } @@ -22,7 +21,7 @@ impl Writer { #[track_caller] pub fn write(&self, tree: TypeTree) { - if self.config.package { + if config().package { self.write_package(&tree); } else { self.write_file(tree); @@ -31,13 +30,13 @@ impl Writer { #[track_caller] fn write_file(&self, tree: TypeTree) { - let tokens = if self.config.flat { + let tokens = if config().flat { self.write_flat(tree) } else { self.write_modules(&tree) }; - write_to_file(&self.config.output, self.format(&tokens.into_string())); + write_to_file(&config().output, self.format(&tokens.into_string())); } fn write_flat(&self, tree: TypeTree) -> TokenStream { @@ -68,7 +67,7 @@ impl Writer { fn write_package(&self, tree: &TypeTree) { for name in tree.nested.keys() { - _ = std::fs::remove_dir_all(format!("{}/src/{name}", &self.config.output)); + _ = std::fs::remove_dir_all(format!("{}/src/{name}", &config().output)); } let trees = tree.flatten_trees(); @@ -76,7 +75,7 @@ impl Writer { trees.par_iter().for_each(|tree| { let directory = format!( "{}/src/{}", - &self.config.output, + &config().output, tree.namespace.replace('.', "/") ); @@ -102,11 +101,11 @@ impl Writer { write_to_file(&output, self.format(&tokens.into_string())); }); - if self.config.no_toml { + if config().no_toml { return; } - let toml_path = format!("{}/Cargo.toml", &self.config.output); + let toml_path = format!("{}/Cargo.toml", &config().output); let mut toml = String::new(); for line in read_file_lines(&toml_path) { diff --git a/crates/libs/bindgen/src/writer/names.rs b/crates/libs/bindgen/src/writer/names.rs index e222b547ff..271cd71191 100644 --- a/crates/libs/bindgen/src/writer/names.rs +++ b/crates/libs/bindgen/src/writer/names.rs @@ -2,10 +2,10 @@ use super::*; impl Writer { pub fn write_core(&self) -> TokenStream { - if self.config.sys { - if self.config.package || !self.config.no_core { + if config().sys { + if config().package || !config().no_core { quote! { windows_sys::core:: } - } else if self.config.flat { + } else if config().flat { quote! {} } else { let mut tokens = TokenStream::new(); @@ -56,10 +56,10 @@ impl Writer { } if let Some(reference) = { - if self.config.types.contains_key(&type_name) { + if config().types.contains_key(&type_name) { None } else { - self.config.references.contains(type_name) + config().references.contains(type_name) } } { tokens.push_str(&reference.name); @@ -82,7 +82,7 @@ impl Writer { tokens } else { - if self.config.flat || type_name.namespace() == self.namespace { + if config().flat || type_name.namespace() == self.namespace { return tokens; }