Skip to content
/ rust Public
forked from rust-lang/rust

Commit ac00a37

Browse files
authored
Rollup merge of rust-lang#139853 - Kobzol:lld-llvm-config, r=onur-ozkan
Disable combining LLD with external llvm-config When an external `llvm-config` is used, we don't really know anything about the external LLD, as we don't build it ourselves. Therefore, we probably shouldn't allow using `rust-lld` nor copy it to the target sysroot. Fixes: rust-lang#139477 CC ``@cuviper`` r? ``@onur-ozkan`` try-job: dist-x86_64-linux try-job: dist-aarch64-linux
2 parents 540fb22 + a870bba commit ac00a37

File tree

8 files changed

+65
-56
lines changed

8 files changed

+65
-56
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl Step for Std {
155155

156156
// When using `download-rustc`, we already have artifacts for the host available. Don't
157157
// recompile them.
158-
if builder.download_rustc() && builder.is_builder_target(target)
158+
if builder.download_rustc() && builder.config.is_host_target(target)
159159
// NOTE: the beta compiler may generate different artifacts than the downloaded compiler, so
160160
// its artifacts can't be reused.
161161
&& compiler.stage != 0
@@ -229,7 +229,7 @@ impl Step for Std {
229229
// The LLD wrappers and `rust-lld` are self-contained linking components that can be
230230
// necessary to link the stdlib on some targets. We'll also need to copy these binaries to
231231
// the `stage0-sysroot` to ensure the linker is found when bootstrapping on such a target.
232-
if compiler.stage == 0 && builder.is_builder_target(compiler.host) {
232+
if compiler.stage == 0 && builder.config.is_host_target(compiler.host) {
233233
trace!(
234234
"(build == host) copying linking components to `stage0-sysroot` for bootstrapping"
235235
);
@@ -1374,7 +1374,7 @@ pub fn rustc_cargo_env(
13741374
/// Pass down configuration from the LLVM build into the build of
13751375
/// rustc_llvm and rustc_codegen_llvm.
13761376
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1377-
if builder.is_rust_llvm(target) {
1377+
if builder.config.is_rust_llvm(target) {
13781378
cargo.env("LLVM_RUSTLLVM", "1");
13791379
}
13801380
if builder.config.llvm_enzyme {
@@ -2182,7 +2182,7 @@ impl Step for Assemble {
21822182
debug!("copying codegen backends to sysroot");
21832183
copy_codegen_backends_to_sysroot(builder, build_compiler, target_compiler);
21842184

2185-
if builder.config.lld_enabled {
2185+
if builder.config.lld_enabled && !builder.config.is_system_llvm(target_compiler.host) {
21862186
builder.ensure(crate::core::build_steps::tool::LldWrapper {
21872187
build_compiler,
21882188
target_compiler,
@@ -2532,7 +2532,9 @@ pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path)
25322532
// FIXME: to make things simpler for now, limit this to the host and target where we know
25332533
// `strip -g` is both available and will fix the issue, i.e. on a x64 linux host that is not
25342534
// cross-compiling. Expand this to other appropriate targets in the future.
2535-
if target != "x86_64-unknown-linux-gnu" || !builder.is_builder_target(target) || !path.exists()
2535+
if target != "x86_64-unknown-linux-gnu"
2536+
|| !builder.config.is_host_target(target)
2537+
|| !path.exists()
25362538
{
25372539
return;
25382540
}

src/bootstrap/src/core/build_steps/dist.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ impl Step for DebuggerScripts {
612612
fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
613613
// The only true set of target libraries came from the build triple, so
614614
// let's reduce redundant work by only producing archives from that host.
615-
if !builder.is_builder_target(compiler.host) {
615+
if !builder.config.is_host_target(compiler.host) {
616616
builder.info("\tskipping, not a build host");
617617
true
618618
} else {
@@ -671,7 +671,8 @@ fn copy_target_libs(
671671
&self_contained_dst.join(path.file_name().unwrap()),
672672
FileType::NativeLibrary,
673673
);
674-
} else if dependency_type == DependencyType::Target || builder.is_builder_target(target) {
674+
} else if dependency_type == DependencyType::Target || builder.config.is_host_target(target)
675+
{
675676
builder.copy_link(&path, &dst.join(path.file_name().unwrap()), FileType::NativeLibrary);
676677
}
677678
}
@@ -824,7 +825,7 @@ impl Step for Analysis {
824825
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
825826
let compiler = self.compiler;
826827
let target = self.target;
827-
if !builder.is_builder_target(compiler.host) {
828+
if !builder.config.is_host_target(compiler.host) {
828829
return None;
829830
}
830831

@@ -2118,7 +2119,7 @@ fn maybe_install_llvm(
21182119
//
21192120
// If the LLVM is coming from ourselves (just from CI) though, we
21202121
// still want to install it, as it otherwise won't be available.
2121-
if builder.is_system_llvm(target) {
2122+
if builder.config.is_system_llvm(target) {
21222123
trace!("system LLVM requested, no install");
21232124
return false;
21242125
}

src/bootstrap/src/core/build_steps/llvm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl Step for Llvm {
485485
}
486486

487487
// https://llvm.org/docs/HowToCrossCompileLLVM.html
488-
if !builder.is_builder_target(target) {
488+
if !builder.config.is_host_target(target) {
489489
let LlvmResult { llvm_config, .. } =
490490
builder.ensure(Llvm { target: builder.config.build });
491491
if !builder.config.dry_run() {
@@ -637,7 +637,7 @@ fn configure_cmake(
637637
}
638638
cfg.target(&target.triple).host(&builder.config.build.triple);
639639

640-
if !builder.is_builder_target(target) {
640+
if !builder.config.is_host_target(target) {
641641
cfg.define("CMAKE_CROSSCOMPILING", "True");
642642

643643
// NOTE: Ideally, we wouldn't have to do this, and `cmake-rs` would just handle it for us.
@@ -1098,7 +1098,7 @@ impl Step for Lld {
10981098
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
10991099
.define("LLVM_INCLUDE_TESTS", "OFF");
11001100

1101-
if !builder.is_builder_target(target) {
1101+
if !builder.config.is_host_target(target) {
11021102
// Use the host llvm-tblgen binary.
11031103
cfg.define(
11041104
"LLVM_TABLEGEN_EXE",

src/bootstrap/src/core/build_steps/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
18941894
.arg(llvm_components.trim());
18951895
llvm_components_passed = true;
18961896
}
1897-
if !builder.is_rust_llvm(target) {
1897+
if !builder.config.is_rust_llvm(target) {
18981898
cmd.arg("--system-llvm");
18991899
}
19001900

@@ -2668,7 +2668,7 @@ impl Step for Crate {
26682668
cargo
26692669
} else {
26702670
// Also prepare a sysroot for the target.
2671-
if !builder.is_builder_target(target) {
2671+
if !builder.config.is_host_target(target) {
26722672
builder.ensure(compile::Std::new(compiler, target).force_recompile(true));
26732673
builder.ensure(RemoteCopyLibs { compiler, target });
26742674
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,8 @@ fn test_is_builder_target() {
11071107
let build = Build::new(config);
11081108
let builder = Builder::new(&build);
11091109

1110-
assert!(builder.is_builder_target(target1));
1111-
assert!(!builder.is_builder_target(target2));
1110+
assert!(builder.config.is_host_target(target1));
1111+
assert!(!builder.config.is_host_target(target2));
11121112
}
11131113
}
11141114

src/bootstrap/src/core/config/config.rs

+42
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,12 @@ impl Config {
23972397
);
23982398
}
23992399

2400+
if config.lld_enabled && config.is_system_llvm(config.build) {
2401+
eprintln!(
2402+
"Warning: LLD is enabled when using external llvm-config. LLD will not be built and copied to the sysroot."
2403+
);
2404+
}
2405+
24002406
let default_std_features = BTreeSet::from([String::from("panic-unwind")]);
24012407
config.rust_std_features = std_features.unwrap_or(default_std_features);
24022408

@@ -3240,6 +3246,42 @@ impl Config {
32403246

32413247
Some(commit.to_string())
32423248
}
3249+
3250+
/// Checks if the given target is the same as the host target.
3251+
pub fn is_host_target(&self, target: TargetSelection) -> bool {
3252+
self.build == target
3253+
}
3254+
3255+
/// Returns `true` if this is an external version of LLVM not managed by bootstrap.
3256+
/// In particular, we expect llvm sources to be available when this is false.
3257+
///
3258+
/// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set.
3259+
pub fn is_system_llvm(&self, target: TargetSelection) -> bool {
3260+
match self.target_config.get(&target) {
3261+
Some(Target { llvm_config: Some(_), .. }) => {
3262+
let ci_llvm = self.llvm_from_ci && self.is_host_target(target);
3263+
!ci_llvm
3264+
}
3265+
// We're building from the in-tree src/llvm-project sources.
3266+
Some(Target { llvm_config: None, .. }) => false,
3267+
None => false,
3268+
}
3269+
}
3270+
3271+
/// Returns `true` if this is our custom, patched, version of LLVM.
3272+
///
3273+
/// This does not necessarily imply that we're managing the `llvm-project` submodule.
3274+
pub fn is_rust_llvm(&self, target: TargetSelection) -> bool {
3275+
match self.target_config.get(&target) {
3276+
// We're using a user-controlled version of LLVM. The user has explicitly told us whether the version has our patches.
3277+
// (They might be wrong, but that's not a supported use-case.)
3278+
// In particular, this tries to support `submodules = false` and `patches = false`, for using a newer version of LLVM that's not through `rust-lang/llvm-project`.
3279+
Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
3280+
// The user hasn't promised the patches match.
3281+
// This only has our patches if it's downloaded from CI or built from source.
3282+
_ => !self.is_system_llvm(target),
3283+
}
3284+
}
32433285
}
32443286

32453287
/// Compares the current `Llvm` options against those in the CI LLVM builder and detects any incompatible options.

src/bootstrap/src/core/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ than building it.
326326
if target.contains("musl") && !target.contains("unikraft") {
327327
// If this is a native target (host is also musl) and no musl-root is given,
328328
// fall back to the system toolchain in /usr before giving up
329-
if build.musl_root(*target).is_none() && build.is_builder_target(*target) {
329+
if build.musl_root(*target).is_none() && build.config.is_host_target(*target) {
330330
let target = build.config.target_config.entry(*target).or_default();
331331
target.musl_root = Some("/usr".into());
332332
}

src/bootstrap/src/lib.rs

+3-39
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use utils::channel::GitInfo;
3535

3636
use crate::core::builder;
3737
use crate::core::builder::Kind;
38-
use crate::core::config::{DryRun, LldMode, LlvmLibunwind, Target, TargetSelection, flags};
38+
use crate::core::config::{DryRun, LldMode, LlvmLibunwind, TargetSelection, flags};
3939
use crate::utils::exec::{BehaviorOnFailure, BootstrapCommand, CommandOutput, OutputMode, command};
4040
use crate::utils::helpers::{
4141
self, dir_is_empty, exe, libdir, output, set_file_times, split_debuginfo, symlink_dir,
@@ -803,7 +803,7 @@ impl Build {
803803
/// Note that if LLVM is configured externally then the directory returned
804804
/// will likely be empty.
805805
fn llvm_out(&self, target: TargetSelection) -> PathBuf {
806-
if self.config.llvm_from_ci && self.is_builder_target(target) {
806+
if self.config.llvm_from_ci && self.config.is_host_target(target) {
807807
self.config.ci_llvm_root()
808808
} else {
809809
self.out.join(target).join("llvm")
@@ -851,37 +851,6 @@ impl Build {
851851
if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
852852
}
853853

854-
/// Returns `true` if this is an external version of LLVM not managed by bootstrap.
855-
/// In particular, we expect llvm sources to be available when this is false.
856-
///
857-
/// NOTE: this is not the same as `!is_rust_llvm` when `llvm_has_patches` is set.
858-
fn is_system_llvm(&self, target: TargetSelection) -> bool {
859-
match self.config.target_config.get(&target) {
860-
Some(Target { llvm_config: Some(_), .. }) => {
861-
let ci_llvm = self.config.llvm_from_ci && self.is_builder_target(target);
862-
!ci_llvm
863-
}
864-
// We're building from the in-tree src/llvm-project sources.
865-
Some(Target { llvm_config: None, .. }) => false,
866-
None => false,
867-
}
868-
}
869-
870-
/// Returns `true` if this is our custom, patched, version of LLVM.
871-
///
872-
/// This does not necessarily imply that we're managing the `llvm-project` submodule.
873-
fn is_rust_llvm(&self, target: TargetSelection) -> bool {
874-
match self.config.target_config.get(&target) {
875-
// We're using a user-controlled version of LLVM. The user has explicitly told us whether the version has our patches.
876-
// (They might be wrong, but that's not a supported use-case.)
877-
// In particular, this tries to support `submodules = false` and `patches = false`, for using a newer version of LLVM that's not through `rust-lang/llvm-project`.
878-
Some(Target { llvm_has_rust_patches: Some(patched), .. }) => *patched,
879-
// The user hasn't promised the patches match.
880-
// This only has our patches if it's downloaded from CI or built from source.
881-
_ => !self.is_system_llvm(target),
882-
}
883-
}
884-
885854
/// Returns the path to `FileCheck` binary for the specified target
886855
fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
887856
let target_config = self.config.target_config.get(&target);
@@ -1356,7 +1325,7 @@ Executed at: {executed_at}"#,
13561325
// need to use CXX compiler as linker to resolve the exception functions
13571326
// that are only existed in CXX libraries
13581327
Some(self.cxx.borrow()[&target].path().into())
1359-
} else if !self.is_builder_target(target)
1328+
} else if !self.config.is_host_target(target)
13601329
&& helpers::use_host_linker(target)
13611330
&& !target.is_msvc()
13621331
{
@@ -2025,11 +1994,6 @@ to download LLVM rather than building it.
20251994
stream.reset().unwrap();
20261995
result
20271996
}
2028-
2029-
/// Checks if the given target is the same as the builder target.
2030-
fn is_builder_target(&self, target: TargetSelection) -> bool {
2031-
self.config.build == target
2032-
}
20331997
}
20341998

20351999
#[cfg(unix)]

0 commit comments

Comments
 (0)