Skip to content

Commit 9423c4f

Browse files
authored
Rollup merge of #70123 - cuviper:library-path, r=Mark-Simulacrum
Ensure LLVM is in the link path for rustc tools The build script for `rustc_llvm` outputs LLVM information in `cargo:rustc-link-lib` and `cargo:rustc-link-search` so the compiler can be linked correctly. However, while the lib is carried along in metadata, the search paths are not. So when cargo is invoked again later for rustc _tools_, they'll also try to link with LLVM, but the necessary paths may be left out. Rustbuild can use the environment to set the LLVM link path for tools -- `LIB` for MSVC toolchains and `LIBRARY_PATH` for everyone else. Fixes #68714.
2 parents bee074f + 3a2a442 commit 9423c4f

File tree

4 files changed

+57
-45
lines changed

4 files changed

+57
-45
lines changed

src/bootstrap/builder.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
1111
use std::process::Command;
1212
use std::time::{Duration, Instant};
1313

14-
use build_helper::t;
14+
use build_helper::{output, t};
1515

1616
use crate::cache::{Cache, Interned, INTERNER};
1717
use crate::check;
@@ -23,7 +23,7 @@ use crate::install;
2323
use crate::native;
2424
use crate::test;
2525
use crate::tool;
26-
use crate::util::{self, add_lib_path, exe, libdir};
26+
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir};
2727
use crate::{Build, DocTests, GitRepo, Mode};
2828

2929
pub use crate::Compiler;
@@ -660,7 +660,7 @@ impl<'a> Builder<'a> {
660660
return;
661661
}
662662

663-
add_lib_path(vec![self.rustc_libdir(compiler)], &mut cmd.command);
663+
add_dylib_path(vec![self.rustc_libdir(compiler)], &mut cmd.command);
664664
}
665665

666666
/// Gets a path to the compiler specified.
@@ -698,6 +698,20 @@ impl<'a> Builder<'a> {
698698
cmd
699699
}
700700

701+
/// Return the path to `llvm-config` for the target, if it exists.
702+
///
703+
/// Note that this returns `None` if LLVM is disabled, or if we're in a
704+
/// check build or dry-run, where there's no need to build all of LLVM.
705+
fn llvm_config(&self, target: Interned<String>) -> Option<PathBuf> {
706+
if self.config.llvm_enabled() && self.kind != Kind::Check && !self.config.dry_run {
707+
let llvm_config = self.ensure(native::Llvm { target });
708+
if llvm_config.is_file() {
709+
return Some(llvm_config);
710+
}
711+
}
712+
None
713+
}
714+
701715
/// Prepares an invocation of `cargo` to be run.
702716
///
703717
/// This will create a `Command` that represents a pending execution of
@@ -1034,6 +1048,17 @@ impl<'a> Builder<'a> {
10341048
.env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_libdir(compiler));
10351049
}
10361050

1051+
// Tools that use compiler libraries may inherit the `-lLLVM` link
1052+
// requirement, but the `-L` library path is not propagated across
1053+
// separate Cargo projects. We can add LLVM's library path to the
1054+
// platform-specific environment variable as a workaround.
1055+
if mode == Mode::ToolRustc {
1056+
if let Some(llvm_config) = self.llvm_config(target) {
1057+
let llvm_libdir = output(Command::new(&llvm_config).arg("--libdir"));
1058+
add_link_lib_path(vec![llvm_libdir.trim().into()], &mut cargo);
1059+
}
1060+
}
1061+
10371062
if self.config.incremental {
10381063
cargo.env("CARGO_INCREMENTAL", "1");
10391064
} else {

src/bootstrap/compile.rs

-38
Original file line numberDiff line numberDiff line change
@@ -451,44 +451,6 @@ impl Step for Rustc {
451451
false,
452452
);
453453

454-
// We used to build librustc_codegen_llvm as a separate step,
455-
// which produced a dylib that the compiler would dlopen() at runtime.
456-
// This meant that we only needed to make sure that libLLVM.so was
457-
// installed by the time we went to run a tool using it - since
458-
// librustc_codegen_llvm was effectively a standalone artifact,
459-
// other crates were completely oblivious to its dependency
460-
// on `libLLVM.so` during build time.
461-
//
462-
// However, librustc_codegen_llvm is now built as an ordinary
463-
// crate during the same step as the rest of the compiler crates.
464-
// This means that any crates depending on it will see the fact
465-
// that it uses `libLLVM.so` as a native library, and will
466-
// cause us to pass `-llibLLVM.so` to the linker when we link
467-
// a binary.
468-
//
469-
// For `rustc` itself, this works out fine.
470-
// During the `Assemble` step, we call `dist::maybe_install_llvm_dylib`
471-
// to copy libLLVM.so into the `stage` directory. We then link
472-
// the compiler binary, which will find `libLLVM.so` in the correct place.
473-
//
474-
// However, this is insufficient for tools that are build against stage0
475-
// (e.g. stage1 rustdoc). Since `Assemble` for stage0 doesn't actually do anything,
476-
// we won't have `libLLVM.so` in the stage0 sysroot. In the past, this wasn't
477-
// a problem - we would copy the tool binary into its correct stage directory
478-
// (e.g. stage1 for a stage1 rustdoc built against a stage0 compiler).
479-
// Since libLLVM.so wasn't resolved until runtime, it was fine for it to
480-
// not exist while we were building it.
481-
//
482-
// To ensure that we can still build stage1 tools against a stage0 compiler,
483-
// we explicitly copy libLLVM.so into the stage0 sysroot when building
484-
// the stage0 compiler. This ensures that tools built against stage0
485-
// will see libLLVM.so at build time, making the linker happy.
486-
if compiler.stage == 0 {
487-
builder.info(&format!("Installing libLLVM.so to stage 0 ({})", compiler.host));
488-
let sysroot = builder.sysroot(compiler);
489-
dist::maybe_install_llvm_dylib(builder, compiler.host, &sysroot);
490-
}
491-
492454
builder.ensure(RustcLink {
493455
compiler: builder.compiler(compiler.stage, builder.config.build),
494456
target_compiler: compiler,

src/bootstrap/tool.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::channel;
1212
use crate::channel::GitInfo;
1313
use crate::compile;
1414
use crate::toolstate::ToolState;
15-
use crate::util::{add_lib_path, exe, CiEnv};
15+
use crate::util::{add_dylib_path, exe, CiEnv};
1616
use crate::Compiler;
1717
use crate::Mode;
1818

@@ -388,7 +388,7 @@ pub struct ErrorIndex {
388388
impl ErrorIndex {
389389
pub fn command(builder: &Builder<'_>, compiler: Compiler) -> Command {
390390
let mut cmd = Command::new(builder.ensure(ErrorIndex { compiler }));
391-
add_lib_path(
391+
add_dylib_path(
392392
vec![PathBuf::from(&builder.sysroot_libdir(compiler, compiler.host))],
393393
&mut cmd,
394394
);
@@ -689,7 +689,7 @@ impl<'a> Builder<'a> {
689689
}
690690
}
691691

692-
add_lib_path(lib_paths, &mut cmd);
692+
add_dylib_path(lib_paths, &mut cmd);
693693
cmd
694694
}
695695
}

src/bootstrap/util.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn libdir(target: &str) -> &'static str {
4040
}
4141

4242
/// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
43-
pub fn add_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {
43+
pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut Command) {
4444
let mut list = dylib_path();
4545
for path in path {
4646
list.insert(0, path);
@@ -72,6 +72,31 @@ pub fn dylib_path() -> Vec<PathBuf> {
7272
env::split_paths(&var).collect()
7373
}
7474

75+
/// Adds a list of lookup paths to `cmd`'s link library lookup path.
76+
pub fn add_link_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {
77+
let mut list = link_lib_path();
78+
for path in path {
79+
list.insert(0, path);
80+
}
81+
cmd.env(link_lib_path_var(), t!(env::join_paths(list)));
82+
}
83+
84+
/// Returns the environment variable which the link library lookup path
85+
/// resides in for this platform.
86+
fn link_lib_path_var() -> &'static str {
87+
if cfg!(target_env = "msvc") { "LIB" } else { "LIBRARY_PATH" }
88+
}
89+
90+
/// Parses the `link_lib_path_var()` environment variable, returning a list of
91+
/// paths that are members of this lookup path.
92+
fn link_lib_path() -> Vec<PathBuf> {
93+
let var = match env::var_os(link_lib_path_var()) {
94+
Some(v) => v,
95+
None => return vec![],
96+
};
97+
env::split_paths(&var).collect()
98+
}
99+
75100
/// `push` all components to `buf`. On windows, append `.exe` to the last component.
76101
pub fn push_exe_path(mut buf: PathBuf, components: &[&str]) -> PathBuf {
77102
let (&file, components) = components.split_last().expect("at least one component required");

0 commit comments

Comments
 (0)