Skip to content

Commit 353dd71

Browse files
authored
Rollup merge of #112454 - ferrocene:pa-compiletest-dynamic-linking, r=davidtwco
Make compiletest aware of targets without dynamic linking Some parts of the compiletest internals and some tests require dynamic linking to work, which is not supported by all targets. Before this PR, this was handled by if branches matching on the target name. This PR loads whether a target supports dynamic linking or not from the target spec, and adds a `// needs-dynamic-linking` attribute for tests that require it. Note that I was not able to replace all the old conditions based on the target name, as some targets have `dynamic_linking: true` in their spec but pretend they don't have it in compiletest. Also, to get this to work I had to *partially* revert #111472 (cc `@djkoloski` `@tmandry` `@bjorn3).` On one hand, only the target spec contains whether a target supports dynamic linking, but on the other hand a subset of the fields can be overridden through `-C` flags (as far as I'm aware only `-C panic=$strategy`). The solution I came up with is to take the target spec as the base, and then override the panic strategy based on `--print=cfg`. Hopefully that should not break y'all again.
2 parents 1880e83 + 767c4b9 commit 353dd71

File tree

6 files changed

+35
-66
lines changed

6 files changed

+35
-66
lines changed

src/tools/compiletest/src/common.rs

+26-64
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl TargetCfgs {
453453
let mut all_families = HashSet::new();
454454
let mut all_pointer_widths = HashSet::new();
455455

456-
for (target, cfg) in targets.into_iter() {
456+
for (target, cfg) in targets.iter() {
457457
all_archs.insert(cfg.arch.clone());
458458
all_oses.insert(cfg.os.clone());
459459
all_oses_and_envs.insert(cfg.os_and_env());
@@ -464,11 +464,11 @@ impl TargetCfgs {
464464
}
465465
all_pointer_widths.insert(format!("{}bit", cfg.pointer_width));
466466

467-
all_targets.insert(target.into());
467+
all_targets.insert(target.clone());
468468
}
469469

470470
Self {
471-
current: Self::get_current_target_config(config),
471+
current: Self::get_current_target_config(config, &targets),
472472
all_targets,
473473
all_archs,
474474
all_oses,
@@ -480,16 +480,20 @@ impl TargetCfgs {
480480
}
481481
}
482482

483-
fn get_current_target_config(config: &Config) -> TargetCfg {
484-
let mut arch = None;
485-
let mut os = None;
486-
let mut env = None;
487-
let mut abi = None;
488-
let mut families = Vec::new();
489-
let mut pointer_width = None;
490-
let mut endian = None;
491-
let mut panic = None;
492-
483+
fn get_current_target_config(
484+
config: &Config,
485+
targets: &HashMap<String, TargetCfg>,
486+
) -> TargetCfg {
487+
let mut cfg = targets[&config.target].clone();
488+
489+
// To get the target information for the current target, we take the target spec obtained
490+
// from `--print=all-target-specs-json`, and then we enrich it with the information
491+
// gathered from `--print=cfg --target=$target`.
492+
//
493+
// This is done because some parts of the target spec can be overridden with `-C` flags,
494+
// which are respected for `--print=cfg` but not for `--print=all-target-specs-json`. The
495+
// code below extracts them from `--print=cfg`: make sure to only override fields that can
496+
// actually be changed with `-C` flags.
493497
for config in
494498
rustc_output(config, &["--print=cfg", "--target", &config.target]).trim().lines()
495499
{
@@ -507,60 +511,16 @@ impl TargetCfgs {
507511
})
508512
.unwrap_or_else(|| (config, None));
509513

510-
match name {
511-
"target_arch" => {
512-
arch = Some(value.expect("target_arch should be a key-value pair").to_string());
513-
}
514-
"target_os" => {
515-
os = Some(value.expect("target_os sould be a key-value pair").to_string());
516-
}
517-
"target_env" => {
518-
env = Some(value.expect("target_env should be a key-value pair").to_string());
519-
}
520-
"target_abi" => {
521-
abi = Some(value.expect("target_abi should be a key-value pair").to_string());
522-
}
523-
"target_family" => {
524-
families
525-
.push(value.expect("target_family should be a key-value pair").to_string());
526-
}
527-
"target_pointer_width" => {
528-
pointer_width = Some(
529-
value
530-
.expect("target_pointer_width should be a key-value pair")
531-
.parse::<u32>()
532-
.expect("target_pointer_width should be a valid u32"),
533-
);
534-
}
535-
"target_endian" => {
536-
endian = Some(match value.expect("target_endian should be a key-value pair") {
537-
"big" => Endian::Big,
538-
"little" => Endian::Little,
539-
_ => panic!("target_endian should be either 'big' or 'little'"),
540-
});
541-
}
542-
"panic" => {
543-
panic = Some(match value.expect("panic should be a key-value pair") {
544-
"abort" => PanicStrategy::Abort,
545-
"unwind" => PanicStrategy::Unwind,
546-
_ => panic!("panic should be either 'abort' or 'unwind'"),
547-
});
548-
}
549-
_ => (),
514+
match (name, value) {
515+
// Can be overridden with `-C panic=$strategy`.
516+
("panic", Some("abort")) => cfg.panic = PanicStrategy::Abort,
517+
("panic", Some("unwind")) => cfg.panic = PanicStrategy::Unwind,
518+
("panic", other) => panic!("unexpected value for panic cfg: {other:?}"),
519+
_ => {}
550520
}
551521
}
552522

553-
TargetCfg {
554-
arch: arch.expect("target configuration should specify target_arch"),
555-
os: os.expect("target configuration should specify target_os"),
556-
env: env.expect("target configuration should specify target_env"),
557-
abi: abi.expect("target configuration should specify target_abi"),
558-
families,
559-
pointer_width: pointer_width
560-
.expect("target configuration should specify target_pointer_width"),
561-
endian: endian.expect("target configuration should specify target_endian"),
562-
panic: panic.expect("target configuration should specify panic"),
563-
}
523+
cfg
564524
}
565525
}
566526

@@ -582,6 +542,8 @@ pub struct TargetCfg {
582542
endian: Endian,
583543
#[serde(rename = "panic-strategy", default)]
584544
pub(crate) panic: PanicStrategy,
545+
#[serde(default)]
546+
pub(crate) dynamic_linking: bool,
585547
}
586548

587549
impl TargetCfg {

src/tools/compiletest/src/header/needs.rs

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ pub(super) fn handle_needs(
130130
condition: config.git_hash,
131131
ignore_reason: "ignored when git hashes have been omitted for building",
132132
},
133+
Need {
134+
name: "needs-dynamic-linking",
135+
condition: config.target_cfg().dynamic_linking,
136+
ignore_reason: "ignored on targets without dynamic linking",
137+
},
133138
];
134139

135140
let (name, comment) = match ln.split_once([':', ' ']) {

src/tools/compiletest/src/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,8 @@ impl<'test> TestCx<'test> {
18101810
|| self.config.target.contains("wasm32")
18111811
|| self.config.target.contains("nvptx")
18121812
|| self.is_vxworks_pure_static()
1813-
|| self.config.target.contains("sgx")
18141813
|| self.config.target.contains("bpf")
1814+
|| !self.config.target_cfg().dynamic_linking
18151815
{
18161816
// We primarily compile all auxiliary libraries as dynamic libraries
18171817
// to avoid code size bloat and large binaries as much as possible

tests/ui/issues/issue-12133-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// aux-build:issue-12133-dylib2.rs
55
// ignore-emscripten no dylib support
66
// ignore-musl
7-
// ignore-sgx no dylib support
7+
// needs-dynamic-linking
88

99
// pretty-expanded FIXME #23616
1010

tests/ui/issues/issue-85461.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: -Cinstrument-coverage -Ccodegen-units=4 --crate-type dylib -Copt-level=0
22
// build-pass
33
// needs-profiler-support
4+
// needs-dynamic-linking
45

56
// Regression test for #85461 where MSVC sometimes fails to link instrument-coverage binaries
67
// with dead code and #[inline(always)].

tests/ui/proc-macro/crt-static.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// build-pass
88
// force-host
99
// no-prefer-dynamic
10+
// needs-dynamic-linking
1011

1112
#![crate_type = "proc-macro"]
1213

0 commit comments

Comments
 (0)