2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2014-03-21 20:05:05 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-02-10 08:36:31 -06:00
|
|
|
|
2013-12-21 16:28:04 -06:00
|
|
|
use std::cell::RefCell;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::os;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::fs;
|
2014-04-29 13:38:51 -05:00
|
|
|
use std::unstable::dynamic_lib::DynamicLibrary;
|
2014-02-19 21:29:58 -06:00
|
|
|
use collections::HashSet;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2014-04-08 12:15:46 -05:00
|
|
|
use myfs = util::fs;
|
|
|
|
|
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.
|
2014-04-07 15:30:48 -05:00
|
|
|
pub type pick<'a> = |path: &Path|: 'a -> FileMatch;
|
2013-01-29 18:51:16 -06:00
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
pub struct FileSearch<'a> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub sysroot: &'a Path,
|
|
|
|
pub addl_lib_search_paths: &'a RefCell<HashSet<Path>>,
|
2014-04-17 10:52:25 -05:00
|
|
|
pub triple: &'a str,
|
2012-01-13 02:32:05 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
impl<'a> FileSearch<'a> {
|
2014-01-13 10:31:57 -06:00
|
|
|
pub fn for_each_lib_search_path(&self, f: |&Path| -> FileMatch) {
|
|
|
|
let mut visited_dirs = HashSet::new();
|
|
|
|
let mut found = false;
|
|
|
|
|
|
|
|
debug!("filesearch: searching additional lib search paths [{:?}]",
|
2014-03-20 21:49:20 -05:00
|
|
|
self.addl_lib_search_paths.borrow().len());
|
|
|
|
for path in self.addl_lib_search_paths.borrow().iter() {
|
2014-01-13 10:31:57 -06:00
|
|
|
match f(path) {
|
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
visited_dirs.insert(path.as_vec().to_owned());
|
|
|
|
}
|
2013-05-03 12:08:08 -05:00
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
debug!("filesearch: searching lib path");
|
2014-01-13 10:31:57 -06:00
|
|
|
let tlib_path = make_target_lib_path(self.sysroot,
|
2014-04-17 10:52:25 -05:00
|
|
|
self.triple);
|
2014-01-13 10:31:57 -06:00
|
|
|
if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
|
|
|
|
match f(&tlib_path) {
|
|
|
|
FileMatches => found = true,
|
|
|
|
FileDoesntMatch => ()
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2014-04-17 10:52:25 -05:00
|
|
|
|
2014-01-13 10:31:57 -06:00
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_owned());
|
|
|
|
// Try RUST_PATH
|
|
|
|
if !found {
|
|
|
|
let rustpath = rust_path();
|
|
|
|
for path in rustpath.iter() {
|
2014-04-17 10:52:25 -05:00
|
|
|
let tlib_path = make_rustpkg_lib_path(
|
|
|
|
self.sysroot, path, self.triple);
|
2014-01-13 10:31:57 -06:00
|
|
|
debug!("is {} in visited_dirs? {:?}", tlib_path.display(),
|
|
|
|
visited_dirs.contains_equiv(&tlib_path.as_vec().to_owned()));
|
|
|
|
|
|
|
|
if !visited_dirs.contains_equiv(&tlib_path.as_vec()) {
|
|
|
|
visited_dirs.insert(tlib_path.as_vec().to_owned());
|
|
|
|
// Don't keep searching the RUST_PATH if one match turns up --
|
|
|
|
// if we did, we'd get a "multiple matching crates" error
|
|
|
|
match f(&tlib_path) {
|
|
|
|
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
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
pub fn get_lib_path(&self) -> Path {
|
|
|
|
make_target_lib_path(self.sysroot, self.triple)
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2014-01-13 10:31:57 -06:00
|
|
|
pub fn search(&self, pick: pick) {
|
|
|
|
self.for_each_lib_search_path(|lib_search_path| {
|
|
|
|
debug!("searching {}", lib_search_path.display());
|
2014-01-29 20:42:19 -06:00
|
|
|
match fs::readdir(lib_search_path) {
|
2014-01-13 10:31:57 -06:00
|
|
|
Ok(files) => {
|
|
|
|
let mut rslt = FileDoesntMatch;
|
2014-04-22 01:25:18 -05:00
|
|
|
fn is_rlib(p: & &Path) -> bool {
|
2014-01-13 10:31:57 -06:00
|
|
|
p.extension_str() == Some("rlib")
|
2014-04-22 01:25:18 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
// 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) {
|
|
|
|
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-10-25 19:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
rslt
|
2013-07-31 15:47:32 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
Err(..) => FileDoesntMatch,
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-09 07:24:58 -05:00
|
|
|
pub fn new(sysroot: &'a Path,
|
2014-04-17 10:52:25 -05:00
|
|
|
triple: &'a str,
|
2014-03-09 07:24:58 -05:00
|
|
|
addl_lib_search_paths: &'a RefCell<HashSet<Path>>) -> FileSearch<'a> {
|
2014-04-17 10:52:25 -05:00
|
|
|
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
|
2014-03-09 07:24:58 -05:00
|
|
|
FileSearch {
|
2014-01-13 10:31:57 -06:00
|
|
|
sysroot: sysroot,
|
|
|
|
addl_lib_search_paths: addl_lib_search_paths,
|
2014-04-17 10:52:25 -05:00
|
|
|
triple: triple,
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2014-01-13 10:31:57 -06:00
|
|
|
}
|
2014-04-29 13:38:51 -05:00
|
|
|
|
|
|
|
pub fn add_dylib_search_paths(&self) {
|
|
|
|
self.for_each_lib_search_path(|lib_search_path| {
|
|
|
|
DynamicLibrary::add_search_path(lib_search_path);
|
|
|
|
FileDoesntMatch
|
|
|
|
})
|
|
|
|
}
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
|
2014-03-25 23:25:43 -05:00
|
|
|
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> Path {
|
|
|
|
let mut p = Path::new(find_libdir(sysroot));
|
2013-09-26 19:21:59 -05:00
|
|
|
assert!(p.is_relative());
|
2014-01-04 19:55:20 -06:00
|
|
|
p.push(rustlibdir());
|
2013-10-05 21:49:32 -05:00
|
|
|
p.push(target_triple);
|
2014-01-07 10:51:15 -06:00
|
|
|
p.push("lib");
|
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 {
|
2014-03-25 23:25:43 -05:00
|
|
|
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:52:25 -05:00
|
|
|
fn make_rustpkg_lib_path(sysroot: &Path,
|
|
|
|
dir: &Path,
|
|
|
|
triple: &str) -> Path {
|
2014-03-25 23:25:43 -05:00
|
|
|
let mut p = dir.join(find_libdir(sysroot));
|
2014-04-17 10:52:25 -05:00
|
|
|
p.push(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 {
|
2014-01-22 16:45:52 -06:00
|
|
|
// Follow symlinks. If the resolved path is relative, make it absolute.
|
|
|
|
fn canonicalize(path: Option<Path>) -> Option<Path> {
|
2014-04-08 12:15:46 -05:00
|
|
|
path.and_then(|path|
|
|
|
|
match myfs::realpath(&path) {
|
|
|
|
Ok(canon) => Some(canon),
|
|
|
|
Err(e) => fail!("failed to get realpath: {}", e),
|
2014-01-22 16:45:52 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
match canonicalize(os::self_exe_name()) {
|
2014-03-09 07:24:58 -05:00
|
|
|
Some(mut p) => { p.pop(); p.pop(); p }
|
|
|
|
None => fail!("can't determine value for 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
|
2014-03-04 12:02:49 -06:00
|
|
|
pub fn rust_path() -> Vec<Path> {
|
|
|
|
let mut env_rust_path: Vec<Path> = match get_rust_path() {
|
2013-07-31 15:47:32 -05:00
|
|
|
Some(env_path) => {
|
2014-03-28 14:42:34 -05:00
|
|
|
let env_path_components =
|
|
|
|
env_path.split_str(PATH_ENTRY_SEPARATOR);
|
|
|
|
env_path_components.map(|s| Path::new(s)).collect()
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2014-03-04 12:02:49 -06:00
|
|
|
None => Vec::new()
|
2013-07-31 15:47:32 -05:00
|
|
|
};
|
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"
|
2014-03-25 21:17:02 -05:00
|
|
|
#[cfg(unix)]
|
2014-03-25 23:25:43 -05:00
|
|
|
fn find_libdir(sysroot: &Path) -> ~str {
|
|
|
|
// FIXME: This is a quick hack to make the rustc binary able to locate
|
|
|
|
// Rust libraries in Linux environments where libraries might be installed
|
|
|
|
// to lib64/lib32. This would be more foolproof by basing the sysroot off
|
|
|
|
// of the directory where librustc is located, rather than where the rustc
|
|
|
|
// binary is.
|
|
|
|
|
2014-03-26 21:21:23 -05:00
|
|
|
if sysroot.join(primary_libdir_name()).join(rustlibdir()).exists() {
|
2014-03-25 23:25:43 -05:00
|
|
|
return primary_libdir_name();
|
|
|
|
} else {
|
|
|
|
return secondary_libdir_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_word_size = "64")]
|
2014-04-15 20:17:48 -05:00
|
|
|
fn primary_libdir_name() -> ~str { "lib64".to_owned() }
|
2014-03-25 23:25:43 -05:00
|
|
|
|
|
|
|
#[cfg(target_word_size = "32")]
|
2014-04-15 20:17:48 -05:00
|
|
|
fn primary_libdir_name() -> ~str { "lib32".to_owned() }
|
2014-03-25 23:25:43 -05:00
|
|
|
|
2014-04-15 20:17:48 -05:00
|
|
|
fn secondary_libdir_name() -> ~str { "lib".to_owned() }
|
2014-03-25 21:17:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-03-25 23:25:43 -05:00
|
|
|
fn find_libdir(_sysroot: &Path) -> ~str {
|
2014-04-15 20:17:48 -05:00
|
|
|
"bin".to_owned()
|
2013-08-06 23:50:23 -05:00
|
|
|
}
|
2014-01-04 19:55:20 -06:00
|
|
|
|
|
|
|
// The name of rustc's own place to organize libraries.
|
|
|
|
// Used to be "rustc", now the default is "rustlib"
|
|
|
|
pub fn rustlibdir() -> ~str {
|
2014-04-15 20:17:48 -05:00
|
|
|
"rustlib".to_owned()
|
2014-01-04 19:55:20 -06:00
|
|
|
}
|