rust/src/rustc/metadata/filesearch.rs

177 lines
5.3 KiB
Rust
Raw Normal View History

// A module for searching for libraries
// FIXME (#2658): I'm not happy how this module turned out. Should
// probably just be folded into cstore.
import result::result;
export filesearch;
export mk_filesearch;
export pick;
export pick_file;
export search;
2011-10-04 17:23:32 -05:00
export relative_target_lib_path;
export get_cargo_sysroot;
export get_cargo_root;
export get_cargo_root_nearest;
export libdir;
2012-08-14 15:38:35 -05:00
import path::Path;
2012-08-14 15:38:35 -05:00
type pick<T> = fn(path: Path) -> option<T>;
2012-08-14 15:38:35 -05:00
fn pick_file(file: Path, path: Path) -> option<Path> {
if path::basename(path) == file { option::some(path) }
else { option::none }
}
trait filesearch {
2012-08-14 15:38:35 -05:00
fn sysroot() -> Path;
fn lib_search_paths() -> ~[Path];
fn get_target_lib_path() -> Path;
fn get_target_lib_file_path(file: Path) -> Path;
}
2012-08-14 15:38:35 -05:00
fn mk_filesearch(maybe_sysroot: option<Path>,
target_triple: ~str,
2012-08-14 15:38:35 -05:00
addl_lib_search_paths: ~[Path]) -> filesearch {
type filesearch_impl = {sysroot: Path,
addl_lib_search_paths: ~[Path],
target_triple: ~str};
2012-08-07 20:10:06 -05:00
impl filesearch_impl: filesearch {
2012-08-14 15:38:35 -05:00
fn sysroot() -> Path { self.sysroot }
fn lib_search_paths() -> ~[Path] {
let mut paths = self.addl_lib_search_paths;
vec::push(paths,
make_target_lib_path(self.sysroot, self.target_triple));
2012-08-06 14:34:08 -05:00
match get_cargo_lib_path_nearest() {
2012-08-03 21:59:04 -05:00
result::ok(p) => vec::push(paths, p),
result::err(p) => ()
}
2012-08-06 14:34:08 -05:00
match get_cargo_lib_path() {
2012-08-03 21:59:04 -05:00
result::ok(p) => vec::push(paths, p),
result::err(p) => ()
}
paths
}
2012-08-14 15:38:35 -05:00
fn get_target_lib_path() -> Path {
make_target_lib_path(self.sysroot, self.target_triple)
}
2012-08-14 15:38:35 -05:00
fn get_target_lib_file_path(file: Path) -> Path {
path::connect(self.get_target_lib_path(), file)
}
}
let sysroot = get_sysroot(maybe_sysroot);
2012-08-22 19:24:52 -05:00
debug!("using sysroot = %s", sysroot);
{sysroot: sysroot,
addl_lib_search_paths: addl_lib_search_paths,
target_triple: target_triple} as filesearch
}
fn search<T: copy>(filesearch: filesearch, pick: pick<T>) -> option<T> {
let mut rslt = none;
2012-06-30 18:19:07 -05:00
for filesearch.lib_search_paths().each |lib_search_path| {
2012-08-22 19:24:52 -05:00
debug!("searching %s", lib_search_path);
2012-06-30 18:19:07 -05:00
for os::list_dir_path(lib_search_path).each |path| {
2012-08-22 19:24:52 -05:00
debug!("testing %s", path);
let maybe_picked = pick(path);
if option::is_some(maybe_picked) {
2012-08-22 19:24:52 -05:00
debug!("picked %s", path);
rslt = maybe_picked;
break;
} else {
2012-08-22 19:24:52 -05:00
debug!("rejected %s", path);
}
}
if option::is_some(rslt) { break; }
}
2012-08-01 19:30:05 -05:00
return rslt;
}
2012-08-14 15:38:35 -05:00
fn relative_target_lib_path(target_triple: ~str) -> ~[Path] {
~[libdir(), ~"rustc", target_triple, libdir()]
2011-10-04 17:23:32 -05:00
}
2012-08-14 15:38:35 -05:00
fn make_target_lib_path(sysroot: Path,
target_triple: ~str) -> Path {
let path = vec::append(~[sysroot],
relative_target_lib_path(target_triple));
let path = path::connect_many(path);
2012-08-01 19:30:05 -05:00
return path;
}
2012-08-14 15:38:35 -05:00
fn get_default_sysroot() -> Path {
2012-08-06 14:34:08 -05:00
match os::self_exe_path() {
2012-08-03 21:59:04 -05:00
option::some(p) => path::normalize(path::connect(p, ~"..")),
option::none => fail ~"can't determine value for sysroot"
}
}
2012-08-14 15:38:35 -05:00
fn get_sysroot(maybe_sysroot: option<Path>) -> Path {
2012-08-06 14:34:08 -05:00
match maybe_sysroot {
2012-08-03 21:59:04 -05:00
option::some(sr) => sr,
option::none => get_default_sysroot()
}
2011-11-10 10:41:42 -06:00
}
2012-08-14 15:38:35 -05:00
fn get_cargo_sysroot() -> result<Path, ~str> {
let path = ~[get_default_sysroot(), libdir(), ~"cargo"];
result::ok(path::connect_many(path))
}
2012-08-14 15:38:35 -05:00
fn get_cargo_root() -> result<Path, ~str> {
2012-08-06 14:34:08 -05:00
match os::getenv(~"CARGO_ROOT") {
2012-08-03 21:59:04 -05:00
some(_p) => result::ok(_p),
2012-08-06 14:34:08 -05:00
none => match os::homedir() {
2012-08-03 21:59:04 -05:00
some(_q) => result::ok(path::connect(_q, ~".cargo")),
none => result::err(~"no CARGO_ROOT or home directory")
}
}
}
2012-08-14 15:38:35 -05:00
fn get_cargo_root_nearest() -> result<Path, ~str> {
2012-06-30 18:19:07 -05:00
do result::chain(get_cargo_root()) |p| {
let cwd = os::getcwd();
let mut dirname = path::dirname(cwd);
let mut dirpath = path::split(dirname);
let cwd_cargo = path::connect(cwd, ~".cargo");
let mut par_cargo = path::connect(dirname, ~".cargo");
let mut rslt = result::ok(cwd_cargo);
if !os::path_is_dir(cwd_cargo) && cwd_cargo != p {
while vec::is_not_empty(dirpath) && par_cargo != p {
if os::path_is_dir(par_cargo) {
rslt = result::ok(par_cargo);
break;
}
vec::pop(dirpath);
dirname = path::dirname(dirname);
par_cargo = path::connect(dirname, ~".cargo");
}
}
rslt
}
}
2012-08-14 15:38:35 -05:00
fn get_cargo_lib_path() -> result<Path, ~str> {
2012-06-30 18:19:07 -05:00
do result::chain(get_cargo_root()) |p| {
result::ok(path::connect(p, libdir()))
}
}
2012-08-14 15:38:35 -05:00
fn get_cargo_lib_path_nearest() -> result<Path, ~str> {
2012-06-30 18:19:07 -05:00
do result::chain(get_cargo_root_nearest()) |p| {
result::ok(path::connect(p, libdir()))
}
}
// The name of the directory rustc expects libraries to be located.
// On Unix should be "lib", on windows "bin"
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
}