Skip to content

Commit

Permalink
Consolidate duplicated targeted dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
sayrer committed Jun 26, 2022
1 parent e655d34 commit f97d93c
Show file tree
Hide file tree
Showing 5 changed files with 1,400 additions and 6 deletions.
1 change: 0 additions & 1 deletion impl/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ impl CrateDependencyContext {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct CrateTargetedDepContext {
pub target: String,
pub deps: CrateDependencyContext,
pub platform_targets: Vec<String>,
}
Expand Down
33 changes: 33 additions & 0 deletions impl/src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,39 @@ pub mod tests {
assert_eq!(flate2.targeted_deps[0].deps.dependencies.len(), 0);
}

#[test]
// Tests the fix for https://github.com/google/cargo-raze/issues/451.
// Bazel errors out if mutually exclusive select branches contain the
// same dependency. rust-errno is a real world example of this problem,
// where libc is listed under 'cfg(unix)' and 'cfg(target_os="wasi")'.
fn test_plan_build_consolidates_targets_across_platforms() {
let mut settings = dummy_raze_settings();
settings.genmode = GenMode::Remote;
let mut triples = HashSet::new();
triples.insert("wasm32-wasi".to_string());
triples.insert("x86_64-unknown-linux-gnu".to_string());
settings.target = None;
settings.targets = Some(triples);

let planner = BuildPlannerImpl::new(
dummy_workspace_crate_metadata(templates::SUBPLAN_CONSOLIDATES_TARGETED_DEPS),
settings,
);
let planned_build = planner.plan_build(None).unwrap();

let errno = planned_build
.crate_contexts
.iter()
.find(|ctx| ctx.pkg_name == "errno")
.unwrap();

assert_eq!(errno.targeted_deps.len(), 1);
assert_eq!(
errno.targeted_deps[0].platform_targets,
vec!["wasm32-wasi", "x86_64-unknown-linux-gnu",]
);
}

fn dummy_binary_dependency_metadata(is_remote_genmode: bool) -> (RazeMetadata, RazeSettings) {
let (mut fetcher, server, index_dir) = dummy_raze_metadata_fetcher();

Expand Down
27 changes: 22 additions & 5 deletions impl/src/planning/subplanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ impl<'planner> CrateSubplanner<'planner> {
ctx.subtract(&default_deps);
}

// Build a list of dependencies while addression a potential allowlist of target triples
let mut targeted_deps = deps
// Build a list of dependencies while addressing a potential allowlist of target triples
let targeted_deps = deps
.into_iter()
.map(|(target, deps)| {
let target = target.unwrap();
Expand All @@ -318,7 +318,6 @@ impl<'planner> CrateSubplanner<'planner> {
.collect();

Ok(CrateTargetedDepContext {
target,
deps,
platform_targets,
})
Expand All @@ -329,7 +328,25 @@ impl<'planner> CrateSubplanner<'planner> {
})
.collect::<Result<Vec<_>>>()?;

targeted_deps.sort();
// Consolidate any dependencies duplicated across platforms
let mut platform_map: BTreeMap<CrateDependencyContext, Vec<String>> = BTreeMap::new();
for targeted_dep in &targeted_deps {
platform_map
.entry(targeted_dep.deps.clone())
.and_modify(|e| e.append(&mut targeted_dep.platform_targets.clone()))
.or_insert_with(|| targeted_dep.platform_targets.clone());
}
let grouped_targeted_deps: Vec<CrateTargetedDepContext> = platform_map
.iter()
.map(|dep| {
let mut targets = dep.1.clone();
targets.sort();
CrateTargetedDepContext {
deps: dep.0.clone(),
platform_targets: targets,
}
})
.collect();

let mut workspace_member_dependents: Vec<Utf8PathBuf> = Vec::new();
let mut workspace_member_dev_dependents: Vec<Utf8PathBuf> = Vec::new();
Expand Down Expand Up @@ -405,7 +422,7 @@ impl<'planner> CrateSubplanner<'planner> {
is_binary_dependency,
is_proc_macro,
default_deps,
targeted_deps,
targeted_deps: grouped_targeted_deps,
workspace_path_to_crate: self.crate_catalog_entry.workspace_path(self.settings)?,
build_script_target: build_script_target_opt,
links: package.links.clone(),
Expand Down
2 changes: 2 additions & 0 deletions impl/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub mod templates {
pub const PLAN_BUILD_PRODUCES_PROC_MACRO_DEPENDENCIES: &str =
"plan_build_produces_proc_macro_dependencies.json.template";
pub const SEMVER_MATCHING: &str = "semver_matching.json.template";
pub const SUBPLAN_CONSOLIDATES_TARGETED_DEPS: &str =
"subplan_consolidates_targeted_deps.json.template";
pub const SUBPLAN_PRODUCES_CRATE_ROOT_WITH_FORWARD_SLASH: &str =
"subplan_produces_crate_root_with_forward_slash.json.template";
pub const SUBPLAN_OMITS_PLATFORM_DEPS_ALREADY_IN_DEFAULT_DEPS: &str =
Expand Down
Loading

0 comments on commit f97d93c

Please sign in to comment.