Skip to content

Commit 5572942

Browse files
authored
Rollup merge of rust-lang#150151 - davidtwco:destabilise-target-spec-json, r=Kivooeo
Destabilise `target-spec-json` Per rust-lang/compiler-team#944: > Per rust-lang#71009, the ability to load target spec JSONs was stabilised accidentally. Within the team, we've always considered the format to be unstable and have changed it freely. This has been feasible as custom targets can only be used with core, like any other target, and so custom targets de-facto require nightly to be used (i.e. to build core manually or use Cargo's -Zbuild-std). > > Current build-std RFCs (rust-lang/rfcs#3873, rust-lang/rfcs#3874) propose a mechanism for building core on stable (at the request of Rust for Linux), which combined with a stable target-spec-json format, permit the current format to be used much more widely on stable toolchains. This would prevent us from improving the format - making it less tied to LLVM, switching to TOML, enabling keys in the spec to be stabilised individually, etc. > > De-stabilising the format gives us the opportunity to improve the format before it is too challenging to do so. Internal company toolchains and projects like Rust for Linux already use target-spec-json, but must use nightly at some point while doing so, so while it could be inconvenient for those users to destabilise this, it is hoped that an minimal alternative that we could choose to stabilise can be proposed relatively quickly.
2 parents aa11d2a + 6c4c438 commit 5572942

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,10 @@ fn get_backend_from_raw_matches(
11241124
let backend_name = debug_flags
11251125
.iter()
11261126
.find_map(|x| x.strip_prefix("codegen-backend=").or(x.strip_prefix("codegen_backend=")));
1127+
let unstable_options = debug_flags.iter().find(|x| *x == "unstable-options").is_some();
11271128
let target = parse_target_triple(early_dcx, matches);
11281129
let sysroot = Sysroot::new(matches.opt_str("sysroot").map(PathBuf::from));
1129-
let target = config::build_target_config(early_dcx, &target, sysroot.path());
1130+
let target = config::build_target_config(early_dcx, &target, sysroot.path(), unstable_options);
11301131

11311132
get_codegen_backend(early_dcx, &sysroot, backend_name, &target)
11321133
}

compiler/rustc_interface/src/interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
427427
&early_dcx,
428428
&config.opts.target_triple,
429429
config.opts.sysroot.path(),
430+
config.opts.unstable_opts.unstable_options,
430431
);
431432
let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
432433
let path_mapping = config.opts.file_path_mapping();

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ where
4646
&early_dcx,
4747
&sessopts.target_triple,
4848
sessopts.sysroot.path(),
49+
sessopts.unstable_opts.unstable_options,
4950
);
5051
let hash_kind = sessopts.unstable_opts.src_hash_algorithm(&target);
5152
let checksum_hash_kind = sessopts.unstable_opts.checksum_hash_algorithm();

compiler/rustc_session/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,8 +1586,9 @@ pub fn build_target_config(
15861586
early_dcx: &EarlyDiagCtxt,
15871587
target: &TargetTuple,
15881588
sysroot: &Path,
1589+
unstable_options: bool,
15891590
) -> Target {
1590-
match Target::search(target, sysroot) {
1591+
match Target::search(target, sysroot, unstable_options) {
15911592
Ok((target, warnings)) => {
15921593
for warning in warnings.warning_messages() {
15931594
early_dcx.early_warn(warning)

compiler/rustc_session/src/session.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,11 @@ pub fn build_session(
10021002
}
10031003

10041004
let host_triple = TargetTuple::from_tuple(config::host_tuple());
1005-
let (host, target_warnings) = Target::search(&host_triple, sopts.sysroot.path())
1006-
.unwrap_or_else(|e| dcx.handle().fatal(format!("Error loading host specification: {e}")));
1005+
let (host, target_warnings) =
1006+
Target::search(&host_triple, sopts.sysroot.path(), sopts.unstable_opts.unstable_options)
1007+
.unwrap_or_else(|e| {
1008+
dcx.handle().fatal(format!("Error loading host specification: {e}"))
1009+
});
10071010
for warning in target_warnings.warning_messages() {
10081011
dcx.handle().warn(warning)
10091012
}

compiler/rustc_target/src/spec/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,10 +3299,19 @@ impl Target {
32993299
pub fn search(
33003300
target_tuple: &TargetTuple,
33013301
sysroot: &Path,
3302+
unstable_options: bool,
33023303
) -> Result<(Target, TargetWarnings), String> {
33033304
use std::{env, fs};
33043305

3305-
fn load_file(path: &Path) -> Result<(Target, TargetWarnings), String> {
3306+
fn load_file(
3307+
path: &Path,
3308+
unstable_options: bool,
3309+
) -> Result<(Target, TargetWarnings), String> {
3310+
if !unstable_options {
3311+
return Err(
3312+
"custom targets are unstable and require `-Zunstable-options`".to_string()
3313+
);
3314+
}
33063315
let contents = fs::read_to_string(path).map_err(|e| e.to_string())?;
33073316
Target::from_json(&contents)
33083317
}
@@ -3326,7 +3335,7 @@ impl Target {
33263335
for dir in env::split_paths(&target_path) {
33273336
let p = dir.join(&path);
33283337
if p.is_file() {
3329-
return load_file(&p);
3338+
return load_file(&p, unstable_options);
33303339
}
33313340
}
33323341

@@ -3339,7 +3348,7 @@ impl Target {
33393348
Path::new("target.json"),
33403349
]);
33413350
if p.is_file() {
3342-
return load_file(&p);
3351+
return load_file(&p, unstable_options);
33433352
}
33443353

33453354
Err(format!("could not find specification for target {target_tuple:?}"))

tests/run-make/target-specs/rmake.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ fn main() {
1515
.run_fail()
1616
.assert_stderr_contains("error loading target specification");
1717
rustc()
18+
.arg("-Zunstable-options")
1819
.input("foo.rs")
1920
.target("my-incomplete-platform.json")
2021
.run_fail()
2122
.assert_stderr_contains("missing field `llvm-target`");
23+
let test_platform = rustc()
24+
.input("foo.rs")
25+
.target("my-x86_64-unknown-linux-gnu-platform")
26+
.crate_type("lib")
27+
.emit("asm")
28+
.run_fail()
29+
.assert_stderr_contains("custom targets are unstable and require `-Zunstable-options`");
2230
rustc()
31+
.arg("-Zunstable-options")
2332
.env("RUST_TARGET_PATH", ".")
2433
.input("foo.rs")
2534
.target("my-awesome-platform")
2635
.crate_type("lib")
2736
.emit("asm")
2837
.run();
2938
rustc()
39+
.arg("-Zunstable-options")
3040
.env("RUST_TARGET_PATH", ".")
3141
.input("foo.rs")
3242
.target("my-x86_64-unknown-linux-gnu-platform")
@@ -52,27 +62,31 @@ fn main() {
5262
.actual_text("test-platform-2", test_platform_2)
5363
.run();
5464
rustc()
65+
.arg("-Zunstable-options")
5566
.input("foo.rs")
5667
.target("endianness-mismatch")
5768
.run_fail()
5869
.assert_stderr_contains(r#""data-layout" claims architecture is little-endian"#);
5970
rustc()
71+
.arg("-Zunstable-options")
6072
.input("foo.rs")
6173
.target("mismatching-data-layout")
6274
.crate_type("lib")
6375
.run_fail()
6476
.assert_stderr_contains("data-layout for target");
6577
rustc()
78+
.arg("-Zunstable-options")
6679
.input("foo.rs")
6780
.target("require-explicit-cpu")
6881
.crate_type("lib")
6982
.run_fail()
7083
.assert_stderr_contains("target requires explicitly specifying a cpu");
7184
rustc()
85+
.arg("-Zunstable-options")
7286
.input("foo.rs")
7387
.target("require-explicit-cpu")
7488
.crate_type("lib")
7589
.arg("-Ctarget-cpu=generic")
7690
.run();
77-
rustc().target("require-explicit-cpu").arg("--print=target-cpus").run();
91+
rustc().arg("-Zunstable-options").target("require-explicit-cpu").print("target-cpus").run();
7892
}

0 commit comments

Comments
 (0)