Skip to content

Commit 48a0795

Browse files
authored
Unrolled build for #149049
Rollup merge of #149049 - Zalathar:only-elf, r=clubby789 compiletest: Use JSON "binary-format" to decide `//@ only-elf` and `//@ ignore-elf` Some tests only apply to ELF targets, or want to enable different test revisions for ELF and non-ELF targets. So compiletest supports the `//@ only-elf` and `//@ ignore-elf` directives to make that possible. Historically, deciding whether the current target is an ELF target relied on a handful of ad-hoc string comparisons against the target tuple. However, nowadays compiletest has access to the output of `--print=all-target-specs-json`, and that JSON output specifies the binary format of each target, making it much easier to determine whether a target is ELF or not.
2 parents f9e7961 + 7560717 commit 48a0795

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::{BTreeSet, HashMap, HashSet};
23
use std::iter;
34
use std::process::Command;
@@ -1011,6 +1012,13 @@ pub struct TargetCfg {
10111012
// target spec).
10121013
pub(crate) rustc_abi: Option<String>,
10131014

1015+
/// ELF is the "default" binary format, so the compiler typically doesn't
1016+
/// emit a `"binary-format"` field for ELF targets.
1017+
///
1018+
/// See `impl ToJson for Target` in `compiler/rustc_target/src/spec/json.rs`.
1019+
#[serde(default = "default_binary_format_elf")]
1020+
pub(crate) binary_format: Cow<'static, str>,
1021+
10141022
// Not present in target cfg json output, additional derived information.
10151023
#[serde(skip)]
10161024
/// Supported target atomic widths: e.g. `8` to `128` or `ptr`. This is derived from the builtin
@@ -1032,6 +1040,10 @@ fn default_reloc_model() -> String {
10321040
"pic".into()
10331041
}
10341042

1043+
fn default_binary_format_elf() -> Cow<'static, str> {
1044+
Cow::Borrowed("elf")
1045+
}
1046+
10351047
#[derive(Eq, PartialEq, Clone, Debug, Default, serde::Deserialize)]
10361048
#[serde(rename_all = "kebab-case")]
10371049
pub enum Endian {

src/tools/compiletest/src/directives/cfg.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ fn parse_cfg_name_directive<'a>(
169169

170170
condition! {
171171
name: "elf",
172-
condition: !config.target.contains("windows")
173-
&& !config.target.contains("wasm")
174-
&& !config.target.contains("apple")
175-
&& !config.target.contains("aix")
176-
&& !config.target.contains("uefi"),
172+
condition: target_cfg.binary_format == "elf",
177173
message: "when the target binary format is ELF"
178174
}
179175

src/tools/compiletest/src/directives/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,29 @@ fn ignore_coverage() {
766766
assert!(check_ignore(&config, "//@ ignore-coverage-run"));
767767
}
768768

769+
#[test]
770+
fn only_ignore_elf() {
771+
let cases = &[
772+
("aarch64-apple-darwin", false),
773+
("aarch64-unknown-linux-gnu", true),
774+
("powerpc64-ibm-aix", false),
775+
("wasm32-unknown-unknown", false),
776+
("wasm32-wasip1", false),
777+
("x86_64-apple-darwin", false),
778+
("x86_64-pc-windows-msvc", false),
779+
("x86_64-unknown-freebsd", true),
780+
("x86_64-unknown-illumos", true),
781+
("x86_64-unknown-linux-gnu", true),
782+
("x86_64-unknown-none", true),
783+
("x86_64-unknown-uefi", false),
784+
];
785+
for &(target, is_elf) in cases {
786+
let config = &cfg().target(target).build();
787+
assert_eq!(is_elf, check_ignore(config, "//@ ignore-elf"), "`//@ ignore-elf` for {target}");
788+
assert_eq!(is_elf, !check_ignore(config, "//@ only-elf"), "`//@ only-elf` for {target}");
789+
}
790+
}
791+
769792
#[test]
770793
fn threads_support() {
771794
let threads = [

0 commit comments

Comments
 (0)