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.rs53
1 files changed, 22 insertions, 31 deletions
diff --git a/crates/project-model/src/rustc_cfg.rs b/crates/project-model/src/rustc_cfg.rs
index 0aee002fbb..efbdfffd9a 100644
--- a/crates/project-model/src/rustc_cfg.rs
+++ b/crates/project-model/src/rustc_cfg.rs
@@ -8,17 +8,13 @@ use rustc_hash::FxHashMap;
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath, Sysroot};
/// 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`].
pub(crate) enum RustcCfgConfig<'a> {
- Cargo(&'a ManifestPath),
- Explicit(&'a Sysroot),
- Discover,
+ /// Use `rustc --print cfg`, either from with the binary from the sysroot or by discovering via
+ /// [`toolchain::rustc`].
+ Rustc(Option<&'a Sysroot>),
+ /// Use `cargo --print cfg`, either from with the binary from the sysroot or by discovering via
+ /// [`toolchain::cargo`].
+ Cargo(Option<&'a Sysroot>, &'a ManifestPath),
}
pub(crate) fn get(
@@ -71,36 +67,31 @@ fn get_rust_cfgs(
extra_env: &FxHashMap<String, String>,
config: RustcCfgConfig<'_>,
) -> anyhow::Result<String> {
- let mut cmd = match config {
- RustcCfgConfig::Cargo(cargo_toml) => {
- let mut cmd = Command::new(toolchain::cargo());
+ match config {
+ RustcCfgConfig::Cargo(sysroot, cargo_oml) => {
+ let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
+ let mut cmd = Command::new(cargo);
cmd.envs(extra_env);
- cmd.current_dir(cargo_toml.parent())
+ cmd.current_dir(cargo_oml.parent())
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(["--target", target]);
}
- return utf8_stdout(cmd).context("Unable to run `cargo rustc`");
+ utf8_stdout(cmd).context("Unable to run `cargo rustc`")
}
- RustcCfgConfig::Explicit(sysroot) => {
- let rustc: std::path::PathBuf = sysroot.discover_rustc()?.into();
+ RustcCfgConfig::Rustc(sysroot) => {
+ let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
tracing::debug!(?rustc, "using explicit rustc from sysroot");
- Command::new(rustc)
- }
- RustcCfgConfig::Discover => {
- let rustc = toolchain::rustc();
- tracing::debug!(?rustc, "using rustc from env");
- Command::new(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]);
+ }
- cmd.envs(extra_env);
- cmd.args(["--print", "cfg", "-O"]);
- if let Some(target) = target {
- cmd.args(["--target", target]);
+ utf8_stdout(cmd).context("Unable to run `rustc`")
+ }
}
-
- utf8_stdout(cmd).context("Unable to run `rustc`")
}