fix: Fix target layout fetching

This commit is contained in:
Lukas Wirth 2024-02-12 21:29:52 +01:00
parent cf8733353d
commit a7641a8f57
2 changed files with 45 additions and 29 deletions

View file

@ -67,7 +67,7 @@ fn get_rust_cfgs(
extra_env: &FxHashMap<String, String>,
config: RustcCfgConfig<'_>,
) -> anyhow::Result<String> {
match config {
let sysroot = match config {
RustcCfgConfig::Cargo(sysroot, cargo_oml) => {
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
let mut cmd = Command::new(cargo);
@ -79,9 +79,17 @@ fn get_rust_cfgs(
cmd.args(["--target", target]);
}
utf8_stdout(cmd).context("Unable to run `cargo rustc`")
match utf8_stdout(cmd) {
Ok(it) => return Ok(it),
Err(e) => {
tracing::warn!("failed to run `cargo rustc --print cfg`, falling back to invoking rustc directly: {e}");
sysroot
}
RustcCfgConfig::Rustc(sysroot) => {
}
}
RustcCfgConfig::Rustc(sysroot) => sysroot,
};
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
tracing::debug!(?rustc, "using explicit rustc from sysroot");
let mut cmd = Command::new(rustc);
@ -91,7 +99,5 @@ fn get_rust_cfgs(
cmd.args(["--target", target]);
}
utf8_stdout(cmd).context("Unable to run `rustc`")
}
}
utf8_stdout(cmd).context("unable to fetch cfgs via `rustc --print cfg -O`")
}

View file

@ -20,20 +20,34 @@ pub fn get(
target: Option<&str>,
extra_env: &FxHashMap<String, String>,
) -> anyhow::Result<String> {
let output = match config {
let process = |output: String| {
(|| 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")
})
};
let sysroot = 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"])
.args(["rustc", "--", "-Z", "unstable-options", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(["--target", target]);
}
utf8_stdout(cmd)
match utf8_stdout(cmd) {
Ok(output) => return process(output),
Err(e) => {
tracing::warn!("failed to run `cargo rustc --print target-spec-json`, falling back to invoking rustc directly: {e}");
sysroot
}
RustcDataLayoutConfig::Rustc(sysroot) => {
}
}
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
};
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
let mut cmd = Command::new(rustc);
cmd.envs(extra_env)
@ -42,9 +56,5 @@ pub fn get(
if let Some(target) = target {
cmd.args(["--target", target]);
}
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"))
process(utf8_stdout(cmd)?)
}