Skip to content

Commit

Permalink
Use dedicated arch cfg writer (#3430)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 9, 2025
1 parent aa600c0 commit c8616a4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
2 changes: 2 additions & 0 deletions crates/libs/bindgen/src/types/cpp_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ impl CppDelegate {
self.dependencies(&mut dependencies);
}

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

quote! {
#arches
#cfg
pub type #name = Option<unsafe extern "system" fn(#(#params),*) #return_sig>;
}
Expand Down
3 changes: 3 additions & 0 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ 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 = quote! { #arches #cfg };
let window_long = self.write_window_long();

if writer.config.sys {
return quote! {
#cfg
Expand Down
3 changes: 2 additions & 1 deletion crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ impl CppStruct {
self.dependencies(&mut dependencies);
}

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

fn write_with_cfg(&self, writer: &Writer, cfg: &TokenStream) -> TokenStream {
Expand Down
65 changes: 35 additions & 30 deletions crates/libs/bindgen/src/writer/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
use super::*;

pub fn write_arches<R: HasAttributes>(row: R) -> TokenStream {
let mut tokens = quote! {};

if let Some(attribute) = row.find_attribute("SupportedArchitectureAttribute") {
if let Some((_, Value::I32(value))) = attribute.args().first() {
let mut arches = BTreeSet::new();

if value & 1 == 1 {
arches.insert("x86");
}

if value & 2 == 2 {
arches.insert("x86_64");
arches.insert("arm64ec");
}

if value & 4 == 4 {
arches.insert("aarch64");
}

match arches.len() {
0 => {}
1 => tokens.combine(quote! { #[cfg(#(target_arch = #arches),*)] }),
_ => tokens.combine(quote! { #[cfg(any(#(target_arch = #arches),*))] }),
}
}
}

tokens
}

impl Writer {
pub fn write_cfg<R: HasAttributes>(
&self,
Expand All @@ -9,37 +40,9 @@ impl Writer {
not: bool,
) -> TokenStream {
let mut features = BTreeSet::new();
let mut arches = BTreeSet::new();

for attribute in row.attributes() {
match attribute.name() {
"SupportedArchitectureAttribute" => {
if let Some((_, Value::I32(value))) = attribute.args().first() {
if value & 1 == 1 {
arches.insert("x86");
}
if value & 2 == 2 {
arches.insert("x86_64");
arches.insert("arm64ec");
}
if value & 4 == 4 {
arches.insert("aarch64");
}
}
}
"DeprecatedAttribute" => {
features.insert("deprecated".to_string());
}
_ => {}
}
}

let mut tokens = quote! {};

match arches.len() {
0 => {}
1 => tokens.combine(quote! { #[cfg(#(target_arch = #arches),*)] }),
_ => tokens.combine(quote! { #[cfg(any(#(target_arch = #arches),*))] }),
if row.has_attribute("DeprecatedAttribute") {
features.insert("deprecated".to_string());
}

let mut compact: Vec<&'static str> = dependencies.keys().map(|tn| tn.namespace()).collect();
Expand Down Expand Up @@ -76,6 +79,8 @@ impl Writer {
features.insert(feature);
}

let mut tokens = quote! {};

match features.len() {
0 => {}
1 => {
Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod names;
mod value;

use super::*;
pub use cfg::*;
use rayon::prelude::*;

#[derive(Clone)]
Expand Down

0 comments on commit c8616a4

Please sign in to comment.