2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
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-03-25 15:21:04 -05:00
|
|
|
pub type pick<'self, T> = &'self fn(path: &Path) -> Option<T>;
|
2013-01-29 18:51:16 -06:00
|
|
|
|
|
|
|
pub fn pick_file(file: Path, path: &Path) -> Option<Path> {
|
2012-08-20 14:23:37 -05:00
|
|
|
if path.file_path() == file { option::Some(copy *path) }
|
|
|
|
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;
|
|
|
|
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool);
|
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-04-17 11:15:37 -05:00
|
|
|
addl_lib_search_paths: ~[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-02-19 01:40:42 -06:00
|
|
|
addl_lib_search_paths: ~[Path],
|
|
|
|
target_triple: ~str
|
|
|
|
}
|
|
|
|
impl FileSearch for FileSearchImpl {
|
2013-05-03 18:47:53 -05:00
|
|
|
fn sysroot(&self) -> @Path { self.sysroot }
|
|
|
|
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool) {
|
|
|
|
debug!("filesearch: searching additional lib search paths");
|
2013-05-03 19:24:44 -05:00
|
|
|
// a little weird
|
|
|
|
self.addl_lib_search_paths.each(f);
|
2013-05-03 18:47:53 -05:00
|
|
|
|
|
|
|
debug!("filesearch: searching target lib path");
|
|
|
|
if !f(&make_target_lib_path(self.sysroot,
|
|
|
|
self.target_triple)) {
|
|
|
|
return;
|
2012-06-28 17:00:03 -05:00
|
|
|
}
|
2013-05-03 18:47:53 -05:00
|
|
|
debug!("filesearch: searching rustpkg lib path nearest");
|
|
|
|
if match get_rustpkg_lib_path_nearest() {
|
|
|
|
result::Ok(ref p) => f(p),
|
|
|
|
result::Err(_) => true
|
|
|
|
} {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug!("filesearch: searching rustpkg lib path");
|
|
|
|
match get_rustpkg_lib_path() {
|
|
|
|
result::Ok(ref p) => f(p),
|
|
|
|
result::Err(_) => true
|
2013-05-03 19:24:44 -05:00
|
|
|
};
|
2011-10-03 14:46:22 -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,
|
|
|
|
target_triple: str::from_slice(target_triple)
|
2013-02-26 20:42:00 -06:00
|
|
|
} as @FileSearch
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn search<T:Copy>(filesearch: @FileSearch, pick: pick<T>) -> Option<T> {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut rslt = None;
|
2013-05-03 18:47:53 -05:00
|
|
|
for 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());
|
2012-09-19 18:55:01 -05:00
|
|
|
for os::list_dir_path(lib_search_path).each |path| {
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("testing %s", path.to_str());
|
2012-09-19 18:55:01 -05:00
|
|
|
let maybe_picked = pick(*path);
|
2012-09-21 21:37:57 -05:00
|
|
|
if maybe_picked.is_some() {
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("picked %s", path.to_str());
|
2012-04-06 13:01:43 -05:00
|
|
|
rslt = maybe_picked;
|
|
|
|
break;
|
2011-10-03 15:54:13 -05:00
|
|
|
} else {
|
2012-08-24 17:28:43 -05:00
|
|
|
debug!("rejected %s", path.to_str());
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
if rslt.is_some() { break; }
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return rslt;
|
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",
|
|
|
|
str::from_slice(target_triple),
|
|
|
|
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-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-02-11 21:26:38 -06: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-01-14 04:55:47 -06:00
|
|
|
pub fn get_rustpkg_sysroot() -> Result<Path, ~str> {
|
|
|
|
result::Ok(get_or_default_sysroot().push_many([libdir(), ~"rustpkg"]))
|
2012-02-07 02:15:39 -06:00
|
|
|
}
|
|
|
|
|
2013-01-14 04:55:47 -06:00
|
|
|
pub fn get_rustpkg_root() -> Result<Path, ~str> {
|
|
|
|
match os::getenv(~"RUSTPKG_ROOT") {
|
2012-12-04 12:50:00 -06:00
|
|
|
Some(ref _p) => result::Ok(Path((*_p))),
|
2012-08-20 14:23:37 -05:00
|
|
|
None => match os::homedir() {
|
2013-01-14 04:55:47 -06:00
|
|
|
Some(ref _q) => result::Ok((*_q).push(".rustpkg")),
|
|
|
|
None => result::Err(~"no RUSTPKG_ROOT or home directory")
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:55:47 -06:00
|
|
|
pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
|
|
|
|
do result::chain(get_rustpkg_root()) |p| {
|
2012-02-05 03:30:03 -06:00
|
|
|
let cwd = os::getcwd();
|
2013-01-14 04:55:47 -06:00
|
|
|
let cwd_rustpkg = cwd.push(".rustpkg");
|
|
|
|
let rustpkg_is_non_root_file =
|
|
|
|
!os::path_is_dir(&cwd_rustpkg) && cwd_rustpkg != p;
|
|
|
|
let mut par_rustpkg = cwd.pop().push(".rustpkg");
|
|
|
|
let mut rslt = result::Ok(cwd_rustpkg);
|
|
|
|
|
|
|
|
if rustpkg_is_non_root_file {
|
|
|
|
while par_rustpkg != p {
|
|
|
|
if os::path_is_dir(&par_rustpkg) {
|
|
|
|
rslt = result::Ok(par_rustpkg);
|
2012-03-26 05:39:20 -05:00
|
|
|
break;
|
|
|
|
}
|
2013-01-14 04:55:47 -06:00
|
|
|
if par_rustpkg.components.len() == 1 {
|
|
|
|
// We just checked /.rustpkg, stop now.
|
2012-08-24 17:28:43 -05:00
|
|
|
break;
|
|
|
|
}
|
2013-01-14 04:55:47 -06:00
|
|
|
par_rustpkg = par_rustpkg.pop().pop().push(".rustpkg");
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-26 05:39:20 -05:00
|
|
|
rslt
|
2012-02-05 03:30:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:55:47 -06:00
|
|
|
fn get_rustpkg_lib_path() -> Result<Path, ~str> {
|
|
|
|
do result::chain(get_rustpkg_root()) |p| {
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Ok(p.push(libdir()))
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
|
2013-01-14 04:55:47 -06:00
|
|
|
fn get_rustpkg_lib_path_nearest() -> Result<Path, ~str> {
|
|
|
|
do result::chain(get_rustpkg_root_nearest()) |p| {
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Ok(p.push(libdir()))
|
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-01-29 18:51:16 -06:00
|
|
|
pub fn libdir() -> ~str {
|
2012-08-22 19:24:52 -05:00
|
|
|
let libdir = env!("CFG_LIBDIR");
|
2012-01-10 19:45:03 -06:00
|
|
|
if str::is_empty(libdir) {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"rustc compiled without CFG_LIBDIR environment variable");
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
libdir
|
|
|
|
}
|