Skip to content

Commit 6c4c438

Browse files
committed
destabilise target-spec-json
1 parent 07a5b02 commit 6c4c438

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)