1 #![allow(non_camel_case_types)]
3 pub use self::FileMatch::*;
8 use std::path::{Path, PathBuf};
10 use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
12 use rustc_fs_util::fix_windows_verbatim_for_gcc;
14 #[derive(Copy, Clone)]
20 // A module for searching for libraries
23 pub struct FileSearch<'a> {
26 search_paths: &'a [SearchPath],
27 tlib_path: &'a SearchPath,
31 impl<'a> FileSearch<'a> {
32 pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
36 .filter(move |sp| sp.kind.matches(kind))
37 .chain(std::iter::once(self.tlib_path))
40 pub fn get_lib_path(&self) -> PathBuf {
41 make_target_lib_path(self.sysroot, self.triple)
44 pub fn search<F>(&self, mut pick: F)
46 F: FnMut(&SearchPathFile, PathKind) -> FileMatch,
48 for search_path in self.search_paths() {
49 debug!("searching {}", search_path.dir.display());
50 fn is_rlib(spf: &SearchPathFile) -> bool {
51 if let Some(f) = &spf.file_name_str { f.ends_with(".rlib") } else { false }
53 // Reading metadata out of rlibs is faster, and if we find both
54 // an rlib and a dylib we only read one of the files of
55 // metadata, so in the name of speed, bring all rlib files to
56 // the front of the search list.
57 let files1 = search_path.files.iter().filter(|spf| is_rlib(&spf));
58 let files2 = search_path.files.iter().filter(|spf| !is_rlib(&spf));
59 for spf in files1.chain(files2) {
60 debug!("testing {}", spf.path.display());
61 let maybe_picked = pick(spf, search_path.kind);
64 debug!("picked {}", spf.path.display());
67 debug!("rejected {}", spf.path.display());
77 search_paths: &'a Vec<SearchPath>,
78 tlib_path: &'a SearchPath,
81 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
82 FileSearch { sysroot, triple, search_paths, tlib_path, kind }
85 // Returns just the directories within the search paths.
86 pub fn search_path_dirs(&self) -> Vec<PathBuf> {
87 self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
90 // Returns a list of directories where target-specific tool binaries are located.
91 pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
92 let mut p = PathBuf::from(self.sysroot);
93 p.push(find_libdir(self.sysroot).as_ref());
101 pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
102 let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
103 assert!(p.is_relative());
104 p.push(RUST_LIB_DIR);
105 p.push(target_triple);
110 pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
111 sysroot.join(&relative_target_lib_path(sysroot, target_triple))
114 pub fn get_or_default_sysroot() -> PathBuf {
115 // Follow symlinks. If the resolved path is relative, make it absolute.
116 fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
117 path.and_then(|path| {
118 match fs::canonicalize(&path) {
119 // See comments on this target function, but the gist is that
120 // gcc chokes on verbatim paths which fs::canonicalize generates
121 // so we try to avoid those kinds of paths.
122 Ok(canon) => Some(fix_windows_verbatim_for_gcc(&canon)),
123 Err(e) => panic!("failed to get realpath: {}", e),
128 match env::current_exe() {
129 Ok(exe) => match canonicalize(Some(exe)) {
135 None => panic!("can't determine value for sysroot"),
137 Err(ref e) => panic!(format!("failed to get current_exe: {}", e)),
141 // The name of the directory rustc expects libraries to be located.
142 fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
143 // FIXME: This is a quick hack to make the rustc binary able to locate
144 // Rust libraries in Linux environments where libraries might be installed
145 // to lib64/lib32. This would be more foolproof by basing the sysroot off
146 // of the directory where `librustc_driver` is located, rather than
147 // where the rustc binary is.
148 // If --libdir is set during configuration to the value other than
149 // "lib" (i.e., non-default), this value is used (see issue #16552).
151 #[cfg(target_pointer_width = "64")]
152 const PRIMARY_LIB_DIR: &str = "lib64";
154 #[cfg(target_pointer_width = "32")]
155 const PRIMARY_LIB_DIR: &str = "lib32";
157 const SECONDARY_LIB_DIR: &str = "lib";
159 match option_env!("CFG_LIBDIR_RELATIVE") {
160 Some(libdir) if libdir != "lib" => libdir.into(),
162 if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
163 PRIMARY_LIB_DIR.into()
165 SECONDARY_LIB_DIR.into()
171 // The name of rustc's own place to organize libraries.
172 // Used to be "rustc", now the default is "rustlib"
173 const RUST_LIB_DIR: &str = "rustlib";