Skip to content

Commit a6c58d4

Browse files
authored
test: avoid hardcoded target spec json (#15880)
Read a real target spec JSON so we no longer need to hardcode a target spec JSON here. Cargo itself should not care about the spec schema. Let's stop bothering compiler contributors. * rust-lang/rust#144443 * rust-lang/rust#145764 (comment) * [#t-compiler > Is it possible to add a commit to a submodule? @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Is.20it.20possible.20to.20add.20a.20commit.20to.20a.20submodule.3F/near/535865215)
2 parents 2c2e683 + 56f44bb commit a6c58d4

File tree

4 files changed

+50
-64
lines changed

4 files changed

+50
-64
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::fs;
5050
use std::os;
5151
use std::path::{Path, PathBuf};
5252
use std::process::{Command, Output};
53+
use std::sync::LazyLock;
5354
use std::sync::OnceLock;
5455
use std::thread::JoinHandle;
5556
use std::time::{self, Duration};
@@ -1264,6 +1265,28 @@ pub fn basic_lib_manifest(name: &str) -> String {
12641265
)
12651266
}
12661267

1268+
/// Gets a valid target spec JSON from rustc.
1269+
///
1270+
/// To avoid any hardcoded value, this fetches `x86_64-unknown-none` target
1271+
/// spec JSON directly from `rustc`, as Cargo shouldn't know the JSON schema.
1272+
pub fn target_spec_json() -> &'static str {
1273+
static TARGET_SPEC_JSON: LazyLock<String> = LazyLock::new(|| {
1274+
let json = std::process::Command::new("rustc")
1275+
.env("RUSTC_BOOTSTRAP", "1")
1276+
.arg("--print")
1277+
.arg("target-spec-json")
1278+
.arg("-Zunstable-options")
1279+
.arg("--target")
1280+
.arg("x86_64-unknown-none")
1281+
.output()
1282+
.expect("rustc --print target-spec-json")
1283+
.stdout;
1284+
String::from_utf8(json).expect("utf8 target spec json")
1285+
});
1286+
1287+
TARGET_SPEC_JSON.as_str()
1288+
}
1289+
12671290
struct RustcInfo {
12681291
verbose_version: String,
12691292
host: String,

tests/build-std/main.rs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
2121
#![allow(clippy::disallowed_methods)]
2222

23-
use cargo_test_support::{Execs, basic_manifest, paths, project, rustc_host, str};
23+
use cargo_test_support::Execs;
24+
use cargo_test_support::basic_manifest;
25+
use cargo_test_support::paths;
26+
use cargo_test_support::project;
27+
use cargo_test_support::rustc_host;
28+
use cargo_test_support::str;
29+
use cargo_test_support::target_spec_json;
2430
use cargo_test_support::{Project, prelude::*};
2531
use std::env;
2632
use std::path::{Path, PathBuf};
@@ -262,20 +268,7 @@ fn cross_custom() {
262268
)
263269
.file("dep/Cargo.toml", &basic_manifest("dep", "0.1.0"))
264270
.file("dep/src/lib.rs", "#![no_std] pub fn answer() -> u32 { 42 }")
265-
.file(
266-
"custom-target.json",
267-
r#"
268-
{
269-
"llvm-target": "x86_64-unknown-none-gnu",
270-
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
271-
"arch": "x86_64",
272-
"target-endian": "little",
273-
"target-pointer-width": "64",
274-
"os": "none",
275-
"linker-flavor": "ld.lld"
276-
}
277-
"#,
278-
)
271+
.file("custom-target.json", target_spec_json())
279272
.build();
280273

281274
p.cargo("build --target custom-target.json -v")
@@ -302,23 +295,7 @@ fn custom_test_framework() {
302295
}
303296
"#,
304297
)
305-
.file(
306-
"target.json",
307-
r#"
308-
{
309-
"llvm-target": "x86_64-unknown-none-gnu",
310-
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
311-
"arch": "x86_64",
312-
"target-endian": "little",
313-
"target-pointer-width": "64",
314-
"os": "none",
315-
"linker-flavor": "ld.lld",
316-
"linker": "rust-lld",
317-
"executables": true,
318-
"panic-strategy": "abort"
319-
}
320-
"#,
321-
)
298+
.file("target.json", target_spec_json())
322299
.build();
323300

324301
// This is a bit of a hack to use the rust-lld that ships with most toolchains.

tests/testsuite/custom_target.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use std::fs;
44

55
use crate::prelude::*;
6-
use cargo_test_support::{basic_manifest, project, str};
6+
use cargo_test_support::basic_manifest;
7+
use cargo_test_support::project;
8+
use cargo_test_support::str;
9+
use cargo_test_support::target_spec_json;
710

811
const MINIMAL_LIB: &str = r#"
912
#![allow(internal_features)]
@@ -31,20 +34,6 @@ pub trait Copy {
3134
}
3235
"#;
3336

34-
const SIMPLE_SPEC: &str = r#"
35-
{
36-
"llvm-target": "x86_64-unknown-none-gnu",
37-
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
38-
"arch": "x86_64",
39-
"target-endian": "little",
40-
"target-pointer-width": "64",
41-
"os": "none",
42-
"linker-flavor": "ld.lld",
43-
"linker": "rust-lld",
44-
"executables": true
45-
}
46-
"#;
47-
4837
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
4938
fn custom_target_minimal() {
5039
let p = project()
@@ -59,7 +48,7 @@ fn custom_target_minimal() {
5948
"
6049
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
6150
)
62-
.file("custom-target.json", SIMPLE_SPEC)
51+
.file("custom-target.json", target_spec_json())
6352
.build();
6453

6554
p.cargo("build --lib --target custom-target.json -v").run();
@@ -127,7 +116,7 @@ fn custom_target_dependency() {
127116
"
128117
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
129118
)
130-
.file("custom-target.json", SIMPLE_SPEC)
119+
.file("custom-target.json", target_spec_json())
131120
.build();
132121

133122
p.cargo("build --lib --target custom-target.json -v").run();
@@ -146,7 +135,7 @@ fn custom_bin_target() {
146135
"
147136
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
148137
)
149-
.file("custom-bin-target.json", SIMPLE_SPEC)
138+
.file("custom-bin-target.json", target_spec_json())
150139
.build();
151140

152141
p.cargo("build --target custom-bin-target.json -v").run();
@@ -167,7 +156,7 @@ fn changing_spec_rebuilds() {
167156
"
168157
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
169158
)
170-
.file("custom-target.json", SIMPLE_SPEC)
159+
.file("custom-target.json", target_spec_json())
171160
.build();
172161

173162
p.cargo("build --lib --target custom-target.json -v").run();
@@ -212,7 +201,7 @@ fn changing_spec_relearns_crate_types() {
212201
"#,
213202
)
214203
.file("src/lib.rs", MINIMAL_LIB)
215-
.file("custom-target.json", SIMPLE_SPEC)
204+
.file("custom-target.json", target_spec_json())
216205
.build();
217206

218207
p.cargo("build --lib --target custom-target.json -v")
@@ -254,8 +243,8 @@ fn custom_target_ignores_filepath() {
254243
"
255244
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
256245
)
257-
.file("b/custom-target.json", SIMPLE_SPEC)
258-
.file("a/custom-target.json", SIMPLE_SPEC)
246+
.file("b/custom-target.json", target_spec_json())
247+
.file("a/custom-target.json", target_spec_json())
259248
.build();
260249

261250
// Should build the library the first time.

tests/testsuite/rustc.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
//! Tests for the `cargo rustc` command.
22
33
use crate::prelude::*;
4-
use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project, str};
4+
use cargo_test_support::basic_bin_manifest;
5+
use cargo_test_support::basic_lib_manifest;
6+
use cargo_test_support::basic_manifest;
7+
use cargo_test_support::project;
8+
use cargo_test_support::str;
9+
use cargo_test_support::target_spec_json;
510

611
#[cargo_test]
712
fn build_lib_for_foo() {
@@ -820,15 +825,7 @@ windows
820825
fn rustc_with_print_cfg_config_toml_env() {
821826
let p = project()
822827
.file("Cargo.toml", &basic_bin_manifest("foo"))
823-
.file(
824-
"targets/best-target.json",
825-
r#"{
826-
"llvm-target": "x86_64-unknown-none",
827-
"target-pointer-width": "64",
828-
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
829-
"arch": "x86_64"
830-
}"#,
831-
)
828+
.file("targets/best-target.json", target_spec_json())
832829
.file(
833830
".cargo/config.toml",
834831
r#"

0 commit comments

Comments
 (0)