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

Commit 1f82376

Browse files
committed
Implement external build plan
This adds a `BuildGraph` trait which aims to abstract away the API for a build plan and it implements it for the in-process Cargo build plan as well as the new, external one (for `cargo build --build-plan` format). In addition to that, since save-analysis (since rls-data 0.18.1) includes the invocation used to compile a given crate, we try to recreate the external build plan from the passed save-analysis files via `build_command` config option.
1 parent 87d0034 commit 1f82376

File tree

8 files changed

+1235
-629
lines changed

8 files changed

+1235
-629
lines changed

src/actions/post_build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ impl Job {
300300
self.cwd
301301
);
302302
if self.analysis.is_empty() {
303+
trace!("reloading from disk: {:?}", self.cwd);
303304
self.handler.reload_analysis_from_disk(&self.cwd);
304305
} else {
306+
trace!("reloading from memory: {:?}", self.cwd);
305307
self.handler
306308
.reload_analysis_from_memory(&self.cwd, self.analysis);
307309
}

src/build/cargo.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ use failure::{self, format_err};
2020
use serde_json;
2121

2222
use crate::actions::progress::ProgressUpdate;
23-
use crate::build::environment::{self, Environment, EnvironmentLock};
24-
use crate::build::plan::Plan;
2523
use crate::build::{BufWriter, BuildResult, CompilationContext, Internals, PackageArg};
24+
use crate::build::cargo_plan::CargoPlan;
25+
use crate::build::environment::{self, Environment, EnvironmentLock};
26+
use crate::build::plan::BuildPlan;
2627
use crate::config::Config;
27-
use log::{debug, error, trace, warn};
28+
use log::{debug, trace, warn};
2829
use rls_data::Analysis;
2930
use rls_vfs::Vfs;
3031

@@ -34,7 +35,6 @@ use std::ffi::OsString;
3435
use std::fmt::Write;
3536
use std::fs::{read_dir, remove_file};
3637
use std::path::{Path, PathBuf};
37-
use std::process::Command;
3838
use std::sync::atomic::{AtomicBool, Ordering};
3939
use std::sync::mpsc::Sender;
4040
use std::sync::{Arc, Mutex};
@@ -188,7 +188,8 @@ fn run_cargo(
188188

189189
// Since Cargo build routine will try to regenerate the unit dep graph,
190190
// we need to clear the existing dep graph.
191-
compilation_cx.lock().unwrap().build_plan = Plan::for_packages(pkg_names);
191+
compilation_cx.lock().unwrap().build_plan =
192+
BuildPlan::Cargo(CargoPlan::with_packages(&manifest_path, pkg_names));
192193

193194
let compile_opts = CompileOptions {
194195
spec,
@@ -328,12 +329,12 @@ impl Executor for RlsExecutor {
328329
/// is fresh and won't be compiled.
329330
fn init(&self, cx: &Context<'_, '_>, unit: &Unit<'_>) {
330331
let mut compilation_cx = self.compilation_cx.lock().unwrap();
331-
let plan = &mut compilation_cx.build_plan;
332+
let plan = compilation_cx.build_plan.as_cargo_mut()
333+
.expect("Build plan should be properly initialized before running Cargo");
334+
332335
let only_primary = |unit: &Unit<'_>| self.is_primary_crate(unit.pkg.package_id());
333336

334-
if let Err(err) = plan.emplace_dep_with_filter(unit, cx, &only_primary) {
335-
error!("{:?}", err);
336-
}
337+
plan.emplace_dep_with_filter(unit, cx, &only_primary);
337338
}
338339

339340
fn force_rebuild(&self, unit: &Unit<'_>) -> bool {
@@ -431,8 +432,8 @@ impl Executor for RlsExecutor {
431432
.collect();
432433
let envs = cargo_cmd.get_envs().clone();
433434

434-
let sysroot =
435-
current_sysroot().expect("need to specify SYSROOT env var or use rustup or multirust");
435+
let sysroot = super::rustc::current_sysroot()
436+
.expect("need to specify SYSROOT env var or use rustup or multirust");
436437

437438
{
438439
let config = self.config.lock().unwrap();
@@ -494,7 +495,8 @@ impl Executor for RlsExecutor {
494495
// Cache executed command for the build plan
495496
{
496497
let mut cx = self.compilation_cx.lock().unwrap();
497-
cx.build_plan.cache_compiler_job(id, target, mode, &cmd);
498+
let plan = cx.build_plan.as_cargo_mut().unwrap();
499+
plan.cache_compiler_job(id, target, mode, &cmd);
498500
}
499501

500502
// Prepare modified cargo-generated args/envs for future rustc calls
@@ -646,25 +648,6 @@ fn parse_arg(args: &[OsString], arg: &str) -> Option<String> {
646648
None
647649
}
648650

649-
fn current_sysroot() -> Option<String> {
650-
let home = env::var("RUSTUP_HOME").or_else(|_| env::var("MULTIRUST_HOME"));
651-
let toolchain = env::var("RUSTUP_TOOLCHAIN").or_else(|_| env::var("MULTIRUST_TOOLCHAIN"));
652-
if let (Ok(home), Ok(toolchain)) = (home, toolchain) {
653-
Some(format!("{}/toolchains/{}", home, toolchain))
654-
} else {
655-
let rustc_exe = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_owned());
656-
env::var("SYSROOT").map(|s| s.to_owned()).ok().or_else(|| {
657-
Command::new(rustc_exe)
658-
.arg("--print")
659-
.arg("sysroot")
660-
.output()
661-
.ok()
662-
.and_then(|out| String::from_utf8(out.stdout).ok())
663-
.map(|s| s.trim().to_owned())
664-
})
665-
}
666-
}
667-
668651
/// `flag_str` is a string of command line args for Rust. This function removes any
669652
/// duplicate flags.
670653
fn dedup_flags(flag_str: &str) -> String {

0 commit comments

Comments
 (0)