diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index 6d5c32e9521..a3d134aee73 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -39,30 +39,9 @@ fn llvm_err(sess: session::session, msg: str) { } else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); } } -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; -} - -fn get_target_lib_path(sess: session::session) -> fs::path { - make_target_lib_path(sess.get_opts().sysroot, - sess.get_opts().target_triple) -} - -fn get_target_lib_file_path(sess: session::session, - file: fs::path) -> fs::path { - fs::connect(get_target_lib_path(sess), file) -} - fn link_intrinsics(sess: session::session, llmod: ModuleRef) { - let path = get_target_lib_file_path(sess, "intrinsics.bc"); + let path = sess.filesearch() + .get_target_lib_file_path("intrinsics.bc"); let membuf = str::as_buf(path, {|buf| llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf) }); @@ -518,8 +497,9 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: str) -> str { // gcc to link the object file with some libs fn link_binary(sess: session::session, saved_out_filename: str) { - let main: str = get_target_lib_file_path(sess, "main.o"); - let stage: str = "-L" + get_target_lib_path(sess); + let main: str = sess.filesearch() + .get_target_lib_file_path("main.o"); + let stage: str = "-L" + sess.filesearch().get_target_lib_path(); let prog: str = "gcc"; // The invocations of gcc share some flags across platforms diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index de9862f9e5e..70007240b63 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -8,7 +8,7 @@ import front::attr; import middle::{trans, resolve, freevars, kind, ty, typeck}; import middle::tstate::ck; import syntax::print::{pp, pprust}; -import util::{ppaux, common}; +import util::{ppaux, common, filesearch}; import back::link; import lib::llvm; import std::{fs, option, str, vec, int, io, run, getopts}; @@ -293,12 +293,6 @@ fn get_arch(triple: str) -> session::arch { } else { log_err "Unknown architecture! " + triple; fail }; } -fn get_default_sysroot(binary: str) -> str { - let dirname = fs::dirname(binary); - if str::eq(dirname, binary) { ret "../"; } - ret fs::connect(dirname, "../"); -} - fn build_target_config(sopts: @session::options) -> @session::config { let target_cfg: @session::config = @{os: get_os(sopts.target_triple), @@ -321,7 +315,7 @@ fn host_triple() -> str { ret ht != "" ? ht : fail "rustc built without CFG_HOST_TRIPLE"; } -fn build_session_options(binary: str, match: getopts::match) +fn build_session_options(match: getopts::match) -> @session::options { let library = opt_present(match, "lib"); let static = opt_present(match, "static"); @@ -368,22 +362,13 @@ fn build_session_options(binary: str, match: getopts::match) } } } else { 0u }; - let sysroot = - alt sysroot_opt { - none. { get_default_sysroot(binary) } - some(s) { s } - }; let target = alt target_opt { none. { host_triple() } some(s) { s } }; - let library_search_paths = [link::make_target_lib_path(sysroot, target)]; - let lsp_vec = getopts::opt_strs(match, "L"); - // FIXME: These should probably go in front of the defaults - for lsp: str in lsp_vec { library_search_paths += [lsp]; } - + let addl_lib_search_paths = getopts::opt_strs(match, "L"); let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg")); let test = opt_present(match, "test"); let do_gc = opt_present(match, "gc"); @@ -400,8 +385,8 @@ fn build_session_options(binary: str, match: getopts::match) time_passes: time_passes, time_llvm_passes: time_llvm_passes, output_type: output_type, - library_search_paths: library_search_paths, - sysroot: sysroot, + addl_lib_search_paths: addl_lib_search_paths, + maybe_sysroot: sysroot_opt, target_triple: target, cfg: cfg, test: test, @@ -412,12 +397,18 @@ fn build_session_options(binary: str, match: getopts::match) ret sopts; } -fn build_session(sopts: @session::options) -> session::session { +fn build_session(binary: str, + sopts: @session::options) -> session::session { let target_cfg = build_target_config(sopts); let cstore = cstore::mk_cstore(); + let filesearch = filesearch::mk_filesearch( + binary, + sopts.maybe_sysroot, + sopts.target_triple, + sopts.addl_lib_search_paths); ret session::session(target_cfg, sopts, cstore, @{cm: codemap::new_codemap(), mutable next_id: 0}, - none, 0u); + none, 0u, filesearch); } fn parse_pretty(sess: session::session, name: str) -> pp_mode { @@ -464,8 +455,8 @@ fn main(args: [str]) { version(binary); ret; } - let sopts = build_session_options(binary, match); - let sess = build_session(sopts); + let sopts = build_session_options(match); + let sess = build_session(binary, sopts); let n_inputs = vec::len::(match.free); let output_file = getopts::opt_maybe_str(match, "o"); if n_inputs == 0u { diff --git a/src/comp/driver/session.rs b/src/comp/driver/session.rs index 001bc9ce2fb..1f3f9baab8a 100644 --- a/src/comp/driver/session.rs +++ b/src/comp/driver/session.rs @@ -6,6 +6,7 @@ import syntax::ast::ty_mach; import std::{uint, map, option, str}; import std::option::{some, none}; import syntax::parse::parser::parse_sess; +import util::filesearch; tag os { os_win32; os_macos; os_linux; } @@ -32,8 +33,8 @@ type options = time_passes: bool, time_llvm_passes: bool, output_type: back::link::output_type, - library_search_paths: [str], - sysroot: str, + addl_lib_search_paths: [str], + maybe_sysroot: option::t, target_triple: str, cfg: ast::crate_cfg, test: bool, @@ -51,7 +52,8 @@ obj session(targ_cfg: @config, // For a library crate, this is always none mutable main_fn: option::t, - mutable err_count: uint) { + mutable err_count: uint, + filesearch: filesearch::filesearch) { fn get_targ_cfg() -> @config { ret targ_cfg; } fn get_opts() -> @options { ret opts; } fn get_cstore() -> metadata::cstore::cstore { cstore } @@ -108,6 +110,7 @@ obj session(targ_cfg: @config, } fn set_main_id(d: node_id) { main_fn = some(d); } fn get_main_id() -> option::t { main_fn } + fn filesearch() -> filesearch::filesearch { filesearch } } // Local Variables: // fill-column: 78; diff --git a/src/comp/metadata/creader.rs b/src/comp/metadata/creader.rs index 460bccb3087..9589a504c47 100644 --- a/src/comp/metadata/creader.rs +++ b/src/comp/metadata/creader.rs @@ -24,7 +24,7 @@ fn read_crates(sess: session::session, crate: ast::crate) { let e = @{sess: sess, crate_cache: @std::map::new_str_hash::(), - library_search_paths: sess.get_opts().library_search_paths, + library_search_paths: sess.filesearch().lib_search_paths(), mutable next_crate_num: 1}; let v = visit::mk_simple_visitor(@{visit_view_item: @@ -37,7 +37,7 @@ fn read_crates(sess: session::session, crate: ast::crate) { type env = @{sess: session::session, crate_cache: @hashmap, - library_search_paths: [str], + library_search_paths: [fs::path], mutable next_crate_num: ast::crate_num}; fn visit_view_item(e: env, i: @ast::view_item) { diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index 5399e6e4e44..c685139d54b 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -117,6 +117,7 @@ mod driver { mod util { mod common; mod ppaux; + mod filesearch; } auth middle::metadata = unsafe; diff --git a/src/comp/util/filesearch.rs b/src/comp/util/filesearch.rs new file mode 100644 index 00000000000..0ae7d82f9c1 --- /dev/null +++ b/src/comp/util/filesearch.rs @@ -0,0 +1,63 @@ +import std::option; +import std::fs; +import std::vec; +import std::str; +import back::link; + +export filesearch; +export mk_filesearch; + +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; +}; + +fn mk_filesearch(binary_name: fs::path, + maybe_sysroot: option::t, + 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) + } + } + + let sysroot = get_sysroot(maybe_sysroot, binary_name); + ret filesearch_impl(sysroot, addl_lib_search_paths, target_triple); +} + +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; +} + +fn get_default_sysroot(binary: fs::path) -> fs::path { + let dirname = fs::dirname(binary); + if str::eq(dirname, binary) { ret "../"; } + ret fs::connect(dirname, "../"); +} + +fn get_sysroot(maybe_sysroot: option::t, + binary: fs::path) -> fs::path { + alt maybe_sysroot { + option::some(sr) { sr } + option::none. { get_default_sysroot(binary) } + } +} \ No newline at end of file