Change implementation of -Z gcc-ld
and lld-wrapper
again
This commit is contained in:
parent
76b0484740
commit
6b68921ca0
@ -2777,20 +2777,24 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
|
|||||||
if let LinkerFlavor::Gcc = flavor {
|
if let LinkerFlavor::Gcc = flavor {
|
||||||
match ld_impl {
|
match ld_impl {
|
||||||
LdImpl::Lld => {
|
LdImpl::Lld => {
|
||||||
let tools_path = sess.get_tools_search_paths(false);
|
// Implement the "self-contained" part of -Zgcc-ld
|
||||||
let gcc_ld_dir = tools_path
|
// by adding rustc distribution directories to the tool search path.
|
||||||
.into_iter()
|
for path in sess.get_tools_search_paths(false) {
|
||||||
.map(|p| p.join("gcc-ld"))
|
|
||||||
.find(|p| {
|
|
||||||
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists()
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
|
|
||||||
cmd.arg({
|
cmd.arg({
|
||||||
let mut arg = OsString::from("-B");
|
let mut arg = OsString::from("-B");
|
||||||
arg.push(gcc_ld_dir);
|
arg.push(path.join("gcc-ld"));
|
||||||
arg
|
arg
|
||||||
});
|
});
|
||||||
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
|
}
|
||||||
|
// Implement the "linker flavor" part of -Zgcc-ld
|
||||||
|
// by asking cc to use some kind of lld.
|
||||||
|
cmd.arg("-fuse-ld=lld");
|
||||||
|
if sess.target.lld_flavor != LldFlavor::Ld {
|
||||||
|
// Tell clang to use a non-default LLD flavor.
|
||||||
|
// Gcc doesn't understand the target option, but we currently assume
|
||||||
|
// that gcc is not used for Apple and Wasm targets (#97402).
|
||||||
|
cmd.arg(format!("--target={}", sess.target.llvm_target));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1276,7 +1276,9 @@ impl Step for Assemble {
|
|||||||
compiler: build_compiler,
|
compiler: build_compiler,
|
||||||
target: target_compiler.host,
|
target: target_compiler.host,
|
||||||
});
|
});
|
||||||
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe("ld", target_compiler.host)));
|
for name in crate::LLD_FILE_NAMES {
|
||||||
|
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(name, target_compiler.host)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) {
|
if builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) {
|
||||||
|
@ -423,8 +423,11 @@ impl Step for Rustc {
|
|||||||
let gcc_lld_src_dir = src_dir.join("gcc-ld");
|
let gcc_lld_src_dir = src_dir.join("gcc-ld");
|
||||||
let gcc_lld_dst_dir = dst_dir.join("gcc-ld");
|
let gcc_lld_dst_dir = dst_dir.join("gcc-ld");
|
||||||
t!(fs::create_dir(&gcc_lld_dst_dir));
|
t!(fs::create_dir(&gcc_lld_dst_dir));
|
||||||
let exe_name = exe("ld", compiler.host);
|
for name in crate::LLD_FILE_NAMES {
|
||||||
builder.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
|
let exe_name = exe(name, compiler.host);
|
||||||
|
builder
|
||||||
|
.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Man pages
|
// Man pages
|
||||||
|
@ -186,6 +186,9 @@ const LLVM_TOOLS: &[&str] = &[
|
|||||||
"opt", // used to optimize LLVM bytecode
|
"opt", // used to optimize LLVM bytecode
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/// LLD file names for all flavors.
|
||||||
|
const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
|
||||||
|
|
||||||
pub const VERSION: usize = 2;
|
pub const VERSION: usize = 2;
|
||||||
|
|
||||||
/// Extra --check-cfg to add when building
|
/// Extra --check-cfg to add when building
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
//! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
|
//! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
|
||||||
//! and since Windows does not support symbolic links for files this wrapper is used in place of a
|
//! and since Windows does not support symbolic links for files this wrapper is used in place of a
|
||||||
//! symbolic link. It execs `../rust-lld -flavor <flavor>` by propagating the flavor argument
|
//! symbolic link. It execs `../rust-lld -flavor <flavor>` by propagating the flavor argument
|
||||||
//! passed to the wrapper as the first two arguments. On Windows it spawns a `..\rust-lld.exe`
|
//! obtained from the wrapper's name as the first two arguments.
|
||||||
//! child process.
|
//! On Windows it spawns a `..\rust-lld.exe` child process.
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@ -53,29 +53,32 @@ fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf {
|
|||||||
rust_lld_path
|
rust_lld_path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract LLD flavor name from the lld-wrapper executable name.
|
||||||
|
fn get_lld_flavor(current_exe_path: &Path) -> Result<&'static str, String> {
|
||||||
|
let stem = current_exe_path.file_stem();
|
||||||
|
Ok(match stem.and_then(|s| s.to_str()) {
|
||||||
|
Some("ld.lld") => "gnu",
|
||||||
|
Some("ld64.lld") => "darwin",
|
||||||
|
Some("lld-link") => "link",
|
||||||
|
Some("wasm-ld") => "wasm",
|
||||||
|
_ => return Err(format!("{:?}", stem)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the command for invoking rust-lld with the correct flavor.
|
/// Returns the command for invoking rust-lld with the correct flavor.
|
||||||
/// LLD only accepts the flavor argument at the first two arguments, so move it there.
|
/// LLD only accepts the flavor argument at the first two arguments, so pass it there.
|
||||||
///
|
///
|
||||||
/// Exits on error.
|
/// Exits on error.
|
||||||
fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
|
fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
|
||||||
let rust_lld_path = get_rust_lld_path(current_exe_path);
|
let rust_lld_path = get_rust_lld_path(current_exe_path);
|
||||||
let mut command = process::Command::new(rust_lld_path);
|
let mut command = process::Command::new(rust_lld_path);
|
||||||
|
|
||||||
let mut flavor = None;
|
let flavor =
|
||||||
let args = env::args_os()
|
get_lld_flavor(current_exe_path).unwrap_or_exit_with("executable has unexpected name");
|
||||||
.skip(1)
|
|
||||||
.filter(|arg| match arg.to_str().and_then(|s| s.strip_prefix("-rustc-lld-flavor=")) {
|
|
||||||
Some(suffix) => {
|
|
||||||
flavor = Some(suffix.to_string());
|
|
||||||
false
|
|
||||||
}
|
|
||||||
None => true,
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
command.arg("-flavor");
|
command.arg("-flavor");
|
||||||
command.arg(flavor.unwrap_or_exit_with("-rustc-lld-flavor=<flavor> is not passed"));
|
command.arg(flavor);
|
||||||
command.args(args);
|
command.args(env::args_os().skip(1));
|
||||||
command
|
command
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user