1 use crate::filesearch::make_target_lib_path; 2 use crate::EarlyErrorHandler; 3 use std::path::{Path, PathBuf}; 4 5 #[derive(Clone, Debug)] 6 pub struct SearchPath { 7 pub kind: PathKind, 8 pub dir: PathBuf, 9 pub files: Vec<SearchPathFile>, 10 } 11 12 /// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But 13 /// it is searched repeatedly by `find_library_crate`, and the searches involve 14 /// checking the prefix and suffix of the filename of each `PathBuf`. This is 15 /// doable, but very slow, because it involves calls to `file_name` and 16 /// `extension` that are themselves slow. 17 /// 18 /// This type augments the `PathBuf` with an `String` containing the 19 /// `PathBuf`'s filename. The prefix and suffix checking is much faster on the 20 /// `String` than the `PathBuf`. (The filename must be valid UTF-8. If it's 21 /// not, the entry should be skipped, because all Rust output files are valid 22 /// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.) 23 #[derive(Clone, Debug)] 24 pub struct SearchPathFile { 25 pub path: PathBuf, 26 pub file_name_str: String, 27 } 28 29 #[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)] 30 pub enum PathKind { 31 Native, 32 Crate, 33 Dependency, 34 Framework, 35 ExternFlag, 36 All, 37 } 38 39 impl PathKind { matches(&self, kind: PathKind) -> bool40 pub fn matches(&self, kind: PathKind) -> bool { 41 match (self, kind) { 42 (PathKind::All, _) | (_, PathKind::All) => true, 43 _ => *self == kind, 44 } 45 } 46 } 47 48 impl SearchPath { from_cli_opt(handler: &EarlyErrorHandler, path: &str) -> Self49 pub fn from_cli_opt(handler: &EarlyErrorHandler, path: &str) -> Self { 50 let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") { 51 (PathKind::Native, stripped) 52 } else if let Some(stripped) = path.strip_prefix("crate=") { 53 (PathKind::Crate, stripped) 54 } else if let Some(stripped) = path.strip_prefix("dependency=") { 55 (PathKind::Dependency, stripped) 56 } else if let Some(stripped) = path.strip_prefix("framework=") { 57 (PathKind::Framework, stripped) 58 } else if let Some(stripped) = path.strip_prefix("all=") { 59 (PathKind::All, stripped) 60 } else { 61 (PathKind::All, path) 62 }; 63 if path.is_empty() { 64 handler.early_error("empty search path given via `-L`"); 65 } 66 67 let dir = PathBuf::from(path); 68 Self::new(kind, dir) 69 } 70 from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self71 pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self { 72 Self::new(PathKind::All, make_target_lib_path(sysroot, triple)) 73 } 74 new(kind: PathKind, dir: PathBuf) -> Self75 fn new(kind: PathKind, dir: PathBuf) -> Self { 76 // Get the files within the directory. 77 let files = match std::fs::read_dir(&dir) { 78 Ok(files) => files 79 .filter_map(|e| { 80 e.ok().and_then(|e| { 81 e.file_name().to_str().map(|s| SearchPathFile { 82 path: e.path(), 83 file_name_str: s.to_string(), 84 }) 85 }) 86 }) 87 .collect::<Vec<_>>(), 88 Err(..) => vec![], 89 }; 90 91 SearchPath { kind, dir, files } 92 } 93 } 94