]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_session/src/filesearch.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_session / src / filesearch.rs
CommitLineData
c295e0f8
XL
1//! A module for searching for libraries
2
1a4d82fc
JJ
3pub use self::FileMatch::*;
4
85aaf69f 5use std::env;
c34b1796 6use std::fs;
c34b1796 7use std::path::{Path, PathBuf};
1a4d82fc 8
ba9703b0 9use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
dfeec247 10use rustc_fs_util::fix_windows_verbatim_for_gcc;
3dfed10e 11use tracing::debug;
1a4d82fc 12
c34b1796 13#[derive(Copy, Clone)]
1a4d82fc
JJ
14pub enum FileMatch {
15 FileMatches,
16 FileDoesntMatch,
17}
970d7e83 18
532ac7d7 19#[derive(Clone)]
1a4d82fc 20pub struct FileSearch<'a> {
0731742a
XL
21 sysroot: &'a Path,
22 triple: &'a str,
23 search_paths: &'a [SearchPath],
24 tlib_path: &'a SearchPath,
25 kind: PathKind,
223e47cc
LB
26}
27
1a4d82fc 28impl<'a> FileSearch<'a> {
0731742a
XL
29 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
30 let kind = self.kind;
dfeec247
XL
31 self.search_paths
32 .iter()
0731742a
XL
33 .filter(move |sp| sp.kind.matches(kind))
34 .chain(std::iter::once(self.tlib_path))
223e47cc
LB
35 }
36
c34b1796 37 pub fn get_lib_path(&self) -> PathBuf {
1a4d82fc
JJ
38 make_target_lib_path(self.sysroot, self.triple)
39 }
223e47cc 40
f035d41b
XL
41 pub fn get_self_contained_lib_path(&self) -> PathBuf {
42 self.get_lib_path().join("self-contained")
43 }
44
85aaf69f 45 pub fn search<F>(&self, mut pick: F)
dfeec247 46 where
ba9703b0 47 F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
85aaf69f 48 {
0731742a
XL
49 for search_path in self.search_paths() {
50 debug!("searching {}", search_path.dir.display());
ba9703b0
XL
51 fn is_rlib(spf: &SearchPathFile) -> bool {
52 if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
c30ab7b3
SL
53 }
54 // Reading metadata out of rlibs is faster, and if we find both
55 // an rlib and a dylib we only read one of the files of
56 // metadata, so in the name of speed, bring all rlib files to
57 // the front of the search list.
ba9703b0
XL
58 let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
59 let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
60 for spf in files1.chain(files2) {
61 debug!("testing {}", spf.path.display());
62 let maybe_picked = pick(spf, search_path.kind);
c30ab7b3
SL
63 match maybe_picked {
64 FileMatches => {
ba9703b0 65 debug!("picked {}", spf.path.display());
1a4d82fc 66 }
c30ab7b3 67 FileDoesntMatch => {
ba9703b0 68 debug!("rejected {}", spf.path.display());
1a4d82fc 69 }
1a4d82fc 70 }
223e47cc 71 }
0731742a 72 }
1a4d82fc
JJ
73 }
74
dfeec247
XL
75 pub fn new(
76 sysroot: &'a Path,
77 triple: &'a str,
5869c6ff 78 search_paths: &'a [SearchPath],
dfeec247
XL
79 tlib_path: &'a SearchPath,
80 kind: PathKind,
81 ) -> FileSearch<'a> {
1a4d82fc 82 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
dfeec247 83 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
223e47cc 84 }
1a4d82fc 85
c295e0f8 86 /// Returns just the directories within the search paths.
0731742a 87 pub fn search_path_dirs(&self) -> Vec<PathBuf> {
dfeec247 88 self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
1a4d82fc 89 }
223e47cc
LB
90}
91
0731742a 92pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
17df50a5
XL
93 let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
94 std::array::IntoIter::new([sysroot, Path::new(&rustlib_path), Path::new("lib")])
95 .collect::<PathBuf>()
223e47cc
LB
96}
97
c295e0f8
XL
98/// This function checks if sysroot is found using env::args().next(), and if it
99/// is not found, uses env::current_exe() to imply sysroot.
c34b1796 100pub fn get_or_default_sysroot() -> PathBuf {
1a4d82fc 101 // Follow symlinks. If the resolved path is relative, make it absolute.
3dfed10e
XL
102 fn canonicalize(path: PathBuf) -> PathBuf {
103 let path = fs::canonicalize(&path).unwrap_or(path);
104 // See comments on this target function, but the gist is that
105 // gcc chokes on verbatim paths which fs::canonicalize generates
106 // so we try to avoid those kinds of paths.
107 fix_windows_verbatim_for_gcc(&path)
1a4d82fc
JJ
108 }
109
5869c6ff
XL
110 // Use env::current_exe() to get the path of the executable following
111 // symlinks/canonicalizing components.
112 fn from_current_exe() -> PathBuf {
113 match env::current_exe() {
114 Ok(exe) => {
115 let mut p = canonicalize(exe);
116 p.pop();
117 p.pop();
118 p
119 }
120 Err(e) => panic!("failed to get current_exe: {}", e),
121 }
122 }
123
124 // Use env::args().next() to get the path of the executable without
125 // following symlinks/canonicalizing any component. This makes the rustc
126 // binary able to locate Rust libraries in systems using content-addressable
127 // storage (CAS).
128 fn from_env_args_next() -> Option<PathBuf> {
129 match env::args_os().next() {
130 Some(first_arg) => {
131 let mut p = PathBuf::from(first_arg);
132
133 // Check if sysroot is found using env::args().next() only if the rustc in argv[0]
134 // is a symlink (see #79253). We might want to change/remove it to conform with
135 // https://www.gnu.org/prep/standards/standards.html#Finding-Program-Files in the
136 // future.
137 if fs::read_link(&p).is_err() {
138 // Path is not a symbolic link or does not exist.
139 return None;
140 }
141
17df50a5 142 // Pop off `bin/rustc`, obtaining the suspected sysroot.
5869c6ff
XL
143 p.pop();
144 p.pop();
17df50a5
XL
145 // Look for the target rustlib directory in the suspected sysroot.
146 let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy");
147 rustlib_path.pop(); // pop off the dummy target.
148 if rustlib_path.exists() { Some(p) } else { None }
5869c6ff
XL
149 }
150 None => None,
3dfed10e 151 }
223e47cc 152 }
5869c6ff
XL
153
154 // Check if sysroot is found using env::args().next(), and if is not found,
155 // use env::current_exe() to imply sysroot.
6a06907d 156 from_env_args_next().unwrap_or_else(from_current_exe)
223e47cc 157}