]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_session/src/filesearch.rs
Rollup merge of #93751 - eholk:issue-93648-drop-tracking-projection, r=tmiasko
[rust.git] / compiler / rustc_session / src / filesearch.rs
1 //! A module for searching for libraries
2
3 use std::env;
4 use std::fs;
5 use std::iter::FromIterator;
6 use std::path::{Path, PathBuf};
7
8 use crate::search_paths::{PathKind, SearchPath};
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 #[derive(Clone)]
19 pub struct FileSearch<'a> {
20     sysroot: &'a Path,
21     triple: &'a str,
22     search_paths: &'a [SearchPath],
23     tlib_path: &'a SearchPath,
24     kind: PathKind,
25 }
26
27 impl<'a> FileSearch<'a> {
28     pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
29         let kind = self.kind;
30         self.search_paths
31             .iter()
32             .filter(move |sp| sp.kind.matches(kind))
33             .chain(std::iter::once(self.tlib_path))
34     }
35
36     pub fn get_lib_path(&self) -> PathBuf {
37         make_target_lib_path(self.sysroot, self.triple)
38     }
39
40     pub fn get_self_contained_lib_path(&self) -> PathBuf {
41         self.get_lib_path().join("self-contained")
42     }
43
44     pub fn new(
45         sysroot: &'a Path,
46         triple: &'a str,
47         search_paths: &'a [SearchPath],
48         tlib_path: &'a SearchPath,
49         kind: PathKind,
50     ) -> FileSearch<'a> {
51         debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
52         FileSearch { sysroot, triple, search_paths, tlib_path, kind }
53     }
54
55     /// Returns just the directories within the search paths.
56     pub fn search_path_dirs(&self) -> Vec<PathBuf> {
57         self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
58     }
59 }
60
61 pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
62     let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
63     PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
64 }
65
66 /// This function checks if sysroot is found using env::args().next(), and if it
67 /// is not found, uses env::current_exe() to imply sysroot.
68 pub fn get_or_default_sysroot() -> PathBuf {
69     // Follow symlinks.  If the resolved path is relative, make it absolute.
70     fn canonicalize(path: PathBuf) -> PathBuf {
71         let path = fs::canonicalize(&path).unwrap_or(path);
72         // See comments on this target function, but the gist is that
73         // gcc chokes on verbatim paths which fs::canonicalize generates
74         // so we try to avoid those kinds of paths.
75         fix_windows_verbatim_for_gcc(&path)
76     }
77
78     // Use env::current_exe() to get the path of the executable following
79     // symlinks/canonicalizing components.
80     fn from_current_exe() -> PathBuf {
81         match env::current_exe() {
82             Ok(exe) => {
83                 let mut p = canonicalize(exe);
84                 p.pop();
85                 p.pop();
86                 p
87             }
88             Err(e) => panic!("failed to get current_exe: {}", e),
89         }
90     }
91
92     // Use env::args().next() to get the path of the executable without
93     // following symlinks/canonicalizing any component. This makes the rustc
94     // binary able to locate Rust libraries in systems using content-addressable
95     // storage (CAS).
96     fn from_env_args_next() -> Option<PathBuf> {
97         match env::args_os().next() {
98             Some(first_arg) => {
99                 let mut p = PathBuf::from(first_arg);
100
101                 // Check if sysroot is found using env::args().next() only if the rustc in argv[0]
102                 // is a symlink (see #79253). We might want to change/remove it to conform with
103                 // https://www.gnu.org/prep/standards/standards.html#Finding-Program-Files in the
104                 // future.
105                 if fs::read_link(&p).is_err() {
106                     // Path is not a symbolic link or does not exist.
107                     return None;
108                 }
109
110                 // Pop off `bin/rustc`, obtaining the suspected sysroot.
111                 p.pop();
112                 p.pop();
113                 // Look for the target rustlib directory in the suspected sysroot.
114                 let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy");
115                 rustlib_path.pop(); // pop off the dummy target.
116                 if rustlib_path.exists() { Some(p) } else { None }
117             }
118             None => None,
119         }
120     }
121
122     // Check if sysroot is found using env::args().next(), and if is not found,
123     // use env::current_exe() to imply sysroot.
124     from_env_args_next().unwrap_or_else(from_current_exe)
125 }