Skip to content

Commit 65fd4a7

Browse files
committed
Redo Cargo features
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.
1 parent f67242b commit 65fd4a7

File tree

116 files changed

+3205
-4184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+3205
-4184
lines changed

Cargo.lock

-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dispatch2/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ authors = ["Mads Marquart <mads@marquart.dk>", "Mary <mary@mary.zone>"]
1717
workspace = true
1818

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

4141
[features]
4242
default = ["std"]
43-
std = ["alloc", "bitflags/std"]
43+
std = ["alloc"]
4444
alloc = []
4545
block2 = ["dep:block2"]
4646
libc = ["dep:libc"]

crates/header-translator/src/bin/check_framework_features.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,20 @@ fn main() -> Result<(), Box<dyn Error>> {
132132
for dir in workspace_dir.join("framework-crates").read_dir().unwrap() {
133133
let dir = dir.unwrap();
134134
if dir.file_type().unwrap().is_dir() {
135+
let feature_sets = [vec!["all"]];
136+
// println!("Testing all {dir:?} features");
137+
// let features = get_features(&dir.path().join("Cargo.toml"))?;
138+
// let feature_sets = features.iter().map(|feature| {
139+
// let mut set = vec![&**feature];
140+
// if features.contains(&"objc2".to_string()) {
141+
// set.push("objc2");
142+
// }
143+
// set
144+
// });
135145
test_feature_sets(
136146
&mut success,
137147
workspace_dir,
138-
[vec!["all"]],
148+
feature_sets,
139149
dir.file_name().to_str().unwrap(),
140150
)?;
141151
}

crates/header-translator/src/cfgs.rs

+32
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,38 @@ impl PlatformCfg {
212212
}
213213
}
214214

215+
pub(crate) fn cfg_features_ln<'a, I, F>(feature_names: I) -> impl Display + 'a
216+
where
217+
I: IntoIterator<Item = F> + Clone + 'a,
218+
F: AsRef<str>,
219+
{
220+
FormatterFn(move |f| {
221+
let mut iter = feature_names.clone().into_iter().peekable();
222+
223+
if let Some(first) = iter.next() {
224+
if iter.peek().is_none() {
225+
// One feature.
226+
writeln!(f, "#[cfg(feature = {:?})]", first.as_ref())?;
227+
} else {
228+
write!(f, "#[cfg(all(")?;
229+
230+
write!(f, "feature = {:?}", first.as_ref())?;
231+
232+
for feature in iter {
233+
write!(f, ", feature = {:?}", feature.as_ref())?;
234+
}
235+
236+
write!(f, "))]")?;
237+
writeln!(f)?;
238+
}
239+
} else {
240+
// No features, no output.
241+
}
242+
243+
Ok(())
244+
})
245+
}
246+
215247
#[cfg(test)]
216248
mod tests {
217249
use super::*;

crates/header-translator/src/config.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ impl Config {
4848
}
4949

5050
pub fn library_from_crate(&self, krate: &str) -> &LibraryConfig {
51-
self.libraries
52-
.values()
53-
.find(|lib| lib.krate == krate)
54-
.unwrap_or_else(|| {
55-
error!("tried to get library config from krate {krate:?}");
56-
self.libraries
57-
.get("__builtin__")
58-
.expect("could not find builtin library")
59-
})
51+
self.try_library_from_crate(krate).unwrap_or_else(|| {
52+
error!("tried to get library config from krate {krate:?}");
53+
self.libraries
54+
.get("__builtin__")
55+
.expect("could not find builtin library")
56+
})
57+
}
58+
59+
pub fn try_library_from_crate(&self, krate: &str) -> Option<&LibraryConfig> {
60+
self.libraries.values().find(|lib| lib.krate == krate)
6061
}
6162

6263
pub fn replace_protocol_name(&self, id: ItemIdentifier) -> ItemIdentifier {

crates/header-translator/src/expr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clang::{Entity, EntityKind, EntityVisitResult, EvaluationResult};
77

88
use crate::availability::Availability;
99
use crate::context::MacroLocation;
10+
use crate::id::ItemTree;
1011
use crate::name_translation::enum_prefix;
1112
use crate::rust_type::Ty;
1213
use crate::unexposed_attr::UnexposedAttr;
@@ -274,7 +275,7 @@ impl Expr {
274275
}
275276
}
276277

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

280281
match self {
@@ -283,18 +284,17 @@ impl Expr {
283284
Self::Float(_) => {}
284285
Self::MacroInvocation { evaluated, id } => {
285286
if evaluated.is_none() {
286-
items.push(id.clone());
287+
items.push(ItemTree::from_id(id.clone()));
287288
}
288289
}
289290
Self::Enum { id, .. } => {
290-
items.push(id.clone());
291+
items.push(ItemTree::from_id(id.clone()));
291292
}
292293
Self::Const(id) => {
293-
items.push(id.clone());
294+
items.push(ItemTree::from_id(id.clone()));
294295
}
295296
Self::Var { id, ty } => {
296-
items.push(id.clone());
297-
items.extend(ty.required_items());
297+
items.push(ItemTree::new(id.clone(), ty.required_items()));
298298
}
299299
Self::Tokens(tokens) => {
300300
for token in tokens {
@@ -309,7 +309,7 @@ impl Expr {
309309
}
310310
}
311311

312-
items
312+
items.into_iter()
313313
}
314314
}
315315

0 commit comments

Comments
 (0)