From fd81fb6a2418d3cbef5244662573e2b61d5f66a7 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 8 Dec 2011 20:08:00 -0800 Subject: [PATCH] rustc: Determine the crate type (lib/bin) in the session, not session opts This is in preparation for adding a #[crate_type] attribute --- src/comp/back/link.rs | 4 ++-- src/comp/driver/rustc.rs | 25 +++++++++++++++++-------- src/comp/driver/session.rs | 5 ++++- src/comp/middle/resolve.rs | 2 +- src/comp/middle/trans.rs | 6 +++--- src/comp/middle/typeck.rs | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index 73137bd7437..dea4481fa26 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -425,7 +425,7 @@ fn build_link_meta(sess: session::session, c: ast::crate, output: str, } fn warn_missing(sess: session::session, name: str, default: str) { - if !sess.get_opts().library { ret; } + if !sess.building_library() { ret; } sess.warn(#fmt["missing crate link meta '%s', using '%s' as default", name, default]); } @@ -611,7 +611,7 @@ fn link_binary(sess: session::session, let used_libs = cstore::get_used_libraries(cstore); for l: str in used_libs { gcc_args += ["-l" + l]; } - if sess.get_opts().library { + if sess.building_library() { gcc_args += [lib_cmd]; // On mac we need to tell the linker to let this library diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index 03086444419..e00c16425f2 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -282,6 +282,7 @@ options: -o write output to --lib compile a library crate + --bin compile an executable crate (default) --static use or produce static libraries --no-core omit the 'core' library (used and imported by default) --pretty [type] pretty-print the input instead of compiling @@ -371,7 +372,13 @@ fn host_triple() -> str { fn build_session_options(match: getopts::match) -> @session::options { - let library = opt_present(match, "lib"); + let crate_type = if opt_present(match, "lib") { + session::lib_crate + } else if opt_present(match, "bin") { + session::bin_crate + } else { + session::unknown_crate + }; let static = opt_present(match, "static"); let parse_only = opt_present(match, "parse-only"); @@ -435,7 +442,7 @@ fn build_session_options(match: getopts::match) let stack_growth = opt_present(match, "stack-growth"); let warn_unused_imports = opt_present(match, "warn-unused-imports"); let sopts: @session::options = - @{library: library, + @{crate_type: crate_type, static: static, libcore: libcore, optimize: opt_level, @@ -495,20 +502,21 @@ fn opts() -> [getopts::opt] { optflag("no-verify"), optmulti("cfg"), optflag("test"), optflag("no-core"), - optflag("lib"), optflag("static"), optflag("gc"), + optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"), optflag("stack-growth"), optflag("no-asm-comments"), optflag("warn-unused-imports")]; } fn build_output_filenames(ifile: str, ofile: option::t, - sopts: @session::options) + sess: session::session) -> @{out_filename: str, obj_filename:str} { let obj_filename = ""; let saved_out_filename: str = ""; + let sopts = sess.get_opts(); let stop_after_codegen = sopts.output_type != link::output_type_exe || - sopts.static && sopts.library; + sopts.static && sess.building_library(); alt ofile { none. { // "-" 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, }; obj_filename = base_filename + "." + suffix; - if sopts.library { + if sess.building_library() { saved_out_filename = std::os::dylib_filename(base_filename); } else { saved_out_filename = base_filename; @@ -580,7 +588,6 @@ fn main(args: [str]) { let sopts = build_session_options(match); let sess = build_session(sopts); let ofile = getopts::opt_maybe_str(match, "o"); - let outputs = build_output_filenames(ifile, ofile, sopts); let cfg = build_configuration(sess, binary, ifile); let pretty = option::map:: option::t { main_fn } fn filesearch() -> filesearch::filesearch { filesearch } + fn building_library() -> bool { opts.crate_type == lib_crate } } // Local Variables: // fill-column: 78; diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 7ed27f2502d..4a8795d9d5e 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -362,7 +362,7 @@ fn visit_fn_with_scope(e: @env, f: ast::_fn, tp: [ast::ty_param], sp: span, // is this a main fn declaration? alt name { some(nm) { - if is_main_name([nm]) && !e.sess.get_opts().library { + if is_main_name([nm]) && !e.sess.building_library() { // This is a main function -- set it in the session // as the main ID e.sess.set_main_id(id); diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 104085579b5..df0de922fa3 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -5528,7 +5528,7 @@ fn register_fn_full(ccx: @crate_ctxt, sp: span, path: [str], _flav: str, ccx.item_ids.insert(node_id, llfn); ccx.item_symbols.insert(node_id, ps); - let is_main: bool = is_main_name(path) && !ccx.sess.get_opts().library; + let is_main: bool = is_main_name(path) && !ccx.sess.building_library(); if is_main { create_main_wrapper(ccx, sp, llfn, node_type); } } @@ -5951,7 +5951,7 @@ fn decl_crate_map(sess: session::session, mapname: str, let n_subcrates = 1; let cstore = sess.get_cstore(); while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; } - let mapname = sess.get_opts().library ? mapname : "toplevel"; + let mapname = sess.building_library() ? mapname : "toplevel"; let sym_name = "_rust_crate_map_" + mapname; let arrtype = T_array(int_type, n_subcrates as uint); let maptype = T_struct([int_type, arrtype]); @@ -5983,7 +5983,7 @@ fn fill_crate_map(ccx: @crate_ctxt, map: ValueRef) { } fn write_metadata(cx: @crate_ctxt, crate: @ast::crate) { - if !cx.sess.get_opts().library { ret; } + if !cx.sess.building_library() { ret; } let llmeta = C_postr(metadata::encoder::encode_metadata(cx, crate)); let llconst = trans_common::C_struct([llmeta]); let llglobal = diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 34fc1816c3f..feaa1215d87 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -2720,7 +2720,7 @@ fn check_main_fn_ty(tcx: ty::ctxt, main_id: ast::node_id) { } fn check_for_main_fn(tcx: ty::ctxt, crate: @ast::crate) { - if !tcx.sess.get_opts().library { + if !tcx.sess.building_library() { alt tcx.sess.get_main_id() { some(id) { check_main_fn_ty(tcx, id); } none. { tcx.sess.span_err(crate.span, "main function not found"); }