Skip to content

Commit b4c439c

Browse files
committed
Improve naming and path operations in crate loader
Simplify the path operation with `join`, clarify some of the names.
1 parent 4c2b21a commit b4c439c

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

compiler/rustc_session/src/filesearch.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use tracing::debug;
1212
pub struct FileSearch<'a> {
1313
sysroot: &'a Path,
1414
triple: &'a str,
15-
search_paths: &'a [SearchPath],
15+
cli_search_paths: &'a [SearchPath],
1616
tlib_path: &'a SearchPath,
1717
kind: PathKind,
1818
}
1919

2020
impl<'a> FileSearch<'a> {
2121
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
2222
let kind = self.kind;
23-
self.search_paths
23+
self.cli_search_paths
2424
.iter()
2525
.filter(move |sp| sp.kind.matches(kind))
2626
.chain(std::iter::once(self.tlib_path))
@@ -37,26 +37,26 @@ impl<'a> FileSearch<'a> {
3737
pub fn new(
3838
sysroot: &'a Path,
3939
triple: &'a str,
40-
search_paths: &'a [SearchPath],
40+
cli_search_paths: &'a [SearchPath],
4141
tlib_path: &'a SearchPath,
4242
kind: PathKind,
4343
) -> FileSearch<'a> {
4444
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
45-
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
45+
FileSearch { sysroot, triple, cli_search_paths, tlib_path, kind }
4646
}
4747
}
4848

4949
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
50-
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
51-
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
50+
let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
51+
sysroot.join(rustlib_path).join("lib")
5252
}
5353

5454
/// Returns a path to the target's `bin` folder within its `rustlib` path in the sysroot. This is
5555
/// where binaries are usually installed, e.g. the self-contained linkers, lld-wrappers, LLVM tools,
5656
/// etc.
5757
pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
58-
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
59-
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("bin")])
58+
let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
59+
sysroot.join(rustlib_path).join("bin")
6060
}
6161

6262
#[cfg(unix)]
@@ -275,7 +275,7 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
275275
p.pop();
276276
p.pop();
277277
// Look for the target rustlib directory in the suspected sysroot.
278-
let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy");
278+
let mut rustlib_path = rustc_target::relative_target_rustlib_path(&p, "dummy");
279279
rustlib_path.pop(); // pop off the dummy target.
280280
rustlib_path.exists().then_some(p)
281281
}

compiler/rustc_target/src/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ const RUST_LIB_DIR: &str = "rustlib";
4141
///
4242
/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
4343
/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
44-
pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
45-
let libdir = find_libdir(sysroot);
46-
PathBuf::from_iter([
47-
Path::new(libdir.as_ref()),
48-
Path::new(RUST_LIB_DIR),
49-
Path::new(target_triple),
50-
])
44+
pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
45+
let libdir = find_relative_libdir(sysroot);
46+
Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
5147
}
5248

5349
/// The name of the directory rustc expects libraries to be located.
54-
fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
50+
fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
5551
// FIXME: This is a quick hack to make the rustc binary able to locate
5652
// Rust libraries in Linux environments where libraries might be installed
5753
// to lib64/lib32. This would be more foolproof by basing the sysroot off

compiler/rustc_target/src/spec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3367,7 +3367,7 @@ impl Target {
33673367

33683368
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
33693369
// as a fallback.
3370-
let rustlib_path = crate::target_rustlib_path(sysroot, target_triple);
3370+
let rustlib_path = crate::relative_target_rustlib_path(sysroot, target_triple);
33713371
let p = PathBuf::from_iter([
33723372
Path::new(sysroot),
33733373
Path::new(&rustlib_path),

0 commit comments

Comments
 (0)