Skip to content

Commit fd81fb6

Browse files
committed
rustc: Determine the crate type (lib/bin) in the session, not session opts
This is in preparation for adding a #[crate_type] attribute
1 parent 9e6ff44 commit fd81fb6

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

src/comp/back/link.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ fn build_link_meta(sess: session::session, c: ast::crate, output: str,
425425
}
426426

427427
fn warn_missing(sess: session::session, name: str, default: str) {
428-
if !sess.get_opts().library { ret; }
428+
if !sess.building_library() { ret; }
429429
sess.warn(#fmt["missing crate link meta '%s', using '%s' as default",
430430
name, default]);
431431
}
@@ -611,7 +611,7 @@ fn link_binary(sess: session::session,
611611
let used_libs = cstore::get_used_libraries(cstore);
612612
for l: str in used_libs { gcc_args += ["-l" + l]; }
613613

614-
if sess.get_opts().library {
614+
if sess.building_library() {
615615
gcc_args += [lib_cmd];
616616

617617
// On mac we need to tell the linker to let this library

src/comp/driver/rustc.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ options:
282282
283283
-o <filename> write output to <filename>
284284
--lib compile a library crate
285+
--bin compile an executable crate (default)
285286
--static use or produce static libraries
286287
--no-core omit the 'core' library (used and imported by default)
287288
--pretty [type] pretty-print the input instead of compiling
@@ -371,7 +372,13 @@ fn host_triple() -> str {
371372

372373
fn build_session_options(match: getopts::match)
373374
-> @session::options {
374-
let library = opt_present(match, "lib");
375+
let crate_type = if opt_present(match, "lib") {
376+
session::lib_crate
377+
} else if opt_present(match, "bin") {
378+
session::bin_crate
379+
} else {
380+
session::unknown_crate
381+
};
375382
let static = opt_present(match, "static");
376383

377384
let parse_only = opt_present(match, "parse-only");
@@ -435,7 +442,7 @@ fn build_session_options(match: getopts::match)
435442
let stack_growth = opt_present(match, "stack-growth");
436443
let warn_unused_imports = opt_present(match, "warn-unused-imports");
437444
let sopts: @session::options =
438-
@{library: library,
445+
@{crate_type: crate_type,
439446
static: static,
440447
libcore: libcore,
441448
optimize: opt_level,
@@ -495,20 +502,21 @@ fn opts() -> [getopts::opt] {
495502
optflag("no-verify"),
496503
optmulti("cfg"), optflag("test"),
497504
optflag("no-core"),
498-
optflag("lib"), optflag("static"), optflag("gc"),
505+
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"),
499506
optflag("stack-growth"),
500507
optflag("no-asm-comments"),
501508
optflag("warn-unused-imports")];
502509
}
503510

504511
fn build_output_filenames(ifile: str, ofile: option::t<str>,
505-
sopts: @session::options)
512+
sess: session::session)
506513
-> @{out_filename: str, obj_filename:str} {
507514
let obj_filename = "";
508515
let saved_out_filename: str = "";
516+
let sopts = sess.get_opts();
509517
let stop_after_codegen =
510518
sopts.output_type != link::output_type_exe ||
511-
sopts.static && sopts.library;
519+
sopts.static && sess.building_library();
512520
alt ofile {
513521
none. {
514522
// "-" as input file will cause the parser to read from stdin so we
@@ -533,7 +541,7 @@ fn build_output_filenames(ifile: str, ofile: option::t<str>,
533541
};
534542
obj_filename = base_filename + "." + suffix;
535543

536-
if sopts.library {
544+
if sess.building_library() {
537545
saved_out_filename = std::os::dylib_filename(base_filename);
538546
} else {
539547
saved_out_filename = base_filename;
@@ -580,7 +588,6 @@ fn main(args: [str]) {
580588
let sopts = build_session_options(match);
581589
let sess = build_session(sopts);
582590
let ofile = getopts::opt_maybe_str(match, "o");
583-
let outputs = build_output_filenames(ifile, ofile, sopts);
584591
let cfg = build_configuration(sess, binary, ifile);
585592
let pretty =
586593
option::map::<str,
@@ -597,9 +604,11 @@ fn main(args: [str]) {
597604
ret;
598605
}
599606

607+
let outputs = build_output_filenames(ifile, ofile, sess);
608+
600609
let stop_after_codegen =
601610
sopts.output_type != link::output_type_exe ||
602-
sopts.static && sopts.library;
611+
sopts.static && sess.building_library();
603612

604613
let temp_filename = outputs.obj_filename;
605614

src/comp/driver/session.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ tag os { os_win32; os_macos; os_linux; }
1313

1414
tag arch { arch_x86; arch_x86_64; arch_arm; }
1515

16+
tag crate_type { bin_crate; lib_crate; unknown_crate; }
17+
1618
type config =
1719
{os: os,
1820
arch: arch,
@@ -24,7 +26,7 @@ type config =
2426
type options =
2527
// The crate config requested for the session, which may be combined
2628
// with additional crate configurations during the compile process
27-
{library: bool,
29+
{crate_type: crate_type,
2830
static: bool,
2931
libcore: bool,
3032
optimize: uint,
@@ -116,6 +118,7 @@ obj session(targ_cfg: @config,
116118
fn set_main_id(d: node_id) { main_fn = some(d); }
117119
fn get_main_id() -> option::t<node_id> { main_fn }
118120
fn filesearch() -> filesearch::filesearch { filesearch }
121+
fn building_library() -> bool { opts.crate_type == lib_crate }
119122
}
120123
// Local Variables:
121124
// fill-column: 78;

src/comp/middle/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn visit_fn_with_scope(e: @env, f: ast::_fn, tp: [ast::ty_param], sp: span,
362362
// is this a main fn declaration?
363363
alt name {
364364
some(nm) {
365-
if is_main_name([nm]) && !e.sess.get_opts().library {
365+
if is_main_name([nm]) && !e.sess.building_library() {
366366
// This is a main function -- set it in the session
367367
// as the main ID
368368
e.sess.set_main_id(id);

src/comp/middle/trans.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5528,7 +5528,7 @@ fn register_fn_full(ccx: @crate_ctxt, sp: span, path: [str], _flav: str,
55285528
ccx.item_ids.insert(node_id, llfn);
55295529
ccx.item_symbols.insert(node_id, ps);
55305530

5531-
let is_main: bool = is_main_name(path) && !ccx.sess.get_opts().library;
5531+
let is_main: bool = is_main_name(path) && !ccx.sess.building_library();
55325532
if is_main { create_main_wrapper(ccx, sp, llfn, node_type); }
55335533
}
55345534

@@ -5951,7 +5951,7 @@ fn decl_crate_map(sess: session::session, mapname: str,
59515951
let n_subcrates = 1;
59525952
let cstore = sess.get_cstore();
59535953
while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; }
5954-
let mapname = sess.get_opts().library ? mapname : "toplevel";
5954+
let mapname = sess.building_library() ? mapname : "toplevel";
59555955
let sym_name = "_rust_crate_map_" + mapname;
59565956
let arrtype = T_array(int_type, n_subcrates as uint);
59575957
let maptype = T_struct([int_type, arrtype]);
@@ -5983,7 +5983,7 @@ fn fill_crate_map(ccx: @crate_ctxt, map: ValueRef) {
59835983
}
59845984

59855985
fn write_metadata(cx: @crate_ctxt, crate: @ast::crate) {
5986-
if !cx.sess.get_opts().library { ret; }
5986+
if !cx.sess.building_library() { ret; }
59875987
let llmeta = C_postr(metadata::encoder::encode_metadata(cx, crate));
59885988
let llconst = trans_common::C_struct([llmeta]);
59895989
let llglobal =

src/comp/middle/typeck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,7 @@ fn check_main_fn_ty(tcx: ty::ctxt, main_id: ast::node_id) {
27202720
}
27212721

27222722
fn check_for_main_fn(tcx: ty::ctxt, crate: @ast::crate) {
2723-
if !tcx.sess.get_opts().library {
2723+
if !tcx.sess.building_library() {
27242724
alt tcx.sess.get_main_id() {
27252725
some(id) { check_main_fn_ty(tcx, id); }
27262726
none. { tcx.sess.span_err(crate.span, "main function not found"); }

0 commit comments

Comments
 (0)