1 //! A module for searching for libraries
3 pub use self::FileMatch::*;
7 use std::iter::FromIterator;
8 use std::path::{Path, PathBuf};
10 use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
11 use rustc_fs_util::fix_windows_verbatim_for_gcc;
14 #[derive(Copy, Clone)]
21 pub struct FileSearch<'a> {
24 search_paths: &'a [SearchPath],
25 tlib_path: &'a SearchPath,
29 impl<'a> FileSearch<'a> {
30 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
34 .filter(move |sp| sp.kind.matches(kind))
35 .chain(std::iter::once(self.tlib_path))
38 pub fn get_lib_path(&self) -> PathBuf {
39 make_target_lib_path(self.sysroot, self.triple)
42 pub fn get_self_contained_lib_path(&self) -> PathBuf {
43 self.get_lib_path().join("self-contained")
46 pub fn search<F>(&self, mut pick: F)
48 F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
50 for search_path in self.search_paths() {
51 debug!("searching {}", search_path.dir.display());
52 fn is_rlib(spf: &SearchPathFile) -> bool {
53 if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
55 // Reading metadata out of rlibs is faster, and if we find both
56 // an rlib and a dylib we only read one of the files of
57 // metadata, so in the name of speed, bring all rlib files to
58 // the front of the search list.
59 let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
60 let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
61 for spf in files1.chain(files2) {
62 debug!("testing {}", spf.path.display());
63 let maybe_picked = pick(spf, search_path.kind);
66 debug!("picked {}", spf.path.display());
69 debug!("rejected {}", spf.path.display());
79 search_paths: &'a [SearchPath],
80 tlib_path: &'a SearchPath,
83 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
84 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
87 /// Returns just the directories within the search paths.
88 pub fn search_path_dirs(&self) -> Vec<PathBuf> {
89 self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
93 pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
94 let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
95 PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
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.
100 pub fn get_or_default_sysroot() -> PathBuf {
101 // Follow symlinks. If the resolved path is relative, make it absolute.
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)
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() {
115 let mut p = canonicalize(exe);
120 Err(e) => panic!("failed to get current_exe: {}", e),
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
128 fn from_env_args_next() -> Option<PathBuf> {
129 match env::args_os().next() {
131 let mut p = PathBuf::from(first_arg);
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
137 if fs::read_link(&p).is_err() {
138 // Path is not a symbolic link or does not exist.
142 // Pop off `bin/rustc`, obtaining the suspected sysroot.
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 }
154 // Check if sysroot is found using env::args().next(), and if is not found,
155 // use env::current_exe() to imply sysroot.
156 from_env_args_next().unwrap_or_else(from_current_exe)