rustc_session/
search_paths.rs

1use std::path::{Path, PathBuf};
2use std::sync::Arc;
3
4use rustc_macros::{Decodable, Encodable, HashStable_Generic};
5use rustc_target::spec::TargetTuple;
6
7use crate::EarlyDiagCtxt;
8use crate::filesearch::make_target_lib_path;
9
10#[derive(Clone, Debug)]
11pub struct SearchPath {
12    pub kind: PathKind,
13    pub dir: PathBuf,
14    pub files: FilesIndex,
15}
16
17/// [FilesIndex] contains paths that can be efficiently looked up with (prefix, suffix) pairs.
18#[derive(Clone, Debug)]
19pub struct FilesIndex(Vec<(Arc<str>, SearchPathFile)>);
20
21impl FilesIndex {
22    /// Look up [SearchPathFile] by (prefix, suffix) pair.
23    pub fn query<'this, 'prefix, 'suffix>(
24        &'this self,
25        prefix: &'prefix str,
26        suffix: &'suffix str,
27    ) -> Option<impl Iterator<Item = (String, &'this SearchPathFile)> + use<'this, 'prefix, 'suffix>>
28    {
29        let start = self.0.partition_point(|(k, _)| **k < *prefix);
30        if start == self.0.len() {
31            return None;
32        }
33        let end = self.0[start..].partition_point(|(k, _)| k.starts_with(prefix));
34        let prefixed_items = &self.0[start..][..end];
35
36        let ret = prefixed_items.into_iter().filter_map(move |(k, v)| {
37            k.ends_with(suffix).then(|| {
38                (
39                    String::from(
40                        &v.file_name_str[prefix.len()..v.file_name_str.len() - suffix.len()],
41                    ),
42                    v,
43                )
44            })
45        });
46        Some(ret)
47    }
48    pub fn retain(&mut self, prefixes: &[&str]) {
49        self.0.retain(|(k, _)| prefixes.iter().any(|prefix| k.starts_with(prefix)));
50    }
51}
52/// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
53/// it is searched repeatedly by `find_library_crate`, and the searches involve
54/// checking the prefix and suffix of the filename of each `PathBuf`. This is
55/// doable, but very slow, because it involves calls to `file_name` and
56/// `extension` that are themselves slow.
57///
58/// This type augments the `PathBuf` with an `String` containing the
59/// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
60/// `String` than the `PathBuf`. (The filename must be valid UTF-8. If it's
61/// not, the entry should be skipped, because all Rust output files are valid
62/// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.)
63#[derive(Clone, Debug)]
64pub struct SearchPathFile {
65    pub path: Arc<Path>,
66    pub file_name_str: Arc<str>,
67}
68
69#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
70pub enum PathKind {
71    Native,
72    Crate,
73    Dependency,
74    Framework,
75    ExternFlag,
76    All,
77}
78
79impl PathKind {
80    pub fn matches(&self, kind: PathKind) -> bool {
81        match (self, kind) {
82            (PathKind::All, _) | (_, PathKind::All) => true,
83            _ => *self == kind,
84        }
85    }
86}
87
88impl SearchPath {
89    pub fn from_cli_opt(
90        sysroot: &Path,
91        triple: &TargetTuple,
92        early_dcx: &EarlyDiagCtxt,
93        path: &str,
94        is_unstable_enabled: bool,
95    ) -> Self {
96        let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
97            (PathKind::Native, stripped)
98        } else if let Some(stripped) = path.strip_prefix("crate=") {
99            (PathKind::Crate, stripped)
100        } else if let Some(stripped) = path.strip_prefix("dependency=") {
101            (PathKind::Dependency, stripped)
102        } else if let Some(stripped) = path.strip_prefix("framework=") {
103            (PathKind::Framework, stripped)
104        } else if let Some(stripped) = path.strip_prefix("all=") {
105            (PathKind::All, stripped)
106        } else {
107            (PathKind::All, path)
108        };
109        let dir = match path.strip_prefix("@RUSTC_BUILTIN") {
110            Some(stripped) => {
111                if !is_unstable_enabled {
112                    #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
113                    early_dcx.early_fatal(
114                        "the `-Z unstable-options` flag must also be passed to \
115                         enable the use of `@RUSTC_BUILTIN`",
116                    );
117                }
118
119                make_target_lib_path(sysroot, triple.tuple()).join("builtin").join(stripped)
120            }
121            None => PathBuf::from(path),
122        };
123        if dir.as_os_str().is_empty() {
124            #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
125            early_dcx.early_fatal("empty search path given via `-L`");
126        }
127
128        Self::new(kind, dir)
129    }
130
131    pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
132        Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
133    }
134
135    pub fn new(kind: PathKind, dir: PathBuf) -> Self {
136        // Get the files within the directory.
137        let mut files = match std::fs::read_dir(&dir) {
138            Ok(files) => files
139                .filter_map(|e| {
140                    e.ok().and_then(|e| {
141                        e.file_name().to_str().map(|s| {
142                            let file_name_str: Arc<str> = s.into();
143                            (
144                                Arc::clone(&file_name_str),
145                                SearchPathFile { path: e.path().into(), file_name_str },
146                            )
147                        })
148                    })
149                })
150                .collect::<Vec<_>>(),
151
152            Err(..) => Default::default(),
153        };
154        files.sort_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs));
155        let files = FilesIndex(files);
156        SearchPath { kind, dir, files }
157    }
158}