]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/filesearch.rs
Remove unused #[allow(...)] statements from compiler/
[rust.git] / compiler / rustc_session / src / filesearch.rs
1 pub use self::FileMatch::*;
2
3 use std::borrow::Cow;
4 use std::env;
5 use std::fs;
6 use std::path::{Path, PathBuf};
7
8 use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
9 use rustc_fs_util::fix_windows_verbatim_for_gcc;
10 use tracing::debug;
11
12 #[derive(Copy, Clone)]
13 pub enum FileMatch {
14     FileMatches,
15     FileDoesntMatch,
16 }
17
18 // A module for searching for libraries
19
20 #[derive(Clone)]
21 pub struct FileSearch<'a> {
22     sysroot: &'a Path,
23     triple: &'a str,
24     search_paths: &'a [SearchPath],
25     tlib_path: &'a SearchPath,
26     kind: PathKind,
27 }
28
29 impl<'a> FileSearch<'a> {
30     pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
31         let kind = self.kind;
32         self.search_paths
33             .iter()
34             .filter(move |sp| sp.kind.matches(kind))
35             .chain(std::iter::once(self.tlib_path))
36     }
37
38     pub fn get_lib_path(&self) -> PathBuf {
39         make_target_lib_path(self.sysroot, self.triple)
40     }
41
42     pub fn get_self_contained_lib_path(&self) -> PathBuf {
43         self.get_lib_path().join("self-contained")
44     }
45
46     pub fn search<F>(&self, mut pick: F)
47     where
48         F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
49     {
50         for search_path in self.search_paths() {
51             debug!("searching {}", search_path.dir.display());
52             fn is_rlib(spf: &SearchPathFile) -> bool {
53                 if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
54             }
55             // Reading metadata out of rlibs is faster, and if we find both
56             // an rlib and a dylib we only read one of the files of
57             // metadata, so in the name of speed, bring all rlib files to
58             // the front of the search list.
59             let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
60             let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
61             for spf in files1.chain(files2) {
62                 debug!("testing {}", spf.path.display());
63                 let maybe_picked = pick(spf, search_path.kind);
64                 match maybe_picked {
65                     FileMatches => {
66                         debug!("picked {}", spf.path.display());
67                     }
68                     FileDoesntMatch => {
69                         debug!("rejected {}", spf.path.display());
70                     }
71                 }
72             }
73         }
74     }
75
76     pub fn new(
77         sysroot: &'a Path,
78         triple: &'a str,
79         search_paths: &'a Vec<SearchPath>,
80         tlib_path: &'a SearchPath,
81         kind: PathKind,
82     ) -> FileSearch<'a> {
83         debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
84         FileSearch { sysroot, triple, search_paths, tlib_path, kind }
85     }
86
87     // Returns just the directories within the search paths.
88     pub fn search_path_dirs(&self) -> Vec<PathBuf> {
89         self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
90     }
91
92     // Returns a list of directories where target-specific tool binaries are located.
93     pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
94         let mut p = PathBuf::from(self.sysroot);
95         p.push(find_libdir(self.sysroot).as_ref());
96         p.push(RUST_LIB_DIR);
97         p.push(&self.triple);
98         p.push("bin");
99         if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
100     }
101 }
102
103 pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
104     let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
105     assert!(p.is_relative());
106     p.push(RUST_LIB_DIR);
107     p.push(target_triple);
108     p.push("lib");
109     p
110 }
111
112 pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
113     sysroot.join(&relative_target_lib_path(sysroot, target_triple))
114 }
115
116 pub fn get_or_default_sysroot() -> PathBuf {
117     // Follow symlinks.  If the resolved path is relative, make it absolute.
118     fn canonicalize(path: PathBuf) -> PathBuf {
119         let path = fs::canonicalize(&path).unwrap_or(path);
120         // See comments on this target function, but the gist is that
121         // gcc chokes on verbatim paths which fs::canonicalize generates
122         // so we try to avoid those kinds of paths.
123         fix_windows_verbatim_for_gcc(&path)
124     }
125
126     match env::current_exe() {
127         Ok(exe) => {
128             let mut p = canonicalize(exe);
129             p.pop();
130             p.pop();
131             p
132         }
133         Err(e) => panic!("failed to get current_exe: {}", e),
134     }
135 }
136
137 // The name of the directory rustc expects libraries to be located.
138 fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
139     // FIXME: This is a quick hack to make the rustc binary able to locate
140     // Rust libraries in Linux environments where libraries might be installed
141     // to lib64/lib32. This would be more foolproof by basing the sysroot off
142     // of the directory where `librustc_driver` is located, rather than
143     // where the rustc binary is.
144     // If --libdir is set during configuration to the value other than
145     // "lib" (i.e., non-default), this value is used (see issue #16552).
146
147     #[cfg(target_pointer_width = "64")]
148     const PRIMARY_LIB_DIR: &str = "lib64";
149
150     #[cfg(target_pointer_width = "32")]
151     const PRIMARY_LIB_DIR: &str = "lib32";
152
153     const SECONDARY_LIB_DIR: &str = "lib";
154
155     match option_env!("CFG_LIBDIR_RELATIVE") {
156         Some(libdir) if libdir != "lib" => libdir.into(),
157         _ => {
158             if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
159                 PRIMARY_LIB_DIR.into()
160             } else {
161                 SECONDARY_LIB_DIR.into()
162             }
163         }
164     }
165 }
166
167 // The name of rustc's own place to organize libraries.
168 // Used to be "rustc", now the default is "rustlib"
169 const RUST_LIB_DIR: &str = "rustlib";