Skip to content

Commit d9edf05

Browse files
authored
Unrolled build for rust-lang#126099
Rollup merge of rust-lang#126099 - Nilstrieb:crate-loader-cleanups, r=jieyouxu Crate loader cleanups Minor cleanups I found while trying to understand how all of this works
2 parents b74702f + b4c439c commit d9edf05

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

compiler/rustc_metadata/src/creader.rs

-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
581581
self.tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
582582
hash,
583583
extra_filename,
584-
false, // is_host
585584
path_kind,
586585
);
587586

compiler/rustc_metadata/src/locator.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ use rustc_data_structures::owned_slice::slice_owned;
222222
use rustc_data_structures::svh::Svh;
223223
use rustc_errors::{DiagArgValue, IntoDiagArg};
224224
use rustc_fs_util::try_canonicalize;
225-
use rustc_session::config;
226225
use rustc_session::cstore::CrateSource;
227226
use rustc_session::filesearch::FileSearch;
228227
use rustc_session::search_paths::PathKind;
@@ -309,7 +308,6 @@ impl<'a> CrateLocator<'a> {
309308
is_rlib: bool,
310309
hash: Option<Svh>,
311310
extra_filename: Option<&'a str>,
312-
is_host: bool,
313311
path_kind: PathKind,
314312
) -> CrateLocator<'a> {
315313
let needs_object_code = sess.opts.output_types.should_codegen();
@@ -340,17 +338,9 @@ impl<'a> CrateLocator<'a> {
340338
},
341339
hash,
342340
extra_filename,
343-
target: if is_host { &sess.host } else { &sess.target },
344-
triple: if is_host {
345-
TargetTriple::from_triple(config::host_triple())
346-
} else {
347-
sess.opts.target_triple.clone()
348-
},
349-
filesearch: if is_host {
350-
sess.host_filesearch(path_kind)
351-
} else {
352-
sess.target_filesearch(path_kind)
353-
},
341+
target: &sess.target,
342+
triple: sess.opts.target_triple.clone(),
343+
filesearch: sess.target_filesearch(path_kind),
354344
is_proc_macro: false,
355345
crate_rejections: CrateRejections::default(),
356346
}
@@ -424,12 +414,18 @@ impl<'a> CrateLocator<'a> {
424414
debug!("testing {}", spf.path.display());
425415

426416
let f = &spf.file_name_str;
427-
let (hash, kind) = if f.starts_with(rlib_prefix) && f.ends_with(rlib_suffix) {
428-
(&f[rlib_prefix.len()..(f.len() - rlib_suffix.len())], CrateFlavor::Rlib)
429-
} else if f.starts_with(rmeta_prefix) && f.ends_with(rmeta_suffix) {
430-
(&f[rmeta_prefix.len()..(f.len() - rmeta_suffix.len())], CrateFlavor::Rmeta)
431-
} else if f.starts_with(dylib_prefix) && f.ends_with(dylib_suffix.as_ref()) {
432-
(&f[dylib_prefix.len()..(f.len() - dylib_suffix.len())], CrateFlavor::Dylib)
417+
let (hash, kind) = if let Some(f) = f.strip_prefix(rlib_prefix)
418+
&& let Some(f) = f.strip_suffix(rlib_suffix)
419+
{
420+
(f, CrateFlavor::Rlib)
421+
} else if let Some(f) = f.strip_prefix(rmeta_prefix)
422+
&& let Some(f) = f.strip_suffix(rmeta_suffix)
423+
{
424+
(f, CrateFlavor::Rmeta)
425+
} else if let Some(f) = f.strip_prefix(dylib_prefix)
426+
&& let Some(f) = f.strip_suffix(dylib_suffix.as_ref())
427+
{
428+
(f, CrateFlavor::Dylib)
433429
} else {
434430
if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix.as_ref()) {
435431
self.crate_rejections.via_kind.push(CrateMismatch {

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)