Discover rustc_cfg through unstable cargo options

This commit is contained in:
Lukas Wirth 2021-05-08 18:17:18 +02:00
parent 526040eea8
commit 8989fb8315
3 changed files with 83 additions and 33 deletions

View file

@ -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<CfgFlag> {
pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec<CfgFlag> {
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<CfgFlag> {
}
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 {