From 4e53043395b747b267bee975080896fb781fe19f Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 18 Jul 2025 15:46:00 +0530 Subject: [PATCH 1/3] populate the target_config now before its usage and update the test accordingly --- src/bootstrap/src/core/builder/tests.rs | 2 ++ src/bootstrap/src/core/config/config.rs | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 51a906496923b..7f0c6fbd8545e 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1638,6 +1638,7 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 + [build] rustc 0 -> LldWrapper 1 [build] rustdoc 0 [doc] std 1 crates=[core] "); @@ -1653,6 +1654,7 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 + [build] rustc 0 -> LldWrapper 1 [build] rustdoc 0 [doc] std 1 crates=[alloc,core] "); diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 22a75183404fd..6e04f11542438 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -942,6 +942,7 @@ impl Config { config.rust_profile_use = flags_rust_profile_use; config.rust_profile_generate = flags_rust_profile_generate; + config.apply_target_config(toml.target); config.apply_rust_config(toml.rust, flags_warnings); config.reproducible_artifacts = flags_reproducible_artifact; @@ -967,8 +968,6 @@ impl Config { config.apply_gcc_config(toml.gcc); - config.apply_target_config(toml.target); - match ccache { Some(StringOrBool::String(ref s)) => config.ccache = Some(s.to_string()), Some(StringOrBool::Bool(true)) => { From 4ca5afdc7ac807da8e1beac0813515cc36ef1ac0 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 18 Jul 2025 17:09:29 +0530 Subject: [PATCH 2/3] 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 --- src/bootstrap/src/core/builder/tests.rs | 25 ++++++++++++++++-- src/bootstrap/src/core/config/toml/rust.rs | 30 ++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 7f0c6fbd8545e..81a526593b4d6 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -642,6 +642,7 @@ mod snapshot { }; use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata}; use crate::core::config::TargetSelection; + use crate::core::config::toml::rust::with_lld_opt_in_targets; use crate::utils::cache::Cache; use crate::utils::helpers::get_host_target; use crate::utils::tests::{ConfigBuilder, TestCtx}; @@ -1638,12 +1639,33 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 - [build] rustc 0 -> LldWrapper 1 [build] rustdoc 0 [doc] std 1 crates=[core] "); } + #[test] + fn test_lld_opt_in() { + let target: &'static str = Box::leak(Box::new(host_target())); + let slice: &'static [&'static str] = Box::leak(Box::new([target])); + + with_lld_opt_in_targets(slice, || { + let ctx = TestCtx::new(); + + insta::assert_snapshot!( + ctx.config("doc") + .path("core") + .override_target_no_std(&host_target()) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 0 -> LldWrapper 1 + [build] rustdoc 0 + [doc] std 1 crates=[core] + "); + }); + } + #[test] fn doc_library_no_std_target() { let ctx = TestCtx::new(); @@ -1654,7 +1676,6 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 - [build] rustc 0 -> LldWrapper 1 [build] rustdoc 0 [doc] std 1 crates=[alloc,core] "); diff --git a/src/bootstrap/src/core/config/toml/rust.rs b/src/bootstrap/src/core/config/toml/rust.rs index 307aa52294baf..60484ee9e9077 100644 --- a/src/bootstrap/src/core/config/toml/rust.rs +++ b/src/bootstrap/src/core/config/toml/rust.rs @@ -410,6 +410,31 @@ pub(crate) fn validate_codegen_backends(backends: Vec, section: &str) -> backends } +#[cfg(not(test))] +fn default_lld_opt_in_targets() -> Vec { + vec!["x86_64-unknown-linux-gnu".to_string()] +} + +#[cfg(test)] +thread_local! { + static TEST_LLD_OPT_IN_TARGETS: std::cell::RefCell>> = std::cell::RefCell::new(None); +} + +#[cfg(test)] +fn default_lld_opt_in_targets() -> Vec { + TEST_LLD_OPT_IN_TARGETS.with(|cell| cell.borrow().clone()).unwrap_or_default() +} + +#[cfg(test)] +pub fn with_lld_opt_in_targets(targets: Vec, f: impl FnOnce() -> R) -> R { + TEST_LLD_OPT_IN_TARGETS.with(|cell| { + let prev = cell.replace(Some(targets)); + let result = f(); + cell.replace(prev); + result + }) +} + impl Config { pub fn apply_rust_config(&mut self, toml_rust: Option, warnings: Warnings) { let mut debug = None; @@ -609,12 +634,13 @@ impl Config { // thus, disabled // - similarly, lld will not be built nor used by default when explicitly asked not to, e.g. // when the config sets `rust.lld = false` - if self.host_target.triple == "x86_64-unknown-linux-gnu" && self.hosts == [self.host_target] + if default_lld_opt_in_targets().contains(&self.host_target.triple.to_string()) + && self.hosts == [self.host_target] { let no_llvm_config = self .target_config .get(&self.host_target) - .is_some_and(|target_config| target_config.llvm_config.is_none()); + .is_none_or(|target_config| target_config.llvm_config.is_none()); let enable_lld = self.llvm_from_ci || no_llvm_config; // Prefer the config setting in case an explicit opt-out is needed. self.lld_enabled = lld_enabled.unwrap_or(enable_lld); From fb10084b84b23e042c8888490c771ae5a315cfab Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Mon, 21 Jul 2025 20:10:58 +0530 Subject: [PATCH 3/3] add test_lld_opt_in test --- src/bootstrap/src/core/builder/tests.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 81a526593b4d6..4cc57996f3fff 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1646,22 +1646,15 @@ mod snapshot { #[test] fn test_lld_opt_in() { - let target: &'static str = Box::leak(Box::new(host_target())); - let slice: &'static [&'static str] = Box::leak(Box::new([target])); - - with_lld_opt_in_targets(slice, || { + with_lld_opt_in_targets(vec![host_target()], || { let ctx = TestCtx::new(); - insta::assert_snapshot!( - ctx.config("doc") - .path("core") - .override_target_no_std(&host_target()) + ctx.config("build") + .path("compiler") .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 [build] rustc 0 -> LldWrapper 1 - [build] rustdoc 0 - [doc] std 1 crates=[core] "); }); }