Skip to content

'src/librustc/session/filesearch.rs' refactoring and cleanup. #37082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 37 additions & 45 deletions src/librustc/session/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

pub use self::FileMatch::*;

use std::borrow::Cow;
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -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(..) => (),
}
});
}
Expand Down Expand Up @@ -123,18 +123,18 @@ 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]
}
}

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
Expand Down Expand Up @@ -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
Expand All @@ -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";