Skip to content

Commit 0e6d205

Browse files
committed
use target specs rather than --print=cfg to discover targets
1 parent ef2bf6d commit 0e6d205

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::util::{add_dylib_path, PathBufExt};
1111
use lazycell::LazyCell;
1212
use std::collections::HashSet;
1313
use test::{ColorConfig, OutputFormat};
14+
use serde::de::{Deserialize, Deserializer, Error as _};
1415

1516
macro_rules! string_enum {
1617
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
@@ -114,8 +115,10 @@ string_enum! {
114115
}
115116
}
116117

117-
#[derive(Clone, Copy, Debug, PartialEq)]
118+
#[derive(Clone, Copy, Debug, PartialEq, Default, serde::Deserialize)]
119+
#[serde(rename_all = "kebab-case")]
118120
pub enum PanicStrategy {
121+
#[default]
119122
Unwind,
120123
Abort,
121124
}
@@ -450,72 +453,43 @@ impl TargetCfgs {
450453
}
451454
}
452455

453-
#[derive(Clone, Debug)]
456+
#[derive(Clone, Debug, serde::Deserialize)]
457+
#[serde(rename_all = "kebab-case")]
454458
pub struct TargetCfg {
455459
pub(crate) arch: String,
460+
#[serde(default)]
456461
pub(crate) os: String,
462+
#[serde(default)]
457463
pub(crate) env: String,
464+
#[serde(default)]
458465
pub(crate) abi: String,
466+
#[serde(rename = "target-family", default)]
459467
pub(crate) families: Vec<String>,
468+
#[serde(rename = "target-pointer-width", deserialize_with = "serde_parse_u32")]
460469
pub(crate) pointer_width: u32,
470+
#[serde(rename = "target-endian", default)]
461471
endian: Endian,
472+
#[serde(rename = "panic-strategy", default)]
462473
panic: PanicStrategy,
463474
}
464475

465-
#[derive(Eq, PartialEq, Clone, Debug)]
476+
#[derive(Eq, PartialEq, Clone, Debug, Default, serde::Deserialize)]
477+
#[serde(rename_all = "kebab-case")]
466478
pub enum Endian {
479+
#[default]
467480
Little,
468481
Big,
469482
}
470483

471484
impl TargetCfg {
472485
fn new(config: &Config, target: &str) -> TargetCfg {
473-
let print_cfg = rustc_output(config, &["--print=cfg", "--target", target]);
474-
let mut arch = None;
475-
let mut os = None;
476-
let mut env = None;
477-
let mut abi = None;
478-
let mut families = Vec::new();
479-
let mut pointer_width = None;
480-
let mut endian = None;
481-
let mut panic = None;
482-
for line in print_cfg.lines() {
483-
if let Some((name, value)) = line.split_once('=') {
484-
let value = value.trim_matches('"');
485-
match name {
486-
"target_arch" => arch = Some(value),
487-
"target_os" => os = Some(value),
488-
"target_env" => env = Some(value),
489-
"target_abi" => abi = Some(value),
490-
"target_family" => families.push(value.to_string()),
491-
"target_pointer_width" => pointer_width = Some(value.parse().unwrap()),
492-
"target_endian" => {
493-
endian = Some(match value {
494-
"little" => Endian::Little,
495-
"big" => Endian::Big,
496-
s => panic!("unexpected {s}"),
497-
})
498-
}
499-
"panic" => {
500-
panic = match value {
501-
"abort" => Some(PanicStrategy::Abort),
502-
"unwind" => Some(PanicStrategy::Unwind),
503-
s => panic!("unexpected {s}"),
504-
}
505-
}
506-
_ => {}
507-
}
508-
}
509-
}
510-
TargetCfg {
511-
arch: arch.unwrap().to_string(),
512-
os: os.unwrap().to_string(),
513-
env: env.unwrap().to_string(),
514-
abi: abi.unwrap().to_string(),
515-
families,
516-
pointer_width: pointer_width.unwrap(),
517-
endian: endian.unwrap(),
518-
panic: panic.unwrap(),
486+
let json = rustc_output(
487+
config,
488+
&["--print=target-spec-json", "-Zunstable-options", "--target", target],
489+
);
490+
match serde_json::from_str(&json) {
491+
Ok(res) => res,
492+
Err(err) => panic!("failed to parse target spec for {target}: {err}"),
519493
}
520494
}
521495
}
@@ -524,6 +498,7 @@ fn rustc_output(config: &Config, args: &[&str]) -> String {
524498
let mut command = Command::new(&config.rustc_path);
525499
add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
526500
command.args(&config.target_rustcflags).args(args);
501+
command.env("RUSTC_BOOTSTRAP", "1");
527502

528503
let output = match command.output() {
529504
Ok(output) => output,
@@ -539,6 +514,11 @@ fn rustc_output(config: &Config, args: &[&str]) -> String {
539514
String::from_utf8(output.stdout).unwrap()
540515
}
541516

517+
fn serde_parse_u32<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> {
518+
let string = String::deserialize(deserializer)?;
519+
string.parse().map_err(D::Error::custom)
520+
}
521+
542522
#[derive(Debug, Clone)]
543523
pub struct TestPaths {
544524
pub file: PathBuf, // e.g., compile-test/foo/bar/baz.rs

0 commit comments

Comments
 (0)