Skip to content

Commit 0536b8d

Browse files
committed
Ensure LLVM is in the link path for rustc tools
1 parent 71f5aed commit 0536b8d

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/bootstrap/builder.rs

Lines changed: 16 additions & 2 deletions
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_dylib_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;
@@ -1034,6 +1034,20 @@ impl<'a> Builder<'a> {
10341034
.env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_libdir(compiler));
10351035
}
10361036

1037+
// Tools that use compiler libraries may inherit the `-lLLVM` link
1038+
// requirement, but the `-L` library path is not propagated across
1039+
// separate Cargo projects. We can add LLVM's library path to the
1040+
// platform-specific environment variable as a workaround.
1041+
//
1042+
// Note that this is disabled if LLVM itself is disabled or we're in a
1043+
// check build, where if we're in a check build there's no need to build
1044+
// all of LLVM and such.
1045+
if self.config.llvm_enabled() && self.kind != Kind::Check && mode == Mode::ToolRustc {
1046+
let llvm_config = self.ensure(native::Llvm { target });
1047+
let llvm_libdir = output(Command::new(&llvm_config).arg("--libdir"));
1048+
add_link_lib_path(vec![llvm_libdir.trim().into()], &mut cargo);
1049+
}
1050+
10371051
if self.config.incremental {
10381052
cargo.env("CARGO_INCREMENTAL", "1");
10391053
} else {

src/bootstrap/util.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)