use crate::filesearch::make_target_lib_path;
use crate::EarlyErrorHandler;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct SearchPath {
pub kind: PathKind,
pub dir: PathBuf,
pub files: Vec<SearchPathFile>,
}
#[derive(Clone, Debug)]
pub struct SearchPathFile {
pub path: PathBuf,
pub file_name_str: String,
}
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
pub enum PathKind {
Native,
Crate,
Dependency,
Framework,
ExternFlag,
All,
}
impl PathKind {
pub fn matches(&self, kind: PathKind) -> bool {
match (self, kind) {
(PathKind::All, _) | (_, PathKind::All) => true,
_ => *self == kind,
}
}
}
impl SearchPath {
pub fn from_cli_opt(handler: &EarlyErrorHandler, path: &str) -> Self {
let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
(PathKind::Native, stripped)
} else if let Some(stripped) = path.strip_prefix("crate=") {
(PathKind::Crate, stripped)
} else if let Some(stripped) = path.strip_prefix("dependency=") {
(PathKind::Dependency, stripped)
} else if let Some(stripped) = path.strip_prefix("framework=") {
(PathKind::Framework, stripped)
} else if let Some(stripped) = path.strip_prefix("all=") {
(PathKind::All, stripped)
} else {
(PathKind::All, path)
};
if path.is_empty() {
handler.early_error("empty search path given via `-L`");
}
let dir = PathBuf::from(path);
Self::new(kind, dir)
}
pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
}
fn new(kind: PathKind, dir: PathBuf) -> Self {
let files = match std::fs::read_dir(&dir) {
Ok(files) => files
.filter_map(|e| {
e.ok().and_then(|e| {
e.file_name().to_str().map(|s| SearchPathFile {
path: e.path(),
file_name_str: s.to_string(),
})
})
})
.collect::<Vec<_>>(),
Err(..) => vec![],
};
SearchPath { kind, dir, files }
}
}