From 8989fb8315538aece975663c3be4aba867e9ee86 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 8 May 2021 18:17:18 +0200 Subject: Discover rustc_cfg through unstable cargo options --- crates/project_model/src/rustc_cfg.rs | 38 ++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'crates/project_model/src/rustc_cfg.rs') 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 @@ use std::process::Command; +use paths::AbsPath; + use crate::{cfg_flag::CfgFlag, utf8_stdout}; -pub(crate) fn get(target: Option<&str>) -> Vec { +pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec { let _p = profile::span("rustc_cfg::get"); let mut res = Vec::with_capacity(6 * 2 + 1); @@ -17,12 +19,34 @@ pub(crate) fn get(target: Option<&str>) -> Vec { } let rustc_cfgs = { - let mut cmd = Command::new(toolchain::rustc()); - cmd.args(&["--print", "cfg", "-O"]); - if let Some(target) = target { - cmd.args(&["--target", target]); - } - utf8_stdout(cmd) + cargo_toml + .and_then(|cargo_toml| { + let mut cargo_config = Command::new(toolchain::cargo()); + cargo_config.current_dir(cargo_toml.parent().unwrap()).args(&[ + "+nightly", + "-Z", + "unstable-options", + "rustc", + "--print", + "cfg", + ]); + if let Some(target) = target { + cargo_config.args(&["--target", target]); + } + utf8_stdout(cargo_config).ok() + }) + .map_or_else( + || { + // using unstable cargo features failed, fall back to using plain rustc + let mut cmd = Command::new(toolchain::rustc()); + cmd.args(&["--print", "cfg", "-O"]); + if let Some(target) = target { + cmd.args(&["--target", target]); + } + utf8_stdout(cmd) + }, + Ok, + ) }; match rustc_cfgs { -- cgit v1.2.3