]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/filesearch.rs
Fix stability
[rust.git] / src / librustc / metadata / filesearch.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_camel_case_types)]
12
13 pub use self::FileMatch::*;
14
15 use std::collections::HashSet;
16 use std::env;
17 use std::fs;
18 use std::io::prelude::*;
19 use std::path::{Path, PathBuf};
20
21 use session::search_paths::{SearchPaths, PathKind};
22 use util::fs as rustcfs;
23
24 #[derive(Copy, Clone)]
25 pub enum FileMatch {
26     FileMatches,
27     FileDoesntMatch,
28 }
29
30 // A module for searching for libraries
31 // FIXME (#2658): I'm not happy how this module turned out. Should
32 // probably just be folded into cstore.
33
34 pub struct FileSearch<'a> {
35     pub sysroot: &'a Path,
36     pub search_paths: &'a SearchPaths,
37     pub triple: &'a str,
38     pub kind: PathKind,
39 }
40
41 impl<'a> FileSearch<'a> {
42     pub fn for_each_lib_search_path<F>(&self, mut f: F) where
43         F: FnMut(&Path, PathKind) -> FileMatch,
44     {
45         let mut visited_dirs = HashSet::new();
46         let mut found = false;
47
48         for (path, kind) in self.search_paths.iter(self.kind) {
49             match f(path, kind) {
50                 FileMatches => found = true,
51                 FileDoesntMatch => ()
52             }
53             visited_dirs.insert(path.to_path_buf());
54         }
55
56         debug!("filesearch: searching lib path");
57         let tlib_path = make_target_lib_path(self.sysroot,
58                                              self.triple);
59         if !visited_dirs.contains(&tlib_path) {
60             match f(&tlib_path, PathKind::All) {
61                 FileMatches => found = true,
62                 FileDoesntMatch => ()
63             }
64         }
65
66         visited_dirs.insert(tlib_path);
67         // Try RUST_PATH
68         if !found {
69             let rustpath = rust_path();
70             for path in &rustpath {
71                 let tlib_path = make_rustpkg_lib_path(
72                     self.sysroot, path, self.triple);
73                 debug!("is {} in visited_dirs? {}", tlib_path.display(),
74                         visited_dirs.contains(&tlib_path));
75
76                 if !visited_dirs.contains(&tlib_path) {
77                     visited_dirs.insert(tlib_path.clone());
78                     // Don't keep searching the RUST_PATH if one match turns up --
79                     // if we did, we'd get a "multiple matching crates" error
80                     match f(&tlib_path, PathKind::All) {
81                        FileMatches => {
82                            break;
83                        }
84                        FileDoesntMatch => ()
85                     }
86                 }
87             }
88         }
89     }
90
91     pub fn get_lib_path(&self) -> PathBuf {
92         make_target_lib_path(self.sysroot, self.triple)
93     }
94
95     pub fn search<F>(&self, mut pick: F)
96         where F: FnMut(&Path, PathKind) -> FileMatch
97     {
98         self.for_each_lib_search_path(|lib_search_path, kind| {
99             debug!("searching {}", lib_search_path.display());
100             match fs::read_dir(lib_search_path) {
101                 Ok(files) => {
102                     let files = files.filter_map(|p| p.ok().map(|s| s.path()))
103                                      .collect::<Vec<_>>();
104                     let mut rslt = FileDoesntMatch;
105                     fn is_rlib(p: &Path) -> bool {
106                         p.extension().and_then(|s| s.to_str()) == Some("rlib")
107                     }
108                     // Reading metadata out of rlibs is faster, and if we find both
109                     // an rlib and a dylib we only read one of the files of
110                     // metadata, so in the name of speed, bring all rlib files to
111                     // the front of the search list.
112                     let files1 = files.iter().filter(|p| is_rlib(p));
113                     let files2 = files.iter().filter(|p| !is_rlib(p));
114                     for path in files1.chain(files2) {
115                         debug!("testing {}", path.display());
116                         let maybe_picked = pick(path, kind);
117                         match maybe_picked {
118                             FileMatches => {
119                                 debug!("picked {}", path.display());
120                                 rslt = FileMatches;
121                             }
122                             FileDoesntMatch => {
123                                 debug!("rejected {}", path.display());
124                             }
125                         }
126                     }
127                     rslt
128                 }
129                 Err(..) => FileDoesntMatch,
130             }
131         });
132     }
133
134     pub fn new(sysroot: &'a Path,
135                triple: &'a str,
136                search_paths: &'a SearchPaths,
137                kind: PathKind) -> FileSearch<'a> {
138         debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
139         FileSearch {
140             sysroot: sysroot,
141             search_paths: search_paths,
142             triple: triple,
143             kind: kind,
144         }
145     }
146
147     // Returns a list of directories where target-specific dylibs might be located.
148     pub fn get_dylib_search_paths(&self) -> Vec<PathBuf> {
149         let mut paths = Vec::new();
150         self.for_each_lib_search_path(|lib_search_path, _| {
151             paths.push(lib_search_path.to_path_buf());
152             FileDoesntMatch
153         });
154         paths
155     }
156
157     // Returns a list of directories where target-specific tool binaries are located.
158     pub fn get_tools_search_paths(&self) -> Vec<PathBuf> {
159         let mut p = PathBuf::from(self.sysroot);
160         p.push(&find_libdir(self.sysroot));
161         p.push(&rustlibdir());
162         p.push(&self.triple);
163         p.push("bin");
164         vec![p]
165     }
166 }
167
168 pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
169     let mut p = PathBuf::from(&find_libdir(sysroot));
170     assert!(p.is_relative());
171     p.push(&rustlibdir());
172     p.push(target_triple);
173     p.push("lib");
174     p
175 }
176
177 fn make_target_lib_path(sysroot: &Path,
178                         target_triple: &str) -> PathBuf {
179     sysroot.join(&relative_target_lib_path(sysroot, target_triple))
180 }
181
182 fn make_rustpkg_lib_path(sysroot: &Path,
183                          dir: &Path,
184                          triple: &str) -> PathBuf {
185     let mut p = dir.join(&find_libdir(sysroot));
186     p.push(triple);
187     p
188 }
189
190 pub fn get_or_default_sysroot() -> PathBuf {
191     // Follow symlinks.  If the resolved path is relative, make it absolute.
192     fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
193         path.and_then(|path| {
194             match fs::canonicalize(&path) {
195                 // See comments on this target function, but the gist is that
196                 // gcc chokes on verbatim paths which fs::canonicalize generates
197                 // so we try to avoid those kinds of paths.
198                 Ok(canon) => Some(rustcfs::fix_windows_verbatim_for_gcc(&canon)),
199                 Err(e) => panic!("failed to get realpath: {}", e),
200             }
201         })
202     }
203
204     match canonicalize(env::current_exe().ok()) {
205         Some(mut p) => { p.pop(); p.pop(); p }
206         None => panic!("can't determine value for sysroot")
207     }
208 }
209
210 #[cfg(windows)]
211 const PATH_ENTRY_SEPARATOR: char = ';';
212 #[cfg(not(windows))]
213 const PATH_ENTRY_SEPARATOR: char = ':';
214
215 /// Returns RUST_PATH as a string, without default paths added
216 pub fn get_rust_path() -> Option<String> {
217     env::var("RUST_PATH").ok()
218 }
219
220 /// Returns the value of RUST_PATH, as a list
221 /// of Paths. Includes default entries for, if they exist:
222 /// $HOME/.rust
223 /// DIR/.rust for any DIR that's the current working directory
224 /// or an ancestor of it
225 pub fn rust_path() -> Vec<PathBuf> {
226     let mut env_rust_path: Vec<PathBuf> = match get_rust_path() {
227         Some(env_path) => {
228             let env_path_components =
229                 env_path.split(PATH_ENTRY_SEPARATOR);
230             env_path_components.map(|s| PathBuf::from(s)).collect()
231         }
232         None => Vec::new()
233     };
234     let cwd = env::current_dir().unwrap();
235     // now add in default entries
236     let cwd_dot_rust = cwd.join(".rust");
237     if !env_rust_path.contains(&cwd_dot_rust) {
238         env_rust_path.push(cwd_dot_rust);
239     }
240     if !env_rust_path.contains(&cwd) {
241         env_rust_path.push(cwd.clone());
242     }
243     let mut cur = &*cwd;
244     while let Some(parent) = cur.parent() {
245         let candidate = parent.join(".rust");
246         if !env_rust_path.contains(&candidate) && candidate.exists() {
247             env_rust_path.push(candidate.clone());
248         }
249         cur = parent;
250     }
251     if let Some(h) = env::home_dir() {
252         let p = h.join(".rust");
253         if !env_rust_path.contains(&p) && p.exists() {
254             env_rust_path.push(p);
255         }
256     }
257     env_rust_path
258 }
259
260 // The name of the directory rustc expects libraries to be located.
261 // On Unix should be "lib", on windows "bin"
262 #[cfg(unix)]
263 fn find_libdir(sysroot: &Path) -> String {
264     // FIXME: This is a quick hack to make the rustc binary able to locate
265     // Rust libraries in Linux environments where libraries might be installed
266     // to lib64/lib32. This would be more foolproof by basing the sysroot off
267     // of the directory where librustc is located, rather than where the rustc
268     // binary is.
269     //If --libdir is set during configuration to the value other than
270     // "lib" (i.e. non-default), this value is used (see issue #16552).
271
272     match option_env!("CFG_LIBDIR_RELATIVE") {
273         Some(libdir) if libdir != "lib" => return libdir.to_string(),
274         _ => if sysroot.join(&primary_libdir_name()).join(&rustlibdir()).exists() {
275             return primary_libdir_name();
276         } else {
277             return secondary_libdir_name();
278         }
279     }
280
281     #[cfg(target_pointer_width = "64")]
282     fn primary_libdir_name() -> String {
283         "lib64".to_string()
284     }
285
286     #[cfg(target_pointer_width = "32")]
287     fn primary_libdir_name() -> String {
288         "lib32".to_string()
289     }
290
291     fn secondary_libdir_name() -> String {
292         "lib".to_string()
293     }
294 }
295
296 #[cfg(windows)]
297 fn find_libdir(_sysroot: &Path) -> String {
298     "bin".to_string()
299 }
300
301 // The name of rustc's own place to organize libraries.
302 // Used to be "rustc", now the default is "rustlib"
303 pub fn rustlibdir() -> String {
304     "rustlib".to_string()
305 }