1 //! A module for searching for libraries
5 use std::iter::FromIterator;
6 use std::path::{Path, PathBuf};
8 use crate::search_paths::{PathKind, SearchPath};
9 use rustc_fs_util::fix_windows_verbatim_for_gcc;
11 #[derive(Copy, Clone)]
18 pub struct FileSearch<'a> {
21 search_paths: &'a [SearchPath],
22 tlib_path: &'a SearchPath,
26 impl<'a> FileSearch<'a> {
27 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
31 .filter(move |sp| sp.kind.matches(kind))
32 .chain(std::iter::once(self.tlib_path))
35 pub fn get_lib_path(&self) -> PathBuf {
36 make_target_lib_path(self.sysroot, self.triple)
39 pub fn get_self_contained_lib_path(&self) -> PathBuf {
40 self.get_lib_path().join("self-contained")
46 search_paths: &'a [SearchPath],
47 tlib_path: &'a SearchPath,
50 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
51 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
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()
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")])
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)
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() {
82 let mut p = canonicalize(exe);
87 Err(e) => panic!("failed to get current_exe: {e}"),
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
95 fn from_env_args_next() -> Option<PathBuf> {
96 match env::args_os().next() {
98 let mut p = PathBuf::from(first_arg);
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
104 if fs::read_link(&p).is_err() {
105 // Path is not a symbolic link or does not exist.
109 // Pop off `bin/rustc`, obtaining the suspected sysroot.
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 }
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)