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
|
|
|
|
2012-03-16 17:14:37 -05:00
|
|
|
import result::result;
|
2011-10-03 14:46:22 -05:00
|
|
|
export filesearch;
|
|
|
|
export mk_filesearch;
|
2011-10-03 15:54:13 -05:00
|
|
|
export pick;
|
2011-10-03 16:45:38 -05:00
|
|
|
export pick_file;
|
2011-10-03 15:54:13 -05:00
|
|
|
export search;
|
2011-10-04 17:23:32 -05:00
|
|
|
export relative_target_lib_path;
|
2012-02-07 02:15:39 -06:00
|
|
|
export get_cargo_sysroot;
|
2012-01-05 18:03:28 -06:00
|
|
|
export get_cargo_root;
|
2012-02-05 03:30:03 -06:00
|
|
|
export get_cargo_root_nearest;
|
2012-01-10 19:45:03 -06:00
|
|
|
export libdir;
|
2011-10-03 15:54:13 -05:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
import path::Path;
|
2012-03-12 22:04:27 -05:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
type pick<T> = fn(path: Path) -> option<T>;
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn pick_file(file: Path, path: Path) -> option<Path> {
|
2012-03-12 22:04:27 -05:00
|
|
|
if path::basename(path) == file { option::some(path) }
|
2011-10-03 16:45:38 -05:00
|
|
|
else { option::none }
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
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-01-13 02:32:05 -06:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn mk_filesearch(maybe_sysroot: option<Path>,
|
2012-07-14 00:57:48 -05:00
|
|
|
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],
|
2012-07-14 00:57:48 -05:00
|
|
|
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] {
|
2012-06-28 17:00:03 -05:00
|
|
|
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-06-28 17:00:03 -05:00
|
|
|
}
|
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) => ()
|
2012-06-28 17:00:03 -05:00
|
|
|
}
|
|
|
|
paths
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2012-08-14 15:38:35 -05:00
|
|
|
fn get_target_lib_path() -> Path {
|
2012-01-13 02:32:05 -06:00
|
|
|
make_target_lib_path(self.sysroot, self.target_triple)
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2012-08-14 15:38:35 -05:00
|
|
|
fn get_target_lib_file_path(file: Path) -> Path {
|
2012-03-12 22:04:27 -05:00
|
|
|
path::connect(self.get_target_lib_path(), 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-22 19:24:52 -05:00
|
|
|
debug!("using sysroot = %s", sysroot);
|
2012-01-13 02:32:05 -06:00
|
|
|
{sysroot: sysroot,
|
|
|
|
addl_lib_search_paths: addl_lib_search_paths,
|
|
|
|
target_triple: target_triple} as filesearch
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2012-01-31 19:05:20 -06:00
|
|
|
fn search<T: copy>(filesearch: filesearch, pick: pick<T>) -> option<T> {
|
2012-04-06 13:01:43 -05:00
|
|
|
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);
|
2011-10-03 15:54:13 -05:00
|
|
|
let maybe_picked = pick(path);
|
|
|
|
if option::is_some(maybe_picked) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("picked %s", path);
|
2012-04-06 13:01:43 -05:00
|
|
|
rslt = maybe_picked;
|
|
|
|
break;
|
2011-10-03 15:54:13 -05:00
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("rejected %s", path);
|
2011-10-03 15:54:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-06 13:01:43 -05:00
|
|
|
if option::is_some(rslt) { 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
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn relative_target_lib_path(target_triple: ~str) -> ~[Path] {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[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 {
|
2012-06-29 18:26:56 -05:00
|
|
|
let path = vec::append(~[sysroot],
|
2012-06-28 17:00:03 -05:00
|
|
|
relative_target_lib_path(target_triple));
|
2012-03-12 22:04:27 -05:00
|
|
|
let path = path::connect_many(path);
|
2012-08-01 19:30:05 -05:00
|
|
|
return path;
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
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"
|
2011-10-04 16:14:36 -05:00
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
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-10-03 14:46:22 -05:00
|
|
|
}
|
2011-11-10 10:41:42 -06:00
|
|
|
}
|
2012-01-05 18:03:28 -06:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn get_cargo_sysroot() -> result<Path, ~str> {
|
2012-07-14 00:57:48 -05:00
|
|
|
let path = ~[get_default_sysroot(), libdir(), ~"cargo"];
|
2012-03-12 22:04:27 -05:00
|
|
|
result::ok(path::connect_many(path))
|
2012-02-07 02:15:39 -06:00
|
|
|
}
|
|
|
|
|
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-01-05 18:03:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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| {
|
2012-02-05 03:30:03 -06:00
|
|
|
let cwd = os::getcwd();
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut dirname = path::dirname(cwd);
|
|
|
|
let mut dirpath = path::split(dirname);
|
2012-07-14 00:57:48 -05:00
|
|
|
let cwd_cargo = path::connect(cwd, ~".cargo");
|
|
|
|
let mut par_cargo = path::connect(dirname, ~".cargo");
|
2012-03-26 05:39:20 -05:00
|
|
|
let mut rslt = result::ok(cwd_cargo);
|
2012-02-05 03:30:03 -06:00
|
|
|
|
2012-03-26 05:39:20 -05:00
|
|
|
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);
|
2012-07-14 00:57:48 -05:00
|
|
|
par_cargo = path::connect(dirname, ~".cargo");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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| {
|
2012-03-12 22:04:27 -05:00
|
|
|
result::ok(path::connect(p, libdir()))
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
|
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| {
|
2012-03-12 22:04:27 -05:00
|
|
|
result::ok(path::connect(p, 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"
|
2012-07-14 00:57:48 -05:00
|
|
|
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) {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"rustc compiled without CFG_LIBDIR environment variable";
|
2012-01-10 19:45:03 -06:00
|
|
|
}
|
|
|
|
libdir
|
|
|
|
}
|