Rollup merge of #126099 - Nilstrieb:crate-loader-cleanups, r=jieyouxu

Crate loader cleanups

Minor cleanups I found while trying to understand how all of this works
This commit is contained in:
Jubilee 2024-06-06 21:10:10 -07:00 committed by GitHub
commit 4c771d3117
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 38 deletions

View File

@ -581,7 +581,6 @@ fn maybe_resolve_crate<'b>(
self.tcx.crate_types().iter().all(|c| *c == CrateType::Rlib), self.tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
hash, hash,
extra_filename, extra_filename,
false, // is_host
path_kind, path_kind,
); );

View File

@ -222,7 +222,6 @@
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_errors::{DiagArgValue, IntoDiagArg}; use rustc_errors::{DiagArgValue, IntoDiagArg};
use rustc_fs_util::try_canonicalize; use rustc_fs_util::try_canonicalize;
use rustc_session::config;
use rustc_session::cstore::CrateSource; use rustc_session::cstore::CrateSource;
use rustc_session::filesearch::FileSearch; use rustc_session::filesearch::FileSearch;
use rustc_session::search_paths::PathKind; use rustc_session::search_paths::PathKind;
@ -309,7 +308,6 @@ pub(crate) fn new(
is_rlib: bool, is_rlib: bool,
hash: Option<Svh>, hash: Option<Svh>,
extra_filename: Option<&'a str>, extra_filename: Option<&'a str>,
is_host: bool,
path_kind: PathKind, path_kind: PathKind,
) -> CrateLocator<'a> { ) -> CrateLocator<'a> {
let needs_object_code = sess.opts.output_types.should_codegen(); let needs_object_code = sess.opts.output_types.should_codegen();
@ -340,17 +338,9 @@ pub(crate) fn new(
}, },
hash, hash,
extra_filename, extra_filename,
target: if is_host { &sess.host } else { &sess.target }, target: &sess.target,
triple: if is_host { triple: sess.opts.target_triple.clone(),
TargetTriple::from_triple(config::host_triple()) filesearch: sess.target_filesearch(path_kind),
} else {
sess.opts.target_triple.clone()
},
filesearch: if is_host {
sess.host_filesearch(path_kind)
} else {
sess.target_filesearch(path_kind)
},
is_proc_macro: false, is_proc_macro: false,
crate_rejections: CrateRejections::default(), crate_rejections: CrateRejections::default(),
} }
@ -424,12 +414,18 @@ fn find_library_crate(
debug!("testing {}", spf.path.display()); debug!("testing {}", spf.path.display());
let f = &spf.file_name_str; let f = &spf.file_name_str;
let (hash, kind) = if f.starts_with(rlib_prefix) && f.ends_with(rlib_suffix) { let (hash, kind) = if let Some(f) = f.strip_prefix(rlib_prefix)
(&f[rlib_prefix.len()..(f.len() - rlib_suffix.len())], CrateFlavor::Rlib) && let Some(f) = f.strip_suffix(rlib_suffix)
} else if f.starts_with(rmeta_prefix) && f.ends_with(rmeta_suffix) { {
(&f[rmeta_prefix.len()..(f.len() - rmeta_suffix.len())], CrateFlavor::Rmeta) (f, CrateFlavor::Rlib)
} else if f.starts_with(dylib_prefix) && f.ends_with(dylib_suffix.as_ref()) { } else if let Some(f) = f.strip_prefix(rmeta_prefix)
(&f[dylib_prefix.len()..(f.len() - dylib_suffix.len())], CrateFlavor::Dylib) && let Some(f) = f.strip_suffix(rmeta_suffix)
{
(f, CrateFlavor::Rmeta)
} else if let Some(f) = f.strip_prefix(dylib_prefix)
&& let Some(f) = f.strip_suffix(dylib_suffix.as_ref())
{
(f, CrateFlavor::Dylib)
} else { } else {
if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix.as_ref()) { if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix.as_ref()) {
self.crate_rejections.via_kind.push(CrateMismatch { self.crate_rejections.via_kind.push(CrateMismatch {

View File

@ -12,7 +12,7 @@
pub struct FileSearch<'a> { pub struct FileSearch<'a> {
sysroot: &'a Path, sysroot: &'a Path,
triple: &'a str, triple: &'a str,
search_paths: &'a [SearchPath], cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath, tlib_path: &'a SearchPath,
kind: PathKind, kind: PathKind,
} }
@ -20,7 +20,7 @@ pub struct FileSearch<'a> {
impl<'a> FileSearch<'a> { impl<'a> FileSearch<'a> {
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> { pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
let kind = self.kind; let kind = self.kind;
self.search_paths self.cli_search_paths
.iter() .iter()
.filter(move |sp| sp.kind.matches(kind)) .filter(move |sp| sp.kind.matches(kind))
.chain(std::iter::once(self.tlib_path)) .chain(std::iter::once(self.tlib_path))
@ -37,26 +37,26 @@ pub fn get_self_contained_lib_path(&self) -> PathBuf {
pub fn new( pub fn new(
sysroot: &'a Path, sysroot: &'a Path,
triple: &'a str, triple: &'a str,
search_paths: &'a [SearchPath], cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath, tlib_path: &'a SearchPath,
kind: PathKind, kind: PathKind,
) -> FileSearch<'a> { ) -> FileSearch<'a> {
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple); debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
FileSearch { sysroot, triple, search_paths, tlib_path, kind } FileSearch { sysroot, triple, cli_search_paths, tlib_path, kind }
} }
} }
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf { pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple); let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")]) sysroot.join(rustlib_path).join("lib")
} }
/// Returns a path to the target's `bin` folder within its `rustlib` path in the sysroot. This is /// Returns a path to the target's `bin` folder within its `rustlib` path in the sysroot. This is
/// where binaries are usually installed, e.g. the self-contained linkers, lld-wrappers, LLVM tools, /// where binaries are usually installed, e.g. the self-contained linkers, lld-wrappers, LLVM tools,
/// etc. /// etc.
pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf { pub fn make_target_bin_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple); let rustlib_path = rustc_target::relative_target_rustlib_path(sysroot, target_triple);
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("bin")]) sysroot.join(rustlib_path).join("bin")
} }
#[cfg(unix)] #[cfg(unix)]
@ -275,7 +275,7 @@ fn from_env_args_next() -> Option<PathBuf> {
p.pop(); p.pop();
p.pop(); p.pop();
// Look for the target rustlib directory in the suspected sysroot. // Look for the target rustlib directory in the suspected sysroot.
let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy"); let mut rustlib_path = rustc_target::relative_target_rustlib_path(&p, "dummy");
rustlib_path.pop(); // pop off the dummy target. rustlib_path.pop(); // pop off the dummy target.
rustlib_path.exists().then_some(p) rustlib_path.exists().then_some(p)
} }

View File

@ -41,17 +41,13 @@
/// ///
/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` => /// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`. /// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf { pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let libdir = find_libdir(sysroot); let libdir = find_relative_libdir(sysroot);
PathBuf::from_iter([ Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
Path::new(libdir.as_ref()),
Path::new(RUST_LIB_DIR),
Path::new(target_triple),
])
} }
/// The name of the directory rustc expects libraries to be located. /// The name of the directory rustc expects libraries to be located.
fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> { fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
// FIXME: This is a quick hack to make the rustc binary able to locate // FIXME: This is a quick hack to make the rustc binary able to locate
// Rust libraries in Linux environments where libraries might be installed // Rust libraries in Linux environments where libraries might be installed
// to lib64/lib32. This would be more foolproof by basing the sysroot off // to lib64/lib32. This would be more foolproof by basing the sysroot off

View File

@ -3367,7 +3367,7 @@ fn load_file(path: &Path) -> Result<(Target, TargetWarnings), String> {
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json` // Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
// as a fallback. // as a fallback.
let rustlib_path = crate::target_rustlib_path(sysroot, target_triple); let rustlib_path = crate::relative_target_rustlib_path(sysroot, target_triple);
let p = PathBuf::from_iter([ let p = PathBuf::from_iter([
Path::new(sysroot), Path::new(sysroot),
Path::new(&rustlib_path), Path::new(&rustlib_path),