Skip to content

Commit 54c133d

Browse files
authored
Auto merge of #37082 - frewsxcv:session, r=jseyfried
'src/librustc/session/filesearch.rs' refactoring and cleanup.
2 parents 6572a46 + 0038430 commit 54c133d

File tree

1 file changed

+37
-45
lines changed

1 file changed

+37
-45
lines changed

src/librustc/session/filesearch.rs

+37-45
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
pub use self::FileMatch::*;
1414

15+
use std::borrow::Cow;
1516
use std::collections::HashSet;
1617
use std::env;
1718
use std::fs;
@@ -67,33 +68,32 @@ impl<'a> FileSearch<'a> {
6768
{
6869
self.for_each_lib_search_path(|lib_search_path, kind| {
6970
debug!("searching {}", lib_search_path.display());
70-
match fs::read_dir(lib_search_path) {
71-
Ok(files) => {
72-
let files = files.filter_map(|p| p.ok().map(|s| s.path()))
73-
.collect::<Vec<_>>();
74-
fn is_rlib(p: &Path) -> bool {
75-
p.extension().and_then(|s| s.to_str()) == Some("rlib")
71+
let files = match fs::read_dir(lib_search_path) {
72+
Ok(files) => files,
73+
Err(..) => return,
74+
};
75+
let files = files.filter_map(|p| p.ok().map(|s| s.path()))
76+
.collect::<Vec<_>>();
77+
fn is_rlib(p: &Path) -> bool {
78+
p.extension() == Some("rlib".as_ref())
79+
}
80+
// Reading metadata out of rlibs is faster, and if we find both
81+
// an rlib and a dylib we only read one of the files of
82+
// metadata, so in the name of speed, bring all rlib files to
83+
// the front of the search list.
84+
let files1 = files.iter().filter(|p| is_rlib(p));
85+
let files2 = files.iter().filter(|p| !is_rlib(p));
86+
for path in files1.chain(files2) {
87+
debug!("testing {}", path.display());
88+
let maybe_picked = pick(path, kind);
89+
match maybe_picked {
90+
FileMatches => {
91+
debug!("picked {}", path.display());
7692
}
77-
// Reading metadata out of rlibs is faster, and if we find both
78-
// an rlib and a dylib we only read one of the files of
79-
// metadata, so in the name of speed, bring all rlib files to
80-
// the front of the search list.
81-
let files1 = files.iter().filter(|p| is_rlib(p));
82-
let files2 = files.iter().filter(|p| !is_rlib(p));
83-
for path in files1.chain(files2) {
84-
debug!("testing {}", path.display());
85-
let maybe_picked = pick(path, kind);
86-
match maybe_picked {
87-
FileMatches => {
88-
debug!("picked {}", path.display());
89-
}
90-
FileDoesntMatch => {
91-
debug!("rejected {}", path.display());
92-
}
93-
}
93+
FileDoesntMatch => {
94+
debug!("rejected {}", path.display());
9495
}
9596
}
96-
Err(..) => (),
9797
}
9898
});
9999
}
@@ -123,18 +123,18 @@ impl<'a> FileSearch<'a> {
123123
// Returns a list of directories where target-specific tool binaries are located.
124124
pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
125125
let mut p = PathBuf::from(self.sysroot);
126-
p.push(&find_libdir(self.sysroot));
127-
p.push(&rustlibdir());
126+
p.push(find_libdir(self.sysroot).as_ref());
127+
p.push(RUST_LIB_DIR);
128128
p.push(&self.triple);
129129
p.push("bin");
130130
vec![p]
131131
}
132132
}
133133

134134
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
135-
let mut p = PathBuf::from(&find_libdir(sysroot));
135+
let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
136136
assert!(p.is_relative());
137-
p.push(&rustlibdir());
137+
p.push(RUST_LIB_DIR);
138138
p.push(target_triple);
139139
p.push("lib");
140140
p
@@ -166,7 +166,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
166166
}
167167

168168
// The name of the directory rustc expects libraries to be located.
169-
fn find_libdir(sysroot: &Path) -> String {
169+
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
170170
// FIXME: This is a quick hack to make the rustc binary able to locate
171171
// Rust libraries in Linux environments where libraries might be installed
172172
// to lib64/lib32. This would be more foolproof by basing the sysroot off
@@ -176,31 +176,23 @@ fn find_libdir(sysroot: &Path) -> String {
176176
// "lib" (i.e. non-default), this value is used (see issue #16552).
177177

178178
match option_env!("CFG_LIBDIR_RELATIVE") {
179-
Some(libdir) if libdir != "lib" => return libdir.to_string(),
180-
_ => if sysroot.join(&primary_libdir_name()).join(&rustlibdir()).exists() {
181-
return primary_libdir_name();
179+
Some(libdir) if libdir != "lib" => return libdir.into(),
180+
_ => if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
181+
return PRIMARY_LIB_DIR.into();
182182
} else {
183-
return secondary_libdir_name();
183+
return SECONDARY_LIB_DIR.into();
184184
}
185185
}
186186

187187
#[cfg(target_pointer_width = "64")]
188-
fn primary_libdir_name() -> String {
189-
"lib64".to_string()
190-
}
188+
const PRIMARY_LIB_DIR: &'static str = "lib64";
191189

192190
#[cfg(target_pointer_width = "32")]
193-
fn primary_libdir_name() -> String {
194-
"lib32".to_string()
195-
}
191+
const PRIMARY_LIB_DIR: &'static str = "lib32";
196192

197-
fn secondary_libdir_name() -> String {
198-
"lib".to_string()
199-
}
193+
const SECONDARY_LIB_DIR: &'static str = "lib";
200194
}
201195

202196
// The name of rustc's own place to organize libraries.
203197
// Used to be "rustc", now the default is "rustlib"
204-
pub fn rustlibdir() -> String {
205-
"rustlib".to_string()
206-
}
198+
const RUST_LIB_DIR: &'static str = "rustlib";

0 commit comments

Comments
 (0)