Skip to content

Commit

Permalink
Auto merge of #7626 - ehuss:remove-dep-targets, r=alexcrichton
Browse files Browse the repository at this point in the history
Remove dep_targets.

This is just some code cleanup.  The `dep_targets` method has been replaced by `unit_deps`, which doesn't force the creation of a new Vec.
  • Loading branch information
bors committed Nov 26, 2019
2 parents a41c8ea + cc9f5a8 commit 14654f3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/build_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ impl BuildPlan {
let id = self.plan.invocations.len();
self.invocation_map.insert(unit.buildkey(), id);
let deps = cx
.dep_targets(unit)
.unit_deps(unit)
.iter()
.map(|dep| self.invocation_map[&dep.buildkey()])
.map(|dep| self.invocation_map[&dep.unit.buildkey()])
.collect();
let invocation = Invocation::new(unit, deps);
self.plan.invocations.push(invocation);
Expand Down
20 changes: 9 additions & 11 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ fn metadata_of<'a, 'cfg>(
if !metas.contains_key(unit) {
let meta = compute_metadata(unit, cx, metas);
metas.insert(*unit, meta);
for unit in cx.dep_targets(unit) {
metadata_of(&unit, cx, metas);
for dep in cx.unit_deps(unit) {
metadata_of(&dep.unit, cx, metas);
}
}
metas[unit].clone()
Expand Down Expand Up @@ -532,15 +532,13 @@ fn compute_metadata<'a, 'cfg>(
unit.features.hash(&mut hasher);

// Mix in the target-metadata of all the dependencies of this target.
{
let mut deps_metadata = cx
.dep_targets(unit)
.iter()
.map(|dep| metadata_of(dep, cx, metas))
.collect::<Vec<_>>();
deps_metadata.sort();
deps_metadata.hash(&mut hasher);
}
let mut deps_metadata = cx
.unit_deps(unit)
.iter()
.map(|dep| metadata_of(&dep.unit, cx, metas))
.collect::<Vec<_>>();
deps_metadata.sort();
deps_metadata.hash(&mut hasher);

// Throw in the profile we're compiling with. This helps caching
// `panic=abort` and `panic=unwind` artifacts, additionally with various
Expand Down
38 changes: 14 additions & 24 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,20 @@ impl<'a, 'cfg> Context<'a, 'cfg> {

// If the unit has a build script, add `OUT_DIR` to the
// environment variables.
for dep in self.dep_targets(unit).iter() {
if !unit.target.is_lib() {
continue;
}

if dep.mode.is_run_custom_build() {
let out_dir = self.files().build_script_out_dir(dep).display().to_string();
self.compilation
.extra_env
.entry(dep.pkg.package_id())
.or_insert_with(Vec::new)
.push(("OUT_DIR".to_string(), out_dir));
if unit.target.is_lib() {
for dep in &self.unit_dependencies[unit] {
if dep.unit.mode.is_run_custom_build() {
let out_dir = self
.files()
.build_script_out_dir(&dep.unit)
.display()
.to_string();
self.compilation
.extra_env
.entry(dep.unit.pkg.package_id())
.or_insert_with(Vec::new)
.push(("OUT_DIR".to_string(), out_dir));
}
}
}

Expand Down Expand Up @@ -361,18 +363,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
self.files.as_ref().unwrap().outputs(unit, self.bcx)
}

/// For a package, return all targets which are registered as dependencies
/// for that package.
/// NOTE: This is deprecated, use `unit_deps` instead.
//
// TODO: this ideally should be `-> &[Unit<'a>]`.
pub fn dep_targets(&self, unit: &Unit<'a>) -> Vec<Unit<'a>> {
self.unit_dependencies[unit]
.iter()
.map(|dep| dep.unit)
.collect()
}

/// Direct dependencies for the given unit.
pub fn unit_deps(&self, unit: &Unit<'a>) -> &[UnitDep<'a>] {
&self.unit_dependencies[unit]
Expand Down
41 changes: 20 additions & 21 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ fn emit_build_output(
fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Job> {
assert!(unit.mode.is_run_custom_build());
let bcx = &cx.bcx;
let dependencies = cx.dep_targets(unit);
let dependencies = cx.unit_deps(unit);
let build_script_unit = dependencies
.iter()
.find(|d| !d.mode.is_run_custom_build() && d.target.is_custom_build())
.find(|d| !d.unit.mode.is_run_custom_build() && d.unit.target.is_custom_build())
.map(|d| &d.unit)
.expect("running a script not depending on an actual script");
let script_dir = cx.files().build_script_dir(build_script_unit);
let script_out_dir = cx.files().build_script_out_dir(unit);
Expand Down Expand Up @@ -225,21 +226,19 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
//
// This information will be used at build-time later on to figure out which
// sorts of variables need to be discovered at that time.
let lib_deps = {
dependencies
.iter()
.filter_map(|unit| {
if unit.mode.is_run_custom_build() {
Some((
unit.pkg.manifest().links().unwrap().to_string(),
unit.pkg.package_id(),
))
} else {
None
}
})
.collect::<Vec<_>>()
};
let lib_deps = dependencies
.iter()
.filter_map(|dep| {
if dep.unit.mode.is_run_custom_build() {
Some((
dep.unit.pkg.manifest().links().unwrap().to_string(),
dep.unit.pkg.package_id(),
))
} else {
None
}
})
.collect::<Vec<_>>();
let pkg_name = unit.pkg.to_string();
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
let id = unit.pkg.package_id();
Expand Down Expand Up @@ -563,15 +562,15 @@ fn prepare_metabuild<'a, 'cfg>(
deps: &[String],
) -> CargoResult<()> {
let mut output = Vec::new();
let available_deps = cx.dep_targets(unit);
let available_deps = cx.unit_deps(unit);
// Filter out optional dependencies, and look up the actual lib name.
let meta_deps: Vec<_> = deps
.iter()
.filter_map(|name| {
available_deps
.iter()
.find(|u| u.pkg.name().as_str() == name.as_str())
.map(|dep| dep.target.crate_name())
.find(|d| d.unit.pkg.name().as_str() == name.as_str())
.map(|d| d.unit.target.crate_name())
})
.collect();
for dep in &meta_deps {
Expand Down Expand Up @@ -669,7 +668,7 @@ pub fn build_map<'b, 'cfg>(cx: &mut Context<'b, 'cfg>, units: &[Unit<'b>]) -> Ca
// to rustc invocation caching schemes, so be sure to generate the same
// set of build script dependency orderings via sorting the targets that
// come out of the `Context`.
let mut dependencies = cx.dep_targets(unit);
let mut dependencies: Vec<Unit<'_>> = cx.unit_deps(unit).iter().map(|d| d.unit).collect();
dependencies.sort_by_key(|u| u.pkg.package_id());

for dep_unit in dependencies.iter() {
Expand Down
19 changes: 9 additions & 10 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,25 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
unit: &Unit<'a>,
job: Job,
) -> CargoResult<()> {
let dependencies = cx.dep_targets(unit);
let dependencies = cx.unit_deps(unit);
let mut queue_deps = dependencies
.iter()
.cloned()
.filter(|unit| {
.filter(|dep| {
// Binaries aren't actually needed to *compile* tests, just to run
// them, so we don't include this dependency edge in the job graph.
!unit.target.is_test() && !unit.target.is_bin()
!dep.unit.target.is_test() && !dep.unit.target.is_bin()
})
.map(|dep| {
// Handle the case here where our `unit -> dep` dependency may
// only require the metadata, not the full compilation to
// finish. Use the tables in `cx` to figure out what kind
// of artifact is associated with this dependency.
let artifact = if cx.only_requires_rmeta(unit, &dep) {
let artifact = if cx.only_requires_rmeta(unit, &dep.unit) {
Artifact::Metadata
} else {
Artifact::All
};
(dep, artifact)
(dep.unit, artifact)
})
.collect::<HashMap<_, _>>();

Expand All @@ -205,17 +204,17 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
// transitively contains the `Metadata` edge.
if unit.requires_upstream_objects() {
for dep in dependencies {
depend_on_deps_of_deps(cx, &mut queue_deps, dep);
depend_on_deps_of_deps(cx, &mut queue_deps, dep.unit);
}

fn depend_on_deps_of_deps<'a>(
cx: &Context<'a, '_>,
deps: &mut HashMap<Unit<'a>, Artifact>,
unit: Unit<'a>,
) {
for dep in cx.dep_targets(&unit) {
if deps.insert(dep, Artifact::All).is_none() {
depend_on_deps_of_deps(cx, deps, dep);
for dep in cx.unit_deps(&unit) {
if deps.insert(dep.unit, Artifact::All).is_none() {
depend_on_deps_of_deps(cx, deps, dep.unit);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ fn compile<'a, 'cfg: 'a>(
drop(p);

// Be sure to compile all dependencies of this target as well.
for unit in cx.dep_targets(unit).iter() {
compile(cx, jobs, plan, unit, exec, false)?;
let deps = Vec::from(cx.unit_deps(unit)); // Create vec due to mutable borrow.
for dep in deps {
compile(cx, jobs, plan, &dep.unit, exec, false)?;
}
if build_plan {
plan.add(cx, unit)?;
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/core/compiler/output_depinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ fn add_deps_for_unit<'a, 'b>(
}

// Recursively traverse all transitive dependencies
for dep_unit in context.dep_targets(unit).iter() {
let source_id = dep_unit.pkg.package_id().source_id();
let unit_deps = Vec::from(context.unit_deps(unit)); // Create vec due to mutable borrow.
for dep in unit_deps {
let source_id = dep.unit.pkg.package_id().source_id();
if source_id.is_path() {
add_deps_for_unit(deps, context, dep_unit, visited)?;
add_deps_for_unit(deps, context, &dep.unit, visited)?;
}
}
Ok(())
Expand Down

0 comments on commit 14654f3

Please sign in to comment.