2021-01-18 11:52:12 +00:00
|
|
|
//! Runs `rustc --print cfg` to get built-in cfg flags.
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
2023-09-07 19:06:51 +00:00
|
|
|
use anyhow::Context;
|
2022-09-19 15:31:08 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-05-08 16:17:18 +00:00
|
|
|
|
2023-09-05 19:21:14 +00:00
|
|
|
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
|
2021-01-18 11:52:12 +00:00
|
|
|
|
2023-09-07 19:30:11 +00:00
|
|
|
/// Determines how `rustc --print cfg` is discovered and invoked.
|
|
|
|
///
|
|
|
|
/// There options are supported:
|
|
|
|
/// - [`RustcCfgConfig::Cargo`], which relies on `cargo rustc --print cfg`
|
|
|
|
/// and `RUSTC_BOOTSTRAP`.
|
|
|
|
/// - [`RustcCfgConfig::Explicit`], which uses an explicit path to the `rustc`
|
|
|
|
/// binary in the sysroot.
|
|
|
|
/// - [`RustcCfgConfig::Discover`], which uses [`toolchain::rustc`].
|
2023-09-07 19:19:04 +00:00
|
|
|
pub(crate) enum RustcCfgConfig<'a> {
|
2023-09-07 19:06:51 +00:00
|
|
|
Cargo(&'a ManifestPath),
|
|
|
|
Explicit(&'a Sysroot),
|
|
|
|
Discover,
|
|
|
|
}
|
|
|
|
|
2022-08-18 21:41:17 +00:00
|
|
|
pub(crate) fn get(
|
|
|
|
target: Option<&str>,
|
2022-09-19 15:31:08 +00:00
|
|
|
extra_env: &FxHashMap<String, String>,
|
2023-09-07 19:19:04 +00:00
|
|
|
config: RustcCfgConfig<'_>,
|
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()));
|
|
|
|
|
2023-09-07 19:06:51 +00:00
|
|
|
let rustc_cfgs = get_rust_cfgs(target, extra_env, config);
|
|
|
|
|
|
|
|
let rustc_cfgs = match rustc_cfgs {
|
|
|
|
Ok(cfgs) => cfgs,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(?e, "failed to get rustc cfgs");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let rustc_cfgs =
|
|
|
|
rustc_cfgs.lines().map(|it| it.parse::<CfgFlag>()).collect::<Result<Vec<_>, _>>();
|
|
|
|
|
|
|
|
match rustc_cfgs {
|
2022-02-01 12:32:09 +00:00
|
|
|
Ok(rustc_cfgs) => {
|
2023-09-07 19:06:51 +00:00
|
|
|
tracing::debug!(?rustc_cfgs, "rustc cfgs found");
|
|
|
|
res.extend(rustc_cfgs);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(?e, "failed to get rustc cfgs")
|
2022-02-01 12:32:09 +00:00
|
|
|
}
|
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(
|
|
|
|
target: Option<&str>,
|
2022-09-19 15:31:08 +00:00
|
|
|
extra_env: &FxHashMap<String, String>,
|
2023-09-07 19:19:04 +00:00
|
|
|
config: RustcCfgConfig<'_>,
|
2023-06-19 12:01:47 +00:00
|
|
|
) -> anyhow::Result<String> {
|
2023-09-07 19:06:51 +00:00
|
|
|
let mut cmd = match config {
|
2023-09-07 19:19:04 +00:00
|
|
|
RustcCfgConfig::Cargo(cargo_toml) => {
|
2023-09-07 19:06:51 +00:00
|
|
|
let mut cmd = Command::new(toolchain::cargo());
|
|
|
|
cmd.envs(extra_env);
|
|
|
|
cmd.current_dir(cargo_toml.parent())
|
|
|
|
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
|
|
|
|
.env("RUSTC_BOOTSTRAP", "1");
|
|
|
|
if let Some(target) = target {
|
|
|
|
cmd.args(["--target", target]);
|
|
|
|
}
|
2023-09-05 19:21:14 +00:00
|
|
|
|
2023-09-07 19:06:51 +00:00
|
|
|
return utf8_stdout(cmd).context("Unable to run `cargo rustc`");
|
|
|
|
}
|
2023-09-07 19:19:04 +00:00
|
|
|
RustcCfgConfig::Explicit(sysroot) => {
|
2023-09-07 19:06:51 +00:00
|
|
|
let rustc: std::path::PathBuf = sysroot.discover_rustc()?.into();
|
|
|
|
tracing::debug!(?rustc, "using explicit rustc from sysroot");
|
|
|
|
Command::new(rustc)
|
2023-09-05 19:21:14 +00:00
|
|
|
}
|
2023-09-07 19:19:04 +00:00
|
|
|
RustcCfgConfig::Discover => {
|
2023-09-05 19:21:14 +00:00
|
|
|
let rustc = toolchain::rustc();
|
|
|
|
tracing::debug!(?rustc, "using rustc from env");
|
2023-09-07 19:06:51 +00:00
|
|
|
Command::new(rustc)
|
2023-09-05 19:21:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
2023-09-07 19:06:51 +00:00
|
|
|
|
2023-09-08 18:07:59 +00:00
|
|
|
utf8_stdout(cmd).context("Unable to run `rustc`")
|
2021-05-08 22:07:04 +00:00
|
|
|
}
|