2021-01-18 11:52:12 +00:00
|
|
|
//! Runs `rustc --print cfg` to get built-in cfg flags.
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
2021-05-08 16:17:18 +00:00
|
|
|
use paths::AbsPath;
|
|
|
|
|
2021-01-18 11:52:12 +00:00
|
|
|
use crate::{cfg_flag::CfgFlag, utf8_stdout};
|
|
|
|
|
2021-05-08 16:17:18 +00:00
|
|
|
pub(crate) fn get(cargo_toml: Option<&AbsPath>, target: Option<&str>) -> Vec<CfgFlag> {
|
2021-01-18 11:52:12 +00:00
|
|
|
let _p = profile::span("rustc_cfg::get");
|
2021-03-15 09:15:08 +00:00
|
|
|
let mut res = Vec::with_capacity(6 * 2 + 1);
|
2021-01-18 11:52:12 +00:00
|
|
|
|
|
|
|
// Some nightly-only cfgs, which are required for stdlib
|
|
|
|
res.push(CfgFlag::Atom("target_thread_local".into()));
|
|
|
|
for &ty in ["8", "16", "32", "64", "cas", "ptr"].iter() {
|
|
|
|
for &key in ["target_has_atomic", "target_has_atomic_load_store"].iter() {
|
|
|
|
res.push(CfgFlag::KeyValue { key: key.to_string(), value: ty.into() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rustc_cfgs = {
|
2021-05-08 16:17:18 +00:00
|
|
|
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,
|
|
|
|
)
|
2021-01-18 11:52:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match rustc_cfgs {
|
|
|
|
Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())),
|
|
|
|
Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|