Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/project-model/src/target_data_layout.rs')
-rw-r--r--crates/project-model/src/target_data_layout.rs46
1 files changed, 28 insertions, 18 deletions
diff --git a/crates/project-model/src/target_data_layout.rs b/crates/project-model/src/target_data_layout.rs
index cb995857ec..847829be18 100644
--- a/crates/project-model/src/target_data_layout.rs
+++ b/crates/project-model/src/target_data_layout.rs
@@ -3,16 +3,27 @@ use std::process::Command;
use rustc_hash::FxHashMap;
-use crate::{utf8_stdout, ManifestPath};
+use crate::{utf8_stdout, ManifestPath, Sysroot};
+
+/// Determines how `rustc --print target-spec-json` is discovered and invoked.
+pub enum RustcDataLayoutConfig<'a> {
+ /// Use `rustc --print target-spec-json`, either from with the binary from the sysroot or by discovering via
+ /// [`toolchain::rustc`].
+ Rustc(Option<&'a Sysroot>),
+ /// Use `cargo --print target-spec-json`, either from with the binary from the sysroot or by discovering via
+ /// [`toolchain::cargo`].
+ Cargo(Option<&'a Sysroot>, &'a ManifestPath),
+}
pub fn get(
- cargo_toml: Option<&ManifestPath>,
+ config: RustcDataLayoutConfig<'_>,
target: Option<&str>,
extra_env: &FxHashMap<String, String>,
) -> anyhow::Result<String> {
- let output = (|| {
- if let Some(cargo_toml) = cargo_toml {
- let mut cmd = Command::new(toolchain::rustc());
+ let output = match config {
+ RustcDataLayoutConfig::Cargo(sysroot, cargo_toml) => {
+ 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())
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
@@ -20,21 +31,20 @@ pub fn get(
if let Some(target) = target {
cmd.args(["--target", target]);
}
- match utf8_stdout(cmd) {
- Ok(it) => return Ok(it),
- Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
- }
+ utf8_stdout(cmd)
}
- // using unstable cargo features failed, fall back to using plain rustc
- let mut cmd = Command::new(toolchain::rustc());
- cmd.envs(extra_env)
- .args(["-Z", "unstable-options", "--print", "target-spec-json"])
- .env("RUSTC_BOOTSTRAP", "1");
- if let Some(target) = target {
- cmd.args(["--target", target]);
+ RustcDataLayoutConfig::Rustc(sysroot) => {
+ let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
+ let mut cmd = Command::new(rustc);
+ cmd.envs(extra_env)
+ .args(["-Z", "unstable-options", "--print", "target-spec-json"])
+ .env("RUSTC_BOOTSTRAP", "1");
+ if let Some(target) = target {
+ cmd.args(["--target", target]);
+ }
+ utf8_stdout(cmd)
}
- utf8_stdout(cmd)
- })()?;
+ }?;
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
.ok_or_else(|| anyhow::format_err!("could not fetch target-spec-json from command output"))
}