rustc_session/
search_paths.rs1use 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<'s>(
24 &'s self,
25 prefix: &str,
26 suffix: &str,
27 ) -> Option<impl Iterator<Item = (String, &'s SearchPathFile)>> {
28 let start = self.0.partition_point(|(k, _)| **k < *prefix);
29 if start == self.0.len() {
30 return None;
31 }
32 let end = self.0[start..].partition_point(|(k, _)| k.starts_with(prefix));
33 let prefixed_items = &self.0[start..][..end];
34
35 let ret = prefixed_items.into_iter().filter_map(move |(k, v)| {
36 k.ends_with(suffix).then(|| {
37 (
38 String::from(
39 &v.file_name_str[prefix.len()..v.file_name_str.len() - suffix.len()],
40 ),
41 v,
42 )
43 })
44 });
45 Some(ret)
46 }
47 pub fn retain(&mut self, prefixes: &[&str]) {
48 self.0.retain(|(k, _)| prefixes.iter().any(|prefix| k.starts_with(prefix)));
49 }
50}
51#[derive(Clone, Debug)]
63pub struct SearchPathFile {
64 pub path: Arc<Path>,
65 pub file_name_str: Arc<str>,
66}
67
68#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
69pub enum PathKind {
70 Native,
71 Crate,
72 Dependency,
73 Framework,
74 All,
75}
76
77impl PathKind {
78 pub fn matches(&self, kind: PathKind) -> bool {
79 match (self, kind) {
80 (PathKind::All, _) | (_, PathKind::All) => true,
81 _ => *self == kind,
82 }
83 }
84}
85
86impl SearchPath {
87 pub fn from_cli_opt(
88 sysroot: &Path,
89 triple: &TargetTuple,
90 early_dcx: &EarlyDiagCtxt,
91 path: &str,
92 is_unstable_enabled: bool,
93 ) -> Self {
94 let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
95 (PathKind::Native, stripped)
96 } else if let Some(stripped) = path.strip_prefix("crate=") {
97 (PathKind::Crate, stripped)
98 } else if let Some(stripped) = path.strip_prefix("dependency=") {
99 (PathKind::Dependency, stripped)
100 } else if let Some(stripped) = path.strip_prefix("framework=") {
101 (PathKind::Framework, stripped)
102 } else if let Some(stripped) = path.strip_prefix("all=") {
103 (PathKind::All, stripped)
104 } else {
105 (PathKind::All, path)
106 };
107 let dir = match path.strip_prefix("@RUSTC_BUILTIN") {
108 Some(stripped) => {
109 if !is_unstable_enabled {
110 #[allow(rustc::untranslatable_diagnostic)] early_dcx.early_fatal(
112 "the `-Z unstable-options` flag must also be passed to \
113 enable the use of `@RUSTC_BUILTIN`",
114 );
115 }
116
117 make_target_lib_path(sysroot, triple.tuple()).join("builtin").join(stripped)
118 }
119 None => PathBuf::from(path),
120 };
121 if dir.as_os_str().is_empty() {
122 #[allow(rustc::untranslatable_diagnostic)] early_dcx.early_fatal("empty search path given via `-L`");
124 }
125
126 Self::new(kind, dir)
127 }
128
129 pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
130 Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
131 }
132
133 pub fn new(kind: PathKind, dir: PathBuf) -> Self {
134 let mut files = match std::fs::read_dir(&dir) {
136 Ok(files) => files
137 .filter_map(|e| {
138 e.ok().and_then(|e| {
139 e.file_name().to_str().map(|s| {
140 let file_name_str: Arc<str> = s.into();
141 (
142 Arc::clone(&file_name_str),
143 SearchPathFile { path: e.path().into(), file_name_str },
144 )
145 })
146 })
147 })
148 .collect::<Vec<_>>(),
149
150 Err(..) => Default::default(),
151 };
152 files.sort_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs));
153 let files = FilesIndex(files);
154 SearchPath { kind, dir, files }
155 }
156}