]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/search_paths.rs
Rollup merge of #93751 - eholk:issue-93648-drop-tracking-projection, r=tmiasko
[rust.git] / compiler / rustc_session / src / search_paths.rs
1 use crate::filesearch::make_target_lib_path;
2 use crate::{config, early_error};
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)]
30 pub enum PathKind {
31     Native,
32     Crate,
33     Dependency,
34     Framework,
35     ExternFlag,
36     All,
37 }
38
39 rustc_data_structures::impl_stable_hash_via_hash!(PathKind);
40
41 impl PathKind {
42     pub fn matches(&self, kind: PathKind) -> bool {
43         match (self, kind) {
44             (PathKind::All, _) | (_, PathKind::All) => true,
45             _ => *self == kind,
46         }
47     }
48 }
49
50 impl SearchPath {
51     pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
52         let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
53             (PathKind::Native, stripped)
54         } else if let Some(stripped) = path.strip_prefix("crate=") {
55             (PathKind::Crate, stripped)
56         } else if let Some(stripped) = path.strip_prefix("dependency=") {
57             (PathKind::Dependency, stripped)
58         } else if let Some(stripped) = path.strip_prefix("framework=") {
59             (PathKind::Framework, stripped)
60         } else if let Some(stripped) = path.strip_prefix("all=") {
61             (PathKind::All, stripped)
62         } else {
63             (PathKind::All, path)
64         };
65         if path.is_empty() {
66             early_error(output, "empty search path given via `-L`");
67         }
68
69         let dir = PathBuf::from(path);
70         Self::new(kind, dir)
71     }
72
73     pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
74         Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
75     }
76
77     fn new(kind: PathKind, dir: PathBuf) -> Self {
78         // Get the files within the directory.
79         let files = match std::fs::read_dir(&dir) {
80             Ok(files) => files
81                 .filter_map(|e| {
82                     e.ok().and_then(|e| {
83                         e.file_name().to_str().map(|s| SearchPathFile {
84                             path: e.path(),
85                             file_name_str: s.to_string(),
86                         })
87                     })
88                 })
89                 .collect::<Vec<_>>(),
90             Err(..) => vec![],
91         };
92
93         SearchPath { kind, dir, files }
94     }
95 }