2013-08-06 23:50:23 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-12-21 16:28:04 -06:00
|
|
|
use std::cell::RefCell;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
|
|
|
use std::os;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
|
|
|
use std::io::fs;
|
2013-07-31 15:47:32 -05:00
|
|
|
use std::hashmap::HashSet;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-08-23 13:51:45 -05:00
|
|
|
pub enum FileMatch { FileMatches, FileDoesntMatch }
|
|
|
|
|
2011-10-03 15:54:13 -05:00
|
|
|
// A module for searching for libraries
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2658): I'm not happy how this module turned out. Should
|
|
|
|
// probably just be folded into cstore.
|
2011-10-03 15:54:13 -05:00
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
/// Functions with type `pick` take a parent directory as well as
|
|
|
|
/// a file found in that directory.
|
2013-12-10 01:16:18 -06:00
|
|
|
pub type pick<'a> = 'a |path: &Path| -> FileMatch;
|
2013-01-29 18:51:16 -06:00
|
|
|
|
|
|
|
pub fn pick_file(file: Path, path: &Path) -> Option<Path> {
|
2013-10-10 00:05:14 -05:00
|
|
|
if path.filename() == Some(file.as_vec()) {
|
2013-09-26 19:21:59 -05:00
|
|
|
Some(path.clone())
|
2013-07-02 14:47:32 -05:00
|
|
|
} else {
|
2013-09-26 19:21:59 -05:00
|
|
|
None
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
2011-10-03 16:45:38 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub trait FileSearch {
|
2013-05-03 18:47:53 -05:00
|
|
|
fn sysroot(&self) -> @Path;
|
2013-11-19 15:22:03 -06:00
|
|
|
fn for_each_lib_search_path(&self, f: |&Path| -> FileMatch);
|
2013-02-22 00:41:37 -06:00
|
|
|
fn get_target_lib_path(&self) -> Path;
|
|
|
|
fn get_target_lib_file_path(&self, file: &Path) -> Path;
|
2012-01-13 02:32:05 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
pub fn mk_filesearch(maybe_sysroot: &Option<@Path>,
|
2013-01-29 18:51:16 -06:00
|
|
|
target_triple: &str,
|
2013-12-21 16:28:04 -06:00
|
|
|
addl_lib_search_paths: @RefCell<HashSet<Path>>)
|
2013-03-12 15:00:50 -05:00
|
|
|
-> @FileSearch {
|
2013-02-19 01:40:42 -06:00
|
|
|
struct FileSearchImpl {
|
2013-05-03 18:47:53 -05:00
|
|
|
sysroot: @Path,
|
2013-12-21 16:28:04 -06:00
|
|
|
addl_lib_search_paths: @RefCell<HashSet<Path>>,
|
2013-02-19 01:40:42 -06:00
|
|
|
target_triple: ~str
|
|
|
|
}
|
|
|
|
impl FileSearch for FileSearchImpl {
|
2013-05-03 18:47:53 -05:00
|
|
|
fn sysroot(&self) -> @Path { self.sysroot }
|
2013-11-19 15:22:03 -06:00
|
|
|
|
|
|
|
fn for_each_lib_search_path(&self, f: |&Path| -> FileMatch) {
|
2013-07-31 15:47:32 -05:00
|
|
|
let mut visited_dirs = HashSet::new();
|
2013-08-23 13:51:45 -05:00
|
|
|
let mut found = false;
|
2013-07-31 15:47:32 -05:00
|
|
|
|
2013-12-21 16:28:04 -06:00
|
|
|
let addl_lib_search_paths = self.addl_lib_search_paths.borrow();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("filesearch: searching additional lib search paths [{:?}]",
|
2013-12-21 16:28:04 -06:00
|
|
|
addl_lib_search_paths.get().len());
|
|
|
|
for path in addl_lib_search_paths.get().iter() {
|
2013-08-23 13:51:45 -05:00
|
|
|
match f(path) {
|
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
|
|
|
}
|
2013-09-26 19:21:59 -05:00
|
|
|
visited_dirs.insert(path.as_vec().to_owned());
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-05-03 12:08:08 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("filesearch: searching target lib path");
|
2013-07-31 15:47:32 -05:00
|
|
|
let tlib_path = make_target_lib_path(self.sysroot,
|
|
|
|
self.target_triple);
|
2013-09-26 19:21:59 -05:00
|
|
|
if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
|
2013-08-23 13:51:45 -05:00
|
|
|
match f(&tlib_path) {
|
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
2013-05-03 12:08:08 -05:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-09-26 19:21:59 -05:00
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_owned());
|
2013-07-31 15:47:32 -05:00
|
|
|
// Try RUST_PATH
|
2013-08-23 13:51:45 -05:00
|
|
|
if !found {
|
|
|
|
let rustpath = rust_path();
|
|
|
|
for path in rustpath.iter() {
|
2013-09-12 21:29:21 -05:00
|
|
|
let tlib_path = make_rustpkg_target_lib_path(path, self.target_triple);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is {} in visited_dirs? {:?}", tlib_path.display(),
|
2013-09-26 19:21:59 -05:00
|
|
|
visited_dirs.contains_equiv(&tlib_path.as_vec().to_owned()));
|
2013-08-23 13:51:45 -05:00
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
|
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_owned());
|
2013-08-23 13:51:45 -05:00
|
|
|
// Don't keep searching the RUST_PATH if one match turns up --
|
|
|
|
// if we did, we'd get a "multiple matching crates" error
|
2013-09-12 21:29:21 -05:00
|
|
|
match f(&tlib_path) {
|
2013-08-23 13:51:45 -05:00
|
|
|
FileMatches => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileDoesntMatch => ()
|
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-08-23 13:51:45 -05:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-05-03 12:08:08 -05:00
|
|
|
}
|
2013-02-22 00:41:37 -06:00
|
|
|
fn get_target_lib_path(&self) -> Path {
|
2013-05-03 18:47:53 -05:00
|
|
|
make_target_lib_path(self.sysroot, self.target_triple)
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2013-02-22 00:41:37 -06:00
|
|
|
fn get_target_lib_file_path(&self, file: &Path) -> Path {
|
2013-09-26 19:21:59 -05:00
|
|
|
let mut p = self.get_target_lib_path();
|
2013-10-07 21:16:58 -05:00
|
|
|
p.push(file);
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
let sysroot = get_sysroot(maybe_sysroot);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("using sysroot = {}", sysroot.display());
|
2013-02-26 20:42:00 -06:00
|
|
|
@FileSearchImpl {
|
2013-02-19 01:40:42 -06:00
|
|
|
sysroot: sysroot,
|
|
|
|
addl_lib_search_paths: addl_lib_search_paths,
|
2013-07-23 08:49:17 -05:00
|
|
|
target_triple: target_triple.to_owned()
|
2013-02-26 20:42:00 -06:00
|
|
|
} as @FileSearch
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2013-08-23 13:51:45 -05:00
|
|
|
pub fn search(filesearch: @FileSearch, pick: pick) {
|
2013-11-21 17:42:55 -06:00
|
|
|
filesearch.for_each_lib_search_path(|lib_search_path| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("searching {}", lib_search_path.display());
|
2013-10-31 17:15:30 -05:00
|
|
|
match io::result(|| fs::readdir(lib_search_path)) {
|
2013-10-25 19:04:37 -05:00
|
|
|
Ok(files) => {
|
|
|
|
let mut rslt = FileDoesntMatch;
|
2013-12-16 22:58:21 -06:00
|
|
|
let is_rlib = |p: & &Path| {
|
|
|
|
p.extension_str() == Some("rlib")
|
|
|
|
};
|
|
|
|
// Reading metadata out of rlibs is faster, and if we find both
|
|
|
|
// an rlib and a dylib we only read one of the files of
|
|
|
|
// metadata, so in the name of speed, bring all rlib files to
|
|
|
|
// the front of the search list.
|
|
|
|
let files1 = files.iter().filter(|p| is_rlib(p));
|
|
|
|
let files2 = files.iter().filter(|p| !is_rlib(p));
|
|
|
|
for path in files1.chain(files2) {
|
2013-10-25 19:04:37 -05:00
|
|
|
debug!("testing {}", path.display());
|
|
|
|
let maybe_picked = pick(path);
|
|
|
|
match maybe_picked {
|
|
|
|
FileMatches => {
|
|
|
|
debug!("picked {}", path.display());
|
|
|
|
rslt = FileMatches;
|
|
|
|
}
|
|
|
|
FileDoesntMatch => {
|
|
|
|
debug!("rejected {}", path.display());
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
rslt
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Err(..) => FileDoesntMatch,
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn relative_target_lib_path(target_triple: &str) -> Path {
|
2013-09-26 19:21:59 -05:00
|
|
|
let dir = libdir();
|
2013-12-03 21:15:12 -06:00
|
|
|
let mut p = Path::new(dir.as_slice());
|
2013-09-26 19:21:59 -05:00
|
|
|
assert!(p.is_relative());
|
2013-10-05 21:49:32 -05:00
|
|
|
p.push("rustc");
|
|
|
|
p.push(target_triple);
|
|
|
|
p.push(dir);
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
2012-08-24 17:28:43 -05:00
|
|
|
fn make_target_lib_path(sysroot: &Path,
|
|
|
|
target_triple: &str) -> Path {
|
2013-10-07 21:16:58 -05:00
|
|
|
sysroot.join(&relative_target_lib_path(target_triple))
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2013-09-12 21:29:21 -05:00
|
|
|
fn make_rustpkg_target_lib_path(dir: &Path,
|
|
|
|
target_triple: &str) -> Path {
|
2013-10-05 21:49:32 -05:00
|
|
|
let mut p = dir.join(libdir());
|
|
|
|
p.push(target_triple);
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2013-09-12 21:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub fn get_or_default_sysroot() -> Path {
|
2012-08-06 14:34:08 -05:00
|
|
|
match os::self_exe_path() {
|
2013-09-26 19:21:59 -05:00
|
|
|
option::Some(p) => { let mut p = p; p.pop(); p }
|
2013-10-21 15:08:31 -05:00
|
|
|
option::None => fail!("can't determine value for sysroot")
|
2011-10-04 16:14:36 -05:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
fn get_sysroot(maybe_sysroot: &Option<@Path>) -> @Path {
|
2013-04-17 11:15:37 -05:00
|
|
|
match *maybe_sysroot {
|
2013-05-03 18:47:53 -05:00
|
|
|
option::Some(sr) => sr,
|
|
|
|
option::None => @get_or_default_sysroot()
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2011-11-10 10:41:42 -06:00
|
|
|
}
|
2012-01-05 18:03:28 -06:00
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
static PATH_ENTRY_SEPARATOR: &'static str = ";";
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
static PATH_ENTRY_SEPARATOR: &'static str = ":";
|
|
|
|
|
|
|
|
/// Returns RUST_PATH as a string, without default paths added
|
|
|
|
pub fn get_rust_path() -> Option<~str> {
|
|
|
|
os::getenv("RUST_PATH")
|
2012-02-07 02:15:39 -06:00
|
|
|
}
|
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
/// Returns the value of RUST_PATH, as a list
|
|
|
|
/// of Paths. Includes default entries for, if they exist:
|
|
|
|
/// $HOME/.rust
|
|
|
|
/// DIR/.rust for any DIR that's the current working directory
|
|
|
|
/// or an ancestor of it
|
|
|
|
pub fn rust_path() -> ~[Path] {
|
|
|
|
let mut env_rust_path: ~[Path] = match get_rust_path() {
|
|
|
|
Some(env_path) => {
|
|
|
|
let env_path_components: ~[&str] =
|
2013-11-23 04:18:51 -06:00
|
|
|
env_path.split_str(PATH_ENTRY_SEPARATOR).collect();
|
2013-12-03 21:15:12 -06:00
|
|
|
env_path_components.map(|&s| Path::new(s))
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
None => ~[]
|
|
|
|
};
|
2013-10-07 21:16:58 -05:00
|
|
|
let mut cwd = os::getcwd();
|
2013-07-31 15:47:32 -05:00
|
|
|
// now add in default entries
|
2013-10-05 21:49:32 -05:00
|
|
|
let cwd_dot_rust = cwd.join(".rust");
|
2013-07-31 15:47:32 -05:00
|
|
|
if !env_rust_path.contains(&cwd_dot_rust) {
|
|
|
|
env_rust_path.push(cwd_dot_rust);
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
if !env_rust_path.contains(&cwd) {
|
|
|
|
env_rust_path.push(cwd.clone());
|
|
|
|
}
|
2013-10-07 21:16:58 -05:00
|
|
|
loop {
|
2013-10-10 00:05:14 -05:00
|
|
|
if { let f = cwd.filename(); f.is_none() || f.unwrap() == bytes!("..") } {
|
|
|
|
break
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
2013-10-10 00:05:14 -05:00
|
|
|
cwd.set_filename(".rust");
|
2013-10-25 19:04:37 -05:00
|
|
|
if !env_rust_path.contains(&cwd) && cwd.exists() {
|
2013-10-07 21:16:58 -05:00
|
|
|
env_rust_path.push(cwd.clone());
|
|
|
|
}
|
|
|
|
cwd.pop();
|
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
let h = os::homedir();
|
|
|
|
for h in h.iter() {
|
2013-10-07 21:16:58 -05:00
|
|
|
let p = h.join(".rust");
|
2013-10-25 19:04:37 -05:00
|
|
|
if !env_rust_path.contains(&p) && p.exists() {
|
2013-10-07 21:16:58 -05:00
|
|
|
env_rust_path.push(p);
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
env_rust_path
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The name of the directory rustc expects libraries to be located.
|
|
|
|
// On Unix should be "lib", on windows "bin"
|
2013-08-06 23:50:23 -05:00
|
|
|
pub fn libdir() -> ~str {
|
|
|
|
(env!("CFG_LIBDIR")).to_owned()
|
|
|
|
}
|