Skip to content

Commit 6ee9339

Browse files
committed
add default_lld_opt_in_targets method and modified test accordingly
add test for lld opt in and also add thread_local defined state to change opt in targets make the config lld test parameter smoother to work, and have no_llvm_config set even when target_config is not present
1 parent 28371b8 commit 6ee9339

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/bootstrap/src/core/builder/tests.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ mod snapshot {
642642
};
643643
use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata};
644644
use crate::core::config::TargetSelection;
645+
use crate::core::config::toml::rust::with_lld_opt_in_targets;
645646
use crate::utils::cache::Cache;
646647
use crate::utils::helpers::get_host_target;
647648
use crate::utils::tests::{ConfigBuilder, TestCtx};
@@ -1647,12 +1648,33 @@ mod snapshot {
16471648
.render_steps(), @r"
16481649
[build] llvm <host>
16491650
[build] rustc 0 <host> -> rustc 1 <host>
1650-
[build] rustc 0 <host> -> LldWrapper 1 <host>
16511651
[build] rustdoc 0 <host>
16521652
[doc] std 1 <host> crates=[core]
16531653
");
16541654
}
16551655

1656+
#[test]
1657+
fn test_lld_opt_in() {
1658+
let target: &'static str = Box::leak(Box::new(host_target()));
1659+
let slice: &'static [&'static str] = Box::leak(Box::new([target]));
1660+
1661+
with_lld_opt_in_targets(slice, || {
1662+
let ctx = TestCtx::new();
1663+
1664+
insta::assert_snapshot!(
1665+
ctx.config("doc")
1666+
.path("core")
1667+
.override_target_no_std(&host_target())
1668+
.render_steps(), @r"
1669+
[build] llvm <host>
1670+
[build] rustc 0 <host> -> rustc 1 <host>
1671+
[build] rustc 0 <host> -> LldWrapper 1 <host>
1672+
[build] rustdoc 0 <host>
1673+
[doc] std 1 <host> crates=[core]
1674+
");
1675+
});
1676+
}
1677+
16561678
#[test]
16571679
fn doc_library_no_std_target() {
16581680
let ctx = TestCtx::new();
@@ -1663,7 +1685,6 @@ mod snapshot {
16631685
.render_steps(), @r"
16641686
[build] llvm <host>
16651687
[build] rustc 0 <host> -> rustc 1 <host>
1666-
[build] rustc 0 <host> -> LldWrapper 1 <host>
16671688
[build] rustdoc 0 <host>
16681689
[doc] std 1 <host> crates=[alloc,core]
16691690
");

src/bootstrap/src/core/config/toml/rust.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,31 @@ pub(crate) fn validate_codegen_backends(backends: Vec<String>, section: &str) ->
410410
backends
411411
}
412412

413+
#[cfg(not(test))]
414+
fn default_lld_opt_in_targets() -> Vec<String> {
415+
vec!["x86_64-unknown-linux-gnu".to_string()]
416+
}
417+
418+
#[cfg(test)]
419+
thread_local! {
420+
static TEST_LLD_OPT_IN_TARGETS: std::cell::RefCell<Option<Vec<String>>> = std::cell::RefCell::new(None);
421+
}
422+
423+
#[cfg(test)]
424+
fn default_lld_opt_in_targets() -> Vec<String> {
425+
TEST_LLD_OPT_IN_TARGETS.with(|cell| cell.borrow().clone()).unwrap_or_default()
426+
}
427+
428+
#[cfg(test)]
429+
pub fn with_lld_opt_in_targets<R>(targets: Vec<String>, f: impl FnOnce() -> R) -> R {
430+
TEST_LLD_OPT_IN_TARGETS.with(|cell| {
431+
let prev = cell.replace(Some(targets));
432+
let result = f();
433+
cell.replace(prev);
434+
result
435+
})
436+
}
437+
413438
impl Config {
414439
pub fn apply_rust_config(&mut self, toml_rust: Option<Rust>, warnings: Warnings) {
415440
let mut debug = None;
@@ -617,12 +642,13 @@ impl Config {
617642
// thus, disabled
618643
// - similarly, lld will not be built nor used by default when explicitly asked not to, e.g.
619644
// when the config sets `rust.lld = false`
620-
if self.host_target.triple == "x86_64-unknown-linux-gnu" && self.hosts == [self.host_target]
645+
if default_lld_opt_in_targets().contains(&self.host_target.triple.to_string())
646+
&& self.hosts == [self.host_target]
621647
{
622648
let no_llvm_config = self
623649
.target_config
624650
.get(&self.host_target)
625-
.is_some_and(|target_config| target_config.llvm_config.is_none());
651+
.is_none_or(|target_config| target_config.llvm_config.is_none());
626652
let enable_lld = self.llvm_from_ci || no_llvm_config;
627653
// Prefer the config setting in case an explicit opt-out is needed.
628654
self.lld_enabled = lld_enabled.unwrap_or(enable_lld);

0 commit comments

Comments
 (0)