Skip to content

Commit 4ca5afd

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 4e53043 commit 4ca5afd

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};
@@ -1638,12 +1639,33 @@ mod snapshot {
16381639
.render_steps(), @r"
16391640
[build] llvm <host>
16401641
[build] rustc 0 <host> -> rustc 1 <host>
1641-
[build] rustc 0 <host> -> LldWrapper 1 <host>
16421642
[build] rustdoc 0 <host>
16431643
[doc] std 1 <host> crates=[core]
16441644
");
16451645
}
16461646

1647+
#[test]
1648+
fn test_lld_opt_in() {
1649+
let target: &'static str = Box::leak(Box::new(host_target()));
1650+
let slice: &'static [&'static str] = Box::leak(Box::new([target]));
1651+
1652+
with_lld_opt_in_targets(slice, || {
1653+
let ctx = TestCtx::new();
1654+
1655+
insta::assert_snapshot!(
1656+
ctx.config("doc")
1657+
.path("core")
1658+
.override_target_no_std(&host_target())
1659+
.render_steps(), @r"
1660+
[build] llvm <host>
1661+
[build] rustc 0 <host> -> rustc 1 <host>
1662+
[build] rustc 0 <host> -> LldWrapper 1 <host>
1663+
[build] rustdoc 0 <host>
1664+
[doc] std 1 <host> crates=[core]
1665+
");
1666+
});
1667+
}
1668+
16471669
#[test]
16481670
fn doc_library_no_std_target() {
16491671
let ctx = TestCtx::new();
@@ -1654,7 +1676,6 @@ mod snapshot {
16541676
.render_steps(), @r"
16551677
[build] llvm <host>
16561678
[build] rustc 0 <host> -> rustc 1 <host>
1657-
[build] rustc 0 <host> -> LldWrapper 1 <host>
16581679
[build] rustdoc 0 <host>
16591680
[doc] std 1 <host> crates=[alloc,core]
16601681
");

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;
@@ -609,12 +634,13 @@ impl Config {
609634
// thus, disabled
610635
// - similarly, lld will not be built nor used by default when explicitly asked not to, e.g.
611636
// when the config sets `rust.lld = false`
612-
if self.host_target.triple == "x86_64-unknown-linux-gnu" && self.hosts == [self.host_target]
637+
if default_lld_opt_in_targets().contains(&self.host_target.triple.to_string())
638+
&& self.hosts == [self.host_target]
613639
{
614640
let no_llvm_config = self
615641
.target_config
616642
.get(&self.host_target)
617-
.is_some_and(|target_config| target_config.llvm_config.is_none());
643+
.is_none_or(|target_config| target_config.llvm_config.is_none());
618644
let enable_lld = self.llvm_from_ci || no_llvm_config;
619645
// Prefer the config setting in case an explicit opt-out is needed.
620646
self.lld_enabled = lld_enabled.unwrap_or(enable_lld);

0 commit comments

Comments
 (0)