Auto merge of #37082 - frewsxcv:session, r=jseyfried

'src/librustc/session/filesearch.rs' refactoring and cleanup.
This commit is contained in:
bors 2016-10-16 20:00:14 -07:00 committed by GitHub
commit 54c133d3ba

View File

@ -12,6 +12,7 @@
pub use self::FileMatch::*;
use std::borrow::Cow;
use std::collections::HashSet;
use std::env;
use std::fs;
@ -67,33 +68,32 @@ impl<'a> FileSearch<'a> {
{
self.for_each_lib_search_path(|lib_search_path, kind| {
debug!("searching {}", lib_search_path.display());
match fs::read_dir(lib_search_path) {
Ok(files) => {
let files = files.filter_map(|p| p.ok().map(|s| s.path()))
.collect::<Vec<_>>();
fn is_rlib(p: &Path) -> bool {
p.extension().and_then(|s| s.to_str()) == Some("rlib")
let files = match fs::read_dir(lib_search_path) {
Ok(files) => files,
Err(..) => return,
};
let files = files.filter_map(|p| p.ok().map(|s| s.path()))
.collect::<Vec<_>>();
fn is_rlib(p: &Path) -> bool {
p.extension() == Some("rlib".as_ref())
}
// Reading metadata out of rlibs is faster, and if we find both
// an rlib and a dylib we only read one of the files of
// metadata, so in the name of speed, bring all rlib files to
// the front of the search list.
let files1 = files.iter().filter(|p| is_rlib(p));
let files2 = files.iter().filter(|p| !is_rlib(p));
for path in files1.chain(files2) {
debug!("testing {}", path.display());
let maybe_picked = pick(path, kind);
match maybe_picked {
FileMatches => {
debug!("picked {}", path.display());
}
// Reading metadata out of rlibs is faster, and if we find both
// an rlib and a dylib we only read one of the files of
// metadata, so in the name of speed, bring all rlib files to
// the front of the search list.
let files1 = files.iter().filter(|p| is_rlib(p));
let files2 = files.iter().filter(|p| !is_rlib(p));
for path in files1.chain(files2) {
debug!("testing {}", path.display());
let maybe_picked = pick(path, kind);
match maybe_picked {
FileMatches => {
debug!("picked {}", path.display());
}
FileDoesntMatch => {
debug!("rejected {}", path.display());
}
}
FileDoesntMatch => {
debug!("rejected {}", path.display());
}
}
Err(..) => (),
}
});
}
@ -123,8 +123,8 @@ impl<'a> FileSearch<'a> {
// Returns a list of directories where target-specific tool binaries are located.
pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
let mut p = PathBuf::from(self.sysroot);
p.push(&find_libdir(self.sysroot));
p.push(&rustlibdir());
p.push(find_libdir(self.sysroot).as_ref());
p.push(RUST_LIB_DIR);
p.push(&self.triple);
p.push("bin");
vec![p]
@ -132,9 +132,9 @@ impl<'a> FileSearch<'a> {
}
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let mut p = PathBuf::from(&find_libdir(sysroot));
let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
assert!(p.is_relative());
p.push(&rustlibdir());
p.push(RUST_LIB_DIR);
p.push(target_triple);
p.push("lib");
p
@ -166,7 +166,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
}
// The name of the directory rustc expects libraries to be located.
fn find_libdir(sysroot: &Path) -> String {
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
// FIXME: This is a quick hack to make the rustc binary able to locate
// Rust libraries in Linux environments where libraries might be installed
// to lib64/lib32. This would be more foolproof by basing the sysroot off
@ -176,31 +176,23 @@ fn find_libdir(sysroot: &Path) -> String {
// "lib" (i.e. non-default), this value is used (see issue #16552).
match option_env!("CFG_LIBDIR_RELATIVE") {
Some(libdir) if libdir != "lib" => return libdir.to_string(),
_ => if sysroot.join(&primary_libdir_name()).join(&rustlibdir()).exists() {
return primary_libdir_name();
Some(libdir) if libdir != "lib" => return libdir.into(),
_ => if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
return PRIMARY_LIB_DIR.into();
} else {
return secondary_libdir_name();
return SECONDARY_LIB_DIR.into();
}
}
#[cfg(target_pointer_width = "64")]
fn primary_libdir_name() -> String {
"lib64".to_string()
}
const PRIMARY_LIB_DIR: &'static str = "lib64";
#[cfg(target_pointer_width = "32")]
fn primary_libdir_name() -> String {
"lib32".to_string()
}
const PRIMARY_LIB_DIR: &'static str = "lib32";
fn secondary_libdir_name() -> String {
"lib".to_string()
}
const SECONDARY_LIB_DIR: &'static str = "lib";
}
// The name of rustc's own place to organize libraries.
// Used to be "rustc", now the default is "rustlib"
pub fn rustlibdir() -> String {
"rustlib".to_string()
}
const RUST_LIB_DIR: &'static str = "rustlib";