Fix tests not using appropriate target data

This commit is contained in:
Lukas Wirth 2022-12-21 16:01:17 +01:00
parent 33591cd3f4
commit a694c342fa
7 changed files with 111 additions and 55 deletions

View file

@ -25,6 +25,7 @@ mod sysroot;
mod workspace;
mod rustc_cfg;
mod build_scripts;
mod target_data_layout;
#[cfg(test)]
mod tests;

View file

@ -0,0 +1,40 @@
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
use std::process::Command;
use rustc_hash::FxHashMap;
use crate::{utf8_stdout, ManifestPath};
pub(super) fn get(
cargo_toml: Option<&ManifestPath>,
target: Option<&str>,
extra_env: &FxHashMap<String, String>,
) -> Option<String> {
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())
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
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"),
}
}
// 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", "rustc", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(&["--target", target]);
}
utf8_stdout(cmd)
})()
.ok()?;
Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned())
}

View file

@ -151,6 +151,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
"debug_assertions",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -220,6 +221,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
"debug_assertions",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -298,6 +300,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
"debug_assertions",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -376,6 +379,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
"debug_assertions",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -463,6 +467,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
"feature=use_std",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -548,6 +553,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -619,6 +625,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -699,6 +706,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -779,6 +787,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -866,6 +875,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
"feature=use_std",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -942,6 +952,7 @@ fn cargo_hello_world_project_model() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -1013,6 +1024,7 @@ fn cargo_hello_world_project_model() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -1093,6 +1105,7 @@ fn cargo_hello_world_project_model() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -1173,6 +1186,7 @@ fn cargo_hello_world_project_model() {
"test",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -1260,6 +1274,7 @@ fn cargo_hello_world_project_model() {
"feature=use_std",
],
),
target_layout: None,
env: Env {
entries: {
"CARGO_PKG_LICENSE": "",
@ -1328,6 +1343,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1372,6 +1388,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1406,6 +1423,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1440,6 +1458,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1474,6 +1493,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1518,6 +1538,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1552,6 +1573,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1659,6 +1681,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1693,6 +1716,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1727,6 +1751,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},
@ -1761,6 +1786,7 @@ fn rust_project_hello_world_project_model() {
potential_cfg_options: CfgOptions(
[],
),
target_layout: None,
env: Env {
entries: {},
},

View file

@ -21,8 +21,8 @@ use crate::{
cfg_flag::CfgFlag,
rustc_cfg,
sysroot::SysrootCrate,
utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath, Package,
ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts,
target_data_layout, utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath,
Package, ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts,
};
/// A set of cfg-overrides per crate.
@ -143,40 +143,6 @@ impl fmt::Debug for ProjectWorkspace {
}
}
fn data_layout(
cargo_toml: Option<&ManifestPath>,
target: Option<&str>,
extra_env: &FxHashMap<String, String>,
) -> Option<String> {
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())
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
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"),
}
}
// 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", "rustc", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(&["--target", target]);
}
utf8_stdout(cmd)
})()
.ok()?;
Some(output.split_once(r#""data-layout": "#)?.1.trim_matches('"').to_owned())
}
impl ProjectWorkspace {
pub fn load(
manifest: ProjectManifest,
@ -278,8 +244,11 @@ impl ProjectWorkspace {
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
let cfg_overrides = config.cfg_overrides();
let data_layout =
data_layout(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
let data_layout = target_data_layout::get(
Some(&cargo_toml),
config.target.as_deref(),
&config.extra_env,
);
ProjectWorkspace::Cargo {
cargo,
build_scripts: WorkspaceBuildScripts::default(),