aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src/rustc_cfg.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model/src/rustc_cfg.rs')
-rw-r--r--crates/project_model/src/rustc_cfg.rs63
1 files changed, 31 insertions, 32 deletions
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}