Skip to content

Commit 6eba971

Browse files
committed
metadata: streamline find cli lib logic
1 parent 1f67a7a commit 6eba971

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

compiler/rustc_metadata/src/locator.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -706,49 +706,45 @@ impl<'a> CrateLocator<'a> {
706706
let mut rmetas = FxIndexMap::default();
707707
let mut dylibs = FxIndexMap::default();
708708
for loc in &self.exact_paths {
709-
if !loc.canonicalized().exists() {
710-
return Err(CrateError::ExternLocationNotExist(
711-
self.crate_name,
712-
loc.original().clone(),
713-
));
709+
let canon_loc = loc.canonicalized();
710+
let og_loc = loc.original();
711+
712+
if !canon_loc.exists() {
713+
return Err(CrateError::ExternLocationNotExist(self.crate_name, og_loc.clone()));
714714
}
715-
if !loc.original().is_file() {
716-
return Err(CrateError::ExternLocationNotFile(
717-
self.crate_name,
718-
loc.original().clone(),
719-
));
715+
if !og_loc.is_file() {
716+
return Err(CrateError::ExternLocationNotFile(self.crate_name, og_loc.clone()));
720717
}
721-
let Some(file) = loc.original().file_name().and_then(|s| s.to_str()) else {
722-
return Err(CrateError::ExternLocationNotFile(
723-
self.crate_name,
724-
loc.original().clone(),
725-
));
718+
719+
let Some(file) = og_loc.file_name().and_then(|s| s.to_str()) else {
720+
return Err(CrateError::ExternLocationNotFile(self.crate_name, og_loc.clone()));
726721
};
727722

728-
if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta"))
729-
|| file.starts_with(self.target.dll_prefix.as_ref())
730-
&& file.ends_with(self.target.dll_suffix.as_ref())
731-
{
732-
// Make sure there's at most one rlib and at most one dylib.
733-
// Note to take care and match against the non-canonicalized name:
734-
// some systems save build artifacts into content-addressed stores
735-
// that do not preserve extensions, and then link to them using
736-
// e.g. symbolic links. If we canonicalize too early, we resolve
737-
// the symlink, the file type is lost and we might treat rlibs and
738-
// rmetas as dylibs.
739-
let loc_canon = loc.canonicalized().clone();
740-
let loc = loc.original();
741-
if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") {
742-
rlibs.insert(loc_canon, PathKind::ExternFlag);
743-
} else if loc.file_name().unwrap().to_str().unwrap().ends_with(".rmeta") {
744-
rmetas.insert(loc_canon, PathKind::ExternFlag);
745-
} else {
746-
dylibs.insert(loc_canon, PathKind::ExternFlag);
747-
}
723+
let rlib_like =
724+
|file_name: &str| file_name.starts_with("lib") && file_name.ends_with(".rlib");
725+
let rmeta_like =
726+
|file_name: &str| file_name.starts_with("lib") && file_name.ends_with(".rmeta");
727+
let dll_like = |file_name: &str| {
728+
file_name.starts_with(self.target.dll_prefix.as_ref())
729+
&& file_name.ends_with(self.target.dll_suffix.as_ref())
730+
};
731+
732+
// Make sure there's at most one rlib and at most one dylib.
733+
//
734+
// Note to take care and match against the non-canonicalized name: some systems save
735+
// build artifacts into content-addressed stores that do not preserve extensions, and
736+
// then link to them using e.g. symbolic links. If we canonicalize too early, we resolve
737+
// the symlink, the file type is lost and we might treat rlibs and rmetas as dylibs.
738+
if rlib_like(file) {
739+
rlibs.insert(canon_loc.clone(), PathKind::ExternFlag);
740+
} else if rmeta_like(file) {
741+
rmetas.insert(canon_loc.clone(), PathKind::ExternFlag);
742+
} else if dll_like(file) {
743+
dylibs.insert(canon_loc.clone(), PathKind::ExternFlag);
748744
} else {
749745
self.crate_rejections
750746
.via_filename
751-
.push(CrateMismatch { path: loc.original().clone(), got: String::new() });
747+
.push(CrateMismatch { path: og_loc.clone(), got: String::new() });
752748
}
753749
}
754750

0 commit comments

Comments
 (0)