Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 22e7250

Browse files
committed
Cache executed processes during in the build plan during Cargo routine
1 parent e1d7609 commit 22e7250

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/build/cargo.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use cargo::core::{PackageId, Shell, Target, Workspace, Verbosity};
1212
use cargo::ops::{compile_with_exec, Executor, Context, Packages, CompileOptions, CompileMode, CompileFilter, Unit};
13-
use cargo::util::{Config as CargoConfig, ProcessBuilder, homedir, important_paths, ConfigValue, CargoResult};
13+
use cargo::util::{Config as CargoConfig, process_builder, ProcessBuilder, homedir, important_paths, ConfigValue, CargoResult};
1414
use cargo::util::errors::{CargoErrorKind, process_error};
1515
use serde_json;
1616

@@ -271,7 +271,7 @@ impl Executor for RlsExecutor {
271271
self.is_primary_crate(id)
272272
}
273273

274-
fn exec(&self, cargo_cmd: ProcessBuilder, id: &PackageId, _target: &Target) -> CargoResult<()> {
274+
fn exec(&self, cargo_cmd: ProcessBuilder, id: &PackageId, target: &Target) -> CargoResult<()> {
275275
// Delete any stale data. We try and remove any json files with
276276
// the same crate name as Cargo would emit. This includes files
277277
// with the same crate name but different hashes, e.g., those
@@ -386,6 +386,24 @@ impl Executor for RlsExecutor {
386386
}
387387
}
388388

389+
// Cache executed command for the build plan
390+
{
391+
let mut cx = self.compilation_cx.lock().unwrap();
392+
393+
let mut store_cmd = process_builder::process(&rustc_exe);
394+
store_cmd.args(&args.iter().map(OsString::from).collect::<Vec<_>>());
395+
for (k, v) in cargo_cmd.get_envs().clone() {
396+
if let Some(v) = v {
397+
store_cmd.env(&k, v);
398+
}
399+
}
400+
if let Some(cwd) = cargo_cmd.get_cwd() {
401+
cmd.current_dir(cwd);
402+
}
403+
404+
cx.build_plan.cache_compiler_job(id, target, &store_cmd);
405+
}
406+
389407
// Prepare modified cargo-generated args/envs for future rustc calls
390408
args.insert(0, rustc_exe);
391409
let envs = cargo_cmd.get_envs().clone();

src/build/plan.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,39 @@
1111
use std::collections::HashMap;
1212
use std::fmt;
1313

14-
use cargo::core::{PackageId, Profile, Target};
14+
use cargo::core::{PackageId, Profile, Target, TargetKind};
1515
use cargo::ops::{Kind, Unit, Context};
16-
use cargo::util::{CargoResult};
16+
use cargo::util::{CargoResult, ProcessBuilder};
1717

18+
pub type DependencyGraph = HashMap<OwnedUnit, Vec<OwnedUnit>>;
1819
/// Holds the information how exactly the build will be performed for a given
1920
/// workspace with given, specified features.
2021
/// **TODO:** Use it to schedule an analysis build instead of relying on Cargo
2122
/// invocations.
22-
pub type DependencyGraph = HashMap<OwnedUnit, Vec<OwnedUnit>>;
2323
pub struct Plan {
24-
pub dep_graph: DependencyGraph
24+
pub dep_graph: DependencyGraph,
25+
/// We don't make a distinction between Units with different Profiles,
26+
/// as we're practically interested in bin, lib and (built, not run)
27+
/// build scripts for each package, because for these we can run `rustc` job
28+
pub compiler_jobs: HashMap<(PackageId, TargetKind), ProcessBuilder>,
2529
}
2630

2731
impl Plan {
2832
pub fn new() -> Plan {
2933
Plan {
30-
dep_graph: HashMap::new()
34+
dep_graph: HashMap::new(),
35+
compiler_jobs: HashMap::new(),
3136
}
3237
}
3338

39+
40+
/// Cache a given compiler invocation in `ProcessBuilder` for a given `PackageId`
41+
/// and `TargetKind` in `Target`, to be used when processing cached build plan
42+
pub fn cache_compiler_job(&mut self, id: &PackageId, target: &Target, cmd: &ProcessBuilder) {
43+
let pkg_key = (id.clone(), target.kind().clone());
44+
self.compiler_jobs.insert(pkg_key, cmd.clone());
45+
}
46+
3447
/// Emplace a given `Unit`, along with its `Unit` dependencies (recursively)
3548
/// into dependency graph
3649
#[allow(dead_code)]
@@ -71,18 +84,21 @@ impl Plan {
7184
}
7285

7386
pub fn clear(&mut self) {
74-
self.dep_graph.clear()
87+
self.dep_graph.clear();
88+
self.compiler_jobs.clear();
7589
}
7690
}
7791

7892
impl fmt::Debug for Plan {
7993
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94+
f.write_str("Dep graph:\n")?;
8095
for (key, deps) in &self.dep_graph {
8196
f.write_str(&format!("{:?}\n", key))?;
8297
for dep in deps {
8398
f.write_str(&format!("- {:?}\n", dep))?;
8499
}
85100
}
101+
f.write_str(&format!("Compiler jobs: {:?}\n", self.compiler_jobs))?;
86102
Ok(())
87103
}
88104
}

0 commit comments

Comments
 (0)