Skip to content

Commit

Permalink
Redo Cargo features
Browse files Browse the repository at this point in the history
Using optional dependency features (`package?/feature`) in Cargo is not
too well supported, see:
rust-lang/cargo#10801

So instead we now always enable needed features of optional
dependencies. This fixes #656.

I thought this would be a harder trade-off (e.g. I thought that we'd
have to enable a bunch of sub-dependencies for each dependency, and that
wouldn't be nice since I'd prefer to have each crate as explicitly
imported as possible), but it actually turned out to not be too bad,
only a few crates actually enable sub-crates of their dependencies, and
then it's usually `objc2-core-foundation`.

Additionally, I've cleaned up a lot of our feature handling, which:
- Fixes dependent features.
- Removes unnecessary imports.
- Fixes the last part of #640.

There are still a few errors found by `check_framework_features` (esp.
regarding the `objc2` feature), but those can be fixed later.
  • Loading branch information
madsmtm committed Jan 15, 2025
1 parent f67242b commit 65fd4a7
Show file tree
Hide file tree
Showing 116 changed files with 3,205 additions and 4,184 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/dispatch2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ authors = ["Mads Marquart <[email protected]>", "Mary <[email protected]>"]
workspace = true

[dependencies]
bitflags = { version = "2.5.0", default-features = false }
bitflags = { version = "2.5.0", default-features = false, features = ["std"] }
block2 = { path = "../block2", version = "0.5.1", default-features = false, optional = true, features = ["alloc"] }
libc = { version = "0.2.80", default-features = false, optional = true }
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, optional = true, features = ["std"] }
Expand All @@ -40,7 +40,7 @@ targets = [

[features]
default = ["std"]
std = ["alloc", "bitflags/std"]
std = ["alloc"]
alloc = []
block2 = ["dep:block2"]
libc = ["dep:libc"]
Expand Down
12 changes: 11 additions & 1 deletion crates/header-translator/src/bin/check_framework_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,20 @@ fn main() -> Result<(), Box<dyn Error>> {
for dir in workspace_dir.join("framework-crates").read_dir().unwrap() {
let dir = dir.unwrap();
if dir.file_type().unwrap().is_dir() {
let feature_sets = [vec!["all"]];
// println!("Testing all {dir:?} features");
// let features = get_features(&dir.path().join("Cargo.toml"))?;
// let feature_sets = features.iter().map(|feature| {
// let mut set = vec![&**feature];
// if features.contains(&"objc2".to_string()) {
// set.push("objc2");
// }
// set
// });
test_feature_sets(
&mut success,
workspace_dir,
[vec!["all"]],
feature_sets,
dir.file_name().to_str().unwrap(),
)?;
}
Expand Down
32 changes: 32 additions & 0 deletions crates/header-translator/src/cfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,38 @@ impl PlatformCfg {
}
}

pub(crate) fn cfg_features_ln<'a, I, F>(feature_names: I) -> impl Display + 'a
where
I: IntoIterator<Item = F> + Clone + 'a,
F: AsRef<str>,
{
FormatterFn(move |f| {
let mut iter = feature_names.clone().into_iter().peekable();

if let Some(first) = iter.next() {
if iter.peek().is_none() {
// One feature.
writeln!(f, "#[cfg(feature = {:?})]", first.as_ref())?;
} else {
write!(f, "#[cfg(all(")?;

write!(f, "feature = {:?}", first.as_ref())?;

for feature in iter {
write!(f, ", feature = {:?}", feature.as_ref())?;
}

write!(f, "))]")?;
writeln!(f)?;
}
} else {
// No features, no output.
}

Ok(())
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
19 changes: 10 additions & 9 deletions crates/header-translator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ impl Config {
}

pub fn library_from_crate(&self, krate: &str) -> &LibraryConfig {
self.libraries
.values()
.find(|lib| lib.krate == krate)
.unwrap_or_else(|| {
error!("tried to get library config from krate {krate:?}");
self.libraries
.get("__builtin__")
.expect("could not find builtin library")
})
self.try_library_from_crate(krate).unwrap_or_else(|| {
error!("tried to get library config from krate {krate:?}");
self.libraries
.get("__builtin__")
.expect("could not find builtin library")
})
}

pub fn try_library_from_crate(&self, krate: &str) -> Option<&LibraryConfig> {
self.libraries.values().find(|lib| lib.krate == krate)
}

pub fn replace_protocol_name(&self, id: ItemIdentifier) -> ItemIdentifier {
Expand Down
14 changes: 7 additions & 7 deletions crates/header-translator/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clang::{Entity, EntityKind, EntityVisitResult, EvaluationResult};

use crate::availability::Availability;
use crate::context::MacroLocation;
use crate::id::ItemTree;
use crate::name_translation::enum_prefix;
use crate::rust_type::Ty;
use crate::unexposed_attr::UnexposedAttr;
Expand Down Expand Up @@ -274,7 +275,7 @@ impl Expr {
}
}

pub(crate) fn required_items(&self) -> Vec<ItemIdentifier> {
pub(crate) fn required_items(&self) -> impl Iterator<Item = ItemTree> {
let mut items = Vec::new();

match self {
Expand All @@ -283,18 +284,17 @@ impl Expr {
Self::Float(_) => {}
Self::MacroInvocation { evaluated, id } => {
if evaluated.is_none() {
items.push(id.clone());
items.push(ItemTree::from_id(id.clone()));
}
}
Self::Enum { id, .. } => {
items.push(id.clone());
items.push(ItemTree::from_id(id.clone()));
}
Self::Const(id) => {
items.push(id.clone());
items.push(ItemTree::from_id(id.clone()));
}
Self::Var { id, ty } => {
items.push(id.clone());
items.extend(ty.required_items());
items.push(ItemTree::new(id.clone(), ty.required_items()));
}
Self::Tokens(tokens) => {
for token in tokens {
Expand All @@ -309,7 +309,7 @@ impl Expr {
}
}

items
items.into_iter()
}
}

Expand Down
Loading

0 comments on commit 65fd4a7

Please sign in to comment.