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-09-04 13:54:36 -05:00
|
|
|
use result::Result;
|
2012-10-15 16:56:42 -05:00
|
|
|
export FileSearch;
|
2011-10-03 14:46:22 -05:00
|
|
|
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-20 14:23:37 -05:00
|
|
|
type pick<T> = fn(path: &Path) -> Option<T>;
|
2012-03-12 22:04:27 -05:00
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn pick_file(file: Path, path: &Path) -> Option<Path> {
|
|
|
|
if path.file_path() == file { option::Some(copy *path) }
|
|
|
|
else { option::None }
|
2011-10-03 16:45:38 -05:00
|
|
|
}
|
|
|
|
|
2012-10-15 16:56:42 -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;
|
2012-08-24 17:28:43 -05:00
|
|
|
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-20 14:23:37 -05:00
|
|
|
fn mk_filesearch(maybe_sysroot: Option<Path>,
|
2012-08-24 17:28:43 -05:00
|
|
|
target_triple: &str,
|
2012-10-15 16:56:42 -05:00
|
|
|
addl_lib_search_paths: ~[Path]) -> FileSearch {
|
2012-08-14 15:38:35 -05:00
|
|
|
type filesearch_impl = {sysroot: Path,
|
|
|
|
addl_lib_search_paths: ~[Path],
|
2012-07-14 00:57:48 -05:00
|
|
|
target_triple: ~str};
|
2012-10-15 16:56:42 -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;
|
|
|
|
|
2012-09-26 19:33:34 -05:00
|
|
|
paths.push(
|
|
|
|
make_target_lib_path(&self.sysroot,
|
|
|
|
self.target_triple));
|
2012-08-06 14:34:08 -05:00
|
|
|
match get_cargo_lib_path_nearest() {
|
2012-09-26 19:33:34 -05:00
|
|
|
result::Ok(p) => paths.push(p),
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Err(_) => ()
|
2012-06-28 17:00:03 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match get_cargo_lib_path() {
|
2012-09-26 19:33:34 -05:00
|
|
|
result::Ok(p) => paths.push(p),
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Err(_) => ()
|
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-08-24 17:28:43 -05:00
|
|
|
make_target_lib_path(&self.sysroot, self.target_triple)
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
2012-08-24 17:28:43 -05:00
|
|
|
fn get_target_lib_file_path(file: &Path) -> Path {
|
|
|
|
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());
|
2012-01-13 02:32:05 -06:00
|
|
|
{sysroot: sysroot,
|
|
|
|
addl_lib_search_paths: addl_lib_search_paths,
|
2012-10-15 16:56:42 -05:00
|
|
|
target_triple: str::from_slice(target_triple)} as FileSearch
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
fn search<T: Copy>(filesearch: FileSearch, pick: pick<T>) -> Option<T> {
|
2012-08-20 14:23:37 -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-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
|
|
|
}
|
|
|
|
|
2012-08-24 17:28:43 -05:00
|
|
|
fn relative_target_lib_path(target_triple: &str) -> Path {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-20 14:23:37 -05:00
|
|
|
option::Some(p) => p.pop(),
|
|
|
|
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-20 14:23:37 -05:00
|
|
|
fn get_sysroot(maybe_sysroot: Option<Path>) -> Path {
|
2012-08-06 14:34:08 -05:00
|
|
|
match maybe_sysroot {
|
2012-08-20 14:23:37 -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-26 18:54:31 -05:00
|
|
|
fn get_cargo_sysroot() -> Result<Path, ~str> {
|
|
|
|
result::Ok(get_default_sysroot().push_many([libdir(), ~"cargo"]))
|
2012-02-07 02:15:39 -06:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:54:31 -05:00
|
|
|
fn get_cargo_root() -> Result<Path, ~str> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match os::getenv(~"CARGO_ROOT") {
|
2012-08-26 18:54:31 -05:00
|
|
|
Some(_p) => result::Ok(Path(_p)),
|
2012-08-20 14:23:37 -05:00
|
|
|
None => match os::homedir() {
|
2012-08-26 18:54:31 -05:00
|
|
|
Some(_q) => result::Ok(_q.push(".cargo")),
|
|
|
|
None => result::Err(~"no CARGO_ROOT or home directory")
|
2012-01-05 18:03:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 18:54:31 -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-08-24 17:28:43 -05:00
|
|
|
let cwd_cargo = cwd.push(".cargo");
|
|
|
|
let mut par_cargo = cwd.pop().push(".cargo");
|
2012-08-26 18:54:31 -05:00
|
|
|
let mut rslt = result::Ok(cwd_cargo);
|
2012-02-05 03:30:03 -06:00
|
|
|
|
2012-08-24 17:28:43 -05:00
|
|
|
if !os::path_is_dir(&cwd_cargo) && cwd_cargo != p {
|
|
|
|
while par_cargo != p {
|
|
|
|
if os::path_is_dir(&par_cargo) {
|
2012-08-26 18:54:31 -05:00
|
|
|
rslt = result::Ok(par_cargo);
|
2012-03-26 05:39:20 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-08-24 17:28:43 -05:00
|
|
|
if par_cargo.components.len() == 1 {
|
|
|
|
// We just checked /.cargo, stop now.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
par_cargo = par_cargo.pop().pop().push(".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-26 18:54:31 -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-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
|
|
|
}
|
|
|
|
|
2012-08-26 18:54:31 -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-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"
|
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
|
|
|
|
}
|