Skip to content

Commit d91b7b6

Browse files
committed
Encapsulate current sysroot and lib path handling into util::filesearch
1 parent a8ce543 commit d91b7b6

File tree

6 files changed

+92
-54
lines changed

6 files changed

+92
-54
lines changed

src/comp/back/link.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,9 @@ fn llvm_err(sess: session::session, msg: str) {
3939
} else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
4040
}
4141

42-
fn make_target_lib_path(sysroot: fs::path, target_triple: str) -> fs::path {
43-
let path = [
44-
sysroot,
45-
"lib/rustc",
46-
target_triple,
47-
"lib"
48-
];
49-
check vec::is_not_empty(path);
50-
let path = fs::connect_many(path);
51-
ret path;
52-
}
53-
54-
fn get_target_lib_path(sess: session::session) -> fs::path {
55-
make_target_lib_path(sess.get_opts().sysroot,
56-
sess.get_opts().target_triple)
57-
}
58-
59-
fn get_target_lib_file_path(sess: session::session,
60-
file: fs::path) -> fs::path {
61-
fs::connect(get_target_lib_path(sess), file)
62-
}
63-
6442
fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
65-
let path = get_target_lib_file_path(sess, "intrinsics.bc");
43+
let path = sess.filesearch()
44+
.get_target_lib_file_path("intrinsics.bc");
6645
let membuf = str::as_buf(path, {|buf|
6746
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
6847
});
@@ -518,8 +497,9 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: str) -> str {
518497
// gcc to link the object file with some libs
519498
fn link_binary(sess: session::session,
520499
saved_out_filename: str) {
521-
let main: str = get_target_lib_file_path(sess, "main.o");
522-
let stage: str = "-L" + get_target_lib_path(sess);
500+
let main: str = sess.filesearch()
501+
.get_target_lib_file_path("main.o");
502+
let stage: str = "-L" + sess.filesearch().get_target_lib_path();
523503
let prog: str = "gcc";
524504
// The invocations of gcc share some flags across platforms
525505

src/comp/driver/rustc.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import front::attr;
88
import middle::{trans, resolve, freevars, kind, ty, typeck};
99
import middle::tstate::ck;
1010
import syntax::print::{pp, pprust};
11-
import util::{ppaux, common};
11+
import util::{ppaux, common, filesearch};
1212
import back::link;
1313
import lib::llvm;
1414
import std::{fs, option, str, vec, int, io, run, getopts};
@@ -293,12 +293,6 @@ fn get_arch(triple: str) -> session::arch {
293293
} else { log_err "Unknown architecture! " + triple; fail };
294294
}
295295

296-
fn get_default_sysroot(binary: str) -> str {
297-
let dirname = fs::dirname(binary);
298-
if str::eq(dirname, binary) { ret "../"; }
299-
ret fs::connect(dirname, "../");
300-
}
301-
302296
fn build_target_config(sopts: @session::options) -> @session::config {
303297
let target_cfg: @session::config =
304298
@{os: get_os(sopts.target_triple),
@@ -321,7 +315,7 @@ fn host_triple() -> str {
321315
ret ht != "" ? ht : fail "rustc built without CFG_HOST_TRIPLE";
322316
}
323317

324-
fn build_session_options(binary: str, match: getopts::match)
318+
fn build_session_options(match: getopts::match)
325319
-> @session::options {
326320
let library = opt_present(match, "lib");
327321
let static = opt_present(match, "static");
@@ -368,22 +362,13 @@ fn build_session_options(binary: str, match: getopts::match)
368362
}
369363
}
370364
} else { 0u };
371-
let sysroot =
372-
alt sysroot_opt {
373-
none. { get_default_sysroot(binary) }
374-
some(s) { s }
375-
};
376365
let target =
377366
alt target_opt {
378367
none. { host_triple() }
379368
some(s) { s }
380369
};
381370

382-
let library_search_paths = [link::make_target_lib_path(sysroot, target)];
383-
let lsp_vec = getopts::opt_strs(match, "L");
384-
// FIXME: These should probably go in front of the defaults
385-
for lsp: str in lsp_vec { library_search_paths += [lsp]; }
386-
371+
let addl_lib_search_paths = getopts::opt_strs(match, "L");
387372
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
388373
let test = opt_present(match, "test");
389374
let do_gc = opt_present(match, "gc");
@@ -400,8 +385,8 @@ fn build_session_options(binary: str, match: getopts::match)
400385
time_passes: time_passes,
401386
time_llvm_passes: time_llvm_passes,
402387
output_type: output_type,
403-
library_search_paths: library_search_paths,
404-
sysroot: sysroot,
388+
addl_lib_search_paths: addl_lib_search_paths,
389+
maybe_sysroot: sysroot_opt,
405390
target_triple: target,
406391
cfg: cfg,
407392
test: test,
@@ -412,12 +397,18 @@ fn build_session_options(binary: str, match: getopts::match)
412397
ret sopts;
413398
}
414399

415-
fn build_session(sopts: @session::options) -> session::session {
400+
fn build_session(binary: str,
401+
sopts: @session::options) -> session::session {
416402
let target_cfg = build_target_config(sopts);
417403
let cstore = cstore::mk_cstore();
404+
let filesearch = filesearch::mk_filesearch(
405+
binary,
406+
sopts.maybe_sysroot,
407+
sopts.target_triple,
408+
sopts.addl_lib_search_paths);
418409
ret session::session(target_cfg, sopts, cstore,
419410
@{cm: codemap::new_codemap(), mutable next_id: 0},
420-
none, 0u);
411+
none, 0u, filesearch);
421412
}
422413

423414
fn parse_pretty(sess: session::session, name: str) -> pp_mode {
@@ -464,8 +455,8 @@ fn main(args: [str]) {
464455
version(binary);
465456
ret;
466457
}
467-
let sopts = build_session_options(binary, match);
468-
let sess = build_session(sopts);
458+
let sopts = build_session_options(match);
459+
let sess = build_session(binary, sopts);
469460
let n_inputs = vec::len::<str>(match.free);
470461
let output_file = getopts::opt_maybe_str(match, "o");
471462
if n_inputs == 0u {

src/comp/driver/session.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import syntax::ast::ty_mach;
66
import std::{uint, map, option, str};
77
import std::option::{some, none};
88
import syntax::parse::parser::parse_sess;
9+
import util::filesearch;
910

1011
tag os { os_win32; os_macos; os_linux; }
1112

@@ -32,8 +33,8 @@ type options =
3233
time_passes: bool,
3334
time_llvm_passes: bool,
3435
output_type: back::link::output_type,
35-
library_search_paths: [str],
36-
sysroot: str,
36+
addl_lib_search_paths: [str],
37+
maybe_sysroot: option::t<str>,
3738
target_triple: str,
3839
cfg: ast::crate_cfg,
3940
test: bool,
@@ -51,7 +52,8 @@ obj session(targ_cfg: @config,
5152

5253
// For a library crate, this is always none
5354
mutable main_fn: option::t<node_id>,
54-
mutable err_count: uint) {
55+
mutable err_count: uint,
56+
filesearch: filesearch::filesearch) {
5557
fn get_targ_cfg() -> @config { ret targ_cfg; }
5658
fn get_opts() -> @options { ret opts; }
5759
fn get_cstore() -> metadata::cstore::cstore { cstore }
@@ -108,6 +110,7 @@ obj session(targ_cfg: @config,
108110
}
109111
fn set_main_id(d: node_id) { main_fn = some(d); }
110112
fn get_main_id() -> option::t<node_id> { main_fn }
113+
fn filesearch() -> filesearch::filesearch { filesearch }
111114
}
112115
// Local Variables:
113116
// fill-column: 78;

src/comp/metadata/creader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn read_crates(sess: session::session, crate: ast::crate) {
2424
let e =
2525
@{sess: sess,
2626
crate_cache: @std::map::new_str_hash::<int>(),
27-
library_search_paths: sess.get_opts().library_search_paths,
27+
library_search_paths: sess.filesearch().lib_search_paths(),
2828
mutable next_crate_num: 1};
2929
let v =
3030
visit::mk_simple_visitor(@{visit_view_item:
@@ -37,7 +37,7 @@ fn read_crates(sess: session::session, crate: ast::crate) {
3737
type env =
3838
@{sess: session::session,
3939
crate_cache: @hashmap<str, int>,
40-
library_search_paths: [str],
40+
library_search_paths: [fs::path],
4141
mutable next_crate_num: ast::crate_num};
4242

4343
fn visit_view_item(e: env, i: @ast::view_item) {

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ mod driver {
117117
mod util {
118118
mod common;
119119
mod ppaux;
120+
mod filesearch;
120121
}
121122

122123
auth middle::metadata = unsafe;

src/comp/util/filesearch.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import std::option;
2+
import std::fs;
3+
import std::vec;
4+
import std::str;
5+
import back::link;
6+
7+
export filesearch;
8+
export mk_filesearch;
9+
10+
type filesearch = obj {
11+
fn sysroot() -> fs::path;
12+
fn lib_search_paths() -> [fs::path];
13+
fn get_target_lib_path() -> fs::path;
14+
fn get_target_lib_file_path(file: fs::path) -> fs::path;
15+
};
16+
17+
fn mk_filesearch(binary_name: fs::path,
18+
maybe_sysroot: option::t<fs::path>,
19+
target_triple: str,
20+
addl_lib_search_paths: [fs::path]) -> filesearch {
21+
obj filesearch_impl(sysroot: fs::path,
22+
addl_lib_search_paths: [fs::path],
23+
target_triple: str) {
24+
fn sysroot() -> fs::path { sysroot }
25+
fn lib_search_paths() -> [fs::path] {
26+
addl_lib_search_paths
27+
+ [make_target_lib_path(sysroot, target_triple)]
28+
}
29+
30+
fn get_target_lib_path() -> fs::path {
31+
make_target_lib_path(sysroot, target_triple)
32+
}
33+
34+
fn get_target_lib_file_path(file: fs::path) -> fs::path {
35+
fs::connect(self.get_target_lib_path(), file)
36+
}
37+
}
38+
39+
let sysroot = get_sysroot(maybe_sysroot, binary_name);
40+
ret filesearch_impl(sysroot, addl_lib_search_paths, target_triple);
41+
}
42+
43+
fn make_target_lib_path(sysroot: fs::path,
44+
target_triple: str) -> fs::path {
45+
let path = [sysroot, "lib/rustc", target_triple, "lib"];
46+
check vec::is_not_empty(path);
47+
let path = fs::connect_many(path);
48+
ret path;
49+
}
50+
51+
fn get_default_sysroot(binary: fs::path) -> fs::path {
52+
let dirname = fs::dirname(binary);
53+
if str::eq(dirname, binary) { ret "../"; }
54+
ret fs::connect(dirname, "../");
55+
}
56+
57+
fn get_sysroot(maybe_sysroot: option::t<fs::path>,
58+
binary: fs::path) -> fs::path {
59+
alt maybe_sysroot {
60+
option::some(sr) { sr }
61+
option::none. { get_default_sysroot(binary) }
62+
}
63+
}

0 commit comments

Comments
 (0)