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