1 //! Runs `rustc --print cfg` to get built-in cfg flags.
3 use std::process::Command;
7 use crate::{cfg_flag::CfgFlag, utf8_stdout, CargoConfig, ManifestPath};
10 cargo_toml: Option<&ManifestPath>,
14 let _p = profile::span("rustc_cfg::get");
15 let mut res = Vec::with_capacity(6 * 2 + 1);
17 // Some nightly-only cfgs, which are required for stdlib
18 res.push(CfgFlag::Atom("target_thread_local".into()));
19 for ty in ["8", "16", "32", "64", "cas", "ptr"] {
20 for key in ["target_has_atomic", "target_has_atomic_load_store"] {
21 res.push(CfgFlag::KeyValue { key: key.to_string(), value: ty.into() });
25 match get_rust_cfgs(cargo_toml, target, config) {
28 "rustc cfgs found: {:?}",
31 .map(|it| it.parse::<CfgFlag>().map(|it| it.to_string()))
34 res.extend(rustc_cfgs.lines().filter_map(|it| it.parse().ok()));
36 Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"),
43 cargo_toml: Option<&ManifestPath>,
47 if let Some(cargo_toml) = cargo_toml {
48 let mut cargo_config = Command::new(toolchain::cargo());
49 cargo_config.envs(&config.extra_env);
51 .current_dir(cargo_toml.parent())
52 .args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
53 .env("RUSTC_BOOTSTRAP", "1");
54 if let Some(target) = target {
55 cargo_config.args(&["--target", target]);
57 match utf8_stdout(cargo_config) {
58 Ok(it) => return Ok(it),
59 Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
62 // using unstable cargo features failed, fall back to using plain rustc
63 let mut cmd = Command::new(toolchain::rustc());
64 cmd.envs(&config.extra_env);
65 cmd.args(&["--print", "cfg", "-O"]);
66 if let Some(target) = target {
67 cmd.args(&["--target", target]);