Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 24 additions & 38 deletions crate_universe/src/metadata/cargo_tree_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub(crate) struct CargoTreeEntry {
}

impl CargoTreeEntry {
#[cfg(test)]
pub fn new() -> Self {
Self {
features: BTreeSet::new(),
Expand Down Expand Up @@ -374,48 +375,33 @@ impl TreeResolver {
}
}

// Collect all metadata into a mapping of crate to it's metadata per target.
// Collect all metadata into a mapping of crate to its metadata per target.
let mut result = TreeResolverMetadata::new();
for (crate_id, tree_data) in metadata.into_iter() {
let common = CargoTreeEntry {
features: tree_data
.iter()
.fold(
None,
|common: Option<BTreeSet<String>>, (_, data)| match common {
Some(common) => {
Some(common.intersection(&data.features).cloned().collect())
}
None => Some(data.features.clone()),
},
)
.unwrap_or_default(),
deps: tree_data
.iter()
.fold(
None,
|common: Option<BTreeSet<CrateId>>, (_, data)| match common {
Some(common) => {
Some(common.intersection(&data.deps).cloned().collect())
}
None => Some(data.deps.clone()),
},
)
.unwrap_or_default(),
// Determine the features and deps common to all targets
let common = if target_triples
.iter()
.all(|triple| tree_data.contains_key(triple))
{
let mut tree_data = tree_data.values();
let mut common = tree_data.next().cloned().unwrap_or_default();
for CargoTreeEntry { features, deps } in tree_data {
common.features.retain(|feat| features.contains(feat));
common.deps.retain(|dep| deps.contains(dep));
}
common
} else {
// The crate is not included on all targets, so it cannot have
// any features or deps common to all targets
CargoTreeEntry::default()
};
let mut select: Select<CargoTreeEntry> = Select::default();
for (target_triple, data) in tree_data {
let mut entry = CargoTreeEntry::new();
entry.features.extend(
data.features
.into_iter()
.filter(|f| !common.features.contains(f)),
);
entry
.deps
.extend(data.deps.into_iter().filter(|d| !common.deps.contains(d)));
if !entry.is_empty() {
select.insert(entry, Some(target_triple.to_bazel()));
for (target_triple, mut data) in tree_data {
// Filter out common features and deps to get target-specific features and deps
data.features.retain(|feat| !common.features.contains(feat));
data.deps.retain(|dep| !common.deps.contains(dep));
if !data.is_empty() {
select.insert(data, Some(target_triple.to_bazel()));
}
}
if !common.is_empty() {
Expand Down
98 changes: 61 additions & 37 deletions crate_universe/tests/cargo_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,54 +152,59 @@ fn feature_generator() {

assert_eq!(
json!({
"common": {
"deps": [
"arrayvec 0.7.2",
"bitflags 1.3.2",
"fxhash 0.2.1",
"log 0.4.17",
"naga 0.10.0",
"parking_lot 0.12.1",
"profiling 1.0.7",
"raw-window-handle 0.5.0",
"thiserror 1.0.37",
"wgpu-types 0.14.1",
],
"features": [
"default",
],
},
"selects": {
"x86_64-apple-darwin": {
"deps": [
"arrayvec 0.7.2",
"bitflags 1.3.2",
"block 0.1.6",
"core-graphics-types 0.1.1",
"foreign-types 0.3.2",
"fxhash 0.2.1",
"log 0.4.17",
"metal 0.24.0",
"naga 0.10.0",
"objc 0.2.7",
"parking_lot 0.12.1",
"profiling 1.0.7",
"raw-window-handle 0.5.0",
"thiserror 1.0.37",
"wgpu-types 0.14.1",
],
"features": [
"block",
"default",
"foreign-types",
"metal",
],
},
"x86_64-pc-windows-msvc": {
"deps": [
"arrayvec 0.7.2",
"ash 0.37.1+1.3.235",
"bit-set 0.5.3",
"bitflags 1.3.2",
"d3d12 0.5.0",
"fxhash 0.2.1",
"gpu-alloc 0.5.3",
"gpu-descriptor 0.2.3",
"libloading 0.7.4",
"log 0.4.17",
"naga 0.10.0",
"parking_lot 0.12.1",
"profiling 1.0.7",
"range-alloc 0.1.2",
"raw-window-handle 0.5.0",
"renderdoc-sys 0.7.1",
"smallvec 1.10.0",
"thiserror 1.0.37",
"wgpu-types 0.14.1",
"winapi 0.3.9",
],
"features": [
"ash",
"bit-set",
"default",
"dx11",
"dx12",
"gpu-alloc",
Expand All @@ -211,21 +216,33 @@ fn feature_generator() {
"renderdoc-sys",
"smallvec",
"vulkan",

],
},
"x86_64-unknown-linux-gnu": {
"deps": [
"arrayvec 0.7.2",
"ash 0.37.1+1.3.235",
"bitflags 1.3.2",
"fxhash 0.2.1",
"glow 0.11.2",
"gpu-alloc 0.5.3",
"gpu-descriptor 0.2.3",
"khronos-egl 4.1.0",
"libloading 0.7.4",
"log 0.4.17",
"naga 0.10.0",
"parking_lot 0.12.1",
"profiling 1.0.7",
"raw-window-handle 0.5.0",
"renderdoc-sys 0.7.1",
"smallvec 1.10.0",
"thiserror 1.0.37",
"wgpu-types 0.14.1",
],
"features": [
"ash",
"default",
"egl",
"gles",
"glow",
Expand Down Expand Up @@ -544,33 +561,38 @@ fn host_specific_build_deps() {

assert_eq!(
json!({
"common": {
"deps": [
"bitflags 2.6.0",
],
"features": [
"alloc",
"default",
"fs",
"libc-extra-traits",
"std",
"use-libc-auxv",
],
},
// Note that there is no `wasm32-unknown-unknown` or `x86_64-pc-windows-msvc` entry
// since these platforms do not depend on `rustix`. The chain breaks due to the
// conditions here: https://github.com/Stebalien/tempfile/blob/v3.11.0/Cargo.toml#L25-L33
"selects": {
"x86_64-apple-darwin": {
"deps": [
"bitflags 2.6.0",
"errno 0.3.9",
"libc 0.2.158",
],
"features": [
"alloc",
"default",
"fs",
"libc-extra-traits",
"std",
"use-libc-auxv",
],
},
"x86_64-unknown-linux-gnu": {
"deps": [
"bitflags 2.6.0",
"linux-raw-sys 0.4.14",
],
"features": [
"alloc",
"default",
"fs",
"libc-extra-traits",
"std",
"use-libc-auxv",
],
},
},
}),
Expand All @@ -579,28 +601,30 @@ fn host_specific_build_deps() {

assert_eq!(
json!({
"common": {
"deps": [
"cfg-if 1.0.0",
"fastrand 2.1.1",
"once_cell 1.19.0",
],
},
// Note that windows does not contain `rustix` and instead `windows-sys`.
// This shows correct detection of exec platform constraints.
"selects": {
"x86_64-apple-darwin": {
"deps": [
"cfg-if 1.0.0",
"fastrand 2.1.1",
"once_cell 1.19.0",
"rustix 0.38.36",
],
},
"x86_64-pc-windows-msvc": {
"deps": [
"cfg-if 1.0.0",
"fastrand 2.1.1",
"once_cell 1.19.0",
"windows-sys 0.59.0",
],
},
"x86_64-unknown-linux-gnu": {
"deps": [
"cfg-if 1.0.0",
"fastrand 2.1.1",
"once_cell 1.19.0",
"rustix 0.38.36",
],
},
Expand Down
Loading
Loading