aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-08 13:55:55 +0100
committerGitHub <[email protected]>2020-05-08 13:55:55 +0100
commit51e4b92c1eee25cb6bf1b5ec8e7633dfc5f67a45 (patch)
tree871ebe102579fd2c8a35d2d5df4ed18b27219fd6 /crates/ra_project_model/src
parent8295a9340c1fbda805497035054ead0b10c0d88e (diff)
parentecff5dc141046c5b9e40639657247a05fb9b0344 (diff)
Merge #4375
4375: Cleanup toolchain handling r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs5
-rw-r--r--crates/ra_project_model/src/lib.rs32
-rw-r--r--crates/ra_project_model/src/sysroot.rs49
3 files changed, 29 insertions, 57 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index eb9f33ee8..082af4f96 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -11,7 +11,6 @@ use anyhow::{Context, Result};
11use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; 11use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
12use ra_arena::{Arena, Idx}; 12use ra_arena::{Arena, Idx};
13use ra_db::Edition; 13use ra_db::Edition;
14use ra_env::get_path_for_executable;
15use rustc_hash::FxHashMap; 14use rustc_hash::FxHashMap;
16 15
17/// `CargoWorkspace` represents the logical structure of, well, a Cargo 16/// `CargoWorkspace` represents the logical structure of, well, a Cargo
@@ -147,7 +146,7 @@ impl CargoWorkspace {
147 cargo_features: &CargoConfig, 146 cargo_features: &CargoConfig,
148 ) -> Result<CargoWorkspace> { 147 ) -> Result<CargoWorkspace> {
149 let mut meta = MetadataCommand::new(); 148 let mut meta = MetadataCommand::new();
150 meta.cargo_path(get_path_for_executable("cargo")?); 149 meta.cargo_path(ra_toolchain::cargo());
151 meta.manifest_path(cargo_toml); 150 meta.manifest_path(cargo_toml);
152 if cargo_features.all_features { 151 if cargo_features.all_features {
153 meta.features(CargoOpt::AllFeatures); 152 meta.features(CargoOpt::AllFeatures);
@@ -289,7 +288,7 @@ pub fn load_extern_resources(
289 cargo_toml: &Path, 288 cargo_toml: &Path,
290 cargo_features: &CargoConfig, 289 cargo_features: &CargoConfig,
291) -> Result<ExternResources> { 290) -> Result<ExternResources> {
292 let mut cmd = Command::new(get_path_for_executable("cargo")?); 291 let mut cmd = Command::new(ra_toolchain::cargo());
293 cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); 292 cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
294 if cargo_features.all_features { 293 if cargo_features.all_features {
295 cmd.arg("--all-features"); 294 cmd.arg("--all-features");
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 88a6ffb2a..5a0a87ed7 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -8,13 +8,12 @@ use std::{
8 fs::{read_dir, File, ReadDir}, 8 fs::{read_dir, File, ReadDir},
9 io::{self, BufReader}, 9 io::{self, BufReader},
10 path::{Path, PathBuf}, 10 path::{Path, PathBuf},
11 process::Command, 11 process::{Command, Output},
12}; 12};
13 13
14use anyhow::{bail, Context, Result}; 14use anyhow::{bail, Context, Result};
15use ra_cfg::CfgOptions; 15use ra_cfg::CfgOptions;
16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; 16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
17use ra_env::get_path_for_executable;
18use rustc_hash::FxHashMap; 17use rustc_hash::FxHashMap;
19use serde_json::from_reader; 18use serde_json::from_reader;
20 19
@@ -568,25 +567,18 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
568 } 567 }
569 } 568 }
570 569
571 match (|| -> Result<String> { 570 let rustc_cfgs = || -> Result<String> {
572 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. 571 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
573 let mut cmd = Command::new(get_path_for_executable("rustc")?); 572 let mut cmd = Command::new(ra_toolchain::rustc());
574 cmd.args(&["--print", "cfg", "-O"]); 573 cmd.args(&["--print", "cfg", "-O"]);
575 if let Some(target) = target { 574 if let Some(target) = target {
576 cmd.args(&["--target", target.as_str()]); 575 cmd.args(&["--target", target.as_str()]);
577 } 576 }
578 let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?; 577 let output = output(cmd)?;
579 if !output.status.success() {
580 bail!(
581 "rustc --print cfg -O exited with exit code ({})",
582 output
583 .status
584 .code()
585 .map_or(String::from("no exit code"), |code| format!("{}", code))
586 );
587 }
588 Ok(String::from_utf8(output.stdout)?) 578 Ok(String::from_utf8(output.stdout)?)
589 })() { 579 }();
580
581 match rustc_cfgs {
590 Ok(rustc_cfgs) => { 582 Ok(rustc_cfgs) => {
591 for line in rustc_cfgs.lines() { 583 for line in rustc_cfgs.lines() {
592 match line.find('=') { 584 match line.find('=') {
@@ -599,8 +591,16 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
599 } 591 }
600 } 592 }
601 } 593 }
602 Err(e) => log::error!("failed to get rustc cfgs: {}", e), 594 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
603 } 595 }
604 596
605 cfg_options 597 cfg_options
606} 598}
599
600fn output(mut cmd: Command) -> Result<Output> {
601 let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
602 if !output.status.success() {
603 bail!("{:?} failed, {}", cmd, output.status)
604 }
605 Ok(output)
606}
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 11c26ad89..a8a196e64 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,14 +1,15 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use anyhow::{bail, Context, Result};
4use std::{ 3use std::{
5 env, ops, 4 env, ops,
6 path::{Path, PathBuf}, 5 path::{Path, PathBuf},
7 process::{Command, Output}, 6 process::Command,
8}; 7};
9 8
9use anyhow::{bail, Result};
10use ra_arena::{Arena, Idx}; 10use ra_arena::{Arena, Idx};
11use ra_env::get_path_for_executable; 11
12use crate::output;
12 13
13#[derive(Default, Debug, Clone)] 14#[derive(Default, Debug, Clone)]
14pub struct Sysroot { 15pub struct Sysroot {
@@ -85,50 +86,22 @@ impl Sysroot {
85 } 86 }
86} 87}
87 88
88fn create_command_text(program: &str, args: &[&str]) -> String {
89 format!("{} {}", program, args.join(" "))
90}
91
92fn run_command_in_cargo_dir(
93 cargo_toml: impl AsRef<Path>,
94 program: impl AsRef<Path>,
95 args: &[&str],
96) -> Result<Output> {
97 let program = program.as_ref().as_os_str().to_str().expect("Invalid Unicode in path");
98 let output = Command::new(program)
99 .current_dir(cargo_toml.as_ref().parent().unwrap())
100 .args(args)
101 .output()
102 .context(format!("{} failed", create_command_text(program, args)))?;
103 if !output.status.success() {
104 match output.status.code() {
105 Some(code) => bail!(
106 "failed to run the command: '{}' exited with code {}",
107 create_command_text(program, args),
108 code
109 ),
110 None => bail!(
111 "failed to run the command: '{}' terminated by signal",
112 create_command_text(program, args)
113 ),
114 };
115 }
116 Ok(output)
117}
118
119fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { 89fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
120 if let Ok(path) = env::var("RUST_SRC_PATH") { 90 if let Ok(path) = env::var("RUST_SRC_PATH") {
121 return Ok(path.into()); 91 return Ok(path.into());
122 } 92 }
123 let rustc = get_path_for_executable("rustc")?; 93 let current_dir = cargo_toml.parent().unwrap();
124 let rustc_output = run_command_in_cargo_dir(cargo_toml, &rustc, &["--print", "sysroot"])?; 94 let mut rustc = Command::new(ra_toolchain::rustc());
95 rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
96 let rustc_output = output(rustc)?;
125 let stdout = String::from_utf8(rustc_output.stdout)?; 97 let stdout = String::from_utf8(rustc_output.stdout)?;
126 let sysroot_path = Path::new(stdout.trim()); 98 let sysroot_path = Path::new(stdout.trim());
127 let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); 99 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
128 100
129 if !src_path.exists() { 101 if !src_path.exists() {
130 let rustup = get_path_for_executable("rustup")?; 102 let mut rustup = Command::new(ra_toolchain::rustup());
131 run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?; 103 rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
104 let _output = output(rustup)?;
132 } 105 }
133 if !src_path.exists() { 106 if !src_path.exists() {
134 bail!( 107 bail!(