Skip to content

Commit 2e55abe

Browse files
authored
Rollup merge of rust-lang#129418 - petrochenkov:libsearch2, r=jieyouxu
rustc: Simplify getting sysroot library directory It was very non-obvious that `sess.target_tlib_path`, `make_target_lib_path(...)`, and `sess.target_filesearch(...).search_paths()` result in the same sysroot library directory paths. They are however, indeed the same, because `sess.target_tlib_path` is initialized to `make_target_lib_path(...)` on `Session` creation, and they are used interchangeably. There are still some redundant calls to `make_target_lib_path` and other inconsistent ways to obtain sysroot directories, but fixing that requires some behavior changes, while this PR is a pure refactoring. Some places in the compiler even disagree on the number of sysroots - 1 (explicit `--sysroot` *or* default sysroot), 2 (explicit `--sysroot` *and* default sysroot), or an unclear number of `sysroot_candidates` every of which is considered. The logic currently using `sess.target_tlib_path` or equivalents assumes one sysroot.
2 parents 506e977 + f62b9e0 commit 2e55abe

File tree

3 files changed

+14
-45
lines changed

3 files changed

+14
-45
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1317,11 +1317,9 @@ fn link_sanitizer_runtime(
13171317
name: &str,
13181318
) {
13191319
fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
1320-
let session_tlib =
1321-
filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
1322-
let path = session_tlib.join(filename);
1320+
let path = sess.target_tlib_path.dir.join(filename);
13231321
if path.exists() {
1324-
return session_tlib;
1322+
return sess.target_tlib_path.dir.clone();
13251323
} else {
13261324
let default_sysroot =
13271325
filesearch::get_or_default_sysroot().expect("Failed finding sysroot");
@@ -1612,19 +1610,18 @@ fn print_native_static_libs(
16121610
}
16131611

16141612
fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
1615-
let fs = sess.target_filesearch(PathKind::Native);
1616-
let file_path = fs.get_lib_path().join(name);
1613+
let file_path = sess.target_tlib_path.dir.join(name);
16171614
if file_path.exists() {
16181615
return file_path;
16191616
}
16201617
// Special directory with objects used only in self-contained linkage mode
16211618
if self_contained {
1622-
let file_path = fs.get_self_contained_lib_path().join(name);
1619+
let file_path = sess.target_tlib_path.dir.join("self-contained").join(name);
16231620
if file_path.exists() {
16241621
return file_path;
16251622
}
16261623
}
1627-
for search_path in fs.search_paths() {
1624+
for search_path in sess.target_filesearch(PathKind::Native).search_paths() {
16281625
let file_path = search_path.dir.join(name);
16291626
if file_path.exists() {
16301627
return file_path;
@@ -2131,7 +2128,7 @@ fn add_library_search_dirs(
21312128
| LinkSelfContainedComponents::UNWIND
21322129
| LinkSelfContainedComponents::MINGW,
21332130
) {
2134-
let lib_path = sess.target_filesearch(PathKind::Native).get_self_contained_lib_path();
2131+
let lib_path = sess.target_tlib_path.dir.join("self-contained");
21352132
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
21362133
}
21372134

@@ -2146,8 +2143,7 @@ fn add_library_search_dirs(
21462143
|| sess.target.os == "fuchsia"
21472144
|| sess.target.is_like_osx && !sess.opts.unstable_opts.sanitizer.is_empty()
21482145
{
2149-
let lib_path = sess.target_filesearch(PathKind::Native).get_lib_path();
2150-
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
2146+
cmd.include_path(&fix_windows_verbatim_for_gcc(&sess.target_tlib_path.dir));
21512147
}
21522148

21532149
// Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
@@ -2859,15 +2855,14 @@ fn add_upstream_native_libraries(
28592855
//
28602856
// The returned path will always have `fix_windows_verbatim_for_gcc()` applied to it.
28612857
fn rehome_sysroot_lib_dir(sess: &Session, lib_dir: &Path) -> PathBuf {
2862-
let sysroot_lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
2858+
let sysroot_lib_path = &sess.target_tlib_path.dir;
28632859
let canonical_sysroot_lib_path =
2864-
{ try_canonicalize(&sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
2860+
{ try_canonicalize(sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
28652861

28662862
let canonical_lib_dir = try_canonicalize(lib_dir).unwrap_or_else(|_| lib_dir.to_path_buf());
28672863
if canonical_lib_dir == canonical_sysroot_lib_path {
2868-
// This path, returned by `target_filesearch().get_lib_path()`, has
2869-
// already had `fix_windows_verbatim_for_gcc()` applied if needed.
2870-
sysroot_lib_path
2864+
// This path already had `fix_windows_verbatim_for_gcc()` applied if needed.
2865+
sysroot_lib_path.clone()
28712866
} else {
28722867
fix_windows_verbatim_for_gcc(lib_dir)
28732868
}

Diff for: compiler/rustc_session/src/filesearch.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ use std::{env, fs};
55

66
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
77
use smallvec::{smallvec, SmallVec};
8-
use tracing::debug;
98

109
use crate::search_paths::{PathKind, SearchPath};
1110

1211
#[derive(Clone)]
1312
pub struct FileSearch<'a> {
14-
sysroot: &'a Path,
15-
triple: &'a str,
1613
cli_search_paths: &'a [SearchPath],
1714
tlib_path: &'a SearchPath,
1815
kind: PathKind,
@@ -32,23 +29,12 @@ impl<'a> FileSearch<'a> {
3229
.chain(std::iter::once(self.tlib_path))
3330
}
3431

35-
pub fn get_lib_path(&self) -> PathBuf {
36-
make_target_lib_path(self.sysroot, self.triple)
37-
}
38-
39-
pub fn get_self_contained_lib_path(&self) -> PathBuf {
40-
self.get_lib_path().join("self-contained")
41-
}
42-
4332
pub fn new(
44-
sysroot: &'a Path,
45-
triple: &'a str,
4633
cli_search_paths: &'a [SearchPath],
4734
tlib_path: &'a SearchPath,
4835
kind: PathKind,
4936
) -> FileSearch<'a> {
50-
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
51-
FileSearch { sysroot, triple, cli_search_paths, tlib_path, kind }
37+
FileSearch { cli_search_paths, tlib_path, kind }
5238
}
5339
}
5440

Diff for: compiler/rustc_session/src/session.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -440,22 +440,10 @@ impl Session {
440440
}
441441

442442
pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
443-
filesearch::FileSearch::new(
444-
&self.sysroot,
445-
self.opts.target_triple.triple(),
446-
&self.opts.search_paths,
447-
&self.target_tlib_path,
448-
kind,
449-
)
443+
filesearch::FileSearch::new(&self.opts.search_paths, &self.target_tlib_path, kind)
450444
}
451445
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
452-
filesearch::FileSearch::new(
453-
&self.sysroot,
454-
config::host_triple(),
455-
&self.opts.search_paths,
456-
&self.host_tlib_path,
457-
kind,
458-
)
446+
filesearch::FileSearch::new(&self.opts.search_paths, &self.host_tlib_path, kind)
459447
}
460448

461449
/// Returns a list of directories where target-specific tool binaries are located. Some fallback

0 commit comments

Comments
 (0)