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