]> git.lizzy.rs Git - rust.git/blob - src/librustc/session/filesearch.rs
Auto merge of #57101 - o01eg:fix-57014, r=alexcrichton
[rust.git] / src / librustc / session / filesearch.rs
1 #![allow(non_camel_case_types)]
2
3 pub use self::FileMatch::*;
4
5 use std::borrow::Cow;
6 use std::env;
7 use std::fs;
8 use std::path::{Path, PathBuf};
9
10 use session::search_paths::{SearchPath, PathKind};
11 use rustc_fs_util::fix_windows_verbatim_for_gcc;
12
13 #[derive(Copy, Clone)]
14 pub enum FileMatch {
15     FileMatches,
16     FileDoesntMatch,
17 }
18
19 // A module for searching for libraries
20
21 pub struct FileSearch<'a> {
22     sysroot: &'a Path,
23     triple: &'a str,
24     search_paths: &'a [SearchPath],
25     tlib_path: &'a SearchPath,
26     kind: PathKind,
27 }
28
29 impl<'a> FileSearch<'a> {
30     pub fn search_paths(&self) -> impl Iterator<Item = &'a SearchPath> {
31         let kind = self.kind;
32         self.search_paths.iter()
33             .filter(move |sp| sp.kind.matches(kind))
34             .chain(std::iter::once(self.tlib_path))
35     }
36
37     pub fn get_lib_path(&self) -> PathBuf {
38         make_target_lib_path(self.sysroot, self.triple)
39     }
40
41     pub fn search<F>(&self, mut pick: F)
42         where F: FnMut(&Path, PathKind) -> FileMatch
43     {
44         for search_path in self.search_paths() {
45             debug!("searching {}", search_path.dir.display());
46             fn is_rlib(p: &Path) -> bool {
47                 p.extension() == Some("rlib".as_ref())
48             }
49             // Reading metadata out of rlibs is faster, and if we find both
50             // an rlib and a dylib we only read one of the files of
51             // metadata, so in the name of speed, bring all rlib files to
52             // the front of the search list.
53             let files1 = search_path.files.iter().filter(|p| is_rlib(p));
54             let files2 = search_path.files.iter().filter(|p| !is_rlib(p));
55             for path in files1.chain(files2) {
56                 debug!("testing {}", path.display());
57                 let maybe_picked = pick(path, search_path.kind);
58                 match maybe_picked {
59                     FileMatches => {
60                         debug!("picked {}", path.display());
61                     }
62                     FileDoesntMatch => {
63                         debug!("rejected {}", path.display());
64                     }
65                 }
66             }
67         }
68     }
69
70     pub fn new(sysroot: &'a Path,
71                triple: &'a str,
72                search_paths: &'a Vec<SearchPath>,
73                tlib_path: &'a SearchPath,
74                kind: PathKind)
75                -> FileSearch<'a> {
76         debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
77         FileSearch {
78             sysroot,
79             triple,
80             search_paths,
81             tlib_path,
82             kind,
83         }
84     }
85
86     // Returns just the directories within the search paths.
87     pub fn search_path_dirs(&self) -> Vec<PathBuf> {
88         self.search_paths()
89             .map(|sp| sp.dir.to_path_buf())
90             .collect()
91     }
92
93     // Returns a list of directories where target-specific tool binaries are located.
94     pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
95         let mut p = PathBuf::from(self.sysroot);
96         p.push(find_libdir(self.sysroot).as_ref());
97         p.push(RUST_LIB_DIR);
98         p.push(&self.triple);
99         p.push("bin");
100         vec![p]
101     }
102 }
103
104 pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
105     let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
106     assert!(p.is_relative());
107     p.push(RUST_LIB_DIR);
108     p.push(target_triple);
109     p.push("lib");
110     p
111 }
112
113 pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
114     sysroot.join(&relative_target_lib_path(sysroot, target_triple))
115 }
116
117 pub fn target_lib_path(target_triple: &str) -> PathBuf {
118     let mut p = PathBuf::from(RUST_LIB_DIR);
119     p.push(target_triple);
120     p.push("lib");
121     p
122 }
123
124 pub fn get_or_default_sysroot() -> PathBuf {
125     // Follow symlinks.  If the resolved path is relative, make it absolute.
126     fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
127         path.and_then(|path| {
128             match fs::canonicalize(&path) {
129                 // See comments on this target function, but the gist is that
130                 // gcc chokes on verbatim paths which fs::canonicalize generates
131                 // so we try to avoid those kinds of paths.
132                 Ok(canon) => Some(fix_windows_verbatim_for_gcc(&canon)),
133                 Err(e) => bug!("failed to get realpath: {}", e),
134             }
135         })
136     }
137
138     match env::current_exe() {
139         Ok(exe) => {
140             match canonicalize(Some(exe)) {
141                 Some(mut p) => { p.pop(); p.pop(); p },
142                 None => bug!("can't determine value for sysroot")
143             }
144         }
145         Err(ref e) => panic!(format!("failed to get current_exe: {}", e))
146     }
147 }
148
149 // The name of the directory rustc expects libraries to be located.
150 fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
151     // FIXME: This is a quick hack to make the rustc binary able to locate
152     // Rust libraries in Linux environments where libraries might be installed
153     // to lib64/lib32. This would be more foolproof by basing the sysroot off
154     // of the directory where librustc is located, rather than where the rustc
155     // binary is.
156     // If --libdir is set during configuration to the value other than
157     // "lib" (i.e., non-default), this value is used (see issue #16552).
158
159     #[cfg(target_pointer_width = "64")]
160     const PRIMARY_LIB_DIR: &str = "lib64";
161
162     #[cfg(target_pointer_width = "32")]
163     const PRIMARY_LIB_DIR: &str = "lib32";
164
165     const SECONDARY_LIB_DIR: &str = "lib";
166
167     match option_env!("CFG_LIBDIR_RELATIVE") {
168         Some(libdir) if libdir != "lib" => libdir.into(),
169         _ => if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
170             PRIMARY_LIB_DIR.into()
171         } else {
172             SECONDARY_LIB_DIR.into()
173         }
174     }
175 }
176
177 // The name of rustc's own place to organize libraries.
178 // Used to be "rustc", now the default is "rustlib"
179 const RUST_LIB_DIR: &str = "rustlib";