diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index ef593b24d26..014497a298a 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -50,6 +50,7 @@ use std::fs; use std::os; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; +use std::sync::LazyLock; use std::sync::OnceLock; use std::thread::JoinHandle; use std::time::{self, Duration}; @@ -1264,6 +1265,28 @@ pub fn basic_lib_manifest(name: &str) -> String { ) } +/// Gets a valid target spec JSON from rustc. +/// +/// To avoid any hardcoded value, this fetches `x86_64-unknown-none` target +/// spec JSON directly from `rustc`, as Cargo shouldn't know the JSON schema. +pub fn target_spec_json() -> &'static str { + static TARGET_SPEC_JSON: LazyLock = LazyLock::new(|| { + let json = std::process::Command::new("rustc") + .env("RUSTC_BOOTSTRAP", "1") + .arg("--print") + .arg("target-spec-json") + .arg("-Zunstable-options") + .arg("--target") + .arg("x86_64-unknown-none") + .output() + .expect("rustc --print target-spec-json") + .stdout; + String::from_utf8(json).expect("utf8 target spec json") + }); + + TARGET_SPEC_JSON.as_str() +} + struct RustcInfo { verbose_version: String, host: String, diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index fe7408fd6e6..2958b0abcae 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -20,7 +20,13 @@ #![allow(clippy::disallowed_methods)] -use cargo_test_support::{Execs, basic_manifest, paths, project, rustc_host, str}; +use cargo_test_support::Execs; +use cargo_test_support::basic_manifest; +use cargo_test_support::paths; +use cargo_test_support::project; +use cargo_test_support::rustc_host; +use cargo_test_support::str; +use cargo_test_support::target_spec_json; use cargo_test_support::{Project, prelude::*}; use std::env; use std::path::{Path, PathBuf}; @@ -262,20 +268,7 @@ fn cross_custom() { ) .file("dep/Cargo.toml", &basic_manifest("dep", "0.1.0")) .file("dep/src/lib.rs", "#![no_std] pub fn answer() -> u32 { 42 }") - .file( - "custom-target.json", - r#" - { - "llvm-target": "x86_64-unknown-none-gnu", - "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", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "os": "none", - "linker-flavor": "ld.lld" - } - "#, - ) + .file("custom-target.json", target_spec_json()) .build(); p.cargo("build --target custom-target.json -v") @@ -302,23 +295,7 @@ fn custom_test_framework() { } "#, ) - .file( - "target.json", - r#" - { - "llvm-target": "x86_64-unknown-none-gnu", - "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", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "os": "none", - "linker-flavor": "ld.lld", - "linker": "rust-lld", - "executables": true, - "panic-strategy": "abort" - } - "#, - ) + .file("target.json", target_spec_json()) .build(); // This is a bit of a hack to use the rust-lld that ships with most toolchains. diff --git a/tests/testsuite/custom_target.rs b/tests/testsuite/custom_target.rs index f89a1d6b2e6..4be2335ee38 100644 --- a/tests/testsuite/custom_target.rs +++ b/tests/testsuite/custom_target.rs @@ -3,7 +3,10 @@ use std::fs; use crate::prelude::*; -use cargo_test_support::{basic_manifest, project, str}; +use cargo_test_support::basic_manifest; +use cargo_test_support::project; +use cargo_test_support::str; +use cargo_test_support::target_spec_json; const MINIMAL_LIB: &str = r#" #![allow(internal_features)] @@ -31,20 +34,6 @@ pub trait Copy { } "#; -const SIMPLE_SPEC: &str = r#" -{ - "llvm-target": "x86_64-unknown-none-gnu", - "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", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "os": "none", - "linker-flavor": "ld.lld", - "linker": "rust-lld", - "executables": true -} -"#; - #[cargo_test(nightly, reason = "requires features no_core, lang_items")] fn custom_target_minimal() { let p = project() @@ -59,7 +48,7 @@ fn custom_target_minimal() { " .replace("__MINIMAL_LIB__", MINIMAL_LIB), ) - .file("custom-target.json", SIMPLE_SPEC) + .file("custom-target.json", target_spec_json()) .build(); p.cargo("build --lib --target custom-target.json -v").run(); @@ -127,7 +116,7 @@ fn custom_target_dependency() { " .replace("__MINIMAL_LIB__", MINIMAL_LIB), ) - .file("custom-target.json", SIMPLE_SPEC) + .file("custom-target.json", target_spec_json()) .build(); p.cargo("build --lib --target custom-target.json -v").run(); @@ -146,7 +135,7 @@ fn custom_bin_target() { " .replace("__MINIMAL_LIB__", MINIMAL_LIB), ) - .file("custom-bin-target.json", SIMPLE_SPEC) + .file("custom-bin-target.json", target_spec_json()) .build(); p.cargo("build --target custom-bin-target.json -v").run(); @@ -167,7 +156,7 @@ fn changing_spec_rebuilds() { " .replace("__MINIMAL_LIB__", MINIMAL_LIB), ) - .file("custom-target.json", SIMPLE_SPEC) + .file("custom-target.json", target_spec_json()) .build(); p.cargo("build --lib --target custom-target.json -v").run(); @@ -212,7 +201,7 @@ fn changing_spec_relearns_crate_types() { "#, ) .file("src/lib.rs", MINIMAL_LIB) - .file("custom-target.json", SIMPLE_SPEC) + .file("custom-target.json", target_spec_json()) .build(); p.cargo("build --lib --target custom-target.json -v") @@ -254,8 +243,8 @@ fn custom_target_ignores_filepath() { " .replace("__MINIMAL_LIB__", MINIMAL_LIB), ) - .file("b/custom-target.json", SIMPLE_SPEC) - .file("a/custom-target.json", SIMPLE_SPEC) + .file("b/custom-target.json", target_spec_json()) + .file("a/custom-target.json", target_spec_json()) .build(); // Should build the library the first time. diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index d06e8045149..bf36936f640 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -1,7 +1,12 @@ //! Tests for the `cargo rustc` command. use crate::prelude::*; -use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project, str}; +use cargo_test_support::basic_bin_manifest; +use cargo_test_support::basic_lib_manifest; +use cargo_test_support::basic_manifest; +use cargo_test_support::project; +use cargo_test_support::str; +use cargo_test_support::target_spec_json; #[cargo_test] fn build_lib_for_foo() { @@ -820,15 +825,7 @@ windows fn rustc_with_print_cfg_config_toml_env() { let p = project() .file("Cargo.toml", &basic_bin_manifest("foo")) - .file( - "targets/best-target.json", - r#"{ - "llvm-target": "x86_64-unknown-none", - "target-pointer-width": "64", - "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", - "arch": "x86_64" -}"#, - ) + .file("targets/best-target.json", target_spec_json()) .file( ".cargo/config.toml", r#"