rustc: Determine the crate type (lib/bin) in the session, not session opts

This is in preparation for adding a #[crate_type] attribute
This commit is contained in:
Brian Anderson 2011-12-08 20:08:00 -08:00
parent 9e6ff44d93
commit fd81fb6a24
6 changed files with 28 additions and 16 deletions

View File

@ -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

View File

@ -282,6 +282,7 @@ options:
-o <filename> write output to <filename>
--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<str>,
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<str>,
};
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::<str,
@ -597,9 +604,11 @@ fn main(args: [str]) {
ret;
}
let outputs = build_output_filenames(ifile, ofile, sess);
let stop_after_codegen =
sopts.output_type != link::output_type_exe ||
sopts.static && sopts.library;
sopts.static && sess.building_library();
let temp_filename = outputs.obj_filename;

View File

@ -13,6 +13,8 @@ tag os { os_win32; os_macos; os_linux; }
tag arch { arch_x86; arch_x86_64; arch_arm; }
tag crate_type { bin_crate; lib_crate; unknown_crate; }
type config =
{os: os,
arch: arch,
@ -24,7 +26,7 @@ type config =
type options =
// The crate config requested for the session, which may be combined
// with additional crate configurations during the compile process
{library: bool,
{crate_type: crate_type,
static: bool,
libcore: bool,
optimize: uint,
@ -116,6 +118,7 @@ obj session(targ_cfg: @config,
fn set_main_id(d: node_id) { main_fn = some(d); }
fn get_main_id() -> option::t<node_id> { main_fn }
fn filesearch() -> filesearch::filesearch { filesearch }
fn building_library() -> bool { opts.crate_type == lib_crate }
}
// Local Variables:
// fill-column: 78;

View File

@ -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);

View File

@ -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 =

View File

@ -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"); }