Skip to content

Commit

Permalink
centralize fallback sysroot search paths in get_tools_search_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lqd committed May 22, 2024
1 parent d8284a3 commit bac5344
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
14 changes: 5 additions & 9 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3117,16 +3117,12 @@ fn add_lld_args(
let self_contained_linker = self_contained_cli || self_contained_target;
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
// We tell `cc` where to find the lld wrappers, in a similar way to how we locate
// codegen-backends dylibs:
// - in the tool search paths: the sysroot's `rustlib` bin path,
// - and as a fallback for cases where `--sysroot` only contains a target std: in the
// default host sysroot where rustc is currently located.
let fallback_sysroot_paths = filesearch::sysroot_candidates()
.into_iter()
.map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_triple()));

// codegen-backends dylibs, in the tools search paths, which contain:
// - the sysroot's `rustlib` bin path,
// - and as a fallback for cases using `--sysroot` without tools: in the default host
// sysroot where rustc is currently located.
let mut linker_path_exists = false;
for path in sess.get_tools_search_paths(false).into_iter().chain(fallback_sysroot_paths) {
for path in sess.get_tools_search_paths(false) {
let linker_path = path.join("gcc-ld");
if linker_path.exists() {
linker_path_exists = true;
Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,29 @@ impl Session {
)
}

/// Returns a list of directories where target-specific tool binaries are located.
/// Returns a list of directories where target-specific tool binaries are located. Some fallback
/// directories are also returned, for example if `--sysroot` is used but tools are missing
/// (#125246): we also add the bin directories to the sysroot where rustc is located.
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, config::host_triple());
let p = PathBuf::from_iter([
Path::new(&self.sysroot),
Path::new(&rustlib_path),
Path::new("bin"),
]);
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }

let fallback_sysroot_paths = filesearch::sysroot_candidates()
.into_iter()
.map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_triple()));

if self_contained {
[p.clone(), p.join("self-contained")]
.into_iter()
.chain(fallback_sysroot_paths)
.collect()
} else {
std::iter::once(p).chain(fallback_sysroot_paths).collect()
}
}

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

0 comments on commit bac5344

Please sign in to comment.