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-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
|
|
|
use std::os;
|
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-08-23 13:51:45 -05:00
|
|
|
pub type pick<'self> = &'self fn(path: &Path) -> FileMatch;
|
2013-01-29 18:51:16 -06:00
|
|
|
|
|
|
|
pub fn pick_file(file: Path, path: &Path) -> Option<Path> {
|
2013-07-02 14:47:32 -05:00
|
|
|
if path.file_path() == file {
|
|
|
|
option::Some((*path).clone())
|
|
|
|
} else {
|
|
|
|
option::None
|
|
|
|
}
|
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-08-23 13:51:45 -05:00
|
|
|
fn for_each_lib_search_path(&self, f: &fn(&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-05-27 19:45:16 -05:00
|
|
|
addl_lib_search_paths: @mut ~[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-05-27 19:45:16 -05:00
|
|
|
addl_lib_search_paths: @mut ~[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-08-23 13:51:45 -05:00
|
|
|
fn for_each_lib_search_path(&self, f: &fn(&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-05-27 19:45:16 -05:00
|
|
|
debug!("filesearch: searching additional lib search paths [%?]",
|
|
|
|
self.addl_lib_search_paths.len());
|
2013-07-31 15:47:32 -05:00
|
|
|
for path in self.addl_lib_search_paths.iter() {
|
2013-08-23 13:51:45 -05:00
|
|
|
match f(path) {
|
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
visited_dirs.insert(path.to_str());
|
|
|
|
}
|
2013-05-03 12:08:08 -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);
|
|
|
|
if !visited_dirs.contains(&tlib_path.to_str()) {
|
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
|
|
|
}
|
|
|
|
visited_dirs.insert(tlib_path.to_str());
|
|
|
|
// 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);
|
|
|
|
debug!("is %s in visited_dirs? %?", tlib_path.to_str(),
|
|
|
|
visited_dirs.contains(&tlib_path.to_str()));
|
2013-08-23 13:51:45 -05:00
|
|
|
|
2013-09-12 21:29:21 -05:00
|
|
|
if !visited_dirs.contains(&tlib_path.to_str()) {
|
|
|
|
visited_dirs.insert(tlib_path.to_str());
|
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 {
|
2012-08-24 17:28:43 -05:00
|
|
|
self.get_target_lib_path().push_rel(file)
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
let sysroot = get_sysroot(maybe_sysroot);
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("using sysroot = %s", sysroot.to_str());
|
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-08-02 01:17:20 -05:00
|
|
|
do filesearch.for_each_lib_search_path() |lib_search_path| {
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("searching %s", lib_search_path.to_str());
|
2013-06-21 07:29:53 -05:00
|
|
|
let r = os::list_dir_path(lib_search_path);
|
2013-08-23 13:51:45 -05:00
|
|
|
let mut rslt = FileDoesntMatch;
|
2013-08-03 11:45:23 -05:00
|
|
|
for path in r.iter() {
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("testing %s", path.to_str());
|
2013-07-21 12:33:29 -05:00
|
|
|
let maybe_picked = pick(path);
|
2013-07-31 15:47:32 -05:00
|
|
|
match maybe_picked {
|
2013-08-23 13:51:45 -05:00
|
|
|
FileMatches => {
|
2013-07-31 15:47:32 -05:00
|
|
|
debug!("picked %s", path.to_str());
|
2013-08-23 13:51:45 -05:00
|
|
|
rslt = FileMatches;
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2013-08-23 13:51:45 -05:00
|
|
|
FileDoesntMatch => {
|
2013-07-31 15:47:32 -05:00
|
|
|
debug!("rejected %s", path.to_str());
|
|
|
|
}
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-23 13:51:45 -05:00
|
|
|
rslt
|
2013-08-02 01:17:20 -05: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 {
|
2012-08-24 17:28:43 -05:00
|
|
|
Path(libdir()).push_many([~"rustc",
|
2013-07-23 08:49:17 -05:00
|
|
|
target_triple.to_owned(),
|
2012-08-24 17:28:43 -05:00
|
|
|
libdir()])
|
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 {
|
|
|
|
sysroot.push_rel(&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 {
|
|
|
|
dir.push_rel(&Path(libdir()).push(target_triple.to_owned()))
|
|
|
|
}
|
|
|
|
|
2013-01-04 18:01:26 -06:00
|
|
|
fn get_or_default_sysroot() -> Path {
|
2012-08-06 14:34:08 -05:00
|
|
|
match os::self_exe_path() {
|
2012-12-04 12:50:00 -06:00
|
|
|
option::Some(ref p) => (*p).pop(),
|
2013-05-05 17:18:51 -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] =
|
|
|
|
env_path.split_str_iter(PATH_ENTRY_SEPARATOR).collect();
|
|
|
|
env_path_components.map(|&s| Path(s))
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
None => ~[]
|
|
|
|
};
|
|
|
|
let cwd = os::getcwd();
|
|
|
|
// now add in default entries
|
|
|
|
let cwd_dot_rust = cwd.push(".rust");
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
do cwd.each_parent() |p| {
|
|
|
|
if !env_rust_path.contains(&p.push(".rust")) {
|
|
|
|
push_if_exists(&mut env_rust_path, p);
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
|
|
|
}
|
2013-07-31 15:47:32 -05:00
|
|
|
let h = os::homedir();
|
|
|
|
for h in h.iter() {
|
|
|
|
if !env_rust_path.contains(&h.push(".rust")) {
|
|
|
|
push_if_exists(&mut env_rust_path, h);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-07-31 15:47:32 -05:00
|
|
|
|
|
|
|
/// Adds p/.rust into vec, only if it exists
|
|
|
|
fn push_if_exists(vec: &mut ~[Path], p: &Path) {
|
|
|
|
let maybe_dir = p.push(".rust");
|
|
|
|
if os::path_exists(&maybe_dir) {
|
|
|
|
vec.push(maybe_dir);
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|