Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<String> = 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,
Expand Down
41 changes: 9 additions & 32 deletions tests/build-std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")
Expand All @@ -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.
Expand Down
33 changes: 11 additions & 22 deletions tests/testsuite/custom_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 7 additions & 10 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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#"
Expand Down
Loading