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.rs38
1 files changed, 31 insertions, 7 deletions
diff --git a/crates/project_model/src/rustc_cfg.rs b/crates/project_model/src/rustc_cfg.rs
index 312708575..6de40cfe2 100644
--- a/crates/project_model/src/rustc_cfg.rs
+++ b/crates/project_model/src/rustc_cfg.rs
@@ -2,9 +2,11 @@
2 2
3use std::process::Command; 3use std::process::Command;
4 4
5use paths::AbsPath;
6
5use crate::{cfg_flag::CfgFlag, utf8_stdout}; 7use crate::{cfg_flag::CfgFlag, utf8_stdout};
6 8
7pub(crate) fn get(target: Option<&str>) -> Vec<CfgFlag> { 9pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec<CfgFlag> {
8 let _p = profile::span("rustc_cfg::get"); 10 let _p = profile::span("rustc_cfg::get");
9 let mut res = Vec::with_capacity(6 * 2 + 1); 11 let mut res = Vec::with_capacity(6 * 2 + 1);
10 12
@@ -17,12 +19,34 @@ pub(crate) fn get(target: Option<&str>) -> Vec<CfgFlag> {
17 } 19 }
18 20
19 let rustc_cfgs = { 21 let rustc_cfgs = {
20 let mut cmd = Command::new(toolchain::rustc()); 22 cargo_toml
21 cmd.args(&["--print", "cfg", "-O"]); 23 .and_then(|cargo_toml| {
22 if let Some(target) = target { 24 let mut cargo_config = Command::new(toolchain::cargo());
23 cmd.args(&["--target", target]); 25 cargo_config.current_dir(cargo_toml.parent().unwrap()).args(&[
24 } 26 "+nightly",
25 utf8_stdout(cmd) 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 )
26 }; 50 };
27 51
28 match rustc_cfgs { 52 match rustc_cfgs {