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;
12 #[derive(Copy, Clone)]
19 pub struct FileSearch<'a> {
22 search_paths: &'a [SearchPath],
23 tlib_path: &'a SearchPath,
27 impl<'a> FileSearch<'a> {
28 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
32 .filter(move |sp| sp.kind.matches(kind))
33 .chain(std::iter::once(self.tlib_path))
36 pub fn get_lib_path(&self) -> PathBuf {
37 make_target_lib_path(self.sysroot, self.triple)
40 pub fn get_self_contained_lib_path(&self) -> PathBuf {
41 self.get_lib_path().join("self-contained")
47 search_paths: &'a [SearchPath],
48 tlib_path: &'a SearchPath,
51 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
52 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
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()
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")])
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)
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() {
83 let mut p = canonicalize(exe);
88 Err(e) => panic!("failed to get current_exe: {e}"),
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
96 fn from_env_args_next() -> Option<PathBuf> {
97 match env::args_os().next() {
99 let mut p = PathBuf::from(first_arg);
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
105 if fs::read_link(&p).is_err() {
106 // Path is not a symbolic link or does not exist.
110 // Pop off `bin/rustc`, obtaining the suspected sysroot.
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 }
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)