2021-01-18 11:52:12 +00:00
|
|
|
//! Runs `rustc --print cfg` to get built-in cfg flags.
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
2022-09-19 15:31:08 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-05-08 16:17:18 +00:00
|
|
|
|
2022-09-19 15:31:08 +00:00
|
|
|
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath};
|
2021-01-18 11:52:12 +00:00
|
|
|
|
2022-08-18 21:41:17 +00:00
|
|
|
pub(crate) fn get(
|
|
|
|
cargo_toml: Option<&ManifestPath>,
|
|
|
|
target: Option<&str>,
|
2022-09-19 15:31:08 +00:00
|
|
|
extra_env: &FxHashMap<String, String>,
|
2022-08-18 21:41:17 +00:00
|
|
|
) -> 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()));
|
2021-10-22 06:23:29 +00:00
|
|
|
for ty in ["8", "16", "32", "64", "cas", "ptr"] {
|
|
|
|
for key in ["target_has_atomic", "target_has_atomic_load_store"] {
|
2021-01-18 11:52:12 +00:00
|
|
|
res.push(CfgFlag::KeyValue { key: key.to_string(), value: ty.into() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 19:29:46 +00:00
|
|
|
// Add miri cfg, which is useful for mir eval in stdlib
|
|
|
|
res.push(CfgFlag::Atom("miri".into()));
|
|
|
|
|
2022-09-19 15:31:08 +00:00
|
|
|
match get_rust_cfgs(cargo_toml, target, extra_env) {
|
2022-02-01 12:32:09 +00:00
|
|
|
Ok(rustc_cfgs) => {
|
|
|
|
tracing::debug!(
|
|
|
|
"rustc cfgs found: {:?}",
|
|
|
|
rustc_cfgs
|
|
|
|
.lines()
|
|
|
|
.map(|it| it.parse::<CfgFlag>().map(|it| it.to_string()))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
res.extend(rustc_cfgs.lines().filter_map(|it| it.parse().ok()));
|
|
|
|
}
|
|
|
|
Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"),
|
2021-01-18 11:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2021-05-08 22:07:04 +00:00
|
|
|
|
2022-08-18 21:41:17 +00:00
|
|
|
fn get_rust_cfgs(
|
|
|
|
cargo_toml: Option<&ManifestPath>,
|
|
|
|
target: Option<&str>,
|
2022-09-19 15:31:08 +00:00
|
|
|
extra_env: &FxHashMap<String, String>,
|
2023-06-19 12:01:47 +00:00
|
|
|
) -> anyhow::Result<String> {
|
2022-02-01 12:32:09 +00:00
|
|
|
if let Some(cargo_toml) = cargo_toml {
|
|
|
|
let mut cargo_config = Command::new(toolchain::cargo());
|
2022-09-19 15:31:08 +00:00
|
|
|
cargo_config.envs(extra_env);
|
2022-02-01 12:32:09 +00:00
|
|
|
cargo_config
|
|
|
|
.current_dir(cargo_toml.parent())
|
2022-12-30 08:05:03 +00:00
|
|
|
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
|
2022-02-01 12:32:09 +00:00
|
|
|
.env("RUSTC_BOOTSTRAP", "1");
|
|
|
|
if let Some(target) = target {
|
2022-12-30 08:05:03 +00:00
|
|
|
cargo_config.args(["--target", target]);
|
2021-05-08 22:07:04 +00:00
|
|
|
}
|
2022-02-01 12:32:09 +00:00
|
|
|
match utf8_stdout(cargo_config) {
|
|
|
|
Ok(it) => return Ok(it),
|
|
|
|
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
2021-05-08 22:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 12:32:09 +00:00
|
|
|
// using unstable cargo features failed, fall back to using plain rustc
|
|
|
|
let mut cmd = Command::new(toolchain::rustc());
|
2022-09-19 15:31:08 +00:00
|
|
|
cmd.envs(extra_env);
|
2022-12-30 08:05:03 +00:00
|
|
|
cmd.args(["--print", "cfg", "-O"]);
|
2022-02-01 12:32:09 +00:00
|
|
|
if let Some(target) = target {
|
2022-12-30 08:05:03 +00:00
|
|
|
cmd.args(["--target", target]);
|
2022-02-01 12:32:09 +00:00
|
|
|
}
|
|
|
|
utf8_stdout(cmd)
|
2021-05-08 22:07:04 +00:00
|
|
|
}
|