Skip to content

Commit

Permalink
Fix cfg generation (#3431)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 10, 2025
1 parent c8616a4 commit 4c832c9
Show file tree
Hide file tree
Showing 132 changed files with 4,682 additions and 7,476 deletions.
10 changes: 0 additions & 10 deletions crates/libs/bindgen/src/type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ impl TypeMap {
}
}

pub fn difference(&self, other: &Self) -> Self {
Self(
self.0
.iter()
.filter(|(tn, _)| !other.0.contains_key(tn))
.map(|(tn, ty)| (*tn, ty.clone()))
.collect(),
)
}

pub fn included(&self, config: &Config) -> bool {
self.0.iter().all(|(tn, _)| {
// An empty namespace covers core types like `HRESULT`. This way we don't exclude methods
Expand Down
38 changes: 23 additions & 15 deletions crates/libs/bindgen/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ impl Class {
self.def.type_name()
}

fn write_cfg(&self, writer: &Writer) -> (Cfg, TokenStream) {
if !writer.config.package {
return (Cfg::default(), quote! {});
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
let cfg = Cfg::new(self.def, &dependencies);
let tokens = cfg.write(writer, false);
(cfg, tokens)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let mut required_interfaces = self.required_interfaces();
required_interfaces.sort();
let type_name = self.def.type_name();
let name = to_ident(type_name.name());
let mut dependencies = TypeMap::new();

if writer.config.package {
self.dependencies(&mut dependencies);
}

let cfg = writer.write_cfg(self.def, type_name.namespace(), &dependencies, false);
let (class_cfg, cfg) = self.write_cfg(writer);
let runtime_name = format!("{type_name}");

let runtime_name = quote! {
Expand All @@ -45,13 +51,7 @@ impl Class {
_ => None,
})
{
let mut difference = TypeMap::new();

if writer.config.package {
difference = method.dependencies.difference(&dependencies);
}

let cfg = writer.write_cfg(self.def, type_name.namespace(), &difference, false);
let cfg = method.write_cfg(writer, &class_cfg, false);

let method = method.write(
writer,
Expand Down Expand Up @@ -90,7 +90,15 @@ impl Class {
} else {
let method_name = to_ident(interface.def.name());
let interface_type = interface.write_name(writer);
let cfg = quote! {};

let cfg = if writer.config.package {
let mut dependencies = TypeMap::new();
interface.dependencies(&mut dependencies);

class_cfg.difference(interface.def, &dependencies).write(writer, false)
} else {
quote! {}
};

Some(quote! {
#cfg
Expand Down
19 changes: 11 additions & 8 deletions crates/libs/bindgen/src/types/cpp_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ impl CppConst {
self.type_name().write(writer, &[])
}

pub fn write_cfg(&self, writer: &Writer) -> TokenStream {
if !writer.config.package {
return quote! {};
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
Cfg::new(self.field, &dependencies).write(writer, false)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let name = to_ident(self.field.name());

Expand All @@ -35,14 +45,7 @@ impl CppConst {
}

let field_ty = self.field.ty(None).to_const_type();

let mut dependencies = TypeMap::new();

if writer.config.package {
self.dependencies(&mut dependencies);
}

let cfg = writer.write_cfg(self.field, self.namespace, &dependencies, false);
let cfg = self.write_cfg(writer);

if let Some(constant) = self.field.constant() {
let constant_ty = constant.ty();
Expand Down
19 changes: 11 additions & 8 deletions crates/libs/bindgen/src/types/cpp_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ impl CppDelegate {
.unwrap()
}

pub fn write_cfg(&self, writer: &Writer) -> TokenStream {
if !writer.config.package {
return quote! {};
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
Cfg::new(self.def, &dependencies).write(writer, false)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let type_name = self.def.type_name();
let name = to_ident(type_name.name());
Expand All @@ -46,15 +56,8 @@ impl CppDelegate {
});

let return_sig = writer.write_return_sig(method, &signature, false);

let mut dependencies = TypeMap::new();

if writer.config.package {
self.dependencies(&mut dependencies);
}

let arches = write_arches(self.def);
let cfg = writer.write_cfg(self.def, type_name.namespace(), &dependencies, false);
let cfg = self.write_cfg(writer);

quote! {
#arches
Expand Down
12 changes: 11 additions & 1 deletion crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ impl CppFn {
})
}

pub fn write_cfg(&self, writer: &Writer) -> TokenStream {
if !writer.config.package {
return quote! {};
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
Cfg::new(self.method, &dependencies).write(writer, false)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let name = to_ident(self.method.name());
let signature = self.method.signature(self.namespace, &[]);
Expand All @@ -85,7 +95,7 @@ impl CppFn {

let link = self.write_link(writer, false);
let arches = write_arches(self.method);
let cfg = writer.write_cfg(self.method, self.namespace, &dependencies, false);
let cfg = self.write_cfg(writer);
let cfg = quote! { #arches #cfg };
let window_long = self.write_window_long();

Expand Down
58 changes: 28 additions & 30 deletions crates/libs/bindgen/src/types/cpp_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ impl CppInterface {
.collect()
}

fn write_cfg(&self, writer: &Writer) -> (Cfg, TokenStream) {
if !writer.config.package {
return (Cfg::default(), quote! {});
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
let cfg = Cfg::new(self.def, &dependencies);
let tokens = cfg.write(writer, false);
(cfg, tokens)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let methods = self.get_methods(writer);

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 {
self.dependencies(&mut dependencies);
}

let cfg = writer.write_cfg(self.def, self.def.namespace(), &dependencies, false);
let (class_cfg, cfg) = self.write_cfg(writer);
let vtbl_name = self.write_vtbl_name(writer);

let vtbl = {
Expand All @@ -80,28 +85,23 @@ impl CppInterface {

let methods = methods.iter().map(|method| match method {
CppMethodOrName::Method(method) => {
let mut difference = TypeMap::new();

if writer.config.package {
difference = method.dependencies.difference(&dependencies);
}
let method_cfg = class_cfg.difference(method.def, &method.dependencies);
let yes = method_cfg.write(writer, false);

let name = names.add(method.def);
let abi = method.write_abi(writer, false);
let cfg = writer.write_cfg(self.def, self.def.namespace(), &difference, false);

if cfg.is_empty() {
if yes.is_empty() {
quote! {
pub #name: unsafe extern "system" fn #abi,
}
} else {
let cfg_not =
writer.write_cfg(self.def, self.def.namespace(), &difference, true);
let no = method_cfg.write(writer, true);

quote! {
#cfg
#yes
pub #name: unsafe extern "system" fn #abi,
#cfg_not
#no
#name: usize,
}
}
Expand Down Expand Up @@ -191,14 +191,7 @@ impl CppInterface {
CppMethodOrName::Method(method) => Some(method),
_ => None,
}) {
let mut difference = TypeMap::new();

if writer.config.package {
difference = method.dependencies.difference(&dependencies);
}

let cfg = writer.write_cfg(self.def, self.def.namespace(), &difference, false);

let cfg = method.write_cfg(writer, &class_cfg, false);
let method = method.write(writer, method_names, virtual_names);

methods_tokens.combine(quote! {
Expand All @@ -220,7 +213,7 @@ impl CppInterface {

let impl_name: TokenStream = format!("{}_Impl", self.def.name()).into();

if writer.config.package {
let cfg = if writer.config.package {
fn collect(interface: &CppInterface, dependencies: &mut TypeMap, writer: &Writer) {
for method in interface.get_methods(writer).iter() {
if let CppMethodOrName::Method(method) = method {
Expand All @@ -229,15 +222,20 @@ impl CppInterface {
}
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);

collect(self, &mut dependencies, writer);
base_interfaces.iter().for_each(|interface| {
if let Type::CppInterface(ty) = interface {
collect(ty, &mut dependencies, writer);
}
});
}

let cfg = writer.write_cfg(self.def, self.def.namespace(), &dependencies, false);
Cfg::new(self.def, &dependencies).write(writer, false)
} else {
quote! {}
};

let mut names = MethodNames::new();

Expand Down
10 changes: 10 additions & 0 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ impl CppMethod {
}
}

pub fn write_cfg(&self, writer: &Writer, parent: &Cfg, not: bool) -> TokenStream {
if !writer.config.package {
return quote! {};
}

parent
.difference(self.def, &self.dependencies)
.write(writer, not)
}

pub fn write_generics(&self) -> TokenStream {
let mut tokens = quote! {};

Expand Down
18 changes: 11 additions & 7 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ impl CppStruct {
self.def.has_attribute("NativeTypedefAttribute")
}

pub fn write_cfg(&self, writer: &Writer) -> TokenStream {
if !writer.config.package {
return quote! {};
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
Cfg::new(self.def, &dependencies).write(writer, false)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
if self.is_handle() {
return writer.write_cpp_handle(self.def);
Expand All @@ -57,14 +67,8 @@ impl CppStruct {
}
}

let mut dependencies = TypeMap::new();

if writer.config.package {
self.dependencies(&mut dependencies);
}

let arches = write_arches(self.def);
let cfg = writer.write_cfg(self.def, self.def.namespace(), &dependencies, false);
let cfg = self.write_cfg(writer);
self.write_with_cfg(writer, &quote! { #arches #cfg })
}

Expand Down
19 changes: 11 additions & 8 deletions crates/libs/bindgen/src/types/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ impl Delegate {
self.def.type_name()
}

pub fn write_cfg(&self, writer: &Writer) -> TokenStream {
if !writer.config.package {
return quote! {};
}

let mut dependencies = TypeMap::new();
self.dependencies(&mut dependencies);
Cfg::new(self.def, &dependencies).write(writer, false)
}

pub fn write(&self, writer: &Writer) -> TokenStream {
let name = self.write_name(writer);
//let vtbl_name = self.write_vtbl_name(writer);
Expand All @@ -22,14 +32,7 @@ impl Delegate {
let constraints = writer.write_generic_constraints(&self.generics);
let named_phantoms = writer.write_generic_named_phantoms(&self.generics);
let method = self.method();

let mut dependencies = TypeMap::new();

if writer.config.package {
self.dependencies(&mut dependencies);
}

let cfg = writer.write_cfg(self.def, self.def.namespace(), &dependencies, false);
let cfg = self.write_cfg(writer);

let invoke = method.write(
writer,
Expand Down
Loading

0 comments on commit 4c832c9

Please sign in to comment.