2022-12-21 09:01:17 -06:00
|
|
|
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
|
|
|
|
use std::process::Command;
|
|
|
|
|
2023-01-19 12:21:44 -06:00
|
|
|
use anyhow::Result;
|
2022-12-21 09:01:17 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
|
|
|
use crate::{utf8_stdout, ManifestPath};
|
|
|
|
|
2023-02-06 14:18:20 -06:00
|
|
|
pub fn get(
|
2022-12-21 09:01:17 -06:00
|
|
|
cargo_toml: Option<&ManifestPath>,
|
|
|
|
target: Option<&str>,
|
|
|
|
extra_env: &FxHashMap<String, String>,
|
2023-01-19 12:21:44 -06:00
|
|
|
) -> Result<String> {
|
2022-12-21 09:01:17 -06:00
|
|
|
let output = (|| {
|
|
|
|
if let Some(cargo_toml) = cargo_toml {
|
|
|
|
let mut cmd = Command::new(toolchain::rustc());
|
|
|
|
cmd.envs(extra_env);
|
|
|
|
cmd.current_dir(cargo_toml.parent())
|
2022-12-30 02:05:03 -06:00
|
|
|
.args(["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
2022-12-21 09:01:17 -06:00
|
|
|
.env("RUSTC_BOOTSTRAP", "1");
|
|
|
|
if let Some(target) = target {
|
2022-12-30 02:05:03 -06:00
|
|
|
cmd.args(["--target", target]);
|
2022-12-21 09:01:17 -06:00
|
|
|
}
|
|
|
|
match utf8_stdout(cmd) {
|
|
|
|
Ok(it) => return Ok(it),
|
|
|
|
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// using unstable cargo features failed, fall back to using plain rustc
|
|
|
|
let mut cmd = Command::new(toolchain::rustc());
|
|
|
|
cmd.envs(extra_env)
|
2023-01-19 12:21:44 -06:00
|
|
|
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
|
2022-12-21 09:01:17 -06:00
|
|
|
.env("RUSTC_BOOTSTRAP", "1");
|
|
|
|
if let Some(target) = target {
|
2022-12-30 02:05:03 -06:00
|
|
|
cmd.args(["--target", target]);
|
2022-12-21 09:01:17 -06:00
|
|
|
}
|
|
|
|
utf8_stdout(cmd)
|
2023-01-19 12:21:44 -06:00
|
|
|
})()?;
|
|
|
|
(|| 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"))
|
2022-12-21 09:01:17 -06:00
|
|
|
}
|