Skip to content

Commit c064521

Browse files
committed
Ship LLVM tools for the correct target when cross-compiling
1 parent 7785efd commit c064521

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,14 +2001,52 @@ impl Step for Assemble {
20012001
if builder.config.llvm_enabled(target_compiler.host) {
20022002
trace!("target_compiler.host" = ?target_compiler.host, "LLVM enabled");
20032003

2004-
let llvm::LlvmResult { host_llvm_config, .. } =
2005-
builder.ensure(llvm::Llvm { target: target_compiler.host });
2004+
let target = target_compiler.host;
2005+
let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
20062006
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
20072007
trace!("LLVM tools enabled");
20082008

2009-
let llvm_bin_dir =
2010-
command(host_llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
2011-
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
2009+
let host_llvm_bin_dir = command(&host_llvm_config)
2010+
.arg("--bindir")
2011+
.run_capture_stdout(builder)
2012+
.stdout()
2013+
.trim()
2014+
.to_string();
2015+
2016+
let llvm_bin_dir = if target == builder.host_target {
2017+
PathBuf::from(host_llvm_bin_dir)
2018+
} else {
2019+
// If we're cross-compiling, we cannot run the target llvm-config in order to
2020+
// figure out where binaries are located. We thus have to guess.
2021+
let external_llvm_config = builder
2022+
.config
2023+
.target_config
2024+
.get(&target)
2025+
.and_then(|t| t.llvm_config.clone());
2026+
if let Some(external_llvm_config) = external_llvm_config {
2027+
// If we have an external LLVM, just hope that the bindir is the directory
2028+
// where the LLVM config is located
2029+
external_llvm_config.parent().unwrap().to_path_buf()
2030+
} else {
2031+
// If we have built LLVM locally, then take the path of the host bindir
2032+
// relative to its output build directory, and then apply it to the target
2033+
// LLVM output build directory.
2034+
let host_llvm_out = builder.llvm_out(builder.host_target);
2035+
let target_llvm_out = builder.llvm_out(target);
2036+
if let Ok(relative_path) =
2037+
Path::new(&host_llvm_bin_dir).strip_prefix(host_llvm_out)
2038+
{
2039+
target_llvm_out.join(relative_path)
2040+
} else {
2041+
// This is the most desperate option, just replace the host target with
2042+
// the actual target in the directory path...
2043+
PathBuf::from(
2044+
host_llvm_bin_dir
2045+
.replace(&*builder.host_target.triple, &target.triple),
2046+
)
2047+
}
2048+
}
2049+
};
20122050

20132051
// Since we've already built the LLVM tools, install them to the sysroot.
20142052
// This is the equivalent of installing the `llvm-tools-preview` component via

0 commit comments

Comments
 (0)