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.
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.
11 #![allow(non_camel_case_types)]
13 pub use self::FileMatch::*;
15 use std::collections::HashSet;
18 use std::io::prelude::*;
19 use std::path::{Path, PathBuf};
21 use session::search_paths::{SearchPaths, PathKind};
22 use util::fs as rustcfs;
24 #[derive(Copy, Clone)]
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.
34 pub struct FileSearch<'a> {
35 pub sysroot: &'a Path,
36 pub search_paths: &'a SearchPaths,
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,
45 let mut visited_dirs = HashSet::new();
46 let mut found = false;
48 for (path, kind) in self.search_paths.iter(self.kind) {
50 FileMatches => found = true,
53 visited_dirs.insert(path.to_path_buf());
56 debug!("filesearch: searching lib path");
57 let tlib_path = make_target_lib_path(self.sysroot,
59 if !visited_dirs.contains(&tlib_path) {
60 match f(&tlib_path, PathKind::All) {
61 FileMatches => found = true,
66 visited_dirs.insert(tlib_path);
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));
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) {
91 pub fn get_lib_path(&self) -> PathBuf {
92 make_target_lib_path(self.sysroot, self.triple)
95 pub fn search<F>(&self, mut pick: F)
96 where F: FnMut(&Path, PathKind) -> FileMatch
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) {
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")
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);
119 debug!("picked {}", path.display());
123 debug!("rejected {}", path.display());
129 Err(..) => FileDoesntMatch,
134 pub fn new(sysroot: &'a Path,
136 search_paths: &'a SearchPaths,
137 kind: PathKind) -> FileSearch<'a> {
138 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
141 search_paths: search_paths,
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());
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);
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);
177 fn make_target_lib_path(sysroot: &Path,
178 target_triple: &str) -> PathBuf {
179 sysroot.join(&relative_target_lib_path(sysroot, target_triple))
182 fn make_rustpkg_lib_path(sysroot: &Path,
184 triple: &str) -> PathBuf {
185 let mut p = dir.join(&find_libdir(sysroot));
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),
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")
211 const PATH_ENTRY_SEPARATOR: char = ';';
213 const PATH_ENTRY_SEPARATOR: char = ':';
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()
220 /// Returns the value of RUST_PATH, as a list
221 /// of Paths. Includes default entries for, if they exist:
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() {
228 let env_path_components =
229 env_path.split(PATH_ENTRY_SEPARATOR);
230 env_path_components.map(|s| PathBuf::from(s)).collect()
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);
240 if !env_rust_path.contains(&cwd) {
241 env_rust_path.push(cwd.clone());
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());
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);
260 // The name of the directory rustc expects libraries to be located.
261 // On Unix should be "lib", on windows "bin"
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
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).
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();
277 return secondary_libdir_name();
281 #[cfg(target_pointer_width = "64")]
282 fn primary_libdir_name() -> String {
286 #[cfg(target_pointer_width = "32")]
287 fn primary_libdir_name() -> String {
291 fn secondary_libdir_name() -> String {
297 fn find_libdir(_sysroot: &Path) -> String {
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()