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 use std::cell::RefCell;
16 use std::unstable::dynamic_lib::DynamicLibrary;
17 use collections::HashSet;
21 pub enum FileMatch { FileMatches, FileDoesntMatch }
23 // A module for searching for libraries
24 // FIXME (#2658): I'm not happy how this module turned out. Should
25 // probably just be folded into cstore.
27 /// Functions with type `pick` take a parent directory as well as
28 /// a file found in that directory.
29 pub type pick<'a> = |path: &Path|: 'a -> FileMatch;
31 pub struct FileSearch<'a> {
32 pub sysroot: &'a Path,
33 pub addl_lib_search_paths: &'a RefCell<HashSet<Path>>,
37 impl<'a> FileSearch<'a> {
38 pub fn for_each_lib_search_path(&self, f: |&Path| -> FileMatch) {
39 let mut visited_dirs = HashSet::new();
40 let mut found = false;
42 debug!("filesearch: searching additional lib search paths [{:?}]",
43 self.addl_lib_search_paths.borrow().len());
44 for path in self.addl_lib_search_paths.borrow().iter() {
46 FileMatches => found = true,
49 visited_dirs.insert(path.as_vec().to_owned());
52 debug!("filesearch: searching lib path");
53 let tlib_path = make_target_lib_path(self.sysroot,
55 if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
57 FileMatches => found = true,
62 visited_dirs.insert(tlib_path.as_vec().to_owned());
65 let rustpath = rust_path();
66 for path in rustpath.iter() {
67 let tlib_path = make_rustpkg_lib_path(
68 self.sysroot, path, self.triple);
69 debug!("is {} in visited_dirs? {:?}", tlib_path.display(),
70 visited_dirs.contains_equiv(&tlib_path.as_vec().to_owned()));
72 if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
73 visited_dirs.insert(tlib_path.as_vec().to_owned());
74 // Don't keep searching the RUST_PATH if one match turns up --
75 // if we did, we'd get a "multiple matching crates" error
87 pub fn get_lib_path(&self) -> Path {
88 make_target_lib_path(self.sysroot, self.triple)
91 pub fn search(&self, pick: pick) {
92 self.for_each_lib_search_path(|lib_search_path| {
93 debug!("searching {}", lib_search_path.display());
94 match fs::readdir(lib_search_path) {
96 let mut rslt = FileDoesntMatch;
97 fn is_rlib(p: & &Path) -> bool {
98 p.extension_str() == Some("rlib")
100 // Reading metadata out of rlibs is faster, and if we find both
101 // an rlib and a dylib we only read one of the files of
102 // metadata, so in the name of speed, bring all rlib files to
103 // the front of the search list.
104 let files1 = files.iter().filter(|p| is_rlib(p));
105 let files2 = files.iter().filter(|p| !is_rlib(p));
106 for path in files1.chain(files2) {
107 debug!("testing {}", path.display());
108 let maybe_picked = pick(path);
111 debug!("picked {}", path.display());
115 debug!("rejected {}", path.display());
121 Err(..) => FileDoesntMatch,
126 pub fn new(sysroot: &'a Path,
128 addl_lib_search_paths: &'a RefCell<HashSet<Path>>) -> FileSearch<'a> {
129 debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
132 addl_lib_search_paths: addl_lib_search_paths,
137 pub fn add_dylib_search_paths(&self) {
138 self.for_each_lib_search_path(|lib_search_path| {
139 DynamicLibrary::prepend_search_path(lib_search_path);
145 pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> Path {
146 let mut p = Path::new(find_libdir(sysroot));
147 assert!(p.is_relative());
148 p.push(rustlibdir());
149 p.push(target_triple);
154 fn make_target_lib_path(sysroot: &Path,
155 target_triple: &str) -> Path {
156 sysroot.join(&relative_target_lib_path(sysroot, target_triple))
159 fn make_rustpkg_lib_path(sysroot: &Path,
161 triple: &str) -> Path {
162 let mut p = dir.join(find_libdir(sysroot));
167 pub fn get_or_default_sysroot() -> Path {
168 // Follow symlinks. If the resolved path is relative, make it absolute.
169 fn canonicalize(path: Option<Path>) -> Option<Path> {
171 match myfs::realpath(&path) {
172 Ok(canon) => Some(canon),
173 Err(e) => fail!("failed to get realpath: {}", e),
177 match canonicalize(os::self_exe_name()) {
178 Some(mut p) => { p.pop(); p.pop(); p }
179 None => fail!("can't determine value for sysroot")
184 static PATH_ENTRY_SEPARATOR: &'static str = ";";
186 static PATH_ENTRY_SEPARATOR: &'static str = ":";
188 /// Returns RUST_PATH as a string, without default paths added
189 pub fn get_rust_path() -> Option<String> {
190 os::getenv("RUST_PATH").map(|x| x.to_strbuf())
193 /// Returns the value of RUST_PATH, as a list
194 /// of Paths. Includes default entries for, if they exist:
196 /// DIR/.rust for any DIR that's the current working directory
197 /// or an ancestor of it
198 pub fn rust_path() -> Vec<Path> {
199 let mut env_rust_path: Vec<Path> = match get_rust_path() {
201 let env_path_components =
202 env_path.as_slice().split_str(PATH_ENTRY_SEPARATOR);
203 env_path_components.map(|s| Path::new(s)).collect()
207 let mut cwd = os::getcwd();
208 // now add in default entries
209 let cwd_dot_rust = cwd.join(".rust");
210 if !env_rust_path.contains(&cwd_dot_rust) {
211 env_rust_path.push(cwd_dot_rust);
213 if !env_rust_path.contains(&cwd) {
214 env_rust_path.push(cwd.clone());
217 if { let f = cwd.filename(); f.is_none() || f.unwrap() == bytes!("..") } {
220 cwd.set_filename(".rust");
221 if !env_rust_path.contains(&cwd) && cwd.exists() {
222 env_rust_path.push(cwd.clone());
226 let h = os::homedir();
228 let p = h.join(".rust");
229 if !env_rust_path.contains(&p) && p.exists() {
230 env_rust_path.push(p);
236 // The name of the directory rustc expects libraries to be located.
237 // On Unix should be "lib", on windows "bin"
239 fn find_libdir(sysroot: &Path) -> String {
240 // FIXME: This is a quick hack to make the rustc binary able to locate
241 // Rust libraries in Linux environments where libraries might be installed
242 // to lib64/lib32. This would be more foolproof by basing the sysroot off
243 // of the directory where librustc is located, rather than where the rustc
246 if sysroot.join(primary_libdir_name()).join(rustlibdir()).exists() {
247 return primary_libdir_name();
249 return secondary_libdir_name();
252 #[cfg(target_word_size = "64")]
253 fn primary_libdir_name() -> String {
257 #[cfg(target_word_size = "32")]
258 fn primary_libdir_name() -> String {
262 fn secondary_libdir_name() -> String {
268 fn find_libdir(_sysroot: &Path) -> String {
272 // The name of rustc's own place to organize libraries.
273 // Used to be "rustc", now the default is "rustlib"
274 pub fn rustlibdir() -> String {
275 "rustlib".to_strbuf()