rust/src/librustc/metadata/filesearch.rs

188 lines
6.2 KiB
Rust
Raw Normal View History

// 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.
use core::prelude::*;
// A module for searching for libraries
// FIXME (#2658): I'm not happy how this module turned out. Should
// probably just be folded into cstore.
pub type pick<'self, T> = &'self fn(path: &Path) -> Option<T>;
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 }
}
pub trait FileSearch {
fn sysroot(&self) -> @Path;
2013-05-03 12:08:08 -05:00
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool) -> 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;
}
pub fn mk_filesearch(maybe_sysroot: &Option<@Path>,
target_triple: &str,
addl_lib_search_paths: ~[Path])
-> @FileSearch {
struct FileSearchImpl {
sysroot: @Path,
addl_lib_search_paths: ~[Path],
target_triple: ~str
}
impl FileSearch for FileSearchImpl {
fn sysroot(&self) -> @Path { self.sysroot }
2013-05-03 12:08:08 -05:00
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool) -> bool {
debug!("filesearch: searching additional lib search paths");
// a little weird
self.addl_lib_search_paths.each(f);
debug!("filesearch: searching target lib path");
if !f(&make_target_lib_path(self.sysroot,
self.target_triple)) {
return false;
}
debug!("filesearch: searching rustpkg lib path nearest");
if match get_rustpkg_lib_path_nearest() {
result::Ok(ref p) => f(p),
result::Err(_) => true
} {
return true;
}
debug!("filesearch: searching rustpkg lib path");
match get_rustpkg_lib_path() {
result::Ok(ref p) => f(p),
result::Err(_) => true
}
}
2013-02-22 00:41:37 -06:00
fn get_target_lib_path(&self) -> Path {
make_target_lib_path(self.sysroot, self.target_triple)
}
2013-02-22 00:41:37 -06:00
fn get_target_lib_file_path(&self, file: &Path) -> Path {
self.get_target_lib_path().push_rel(file)
}
}
let sysroot = get_sysroot(maybe_sysroot);
debug!("using sysroot = %s", sysroot.to_str());
@FileSearchImpl {
sysroot: sysroot,
addl_lib_search_paths: addl_lib_search_paths,
target_triple: str::to_owned(target_triple)
} as @FileSearch
}
pub fn search<T:Copy>(filesearch: @FileSearch, pick: pick<T>) -> Option<T> {
2012-08-20 14:23:37 -05:00
let mut rslt = None;
for filesearch.for_each_lib_search_path() |lib_search_path| {
debug!("searching %s", lib_search_path.to_str());
for os::list_dir_path(lib_search_path).each |path| {
debug!("testing %s", path.to_str());
let maybe_picked = pick(*path);
2012-09-21 21:37:57 -05:00
if maybe_picked.is_some() {
debug!("picked %s", path.to_str());
rslt = maybe_picked;
break;
} else {
debug!("rejected %s", path.to_str());
}
}
2012-09-21 21:37:57 -05:00
if rslt.is_some() { break; }
}
2012-08-01 19:30:05 -05:00
return rslt;
}
pub fn relative_target_lib_path(target_triple: &str) -> Path {
Path(libdir()).push_many([~"rustc",
str::to_owned(target_triple),
libdir()])
2011-10-04 17:23:32 -05:00
}
fn make_target_lib_path(sysroot: &Path,
target_triple: &str) -> Path {
sysroot.push_rel(&relative_target_lib_path(target_triple))
}
fn get_or_default_sysroot() -> Path {
2012-08-06 14:34:08 -05:00
match os::self_exe_path() {
option::Some(ref p) => (*p).pop(),
option::None => fail!("can't determine value for sysroot")
}
}
fn get_sysroot(maybe_sysroot: &Option<@Path>) -> @Path {
match *maybe_sysroot {
option::Some(sr) => sr,
option::None => @get_or_default_sysroot()
}
2011-11-10 10:41:42 -06:00
}
pub fn get_rustpkg_sysroot() -> Result<Path, ~str> {
result::Ok(get_or_default_sysroot().push_many([libdir(), ~"rustpkg"]))
}
pub fn get_rustpkg_root() -> Result<Path, ~str> {
match os::getenv("RUSTPKG_ROOT") {
Some(ref _p) => result::Ok(Path((*_p))),
2012-08-20 14:23:37 -05:00
None => match os::homedir() {
Some(ref _q) => result::Ok((*_q).push(".rustpkg")),
None => result::Err(~"no RUSTPKG_ROOT or home directory")
}
}
}
pub fn get_rustpkg_root_nearest() -> Result<Path, ~str> {
do result::chain(get_rustpkg_root()) |p| {
let cwd = os::getcwd();
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);
break;
}
if par_rustpkg.components.len() == 1 {
// We just checked /.rustpkg, stop now.
break;
}
par_rustpkg = par_rustpkg.pop().pop().push(".rustpkg");
}
}
rslt
}
}
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()))
}
}
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()))
}
}
// The name of the directory rustc expects libraries to be located.
// On Unix should be "lib", on windows "bin"
pub fn libdir() -> ~str {
2012-08-22 19:24:52 -05:00
let libdir = env!("CFG_LIBDIR");
if str::is_empty(libdir) {
fail!("rustc compiled without CFG_LIBDIR environment variable");
}
libdir.to_owned()
}