This commit is contained in:
Aleksey Kladov 2020-05-08 14:54:29 +02:00
parent 7c0409e0c7
commit ecff5dc141
7 changed files with 64 additions and 100 deletions

View file

@ -11,7 +11,6 @@ use anyhow::{Context, Result};
use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
use ra_arena::{Arena, Idx};
use ra_db::Edition;
use ra_toolchain::get_path_for_executable;
use rustc_hash::FxHashMap;
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
@ -147,7 +146,7 @@ impl CargoWorkspace {
cargo_features: &CargoConfig,
) -> Result<CargoWorkspace> {
let mut meta = MetadataCommand::new();
meta.cargo_path(get_path_for_executable("cargo")?);
meta.cargo_path(ra_toolchain::cargo());
meta.manifest_path(cargo_toml);
if cargo_features.all_features {
meta.features(CargoOpt::AllFeatures);
@ -289,7 +288,7 @@ pub fn load_extern_resources(
cargo_toml: &Path,
cargo_features: &CargoConfig,
) -> Result<ExternResources> {
let mut cmd = Command::new(get_path_for_executable("cargo")?);
let mut cmd = Command::new(ra_toolchain::cargo());
cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
if cargo_features.all_features {
cmd.arg("--all-features");

View file

@ -8,13 +8,12 @@ use std::{
fs::{read_dir, File, ReadDir},
io::{self, BufReader},
path::{Path, PathBuf},
process::Command,
process::{Command, Output},
};
use anyhow::{bail, Context, Result};
use ra_cfg::CfgOptions;
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
use ra_toolchain::get_path_for_executable;
use rustc_hash::FxHashMap;
use serde_json::from_reader;
@ -568,25 +567,18 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
}
}
match (|| -> Result<String> {
let rustc_cfgs = || -> Result<String> {
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
let mut cmd = Command::new(get_path_for_executable("rustc")?);
let mut cmd = Command::new(ra_toolchain::rustc());
cmd.args(&["--print", "cfg", "-O"]);
if let Some(target) = target {
cmd.args(&["--target", target.as_str()]);
}
let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?;
if !output.status.success() {
bail!(
"rustc --print cfg -O exited with exit code ({})",
output
.status
.code()
.map_or(String::from("no exit code"), |code| format!("{}", code))
);
}
let output = output(cmd)?;
Ok(String::from_utf8(output.stdout)?)
})() {
}();
match rustc_cfgs {
Ok(rustc_cfgs) => {
for line in rustc_cfgs.lines() {
match line.find('=') {
@ -599,8 +591,16 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
}
}
}
Err(e) => log::error!("failed to get rustc cfgs: {}", e),
Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
}
cfg_options
}
fn output(mut cmd: Command) -> Result<Output> {
let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
if !output.status.success() {
bail!("{:?} failed, {}", cmd, output.status)
}
Ok(output)
}

View file

@ -3,12 +3,13 @@
use std::{
env, ops,
path::{Path, PathBuf},
process::{Command, Output},
process::Command,
};
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use ra_arena::{Arena, Idx};
use ra_toolchain::get_path_for_executable;
use crate::output;
#[derive(Default, Debug, Clone)]
pub struct Sysroot {
@ -85,50 +86,22 @@ impl Sysroot {
}
}
fn create_command_text(program: &str, args: &[&str]) -> String {
format!("{} {}", program, args.join(" "))
}
fn run_command_in_cargo_dir(
cargo_toml: impl AsRef<Path>,
program: impl AsRef<Path>,
args: &[&str],
) -> Result<Output> {
let program = program.as_ref().as_os_str().to_str().expect("Invalid Unicode in path");
let output = Command::new(program)
.current_dir(cargo_toml.as_ref().parent().unwrap())
.args(args)
.output()
.context(format!("{} failed", create_command_text(program, args)))?;
if !output.status.success() {
match output.status.code() {
Some(code) => bail!(
"failed to run the command: '{}' exited with code {}",
create_command_text(program, args),
code
),
None => bail!(
"failed to run the command: '{}' terminated by signal",
create_command_text(program, args)
),
};
}
Ok(output)
}
fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
if let Ok(path) = env::var("RUST_SRC_PATH") {
return Ok(path.into());
}
let rustc = get_path_for_executable("rustc")?;
let rustc_output = run_command_in_cargo_dir(cargo_toml, &rustc, &["--print", "sysroot"])?;
let current_dir = cargo_toml.parent().unwrap();
let mut rustc = Command::new(ra_toolchain::rustc());
rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
let rustc_output = output(rustc)?;
let stdout = String::from_utf8(rustc_output.stdout)?;
let sysroot_path = Path::new(stdout.trim());
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
if !src_path.exists() {
let rustup = get_path_for_executable("rustup")?;
run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?;
let mut rustup = Command::new(ra_toolchain::rustup());
rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
let _output = output(rustup)?;
}
if !src_path.exists() {
bail!(