]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_session/src/filesearch.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_session / src / filesearch.rs
CommitLineData
c295e0f8
XL
1//! A module for searching for libraries
2
85aaf69f 3use std::env;
c34b1796 4use std::fs;
a2a8927a 5use std::iter::FromIterator;
c34b1796 6use std::path::{Path, PathBuf};
1a4d82fc 7
5099ac24 8use crate::search_paths::{PathKind, SearchPath};
dfeec247 9use rustc_fs_util::fix_windows_verbatim_for_gcc;
1a4d82fc 10
c34b1796 11#[derive(Copy, Clone)]
1a4d82fc
JJ
12pub enum FileMatch {
13 FileMatches,
14 FileDoesntMatch,
15}
970d7e83 16
532ac7d7 17#[derive(Clone)]
1a4d82fc 18pub struct FileSearch<'a> {
0731742a
XL
19 sysroot: &'a Path,
20 triple: &'a str,
21 search_paths: &'a [SearchPath],
22 tlib_path: &'a SearchPath,
23 kind: PathKind,
223e47cc
LB
24}
25
1a4d82fc 26impl<'a> FileSearch<'a> {
0731742a
XL
27 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
28 let kind = self.kind;
dfeec247
XL
29 self.search_paths
30 .iter()
0731742a
XL
31 .filter(move |sp| sp.kind.matches(kind))
32 .chain(std::iter::once(self.tlib_path))
223e47cc
LB
33 }
34
c34b1796 35 pub fn get_lib_path(&self) -> PathBuf {
1a4d82fc
JJ
36 make_target_lib_path(self.sysroot, self.triple)
37 }
223e47cc 38
f035d41b
XL
39 pub fn get_self_contained_lib_path(&self) -> PathBuf {
40 self.get_lib_path().join("self-contained")
41 }
42
dfeec247
XL
43 pub fn new(
44 sysroot: &'a Path,
45 triple: &'a str,
5869c6ff 46 search_paths: &'a [SearchPath],
dfeec247
XL
47 tlib_path: &'a SearchPath,
48 kind: PathKind,
49 ) -> FileSearch<'a> {
1a4d82fc 50 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
dfeec247 51 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
223e47cc 52 }
1a4d82fc 53
c295e0f8 54 /// Returns just the directories within the search paths.
0731742a 55 pub fn search_path_dirs(&self) -> Vec<PathBuf> {
dfeec247 56 self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
1a4d82fc 57 }
223e47cc
LB
58}
59
0731742a 60pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
17df50a5 61 let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
a2a8927a 62 PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
223e47cc
LB
63}
64
c295e0f8
XL
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.
c34b1796 67pub fn get_or_default_sysroot() -> PathBuf {
1a4d82fc 68 // Follow symlinks. If the resolved path is relative, make it absolute.
3dfed10e
XL
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)
1a4d82fc
JJ
75 }
76
5869c6ff
XL
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 }
5e7ed085 87 Err(e) => panic!("failed to get current_exe: {e}"),
5869c6ff
XL
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
17df50a5 109 // Pop off `bin/rustc`, obtaining the suspected sysroot.
5869c6ff
XL
110 p.pop();
111 p.pop();
17df50a5
XL
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 }
5869c6ff
XL
116 }
117 None => None,
3dfed10e 118 }
223e47cc 119 }
5869c6ff
XL
120
121 // Check if sysroot is found using env::args().next(), and if is not found,
122 // use env::current_exe() to imply sysroot.
6a06907d 123 from_env_args_next().unwrap_or_else(from_current_exe)
223e47cc 124}