Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/project-model/src/rustc_cfg.rs')
| -rw-r--r-- | crates/project-model/src/rustc_cfg.rs | 88 |
1 files changed, 49 insertions, 39 deletions
diff --git a/crates/project-model/src/rustc_cfg.rs b/crates/project-model/src/rustc_cfg.rs index f8f9a5d544..d6e041b3ad 100644 --- a/crates/project-model/src/rustc_cfg.rs +++ b/crates/project-model/src/rustc_cfg.rs @@ -2,15 +2,21 @@ use std::process::Command; +use anyhow::Context; use rustc_hash::FxHashMap; use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot}; +pub(crate) enum Config<'a> { + Cargo(&'a ManifestPath), + Explicit(&'a Sysroot), + Discover, +} + pub(crate) fn get( - cargo_toml: Option<&ManifestPath>, - sysroot: Option<&Sysroot>, target: Option<&str>, extra_env: &FxHashMap<String, String>, + config: Config<'_>, ) -> Vec<CfgFlag> { let _p = profile::span("rustc_cfg::get"); let mut res = Vec::with_capacity(6 * 2 + 1); @@ -26,64 +32,68 @@ pub(crate) fn get( // Add miri cfg, which is useful for mir eval in stdlib res.push(CfgFlag::Atom("miri".into())); - match get_rust_cfgs(cargo_toml, sysroot, target, extra_env) { + 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 { 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())); + tracing::debug!(?rustc_cfgs, "rustc cfgs found"); + res.extend(rustc_cfgs); + } + Err(e) => { + tracing::error!(?e, "failed to get rustc cfgs") } - Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"), } res } fn get_rust_cfgs( - cargo_toml: Option<&ManifestPath>, - sysroot: Option<&Sysroot>, target: Option<&str>, extra_env: &FxHashMap<String, String>, + config: Config<'_>, ) -> anyhow::Result<String> { - if let Some(cargo_toml) = cargo_toml { - let mut cargo_config = Command::new(toolchain::cargo()); - cargo_config.envs(extra_env); - cargo_config - .current_dir(cargo_toml.parent()) - .args(["rustc", "-Z", "unstable-options", "--print", "cfg"]) - .env("RUSTC_BOOTSTRAP", "1"); - if let Some(target) = target { - cargo_config.args(["--target", target]); - } - match utf8_stdout(cargo_config) { - Ok(it) => return Ok(it), - Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"), - } - } + let mut cmd = match config { + Config::Cargo(cargo_toml) => { + 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]); + } - let rustc = match sysroot { - Some(sysroot) => { - let rustc = sysroot.discover_rustc()?.into(); - tracing::debug!(?rustc, "using rustc from sysroot"); - rustc + return utf8_stdout(cmd).context("Unable to run `cargo rustc`"); + } + Config::Explicit(sysroot) => { + let rustc: std::path::PathBuf = sysroot.discover_rustc()?.into(); + tracing::debug!(?rustc, "using explicit rustc from sysroot"); + Command::new(rustc) } - None => { + Config::Discover => { let rustc = toolchain::rustc(); tracing::debug!(?rustc, "using rustc from env"); - rustc + Command::new(rustc) } }; - // using unstable cargo features failed, fall back to using plain rustc - let mut cmd = Command::new(rustc); cmd.envs(extra_env); cmd.args(["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(["--target", target]); } - utf8_stdout(cmd) + + let out = utf8_stdout(cmd).context("Unable to run `rustc`")?; + Ok(out) } |