aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-05-08 23:07:04 +0100
committerLukas Wirth <[email protected]>2021-05-08 23:07:04 +0100
commitb7e6537935d421afd7e02585aaa5cec92bee63b0 (patch)
tree457d24df41b334116de8d9fad447e5b832477dd2 /crates/project_model
parent8989fb8315538aece975663c3be4aba867e9ee86 (diff)
Use RUSTC_BOOTSTRAP=1 instead of +nightly when discovering rust_cfgs throughs cargo
Diffstat (limited to 'crates/project_model')
-rw-r--r--crates/project_model/src/cargo_workspace.rs12
-rw-r--r--crates/project_model/src/rustc_cfg.rs63
2 files changed, 35 insertions, 40 deletions
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index f1ad00d36..b18699b77 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -375,14 +375,10 @@ fn rustc_discover_host_triple(cargo_toml: &AbsPath) -> Option<String> {
375 375
376fn cargo_config_build_target(cargo_toml: &AbsPath) -> Option<String> { 376fn cargo_config_build_target(cargo_toml: &AbsPath) -> Option<String> {
377 let mut cargo_config = Command::new(toolchain::cargo()); 377 let mut cargo_config = Command::new(toolchain::cargo());
378 cargo_config.current_dir(cargo_toml.parent().unwrap()).args(&[ 378 cargo_config
379 "+nightly", 379 .current_dir(cargo_toml.parent().unwrap())
380 "-Z", 380 .args(&["-Z", "unstable-options", "config", "get", "build.target"])
381 "unstable-options", 381 .env("RUSTC_BOOTSTRAP", "1");
382 "config",
383 "get",
384 "build.target",
385 ]);
386 // if successful we receive `build.target = "target-triple"` 382 // if successful we receive `build.target = "target-triple"`
387 log::debug!("Discovering cargo config target by {:?}", cargo_config); 383 log::debug!("Discovering cargo config target by {:?}", cargo_config);
388 match utf8_stdout(cargo_config) { 384 match utf8_stdout(cargo_config) {
diff --git a/crates/project_model/src/rustc_cfg.rs b/crates/project_model/src/rustc_cfg.rs
index 6de40cfe2..012eab256 100644
--- a/crates/project_model/src/rustc_cfg.rs
+++ b/crates/project_model/src/rustc_cfg.rs
@@ -2,6 +2,7 @@
2 2
3use std::process::Command; 3use std::process::Command;
4 4
5use anyhow::Result;
5use paths::AbsPath; 6use paths::AbsPath;
6 7
7use crate::{cfg_flag::CfgFlag, utf8_stdout}; 8use crate::{cfg_flag::CfgFlag, utf8_stdout};
@@ -18,41 +19,39 @@ pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec<Cfg
18 } 19 }
19 } 20 }
20 21
21 let rustc_cfgs = { 22 match get_rust_cfgs(cargo_toml, target) {
22 cargo_toml
23 .and_then(|cargo_toml| {
24 let mut cargo_config = Command::new(toolchain::cargo());
25 cargo_config.current_dir(cargo_toml.parent().unwrap()).args(&[
26 "+nightly",
27 "-Z",
28 "unstable-options",
29 "rustc",
30 "--print",
31 "cfg",
32 ]);
33 if let Some(target) = target {
34 cargo_config.args(&["--target", target]);
35 }
36 utf8_stdout(cargo_config).ok()
37 })
38 .map_or_else(
39 || {
40 // using unstable cargo features failed, fall back to using plain rustc
41 let mut cmd = Command::new(toolchain::rustc());
42 cmd.args(&["--print", "cfg", "-O"]);
43 if let Some(target) = target {
44 cmd.args(&["--target", target]);
45 }
46 utf8_stdout(cmd)
47 },
48 Ok,
49 )
50 };
51
52 match rustc_cfgs {
53 Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())), 23 Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())),
54 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), 24 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
55 } 25 }
56 26
57 res 27 res
58} 28}
29
30fn get_rust_cfgs(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Result<String> {
31 let cargo_rust_cfgs = match cargo_toml {
32 Some(cargo_toml) => {
33 let mut cargo_config = Command::new(toolchain::cargo());
34 cargo_config
35 .current_dir(cargo_toml.parent().unwrap())
36 .args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
37 .env("RUSTC_BOOTSTRAP", "1");
38 if let Some(target) = target {
39 cargo_config.args(&["--target", target]);
40 }
41 utf8_stdout(cargo_config).ok()
42 }
43 None => None,
44 };
45 match cargo_rust_cfgs {
46 Some(stdout) => Ok(stdout),
47 None => {
48 // using unstable cargo features failed, fall back to using plain rustc
49 let mut cmd = Command::new(toolchain::rustc());
50 cmd.args(&["--print", "cfg", "-O"]);
51 if let Some(target) = target {
52 cmd.args(&["--target", target]);
53 }
54 utf8_stdout(cmd)
55 }
56 }
57}