2011-10-03 15:54:13 -05:00
|
|
|
// A module for searching for libraries
|
|
|
|
|
2011-10-03 14:46:22 -05:00
|
|
|
import std::option;
|
|
|
|
import std::fs;
|
|
|
|
import std::vec;
|
|
|
|
import std::str;
|
2011-10-04 16:14:36 -05:00
|
|
|
import std::os;
|
2011-10-03 14:46:22 -05:00
|
|
|
import back::link;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
type pick<@T> = block(path: fs::path) -> option::t<T>;
|
2011-10-03 14:46:22 -05:00
|
|
|
|
2011-10-03 16:45:38 -05:00
|
|
|
fn pick_file(file: fs::path, path: fs::path) -> option::t<fs::path> {
|
|
|
|
if fs::basename(path) == file { option::some(path) }
|
|
|
|
else { option::none }
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:46:22 -05:00
|
|
|
type filesearch = obj {
|
|
|
|
fn sysroot() -> fs::path;
|
|
|
|
fn lib_search_paths() -> [fs::path];
|
|
|
|
fn get_target_lib_path() -> fs::path;
|
|
|
|
fn get_target_lib_file_path(file: fs::path) -> fs::path;
|
|
|
|
};
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
fn mk_filesearch(maybe_sysroot: option::t<fs::path>,
|
2011-10-03 14:46:22 -05:00
|
|
|
target_triple: str,
|
|
|
|
addl_lib_search_paths: [fs::path]) -> filesearch {
|
|
|
|
obj filesearch_impl(sysroot: fs::path,
|
|
|
|
addl_lib_search_paths: [fs::path],
|
|
|
|
target_triple: str) {
|
|
|
|
fn sysroot() -> fs::path { sysroot }
|
|
|
|
fn lib_search_paths() -> [fs::path] {
|
|
|
|
addl_lib_search_paths
|
|
|
|
+ [make_target_lib_path(sysroot, target_triple)]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_target_lib_path() -> fs::path {
|
|
|
|
make_target_lib_path(sysroot, target_triple)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_target_lib_file_path(file: fs::path) -> fs::path {
|
|
|
|
fs::connect(self.get_target_lib_path(), file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
let sysroot = get_sysroot(maybe_sysroot);
|
2011-10-03 15:54:13 -05:00
|
|
|
log #fmt("using sysroot = %s", sysroot);
|
2011-10-03 14:46:22 -05:00
|
|
|
ret filesearch_impl(sysroot, addl_lib_search_paths, target_triple);
|
|
|
|
}
|
|
|
|
|
2011-10-03 15:54:13 -05:00
|
|
|
// FIXME #1001: This can't be an obj method
|
|
|
|
fn search<@T>(filesearch: filesearch, pick: pick<T>) -> option::t<T> {
|
|
|
|
for lib_search_path in filesearch.lib_search_paths() {
|
|
|
|
log #fmt["searching %s", lib_search_path];
|
|
|
|
for path in fs::list_dir(lib_search_path) {
|
|
|
|
log #fmt["testing %s", path];
|
|
|
|
let maybe_picked = pick(path);
|
|
|
|
if option::is_some(maybe_picked) {
|
|
|
|
log #fmt("picked %s", path);
|
|
|
|
ret maybe_picked;
|
|
|
|
} else {
|
|
|
|
log #fmt("rejected %s", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret option::none;
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:46:22 -05:00
|
|
|
fn make_target_lib_path(sysroot: fs::path,
|
|
|
|
target_triple: str) -> fs::path {
|
|
|
|
let path = [sysroot, "lib/rustc", target_triple, "lib"];
|
|
|
|
check vec::is_not_empty(path);
|
|
|
|
let path = fs::connect_many(path);
|
|
|
|
ret path;
|
|
|
|
}
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
fn get_default_sysroot() -> fs::path {
|
|
|
|
alt os::get_exe_path() {
|
|
|
|
option::some(p) { fs::connect(p, "../") }
|
|
|
|
option::none. {
|
|
|
|
fail "can't determine value for sysroot";
|
|
|
|
}
|
|
|
|
}
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-04 16:14:36 -05:00
|
|
|
fn get_sysroot(maybe_sysroot: option::t<fs::path>) -> fs::path {
|
2011-10-03 14:46:22 -05:00
|
|
|
alt maybe_sysroot {
|
|
|
|
option::some(sr) { sr }
|
2011-10-04 16:14:36 -05:00
|
|
|
option::none. { get_default_sysroot() }
|
2011-10-03 14:46:22 -05:00
|
|
|
}
|
|
|
|
}
|