Skip to content

Commit

Permalink
Better features
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Apr 10, 2024
1 parent 0490565 commit d91dab1
Show file tree
Hide file tree
Showing 70 changed files with 6,607 additions and 1,348 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/header-translator/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use clang::Entity;
use crate::context::Context;
use crate::display_helper::FormatterFn;
use crate::file::clean_name;
use crate::Config;

pub trait ToOptionString: fmt::Debug {
fn to_option(&self) -> Option<&str>;
Expand All @@ -33,7 +34,7 @@ impl ToOptionString for () {

#[derive(Debug, Clone)]
pub struct Location {
library: String,
pub library: String,
pub file_name: Option<String>,
}

Expand Down Expand Up @@ -62,6 +63,10 @@ impl Ord for Location {
}

impl Location {
pub fn krate<'a>(&self, config: &'a Config) -> Option<&'a str> {
Some(&config.libraries.get(&self.library)?.krate)
}

pub fn feature_name(&self) -> Option<String> {
if self.library == "System" {
None
Expand Down
38 changes: 30 additions & 8 deletions crates/header-translator/src/library.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fmt;
use std::fs;
use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;

use toml_edit::Entry;
use toml_edit::InlineTable;
use toml_edit::{value, Array, DocumentMut, Formatted, Item, Table, Value};

Expand Down Expand Up @@ -220,9 +220,9 @@ see that for related crates.", self.data.krate, self.link_name)?;
.decor_mut()
.set_suffix("\n");

for (feature, required_features) in self.cargo_features() {
for (feature, required_features) in self.cargo_features(config) {
let mut array: Array = required_features.into_iter().collect();
if !array.is_empty() {
if 1 < array.len() {
for item in array.iter_mut() {
item.decor_mut().set_prefix("\n ");
}
Expand All @@ -237,12 +237,34 @@ see that for related crates.", self.data.krate, self.link_name)?;
Ok(())
}

pub fn cargo_features(&self) -> BTreeMap<String, Vec<String>> {
pub fn cargo_features(&self, config: &Config) -> BTreeMap<String, Vec<String>> {
let mut features = BTreeMap::new();

for file_name in self.files.keys() {
// Own features
for (file_name, file) in &self.files {
let mut required_features = BTreeSet::new();
for stmt in &file.stmts {
for required_item in stmt.required_items_inner() {
if required_item.location().library != self.link_name {
if let Some(feature_name) = required_item.location().feature_name() {
if let Some(krate) = required_item.location().krate(config) {
required_features.insert(format!("{krate}/{feature_name}"));
} else {
warn!(
?required_item,
item = ?stmt.provided_item(),
"failed getting crate name",
);
}
}
}
}
}
let feature_name = format!("{}_{}", self.link_name, clean_name(file_name));
features.insert(feature_name.clone(), Vec::new());
features.insert(
feature_name.clone(),
required_features.into_iter().collect(),
);
}

let _ = features.insert(
Expand Down Expand Up @@ -349,7 +371,7 @@ impl fmt::Display for Library {
fn merge_toml_table(original: &mut Table, addition: Table) {
for (key, mut addition) in addition {
match original.entry(&key) {
Entry::Occupied(mut original) => match (original.get_mut(), addition) {
toml_edit::Entry::Occupied(mut original) => match (original.get_mut(), addition) {
(Item::Value(original), Item::Value(addition)) => {
*original = addition;
}
Expand All @@ -361,7 +383,7 @@ fn merge_toml_table(original: &mut Table, addition: Table) {
}
(original, addition) => panic!("cannot merge items {original:?} and {addition:?}"),
},
Entry::Vacant(original) => {
toml_edit::Entry::Vacant(original) => {
match &mut addition {
Item::Table(table) => {
table.set_position(usize::MAX);
Expand Down
28 changes: 28 additions & 0 deletions crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,34 @@ impl Stmt {
}
}

/// Items required for any part of the statement.
pub(crate) fn required_items_inner(&self) -> Vec<ItemIdentifier> {
let required_by_inner: Vec<_> = match self {
Self::ExternCategory {
cls_required_items,
methods,
..
} => methods
.iter()
.flat_map(|method| method.required_items())
.chain(cls_required_items.clone())
.collect(),
Self::ExternMethods { methods, .. } | Self::ProtocolDecl { methods, .. } => methods
.iter()
.flat_map(|method| method.required_items())
.collect(),
Self::EnumDecl { variants, .. } => variants
.iter()
.flat_map(|(_, _, expr)| expr.required_items())
.collect(),
_ => vec![],
};
self.required_items()
.into_iter()
.chain(required_by_inner)
.collect()
}

fn cfg_gate_ln(&self) -> impl fmt::Display + '_ {
cfg_gate_ln(self.required_items(), [self.location()])
}
Expand Down
33 changes: 28 additions & 5 deletions framework-crates/objc2-accessibility/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,36 @@ unstable-docsrs = []
objc2 = ["objc2-helpers/objc2"] # Required
block2 = ["dep:block2", "objc2-helpers/block2"]

Accessibility_AXAudiograph = []
Accessibility_AXBrailleMap = []
Accessibility_AXAudiograph = [
"objc2-foundation/Foundation_NSArray",
"objc2-foundation/Foundation_NSAttributedString",
"objc2-foundation/Foundation_NSGeometry",
"objc2-foundation/Foundation_NSObject",
"objc2-foundation/Foundation_NSString",
"objc2-foundation/Foundation_NSValue",
]
Accessibility_AXBrailleMap = [
"objc2-foundation/Foundation_NSGeometry",
"objc2-foundation/Foundation_NSObject",
]
Accessibility_AXColorUtilities = []
Accessibility_AXCustomContent = []
Accessibility_AXCustomContent = [
"objc2-foundation/Foundation_NSArray",
"objc2-foundation/Foundation_NSAttributedString",
"objc2-foundation/Foundation_NSObject",
"objc2-foundation/Foundation_NSString",
]
Accessibility_AXFoundation = []
Accessibility_AXHearingUtilities = []
Accessibility_AXSettings = []
Accessibility_AXHearingUtilities = [
"objc2-foundation/Foundation_NSArray",
"objc2-foundation/Foundation_NSNotification",
"objc2-foundation/Foundation_NSString",
"objc2-foundation/Foundation_NSUUID",
]
Accessibility_AXSettings = [
"objc2-foundation/Foundation_NSNotification",
"objc2-foundation/Foundation_NSString",
]
all = [
"Accessibility_AXAudiograph",
"Accessibility_AXBrailleMap",
Expand Down
7 changes: 4 additions & 3 deletions framework-crates/objc2-ad-services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ unstable-docsrs = []
objc2 = ["objc2-helpers/objc2"] # Required
block2 = ["dep:block2", "objc2-helpers/block2"]

AdServices_AAAttribution = []
all = [
"AdServices_AAAttribution",
AdServices_AAAttribution = [
"objc2-foundation/Foundation_NSError",
"objc2-foundation/Foundation_NSString",
]
all = ["AdServices_AAAttribution"]
6 changes: 2 additions & 4 deletions framework-crates/objc2-ad-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@ unstable-docsrs = []
objc2 = ["objc2-helpers/objc2"] # Required
block2 = ["dep:block2", "objc2-helpers/block2"]

AdSupport_ASIdentifierManager = []
all = [
"AdSupport_ASIdentifierManager",
]
AdSupport_ASIdentifierManager = ["objc2-foundation/Foundation_NSUUID"]
all = ["AdSupport_ASIdentifierManager"]
6 changes: 1 addition & 5 deletions framework-crates/objc2-app-kit/Cargo.modified.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
name = "delegate"
required-features = [
"apple",
"Foundation",
"Foundation_NSNotification",
"Foundation_NSString",
"Foundation_NSThread",
"Foundation_NSObject",
"AppKit",
"objc2-foundation/Foundation_NSThread",
"AppKit_NSResponder",
"AppKit_NSApplication",
"AppKit_NSRunningApplication",
Expand All @@ -17,10 +15,8 @@ required-features = [
name = "nspasteboard"
required-features = [
"apple",
"Foundation",
"Foundation_NSArray",
"Foundation_NSString",
"Foundation_NSObject",
"AppKit",
"AppKit_NSPasteboard",
]
Loading

0 comments on commit d91dab1

Please sign in to comment.