Skip to content

Commit d35b381

Browse files
Rollup merge of rust-lang#150151 - 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 b5d8fcf + 6c4c438 commit d35b381

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
@@ -435,6 +435,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
435435
&early_dcx,
436436
&config.opts.target_triple,
437437
config.opts.sysroot.path(),
438+
config.opts.unstable_opts.unstable_options,
438439
);
439440
let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
440441
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
@@ -1589,8 +1589,9 @@ pub fn build_target_config(
15891589
early_dcx: &EarlyDiagCtxt,
15901590
target: &TargetTuple,
15911591
sysroot: &Path,
1592+
unstable_options: bool,
15921593
) -> Target {
1593-
match Target::search(target, sysroot) {
1594+
match Target::search(target, sysroot, unstable_options) {
15941595
Ok((target, warnings)) => {
15951596
for warning in warnings.warning_messages() {
15961597
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
@@ -3298,10 +3298,19 @@ impl Target {
32983298
pub fn search(
32993299
target_tuple: &TargetTuple,
33003300
sysroot: &Path,
3301+
unstable_options: bool,
33013302
) -> Result<(Target, TargetWarnings), String> {
33023303
use std::{env, fs};
33033304

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

@@ -3338,7 +3347,7 @@ impl Target {
33383347
Path::new("target.json"),
33393348
]);
33403349
if p.is_file() {
3341-
return load_file(&p);
3350+
return load_file(&p, unstable_options);
33423351
}
33433352

33443353
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)