diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index f89ccb4c2a6a8..23c249d71e896 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -832,6 +832,40 @@ fn debuginfo_map_cflags(builder: &Builder<'_>, target: TargetSelection) -> Vec, + target: TargetSelection, + llvm_output: &LlvmOutput, + cfg: &mut cmake::Config, + ldflags: &mut LdFlags, +) { + // Apple has it's own ld64 linker, so don't use LLD on Darwin. + if target.contains("apple") { + return; + } + + if builder.config.llvm_use_linker.is_some() || !builder.config.lld_enabled || target.is_msvc() { + // Logic derived from `configure_llvm` + // ThinLTO is only available when building with LLVM, enabling LLD is required. + if builder.config.llvm_thin_lto { + ldflags.push_all("-fuse-ld=lld"); + } + return; + } + + let lld_bin = builder.ensure(Lld { target }).join("bin"); + ldflags.push_all(format!("-B{} -fuse-ld=lld", lld_bin.display())); + + if llvm_output.link_shared() { + // LLD in this case needs the LLVM lib, so tell where to look for it. + let mut dylib_path = helpers::dylib_path(); + dylib_path.insert(0, llvm_output.root_dir().join("lib")); + cfg.env(helpers::dylib_path_var(), t!(env::join_paths(&dylib_path))); + } +} + fn configure_cmake( builder: &Builder<'_>, target: TargetSelection, @@ -1180,13 +1214,8 @@ impl CommandLineStep for RustOffload { let mut cfg = cmake::Config::new(builder.src.join("compiler/rustc_llvm/llvm-wrapper/offload/")); - // Logic copied from `configure_llvm` - // ThinLTO is only available when building with LLVM, enabling LLD is required. - // Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin. let mut ldflags = LdFlags::default(); - if builder.config.llvm_thin_lto && !target.contains("apple") { - ldflags.push_all("-fuse-ld=lld"); - } + try_link_with_in_tree_lld(builder, target, &llvm_output, &mut cfg, &mut ldflags); configure_cmake(builder, target, &mut cfg, true, ldflags, CcFlags::default(), &[]); @@ -1397,13 +1426,8 @@ impl CommandLineStep for OmpOffload { cflags.push_all(format!(" -I {inc_dir}")); } - // Logic copied from `configure_llvm` - // ThinLTO is only available when building with LLVM, enabling LLD is required. - // Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin. let mut ldflags = LdFlags::default(); - if builder.config.llvm_thin_lto && !target.contains("apple") { - ldflags.push_all("-fuse-ld=lld"); - } + try_link_with_in_tree_lld(builder, target, &llvm_output, &mut cfg, &mut ldflags); if let Some(dir) = &cxx_lib_dir { ldflags.push_all(format!("-L{}", dir.display())); @@ -1563,13 +1587,8 @@ impl CommandLineStep for Enzyme { let mut cflags = CcFlags::default(); cflags.push_all("-Wno-deprecated"); - // Logic copied from `configure_llvm` - // ThinLTO is only available when building with LLVM, enabling LLD is required. - // Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin. let mut ldflags = LdFlags::default(); - if builder.config.llvm_thin_lto && !target.contains("apple") { - ldflags.push_all("-fuse-ld=lld"); - } + try_link_with_in_tree_lld(builder, target, &llvm_output, &mut cfg, &mut ldflags); configure_cmake(builder, target, &mut cfg, true, ldflags, cflags, &[]);