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#[derive(Clone, Debug)]
19pub struct FilesIndex(Vec<(Arc<str>, SearchPathFile)>);
20
21impl FilesIndex {
22 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#[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)] 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)] 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 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}