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(#[automatically_derived]
impl ::core::clone::Clone for SearchPath {
#[inline]
fn clone(&self) -> SearchPath {
SearchPath {
kind: ::core::clone::Clone::clone(&self.kind),
dir: ::core::clone::Clone::clone(&self.dir),
files: ::core::clone::Clone::clone(&self.files),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for SearchPath {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "SearchPath",
"kind", &self.kind, "dir", &self.dir, "files", &&self.files)
}
}Debug)]
11pub struct SearchPath {
12 pub kind: PathKind,
13 pub dir: PathBuf,
14 pub files: FilesIndex,
15}
16
17#[derive(#[automatically_derived]
impl ::core::clone::Clone for FilesIndex {
#[inline]
fn clone(&self) -> FilesIndex {
FilesIndex(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for FilesIndex {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "FilesIndex",
&&self.0)
}
}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(#[automatically_derived]
impl ::core::clone::Clone for SearchPathFile {
#[inline]
fn clone(&self) -> SearchPathFile {
SearchPathFile {
path: ::core::clone::Clone::clone(&self.path),
file_name_str: ::core::clone::Clone::clone(&self.file_name_str),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for SearchPathFile {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"SearchPathFile", "path", &self.path, "file_name_str",
&&self.file_name_str)
}
}Debug)]
63pub struct SearchPathFile {
64 pub path: Arc<Path>,
65 pub file_name_str: Arc<str>,
66}
67
68#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for PathKind {
#[inline]
fn eq(&self, other: &PathKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::clone::Clone for PathKind {
#[inline]
fn clone(&self) -> PathKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PathKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PathKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
PathKind::Native => "Native",
PathKind::Crate => "Crate",
PathKind::Dependency => "Dependency",
PathKind::Framework => "Framework",
PathKind::All => "All",
})
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for PathKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::Eq for PathKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {}
}Eq, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for PathKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
PathKind::Native => { 0usize }
PathKind::Crate => { 1usize }
PathKind::Dependency => { 2usize }
PathKind::Framework => { 3usize }
PathKind::All => { 4usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
PathKind::Native => {}
PathKind::Crate => {}
PathKind::Dependency => {}
PathKind::Framework => {}
PathKind::All => {}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for PathKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { PathKind::Native }
1usize => { PathKind::Crate }
2usize => { PathKind::Dependency }
3usize => { PathKind::Framework }
4usize => { PathKind::All }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `PathKind`, expected 0..5, actual {0}",
n));
}
}
}
}
};Decodable, const _: () =
{
impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for PathKind where __CTX: crate::HashStableContext {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
PathKind::Native => {}
PathKind::Crate => {}
PathKind::Dependency => {}
PathKind::Framework => {}
PathKind::All => {}
}
}
}
};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}