Rollup merge of #131743 - tamird:find_commandline_library-tidy, r=lcnr

rustc_metadata: minor tidying

Cleaned up some code while investigating #131720.

See individual commits.
This commit is contained in:
Matthias Krüger 2024-10-18 06:59:05 +02:00 committed by GitHub
commit 02cc3a6da5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -499,8 +499,11 @@ impl<'a> CrateLocator<'a> {
dylibs: FxIndexMap<PathBuf, PathKind>, dylibs: FxIndexMap<PathBuf, PathKind>,
) -> Result<Option<(Svh, Library)>, CrateError> { ) -> Result<Option<(Svh, Library)>, CrateError> {
let mut slot = None; let mut slot = None;
// Order here matters, rmeta should come first. See comment in // Order here matters, rmeta should come first.
// `extract_one` below. //
// Make sure there's at most one rlib and at most one dylib.
//
// See comment in `extract_one` below.
let source = CrateSource { let source = CrateSource {
rmeta: self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot)?, rmeta: self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot)?,
rlib: self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?, rlib: self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?,
@ -706,54 +709,58 @@ impl<'a> CrateLocator<'a> {
let mut rmetas = FxIndexMap::default(); let mut rmetas = FxIndexMap::default();
let mut dylibs = FxIndexMap::default(); let mut dylibs = FxIndexMap::default();
for loc in &self.exact_paths { for loc in &self.exact_paths {
if !loc.canonicalized().exists() { let loc_canon = loc.canonicalized();
return Err(CrateError::ExternLocationNotExist( let loc_orig = loc.original();
self.crate_name, if !loc_canon.exists() {
loc.original().clone(), return Err(CrateError::ExternLocationNotExist(self.crate_name, loc_orig.clone()));
));
} }
if !loc.original().is_file() { if !loc_orig.is_file() {
return Err(CrateError::ExternLocationNotFile( return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
self.crate_name,
loc.original().clone(),
));
} }
let Some(file) = loc.original().file_name().and_then(|s| s.to_str()) else { // Note to take care and match against the non-canonicalized name:
return Err(CrateError::ExternLocationNotFile( // some systems save build artifacts into content-addressed stores
self.crate_name, // that do not preserve extensions, and then link to them using
loc.original().clone(), // e.g. symbolic links. If we canonicalize too early, we resolve
)); // the symlink, the file type is lost and we might treat rlibs and
// rmetas as dylibs.
let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else {
return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
}; };
// FnMut cannot return reference to captured value, so references
if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta")) // must be taken outside the closure.
|| file.starts_with(self.target.dll_prefix.as_ref()) let rlibs = &mut rlibs;
&& file.ends_with(self.target.dll_suffix.as_ref()) let rmetas = &mut rmetas;
{ let dylibs = &mut dylibs;
// Make sure there's at most one rlib and at most one dylib. let type_via_filename = (|| {
// Note to take care and match against the non-canonicalized name: if file.starts_with("lib") {
// some systems save build artifacts into content-addressed stores if file.ends_with(".rlib") {
// that do not preserve extensions, and then link to them using return Some(rlibs);
// e.g. symbolic links. If we canonicalize too early, we resolve }
// the symlink, the file type is lost and we might treat rlibs and if file.ends_with(".rmeta") {
// rmetas as dylibs. return Some(rmetas);
let loc_canon = loc.canonicalized().clone(); }
let loc = loc.original(); }
if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") { let dll_prefix = self.target.dll_prefix.as_ref();
rlibs.insert(loc_canon, PathKind::ExternFlag); let dll_suffix = self.target.dll_suffix.as_ref();
} else if loc.file_name().unwrap().to_str().unwrap().ends_with(".rmeta") { if file.starts_with(dll_prefix) && file.ends_with(dll_suffix) {
rmetas.insert(loc_canon, PathKind::ExternFlag); return Some(dylibs);
} else { }
dylibs.insert(loc_canon, PathKind::ExternFlag); None
})();
match type_via_filename {
Some(type_via_filename) => {
type_via_filename.insert(loc_canon.clone(), PathKind::ExternFlag);
}
None => {
self.crate_rejections
.via_filename
.push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
} }
} else {
self.crate_rejections
.via_filename
.push(CrateMismatch { path: loc.original().clone(), got: String::new() });
} }
} }
// Extract the dylib/rlib/rmeta triple. // Extract the dylib/rlib/rmeta triple.
Ok(self.extract_lib(rlibs, rmetas, dylibs)?.map(|(_, lib)| lib)) self.extract_lib(rlibs, rmetas, dylibs).map(|opt| opt.map(|(_, lib)| lib))
} }
pub(crate) fn into_error(self, root: Option<CratePaths>) -> CrateError { pub(crate) fn into_error(self, root: Option<CratePaths>) -> CrateError {