#![allow(non_camel_case_types)]
pub use self::FileMatch::*;
use std::borrow::Cow;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
use log::debug;
use rustc_fs_util::fix_windows_verbatim_for_gcc;
#[derive(Copy, Clone)]
pub enum FileMatch {
FileMatches,
FileDoesntMatch,
}
#[derive(Clone)]
pub struct FileSearch<'a> {
sysroot: &'a Path,
triple: &'a str,
search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath,
kind: PathKind,
}
impl<'a> FileSearch<'a> {
pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
let kind = self.kind;
self.search_paths
.iter()
.filter(move |sp| sp.kind.matches(kind))
.chain(std::iter::once(self.tlib_path))
}
pub fn get_lib_path(&self) -> PathBuf {
make_target_lib_path(self.sysroot, self.triple)
}
pub fn search<F>(&self, mut pick: F)
where
F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
{
for search_path in self.search_paths() {
debug!("searching {}", search_path.dir.display());
fn is_rlib(spf: &SearchPathFile) -> bool {
if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
}
let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
for spf in files1.chain(files2) {
debug!("testing {}", spf.path.display());
let maybe_picked = pick(spf, search_path.kind);
match maybe_picked {
FileMatches => {
debug!("picked {}", spf.path.display());
}
FileDoesntMatch => {
debug!("rejected {}", spf.path.display());
}
}
}
}
}
pub fn new(
sysroot: &'a Path,
triple: &'a str,
search_paths: &'a Vec<SearchPath>,
tlib_path: &'a SearchPath,
kind: PathKind,
) -> FileSearch<'a> {
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
}
pub fn search_path_dirs(&self) -> Vec<PathBuf> {
self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
}
pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
let mut p = PathBuf::from(self.sysroot);
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).as_ref());
assert!(p.is_relative());
p.push(RUST_LIB_DIR);
p.push(target_triple);
p.push("lib");
p
}
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
}
pub fn get_or_default_sysroot() -> PathBuf {
fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
path.and_then(|path| {
match fs::canonicalize(&path) {
Ok(canon) => Some(fix_windows_verbatim_for_gcc(&canon)),
Err(e) => panic!("failed to get realpath: {}", e),
}
})
}
match env::current_exe() {
Ok(exe) => match canonicalize(Some(exe)) {
Some(mut p) => {
p.pop();
p.pop();
p
}
None => panic!("can't determine value for sysroot"),
},
Err(ref e) => panic!(format!("failed to get current_exe: {}", e)),
}
}
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
#[cfg(target_pointer_width = "64")]
const PRIMARY_LIB_DIR: &str = "lib64";
#[cfg(target_pointer_width = "32")]
const PRIMARY_LIB_DIR: &str = "lib32";
const SECONDARY_LIB_DIR: &str = "lib";
match option_env!("CFG_LIBDIR_RELATIVE") {
Some(libdir) if libdir != "lib" => libdir.into(),
_ => {
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
PRIMARY_LIB_DIR.into()
} else {
SECONDARY_LIB_DIR.into()
}
}
}
}
const RUST_LIB_DIR: &str = "rustlib";