Skip to content

Commit fab28f2

Browse files
committed
rust-lld: fallback to the default default sysroot where rustc is currently located
1 parent 6867d64 commit fab28f2

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -3116,13 +3116,21 @@ fn add_lld_args(
31163116

31173117
let self_contained_linker = self_contained_cli || self_contained_target;
31183118
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
3119+
let mut linker_path_exists = false;
31193120
for path in sess.get_tools_search_paths(false) {
3121+
let linker_path = path.join("gcc-ld");
3122+
linker_path_exists |= linker_path.exists();
31203123
cmd.arg({
31213124
let mut arg = OsString::from("-B");
3122-
arg.push(path.join("gcc-ld"));
3125+
arg.push(linker_path);
31233126
arg
31243127
});
31253128
}
3129+
if !linker_path_exists {
3130+
// As an additional sanity check, we do nothing if the sysroot doesn't contain the
3131+
// linker path at all.
3132+
return;
3133+
}
31263134
}
31273135

31283136
// 2. Implement the "linker flavor" part of this feature by asking `cc` to use some kind of

compiler/rustc_session/src/session.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,24 @@ impl Session {
449449
)
450450
}
451451

452-
/// Returns a list of directories where target-specific tool binaries are located.
452+
/// Returns a list of directories where target-specific tool binaries are located. Some fallback
453+
/// directories are also returned, for example if `--sysroot` is used but tools are missing
454+
/// (#125246): we also add the bin directories to the sysroot where rustc is located.
453455
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
454-
let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, config::host_triple());
455-
let p = PathBuf::from_iter([
456-
Path::new(&self.sysroot),
457-
Path::new(&rustlib_path),
458-
Path::new("bin"),
459-
]);
460-
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
456+
let bin_path = filesearch::make_target_bin_path(&self.sysroot, config::host_triple());
457+
let fallback_sysroot_paths = filesearch::sysroot_candidates()
458+
.into_iter()
459+
.map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_triple()));
460+
let search_paths = std::iter::once(bin_path).chain(fallback_sysroot_paths);
461+
462+
if self_contained {
463+
// The self-contained tools are expected to be e.g. in `bin/self-contained` in the
464+
// sysroot's `rustlib` path, so we add such a subfolder to the bin path, and the
465+
// fallback paths.
466+
search_paths.flat_map(|path| [path.clone(), path.join("self-contained")]).collect()
467+
} else {
468+
search_paths.collect()
469+
}
461470
}
462471

463472
pub fn init_incr_comp_session(&self, session_dir: PathBuf, lock_file: flock::Lock) {

0 commit comments

Comments
 (0)