2013-08-06 23:50:23 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use back::link;
|
2013-01-29 08:28:08 -06:00
|
|
|
use back::{arm, x86, x86_64, mips};
|
2013-03-01 12:44:43 -06:00
|
|
|
use driver::session::{Aggressive};
|
2013-03-26 15:38:07 -05:00
|
|
|
use driver::session::{Session, Session_, No, Less, Default};
|
2013-03-01 12:44:43 -06:00
|
|
|
use driver::session;
|
2012-12-23 16:41:37 -06:00
|
|
|
use front;
|
|
|
|
use lib::llvm::llvm;
|
2013-07-27 01:49:38 -05:00
|
|
|
use lib::llvm::{ContextRef, ModuleRef};
|
|
|
|
use metadata::common::LinkMeta;
|
2012-09-04 13:54:36 -05:00
|
|
|
use metadata::{creader, cstore, filesearch};
|
2012-12-23 16:41:37 -06:00
|
|
|
use metadata;
|
2013-06-14 20:21:47 -05:00
|
|
|
use middle::{trans, freevars, kind, ty, typeck, lint, astencode, reachable};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle;
|
2013-03-21 04:50:02 -05:00
|
|
|
use util::common::time;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
use std::hashmap::{HashMap,HashSet};
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::io;
|
|
|
|
use std::os;
|
|
|
|
use std::vec;
|
2013-05-23 11:39:10 -05:00
|
|
|
use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt};
|
2013-05-18 14:39:17 -05:00
|
|
|
use extra::getopts::{opt_present};
|
|
|
|
use extra::getopts;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::ast;
|
2013-03-13 21:25:28 -05:00
|
|
|
use syntax::abi;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::attr;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr::{AttrMetaMethods};
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::codemap;
|
|
|
|
use syntax::diagnostic;
|
|
|
|
use syntax::parse;
|
2013-05-14 19:27:27 -05:00
|
|
|
use syntax::parse::token;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::print::{pp, pprust};
|
|
|
|
use syntax;
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub enum pp_mode {
|
2012-12-23 16:41:37 -06:00
|
|
|
ppm_normal,
|
|
|
|
ppm_expanded,
|
|
|
|
ppm_typed,
|
|
|
|
ppm_identified,
|
|
|
|
ppm_expanded_identified
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The name used for source code that doesn't originate in a file
|
|
|
|
* (e.g. source from stdin or a string)
|
|
|
|
*/
|
2013-06-12 12:02:55 -05:00
|
|
|
pub fn anon_src() -> @str { @"<anon>" }
|
2012-05-09 21:41:24 -05:00
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
pub fn source_name(input: &input) -> @str {
|
2013-04-17 11:15:37 -05:00
|
|
|
match *input {
|
2013-06-12 12:02:55 -05:00
|
|
|
file_input(ref ifile) => ifile.to_str().to_managed(),
|
2012-08-03 21:59:04 -05:00
|
|
|
str_input(_) => anon_src()
|
2012-05-09 21:41:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 19:06:27 -05:00
|
|
|
pub fn default_configuration(sess: Session) ->
|
2013-07-19 00:38:55 -05:00
|
|
|
ast::CrateConfig {
|
2013-08-13 19:06:27 -05:00
|
|
|
let tos = match sess.targ_cfg.os {
|
|
|
|
session::os_win32 => @"win32",
|
|
|
|
session::os_macos => @"macos",
|
|
|
|
session::os_linux => @"linux",
|
|
|
|
session::os_android => @"android",
|
|
|
|
session::os_freebsd => @"freebsd"
|
2012-01-26 03:52:08 -06:00
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-02-16 05:58:21 -06:00
|
|
|
// ARM is bi-endian, however using NDK seems to default
|
|
|
|
// to little-endian unless a flag is provided.
|
|
|
|
let (end,arch,wordsz) = match sess.targ_cfg.arch {
|
2013-06-15 02:43:19 -05:00
|
|
|
abi::X86 => (@"little", @"x86", @"32"),
|
|
|
|
abi::X86_64 => (@"little", @"x86_64", @"64"),
|
|
|
|
abi::Arm => (@"little", @"arm", @"32"),
|
|
|
|
abi::Mips => (@"big", @"mips", @"32")
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
|
|
|
|
2013-06-15 02:43:19 -05:00
|
|
|
let mk = attr::mk_name_value_item_str;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ~[ // Target bindings.
|
2013-06-12 12:02:55 -05:00
|
|
|
attr::mk_word_item(os::FAMILY.to_managed()),
|
|
|
|
mk(@"target_os", tos),
|
|
|
|
mk(@"target_family", os::FAMILY.to_managed()),
|
|
|
|
mk(@"target_arch", arch),
|
|
|
|
mk(@"target_endian", end),
|
|
|
|
mk(@"target_word_size", wordsz),
|
2013-08-13 19:06:27 -05:00
|
|
|
];
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn append_configuration(cfg: &mut ast::CrateConfig, name: @str) {
|
2013-07-19 06:51:37 -05:00
|
|
|
if !cfg.iter().any(|mi| mi.name() == name) {
|
|
|
|
cfg.push(attr::mk_word_item(name))
|
2012-08-28 12:58:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 19:06:27 -05:00
|
|
|
pub fn build_configuration(sess: Session) ->
|
2013-07-19 00:38:55 -05:00
|
|
|
ast::CrateConfig {
|
2011-12-20 04:17:13 -06:00
|
|
|
// Combine the configuration requested by the session (command line) with
|
|
|
|
// some default and generated configuration items
|
2013-08-13 19:06:27 -05:00
|
|
|
let default_cfg = default_configuration(sess);
|
2013-07-19 06:51:37 -05:00
|
|
|
let mut user_cfg = sess.opts.cfg.clone();
|
2011-12-20 04:17:13 -06:00
|
|
|
// If the user wants a test runner, then add the test cfg
|
2013-07-19 06:51:37 -05:00
|
|
|
if sess.opts.test { append_configuration(&mut user_cfg, @"test") }
|
2012-08-28 12:58:04 -05:00
|
|
|
// If the user requested GC, then add the GC cfg
|
2013-07-19 06:51:37 -05:00
|
|
|
append_configuration(&mut user_cfg, if sess.opts.gc { @"gc" } else { @"nogc" });
|
2012-08-28 12:58:04 -05:00
|
|
|
return vec::append(user_cfg, default_cfg);
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
|
2013-04-17 11:15:37 -05:00
|
|
|
fn parse_cfgspecs(cfgspecs: ~[~str],
|
2013-07-19 00:38:55 -05:00
|
|
|
demitter: diagnostic::Emitter) -> ast::CrateConfig {
|
2013-08-09 22:09:47 -05:00
|
|
|
do cfgspecs.move_iter().map |s| {
|
2013-01-19 09:52:06 -06:00
|
|
|
let sess = parse::new_parse_sess(Some(demitter));
|
2013-06-12 12:02:55 -05:00
|
|
|
parse::parse_meta_from_source_str(@"cfgspec", s.to_managed(), ~[], sess)
|
2013-07-19 00:38:55 -05:00
|
|
|
}.collect::<ast::CrateConfig>()
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub enum input {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Load source from file
|
2012-08-24 17:28:43 -05:00
|
|
|
file_input(Path),
|
2012-07-04 16:53:12 -05:00
|
|
|
/// The string is the source
|
2013-06-12 12:02:55 -05:00
|
|
|
// FIXME (#2319): Don't really want to box the source string
|
|
|
|
str_input(@str)
|
2012-05-09 21:41:24 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &input)
|
2013-07-19 00:38:55 -05:00
|
|
|
-> @ast::Crate {
|
2013-07-27 01:49:38 -05:00
|
|
|
time(sess.time_passes(), ~"parsing", || {
|
|
|
|
match *input {
|
|
|
|
file_input(ref file) => {
|
|
|
|
parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess)
|
|
|
|
}
|
|
|
|
str_input(src) => {
|
|
|
|
parse::parse_crate_from_source_str(
|
|
|
|
anon_src(), src, cfg.clone(), sess.parse_sess)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2012-01-17 09:42:45 -06:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-01-22 19:19:13 -06:00
|
|
|
// For continuing compilation after a parsed crate has been
|
|
|
|
// modified
|
2013-05-28 17:31:32 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
/// Run the "early phases" of the compiler: initial `cfg` processing,
|
|
|
|
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
|
|
|
|
/// harness if one is to be provided and injection of a dependency on the
|
|
|
|
/// standard library and prelude.
|
|
|
|
pub fn phase_2_configure_and_expand(sess: Session,
|
|
|
|
cfg: ast::CrateConfig,
|
|
|
|
mut crate: @ast::Crate) -> @ast::Crate {
|
2012-05-17 23:53:49 -05:00
|
|
|
let time_passes = sess.time_passes();
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
*sess.building_library = session::building_library(sess.opts.crate_type,
|
|
|
|
crate, sess.opts.test);
|
2013-05-09 00:15:54 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
// strip before expansion to allow macros to depend on
|
|
|
|
// configuration variables e.g/ in
|
|
|
|
//
|
|
|
|
// #[macro_escape] #[cfg(foo)]
|
|
|
|
// mod bar { macro_rules! baz!(() => {{}}) }
|
|
|
|
//
|
|
|
|
// baz! should not use this definition unless foo is enabled.
|
|
|
|
crate = time(time_passes, ~"std macros injection", ||
|
|
|
|
syntax::ext::expand::inject_std_macros(sess.parse_sess,
|
|
|
|
cfg.clone(),
|
|
|
|
crate));
|
2013-07-16 00:05:50 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
crate = time(time_passes, ~"configuration 1", ||
|
|
|
|
front::config::strip_unconfigured_items(crate));
|
2013-06-21 22:20:00 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
crate = time(time_passes, ~"expansion", ||
|
|
|
|
syntax::ext::expand::expand_crate(sess.parse_sess, cfg.clone(),
|
|
|
|
crate));
|
2013-05-27 19:45:16 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
// strip again, in case expansion added anything with a #[cfg].
|
|
|
|
crate = time(time_passes, ~"configuration 2", ||
|
|
|
|
front::config::strip_unconfigured_items(crate));
|
2013-05-27 19:45:16 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
crate = time(time_passes, ~"maybe building test harness", ||
|
|
|
|
front::test::modify_for_testing(sess, crate));
|
2013-07-19 06:51:37 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
crate = time(time_passes, ~"std injection", ||
|
|
|
|
front::std_inject::maybe_inject_libstd_ref(sess, crate));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
return crate;
|
|
|
|
}
|
2012-01-26 17:20:29 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub struct CrateAnalysis {
|
|
|
|
exp_map2: middle::resolve::ExportMap2,
|
|
|
|
ty_cx: ty::ctxt,
|
|
|
|
maps: astencode::Maps,
|
2013-07-27 03:25:59 -05:00
|
|
|
reachable: @mut HashSet<ast::NodeId>
|
2013-07-27 01:49:38 -05:00
|
|
|
}
|
2012-05-22 12:54:12 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
/// Run the resolution, typechecking, region checking and other
|
|
|
|
/// miscellaneous analysis passes on the crate. Return various
|
|
|
|
/// structures carrying the results of the analysis.
|
|
|
|
pub fn phase_3_run_analysis_passes(sess: Session,
|
|
|
|
crate: @ast::Crate) -> CrateAnalysis {
|
2012-05-22 12:54:12 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let time_passes = sess.time_passes();
|
|
|
|
let ast_map = time(time_passes, ~"ast indexing", ||
|
|
|
|
syntax::ast_map::map_crate(sess.diagnostic(), crate));
|
2012-07-25 20:36:18 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"external crate/lib resolution", ||
|
|
|
|
creader::read_crates(sess.diagnostic(), crate, sess.cstore,
|
|
|
|
sess.filesearch,
|
|
|
|
session::sess_os_to_meta_os(sess.targ_cfg.os),
|
|
|
|
sess.opts.is_static,
|
|
|
|
token::get_ident_interner()));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let lang_items = time(time_passes, ~"language item collection", ||
|
|
|
|
middle::lang_items::collect_language_items(crate, sess));
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let middle::resolve::CrateMap {
|
|
|
|
def_map: def_map,
|
|
|
|
exp_map2: exp_map2,
|
|
|
|
trait_map: trait_map
|
|
|
|
} =
|
|
|
|
time(time_passes, ~"resolution", ||
|
|
|
|
middle::resolve::resolve_crate(sess, lang_items, crate));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"looking for entry point",
|
|
|
|
|| middle::entry::find_entry_point(sess, crate, ast_map));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let freevars = time(time_passes, ~"freevar finding", ||
|
|
|
|
freevars::annotate_freevars(def_map, crate));
|
2012-07-11 12:28:30 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let region_map = time(time_passes, ~"region resolution", ||
|
|
|
|
middle::region::resolve_crate(sess, def_map, crate));
|
2012-01-17 09:42:45 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let rp_set = time(time_passes, ~"region parameterization inference", ||
|
|
|
|
middle::region::determine_rp_in_crate(sess, ast_map, def_map, crate));
|
2012-01-17 09:42:45 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let ty_cx = ty::mk_ctxt(sess, def_map, ast_map, freevars,
|
|
|
|
region_map, rp_set, lang_items);
|
2012-09-04 16:48:32 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
// passes are timed inside typeck
|
|
|
|
let (method_map, vtable_map) = typeck::check_crate(
|
|
|
|
ty_cx, trait_map, crate);
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
// These next two const passes can probably be merged
|
|
|
|
time(time_passes, ~"const marking", ||
|
|
|
|
middle::const_eval::process_crate(crate, ty_cx));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"const checking", ||
|
|
|
|
middle::check_const::check_crate(sess, crate, ast_map, def_map,
|
|
|
|
method_map, ty_cx));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"privacy checking", ||
|
|
|
|
middle::privacy::check_crate(ty_cx, &method_map, crate));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"effect checking", ||
|
|
|
|
middle::effect::check_crate(ty_cx, method_map, crate));
|
2013-05-23 21:12:16 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"loop checking", ||
|
|
|
|
middle::check_loop::check_crate(ty_cx, crate));
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
time(time_passes, ~"stack checking", ||
|
|
|
|
middle::stack_check::stack_check_crate(ty_cx, crate));
|
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let middle::moves::MoveMaps {moves_map, moved_variables_set,
|
|
|
|
capture_map} =
|
|
|
|
time(time_passes, ~"compute moves", ||
|
|
|
|
middle::moves::compute_moves(ty_cx, method_map, crate));
|
2012-12-04 17:38:04 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"match checking", ||
|
|
|
|
middle::check_match::check_crate(ty_cx, method_map,
|
|
|
|
moves_map, crate));
|
2012-12-07 21:34:57 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"liveness checking", ||
|
|
|
|
middle::liveness::check_crate(ty_cx, method_map,
|
|
|
|
capture_map, crate));
|
2012-11-14 20:05:07 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let (root_map, write_guard_map) =
|
|
|
|
time(time_passes, ~"borrow checking", ||
|
|
|
|
middle::borrowck::check_crate(ty_cx, method_map,
|
|
|
|
moves_map, moved_variables_set,
|
2013-05-27 19:45:16 -05:00
|
|
|
capture_map, crate));
|
2012-11-14 20:05:07 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"kind checking", ||
|
|
|
|
kind::check_crate(ty_cx, method_map, crate));
|
2013-06-14 00:38:17 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let reachable_map =
|
|
|
|
time(time_passes, ~"reachability checking", ||
|
|
|
|
reachable::find_reachable(ty_cx, method_map, crate));
|
2013-06-14 00:38:17 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(time_passes, ~"lint checking", ||
|
|
|
|
lint::check_crate(ty_cx, crate));
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
CrateAnalysis {
|
|
|
|
exp_map2: exp_map2,
|
|
|
|
ty_cx: ty_cx,
|
|
|
|
maps: astencode::Maps {
|
2013-06-14 00:38:17 -05:00
|
|
|
root_map: root_map,
|
|
|
|
method_map: method_map,
|
|
|
|
vtable_map: vtable_map,
|
|
|
|
write_guard_map: write_guard_map,
|
|
|
|
capture_map: capture_map
|
2013-07-27 01:49:38 -05:00
|
|
|
},
|
|
|
|
reachable: reachable_map
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 00:38:17 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub struct CrateTranslation {
|
|
|
|
context: ContextRef,
|
|
|
|
module: ModuleRef,
|
|
|
|
link: LinkMeta
|
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
/// Run the translation phase to LLVM, after which the AST and analysis can
|
|
|
|
/// be discarded.
|
|
|
|
pub fn phase_4_translate_to_llvm(sess: Session,
|
|
|
|
crate: @ast::Crate,
|
|
|
|
analysis: &CrateAnalysis,
|
|
|
|
outputs: &OutputFilenames) -> CrateTranslation {
|
|
|
|
time(sess.time_passes(), ~"translation", ||
|
|
|
|
trans::base::trans_crate(sess, crate, analysis,
|
|
|
|
&outputs.obj_filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run LLVM itself, producing a bitcode file, assembly file or object file
|
|
|
|
/// as a side effect.
|
|
|
|
pub fn phase_5_run_llvm_passes(sess: Session,
|
|
|
|
trans: &CrateTranslation,
|
|
|
|
outputs: &OutputFilenames) {
|
2013-04-28 21:54:41 -05:00
|
|
|
|
2013-04-02 11:42:08 -05:00
|
|
|
// NB: Android hack
|
2013-08-01 05:39:37 -05:00
|
|
|
if sess.targ_cfg.os == session::os_android &&
|
2013-07-27 01:49:38 -05:00
|
|
|
(sess.opts.output_type == link::output_type_object ||
|
|
|
|
sess.opts.output_type == link::output_type_exe) {
|
2013-03-15 03:32:39 -05:00
|
|
|
let output_type = link::output_type_assembly;
|
|
|
|
let obj_filename = outputs.obj_filename.with_filetype("s");
|
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
time(sess.time_passes(), ~"LLVM passes", ||
|
|
|
|
link::write::run_passes(sess,
|
|
|
|
trans.context,
|
|
|
|
trans.module,
|
|
|
|
output_type,
|
2013-06-13 23:25:18 -05:00
|
|
|
&obj_filename));
|
2013-03-15 03:32:39 -05:00
|
|
|
|
|
|
|
link::write::run_ndk(sess, &obj_filename, &outputs.obj_filename);
|
|
|
|
} else {
|
2013-07-27 01:49:38 -05:00
|
|
|
time(sess.time_passes(), ~"LLVM passes", ||
|
|
|
|
link::write::run_passes(sess,
|
|
|
|
trans.context,
|
|
|
|
trans.module,
|
|
|
|
sess.opts.output_type,
|
2013-06-13 23:25:18 -05:00
|
|
|
&outputs.obj_filename));
|
2013-03-15 03:32:39 -05:00
|
|
|
}
|
2013-07-27 01:49:38 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
/// Run the linker on any artifacts that resulted from the LLVM run.
|
|
|
|
/// This should produce either a finished executable or library.
|
|
|
|
pub fn phase_6_link_output(sess: Session,
|
|
|
|
trans: &CrateTranslation,
|
|
|
|
outputs: &OutputFilenames) {
|
|
|
|
time(sess.time_passes(), ~"linking", ||
|
2012-09-18 13:46:39 -05:00
|
|
|
link::link_binary(sess,
|
|
|
|
&outputs.obj_filename,
|
2013-07-27 01:49:38 -05:00
|
|
|
&outputs.out_filename,
|
|
|
|
trans.link));
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub fn stop_after_phase_3(sess: Session) -> bool {
|
|
|
|
if sess.opts.no_trans {
|
|
|
|
debug!("invoked with --no-trans, returning early from compile_input");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub fn stop_after_phase_1(sess: Session) -> bool {
|
|
|
|
if sess.opts.parse_only {
|
|
|
|
debug!("invoked with --parse-only, returning early from compile_input");
|
|
|
|
return true;
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
2013-07-27 01:49:38 -05:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-22 19:19:13 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
pub fn stop_after_phase_5(sess: Session) -> bool {
|
|
|
|
if sess.opts.output_type != link::output_type_exe {
|
|
|
|
debug!("not building executable, returning early from compile_input");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.is_static && *sess.building_library {
|
|
|
|
debug!("building static library, returning early from compile_input");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.jit {
|
|
|
|
debug!("running JIT, returning early from compile_input");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-01-22 19:19:13 -06:00
|
|
|
}
|
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
#[fixed_stack_segment]
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
|
2013-01-29 17:16:07 -06:00
|
|
|
outdir: &Option<Path>, output: &Option<Path>) {
|
2013-07-28 12:56:05 -05:00
|
|
|
// We need nested scopes here, because the intermediate results can keep
|
|
|
|
// large chunks of memory alive and we want to free them as soon as
|
|
|
|
// possible to keep the peak memory usage low
|
2013-07-30 17:40:19 -05:00
|
|
|
let (outputs, trans) = {
|
2013-07-28 12:56:05 -05:00
|
|
|
let expanded_crate = {
|
|
|
|
let crate = phase_1_parse_input(sess, cfg.clone(), input);
|
|
|
|
if stop_after_phase_1(sess) { return; }
|
|
|
|
phase_2_configure_and_expand(sess, cfg, crate)
|
|
|
|
};
|
|
|
|
let analysis = phase_3_run_analysis_passes(sess, expanded_crate);
|
|
|
|
if stop_after_phase_3(sess) { return; }
|
2013-07-30 17:40:19 -05:00
|
|
|
let outputs = build_output_filenames(input, outdir, output, [], sess);
|
|
|
|
let trans = phase_4_translate_to_llvm(sess, expanded_crate,
|
|
|
|
&analysis, outputs);
|
|
|
|
(outputs, trans)
|
2013-07-28 12:56:05 -05:00
|
|
|
};
|
2013-07-27 01:49:38 -05:00
|
|
|
phase_5_run_llvm_passes(sess, &trans, outputs);
|
|
|
|
if stop_after_phase_5(sess) { return; }
|
|
|
|
phase_6_link_output(sess, &trans, outputs);
|
2012-01-17 09:42:45 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn pretty_print_input(sess: Session, cfg: ast::CrateConfig, input: &input,
|
2013-01-29 17:16:07 -06:00
|
|
|
ppm: pp_mode) {
|
2013-07-27 01:49:38 -05:00
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
fn ann_paren_for_expr(node: pprust::ann_node) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match node {
|
2012-08-25 20:38:21 -05:00
|
|
|
pprust::node_expr(s, _) => pprust::popen(s),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
fn ann_typed_post(tcx: ty::ctxt, node: pprust::ann_node) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match node {
|
2012-08-03 21:59:04 -05:00
|
|
|
pprust::node_expr(s, expr) => {
|
2011-12-20 04:17:13 -06:00
|
|
|
pp::space(s.s);
|
2013-05-19 00:07:44 -05:00
|
|
|
pp::word(s.s, "as");
|
2011-12-20 04:17:13 -06:00
|
|
|
pp::space(s.s);
|
|
|
|
pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
|
|
|
|
pprust::pclose(s);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn ann_identified_post(node: pprust::ann_node) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match node {
|
2012-08-03 21:59:04 -05:00
|
|
|
pprust::node_item(s, item) => {
|
2011-12-20 04:17:13 -06:00
|
|
|
pp::space(s.s);
|
2013-08-17 21:47:54 -05:00
|
|
|
pprust::synth_comment(s, item.id.to_str());
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
pprust::node_block(s, ref blk) => {
|
2011-12-20 04:17:13 -06:00
|
|
|
pp::space(s.s);
|
2012-12-04 23:13:02 -06:00
|
|
|
pprust::synth_comment(
|
2013-08-17 21:47:54 -05:00
|
|
|
s, ~"block " + blk.id.to_str());
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
pprust::node_expr(s, expr) => {
|
2011-12-20 04:17:13 -06:00
|
|
|
pp::space(s.s);
|
2013-08-17 21:47:54 -05:00
|
|
|
pprust::synth_comment(s, expr.id.to_str());
|
2011-12-20 04:17:13 -06:00
|
|
|
pprust::pclose(s);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
pprust::node_pat(s, pat) => {
|
2012-05-18 21:06:56 -05:00
|
|
|
pp::space(s.s);
|
2013-08-17 21:47:54 -05:00
|
|
|
pprust::synth_comment(s, ~"pat " + pat.id.to_str());
|
2012-05-18 21:06:56 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let crate = phase_1_parse_input(sess, cfg.clone(), input);
|
|
|
|
|
|
|
|
let (crate, is_expanded) = match ppm {
|
|
|
|
ppm_expanded | ppm_expanded_identified | ppm_typed => {
|
|
|
|
(phase_2_configure_and_expand(sess, cfg, crate), true)
|
|
|
|
}
|
|
|
|
_ => (crate, false)
|
2012-01-17 09:42:45 -06:00
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
let annotation = match ppm {
|
|
|
|
ppm_identified | ppm_expanded_identified => {
|
|
|
|
pprust::pp_ann {
|
|
|
|
pre: ann_paren_for_expr,
|
|
|
|
post: ann_identified_post
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ppm_typed => {
|
|
|
|
let analysis = phase_3_run_analysis_passes(sess, crate);
|
|
|
|
pprust::pp_ann {
|
|
|
|
pre: ann_paren_for_expr,
|
|
|
|
post: |a| ann_typed_post(analysis.ty_cx, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => pprust::no_ann()
|
2012-06-28 17:29:15 -05:00
|
|
|
};
|
2013-07-27 01:49:38 -05:00
|
|
|
|
2012-11-12 20:24:56 -06:00
|
|
|
let src = sess.codemap.get_filemap(source_name(input)).src;
|
2013-06-12 12:02:55 -05:00
|
|
|
do io::with_str_reader(src) |rdr| {
|
2013-05-14 19:27:27 -05:00
|
|
|
pprust::print_crate(sess.codemap, token::get_ident_interner(),
|
2013-07-27 01:49:38 -05:00
|
|
|
sess.span_diagnostic, crate,
|
2012-05-09 21:41:24 -05:00
|
|
|
source_name(input),
|
2013-07-27 01:49:38 -05:00
|
|
|
rdr, io::stdout(),
|
|
|
|
annotation, is_expanded);
|
2012-02-29 23:23:50 -06:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-03-10 10:02:16 -05:00
|
|
|
pub fn get_os(triple: &str) -> Option<session::os> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(name, os) in os_names.iter() {
|
2013-06-15 02:43:19 -05:00
|
|
|
if triple.contains(name) { return Some(os) }
|
|
|
|
}
|
|
|
|
None
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2013-06-15 02:43:19 -05:00
|
|
|
static os_names : &'static [(&'static str, session::os)] = &'static [
|
2013-06-15 13:26:02 -05:00
|
|
|
("mingw32", session::os_win32),
|
2013-06-15 02:43:19 -05:00
|
|
|
("win32", session::os_win32),
|
|
|
|
("darwin", session::os_macos),
|
|
|
|
("android", session::os_android),
|
|
|
|
("linux", session::os_linux),
|
|
|
|
("freebsd", session::os_freebsd)];
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-03-13 21:25:28 -05:00
|
|
|
pub fn get_arch(triple: &str) -> Option<abi::Architecture> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(arch, abi) in architecture_abis.iter() {
|
2013-06-15 02:43:19 -05:00
|
|
|
if triple.contains(arch) { return Some(abi) }
|
|
|
|
}
|
|
|
|
None
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2013-06-15 02:43:19 -05:00
|
|
|
static architecture_abis : &'static [(&'static str, abi::Architecture)] = &'static [
|
|
|
|
("i386", abi::X86),
|
|
|
|
("i486", abi::X86),
|
|
|
|
("i586", abi::X86),
|
|
|
|
("i686", abi::X86),
|
|
|
|
("i786", abi::X86),
|
|
|
|
|
|
|
|
("x86_64", abi::X86_64),
|
|
|
|
|
|
|
|
("arm", abi::Arm),
|
|
|
|
("xscale", abi::Arm),
|
|
|
|
|
|
|
|
("mips", abi::Mips)];
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub fn build_target_config(sopts: @session::options,
|
2013-02-04 16:02:01 -06:00
|
|
|
demitter: diagnostic::Emitter)
|
2013-01-29 17:16:07 -06:00
|
|
|
-> @session::config {
|
2012-08-06 14:34:08 -05:00
|
|
|
let os = match get_os(sopts.target_triple) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(os) => os,
|
|
|
|
None => early_error(demitter, ~"unknown operating system")
|
2012-01-14 01:03:37 -06:00
|
|
|
};
|
2012-08-06 14:34:08 -05:00
|
|
|
let arch = match get_arch(sopts.target_triple) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(arch) => arch,
|
|
|
|
None => early_error(demitter,
|
2012-08-03 21:59:04 -05:00
|
|
|
~"unknown architecture: " + sopts.target_triple)
|
2012-01-14 01:03:37 -06:00
|
|
|
};
|
2012-08-06 14:34:08 -05:00
|
|
|
let (int_type, uint_type, float_type) = match arch {
|
2013-03-13 21:25:28 -05:00
|
|
|
abi::X86 => (ast::ty_i32, ast::ty_u32, ast::ty_f64),
|
|
|
|
abi::X86_64 => (ast::ty_i64, ast::ty_u64, ast::ty_f64),
|
|
|
|
abi::Arm => (ast::ty_i32, ast::ty_u32, ast::ty_f64),
|
|
|
|
abi::Mips => (ast::ty_i32, ast::ty_u32, ast::ty_f64)
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-08-06 14:34:08 -05:00
|
|
|
let target_strs = match arch {
|
2013-03-13 21:25:28 -05:00
|
|
|
abi::X86 => x86::get_target_strs(os),
|
|
|
|
abi::X86_64 => x86_64::get_target_strs(os),
|
|
|
|
abi::Arm => arm::get_target_strs(os),
|
|
|
|
abi::Mips => mips::get_target_strs(os)
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2013-02-19 01:40:42 -06:00
|
|
|
let target_cfg = @session::config {
|
|
|
|
os: os,
|
|
|
|
arch: arch,
|
|
|
|
target_strs: target_strs,
|
|
|
|
int_type: int_type,
|
|
|
|
uint_type: uint_type,
|
|
|
|
float_type: float_type
|
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return target_cfg;
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:50:23 -05:00
|
|
|
pub fn host_triple() -> ~str {
|
|
|
|
// Get the host triple out of the build environment. This ensures that our
|
|
|
|
// idea of the host triple is the same as for the set of libraries we've
|
|
|
|
// actually built. We can't just take LLVM's host triple because they
|
|
|
|
// normalize all ix86 architectures to i386.
|
|
|
|
//
|
|
|
|
// Instead of grabbing the host triple (for the current host), we grab (at
|
|
|
|
// compile time) the target triple that this rustc is built with and
|
|
|
|
// calling that (at runtime) the host triple.
|
|
|
|
(env!("CFG_COMPILER_TRIPLE")).to_owned()
|
|
|
|
}
|
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
pub fn build_session_options(binary: @str,
|
2013-01-29 17:16:07 -06:00
|
|
|
matches: &getopts::Matches,
|
2013-02-04 16:02:01 -06:00
|
|
|
demitter: diagnostic::Emitter)
|
2013-01-29 17:16:07 -06:00
|
|
|
-> @session::options {
|
2013-05-19 00:07:44 -05:00
|
|
|
let crate_type = if opt_present(matches, "lib") {
|
2011-12-20 04:17:13 -06:00
|
|
|
session::lib_crate
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "bin") {
|
2011-12-20 04:17:13 -06:00
|
|
|
session::bin_crate
|
|
|
|
} else {
|
|
|
|
session::unknown_crate
|
|
|
|
};
|
2013-05-19 00:07:44 -05:00
|
|
|
let parse_only = opt_present(matches, "parse-only");
|
|
|
|
let no_trans = opt_present(matches, "no-trans");
|
2012-04-12 19:30:52 -05:00
|
|
|
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
let lint_levels = [lint::allow, lint::warn,
|
|
|
|
lint::deny, lint::forbid];
|
|
|
|
let mut lint_opts = ~[];
|
2012-04-12 19:30:52 -05:00
|
|
|
let lint_dict = lint::get_lint_dict();
|
2013-08-03 11:45:23 -05:00
|
|
|
for level in lint_levels.iter() {
|
2012-09-19 18:55:01 -05:00
|
|
|
let level_name = lint::level_to_str(*level);
|
2013-04-23 04:08:13 -05:00
|
|
|
|
2013-04-23 11:20:55 -05:00
|
|
|
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
|
2013-08-07 21:21:36 -05:00
|
|
|
// to_ascii_move and to_str_move to not do a unnecessary copy.
|
2013-06-11 06:37:22 -05:00
|
|
|
let level_short = level_name.slice_chars(0, 1);
|
2013-04-23 11:20:55 -05:00
|
|
|
let level_short = level_short.to_ascii().to_upper().to_str_ascii();
|
2012-07-31 18:38:41 -05:00
|
|
|
let flags = vec::append(getopts::opt_strs(matches, level_short),
|
|
|
|
getopts::opt_strs(matches, level_name));
|
2013-08-03 11:45:23 -05:00
|
|
|
for lint_name in flags.iter() {
|
2013-06-11 06:46:40 -05:00
|
|
|
let lint_name = lint_name.replace("-", "_");
|
2013-06-12 12:02:55 -05:00
|
|
|
match lint_dict.find_equiv(&lint_name) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
early_error(demitter, fmt!("unknown %s flag: %s",
|
2013-03-22 21:26:41 -05:00
|
|
|
level_name, lint_name));
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(lint) => {
|
2012-09-26 19:33:34 -05:00
|
|
|
lint_opts.push((lint.lint, *level));
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
let mut debugging_opts = 0u;
|
2013-05-19 00:07:44 -05:00
|
|
|
let debug_flags = getopts::opt_strs(matches, "Z");
|
2012-05-17 23:53:49 -05:00
|
|
|
let debug_map = session::debugging_opts_map();
|
2013-08-03 11:45:23 -05:00
|
|
|
for debug_flag in debug_flags.iter() {
|
2012-05-17 23:53:49 -05:00
|
|
|
let mut this_bit = 0u;
|
2013-08-03 11:45:23 -05:00
|
|
|
for tuple in debug_map.iter() {
|
2013-03-20 00:17:42 -05:00
|
|
|
let (name, bit) = match *tuple { (ref a, _, b) => (a, b) };
|
|
|
|
if name == debug_flag { this_bit = bit; break; }
|
2012-05-17 23:53:49 -05:00
|
|
|
}
|
|
|
|
if this_bit == 0u {
|
2012-09-19 18:55:01 -05:00
|
|
|
early_error(demitter, fmt!("unknown debug flag: %s", *debug_flag))
|
2012-05-17 23:53:49 -05:00
|
|
|
}
|
|
|
|
debugging_opts |= this_bit;
|
|
|
|
}
|
2013-08-14 20:41:40 -05:00
|
|
|
|
2012-07-25 14:06:03 -05:00
|
|
|
if debugging_opts & session::debug_llvm != 0 {
|
2013-08-14 20:41:40 -05:00
|
|
|
set_llvm_debug();
|
|
|
|
|
|
|
|
fn set_llvm_debug() {
|
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
unsafe { llvm::LLVMSetDebug(1); }
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-07-25 14:06:03 -05:00
|
|
|
}
|
2012-05-17 23:53:49 -05:00
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
let output_type =
|
|
|
|
if parse_only || no_trans {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_none
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "S") &&
|
|
|
|
opt_present(matches, "emit-llvm") {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_llvm_assembly
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "S") {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_assembly
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "c") {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_object
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "emit-llvm") {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_bitcode
|
|
|
|
} else { link::output_type_exe };
|
2013-08-04 16:59:36 -05:00
|
|
|
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot").map_move(|m| @Path(m));
|
2013-08-08 21:16:00 -05:00
|
|
|
let target = getopts::opt_maybe_str(matches, "target").unwrap_or_default(host_triple());
|
|
|
|
let target_cpu = getopts::opt_maybe_str(matches, "target-cpu").unwrap_or_default(~"generic");
|
|
|
|
let target_feature = getopts::opt_maybe_str(matches, "target-feature").unwrap_or_default(~"");
|
2013-05-19 00:07:44 -05:00
|
|
|
let save_temps = getopts::opt_present(matches, "save-temps");
|
2012-09-07 14:06:42 -05:00
|
|
|
let opt_level = {
|
|
|
|
if (debugging_opts & session::no_opt) != 0 {
|
|
|
|
No
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "O") {
|
|
|
|
if opt_present(matches, "opt-level") {
|
2012-07-14 00:57:48 -05:00
|
|
|
early_error(demitter, ~"-O and --opt-level both provided");
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2012-08-21 19:22:45 -05:00
|
|
|
Default
|
2013-05-19 00:07:44 -05:00
|
|
|
} else if opt_present(matches, "opt-level") {
|
|
|
|
match getopts::opt_str(matches, "opt-level") {
|
2012-08-21 19:22:45 -05:00
|
|
|
~"0" => No,
|
|
|
|
~"1" => Less,
|
|
|
|
~"2" => Default,
|
|
|
|
~"3" => Aggressive,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2013-05-27 19:45:16 -05:00
|
|
|
early_error(demitter, ~"optimization level needs to be between 0-3")
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 14:06:42 -05:00
|
|
|
} else { No }
|
|
|
|
};
|
2013-02-07 17:14:17 -06:00
|
|
|
let gc = debugging_opts & session::gc != 0;
|
|
|
|
let jit = debugging_opts & session::jit != 0;
|
|
|
|
let extra_debuginfo = debugging_opts & session::extra_debug_info != 0;
|
|
|
|
let debuginfo = debugging_opts & session::debug_info != 0 ||
|
|
|
|
extra_debuginfo;
|
2013-08-13 09:31:42 -05:00
|
|
|
|
|
|
|
// If debugging info is generated, do not collapse monomorphized function instances.
|
|
|
|
// Functions with equivalent llvm code still need separate debugging descriptions because names
|
|
|
|
// might differ.
|
|
|
|
if debuginfo {
|
|
|
|
debugging_opts |= session::no_monomorphic_collapse;
|
|
|
|
}
|
|
|
|
|
2013-05-10 17:15:06 -05:00
|
|
|
let statik = debugging_opts & session::statik != 0;
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2013-05-19 00:07:44 -05:00
|
|
|
let addl_lib_search_paths = getopts::opt_strs(matches, "L").map(|s| Path(*s));
|
|
|
|
let linker = getopts::opt_maybe_str(matches, "linker");
|
|
|
|
let linker_args = getopts::opt_strs(matches, "link-args").flat_map( |a| {
|
2013-08-09 22:09:47 -05:00
|
|
|
a.split_iter(' ').map(|arg| arg.to_owned()).collect()
|
2013-04-28 19:57:49 -05:00
|
|
|
});
|
|
|
|
|
2013-05-19 00:07:44 -05:00
|
|
|
let cfg = parse_cfgspecs(getopts::opt_strs(matches, "cfg"), demitter);
|
|
|
|
let test = opt_present(matches, "test");
|
2013-03-04 22:12:23 -06:00
|
|
|
let android_cross_path = getopts::opt_maybe_str(
|
2013-05-19 00:07:44 -05:00
|
|
|
matches, "android-cross-path");
|
2013-04-28 21:54:41 -05:00
|
|
|
|
2013-05-29 03:08:20 -05:00
|
|
|
let custom_passes = match getopts::opt_maybe_str(matches, "passes") {
|
|
|
|
None => ~[],
|
|
|
|
Some(s) => {
|
2013-08-09 22:09:47 -05:00
|
|
|
s.split_iter(|c: char| c == ' ' || c == ',').map(|s| {
|
2013-06-09 08:10:50 -05:00
|
|
|
s.trim().to_owned()
|
|
|
|
}).collect()
|
2013-05-29 03:08:20 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
let sopts = @session::options {
|
|
|
|
crate_type: crate_type,
|
2013-05-10 17:15:06 -05:00
|
|
|
is_static: statik,
|
2013-02-19 01:40:42 -06:00
|
|
|
gc: gc,
|
|
|
|
optimize: opt_level,
|
2013-05-29 03:08:20 -05:00
|
|
|
custom_passes: custom_passes,
|
2013-02-19 01:40:42 -06:00
|
|
|
debuginfo: debuginfo,
|
|
|
|
extra_debuginfo: extra_debuginfo,
|
|
|
|
lint_opts: lint_opts,
|
|
|
|
save_temps: save_temps,
|
|
|
|
jit: jit,
|
|
|
|
output_type: output_type,
|
2013-05-27 19:45:16 -05:00
|
|
|
addl_lib_search_paths: @mut addl_lib_search_paths,
|
2013-05-02 16:12:47 -05:00
|
|
|
linker: linker,
|
2013-04-28 19:57:49 -05:00
|
|
|
linker_args: linker_args,
|
2013-02-19 01:40:42 -06:00
|
|
|
maybe_sysroot: sysroot_opt,
|
|
|
|
target_triple: target,
|
2013-08-08 21:16:00 -05:00
|
|
|
target_cpu: target_cpu,
|
2013-04-22 06:54:12 -05:00
|
|
|
target_feature: target_feature,
|
2013-02-19 01:40:42 -06:00
|
|
|
cfg: cfg,
|
|
|
|
binary: binary,
|
|
|
|
test: test,
|
|
|
|
parse_only: parse_only,
|
|
|
|
no_trans: no_trans,
|
2013-03-04 22:12:23 -06:00
|
|
|
debugging_opts: debugging_opts,
|
|
|
|
android_cross_path: android_cross_path
|
2013-02-19 01:40:42 -06:00
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return sopts;
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub fn build_session(sopts: @session::options,
|
2013-02-04 16:02:01 -06:00
|
|
|
demitter: diagnostic::Emitter) -> Session {
|
2012-11-12 20:24:56 -06:00
|
|
|
let codemap = @codemap::CodeMap::new();
|
2012-02-17 16:15:09 -06:00
|
|
|
let diagnostic_handler =
|
2012-08-20 14:23:37 -05:00
|
|
|
diagnostic::mk_handler(Some(demitter));
|
2012-02-17 16:15:09 -06:00
|
|
|
let span_diagnostic_handler =
|
|
|
|
diagnostic::mk_span_handler(diagnostic_handler, codemap);
|
2012-03-21 17:56:20 -05:00
|
|
|
build_session_(sopts, codemap, demitter, span_diagnostic_handler)
|
2012-02-17 16:15:09 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub fn build_session_(sopts: @session::options,
|
|
|
|
cm: @codemap::CodeMap,
|
2013-02-04 16:02:01 -06:00
|
|
|
demitter: diagnostic::Emitter,
|
2013-08-11 12:26:59 -05:00
|
|
|
span_diagnostic_handler: @mut diagnostic::span_handler)
|
2013-01-29 17:16:07 -06:00
|
|
|
-> Session {
|
2012-01-14 01:18:01 -06:00
|
|
|
let target_cfg = build_target_config(sopts, demitter);
|
2012-07-18 18:18:02 -05:00
|
|
|
let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler,
|
|
|
|
cm);
|
2013-05-14 19:27:27 -05:00
|
|
|
let cstore = @mut cstore::mk_cstore(token::get_ident_interner());
|
2011-12-20 04:17:13 -06:00
|
|
|
let filesearch = filesearch::mk_filesearch(
|
2013-04-17 11:15:37 -05:00
|
|
|
&sopts.maybe_sysroot,
|
2011-12-20 04:17:13 -06:00
|
|
|
sopts.target_triple,
|
2013-05-27 19:45:16 -05:00
|
|
|
sopts.addl_lib_search_paths);
|
2013-02-04 16:02:01 -06:00
|
|
|
@Session_ {
|
|
|
|
targ_cfg: target_cfg,
|
|
|
|
opts: sopts,
|
|
|
|
cstore: cstore,
|
|
|
|
parse_sess: p_s,
|
|
|
|
codemap: cm,
|
|
|
|
// For a library crate, this is always none
|
2013-04-09 03:16:06 -05:00
|
|
|
entry_fn: @mut None,
|
|
|
|
entry_type: @mut None,
|
2013-02-04 16:02:01 -06:00
|
|
|
span_diagnostic: span_diagnostic_handler,
|
|
|
|
filesearch: filesearch,
|
|
|
|
building_library: @mut false,
|
|
|
|
working_dir: os::getcwd(),
|
2013-04-30 00:15:17 -05:00
|
|
|
lints: @mut HashMap::new(),
|
2013-02-04 16:02:01 -06:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
pub fn parse_pretty(sess: Session, name: &str) -> pp_mode {
|
2012-08-06 14:34:08 -05:00
|
|
|
match name {
|
2013-04-17 11:15:37 -05:00
|
|
|
&"normal" => ppm_normal,
|
|
|
|
&"expanded" => ppm_expanded,
|
|
|
|
&"typed" => ppm_typed,
|
|
|
|
&"expanded,identified" => ppm_expanded_identified,
|
|
|
|
&"identified" => ppm_identified,
|
2012-08-02 17:42:56 -05:00
|
|
|
_ => {
|
2013-05-19 00:07:44 -05:00
|
|
|
sess.fatal("argument to `pretty` must be one of `normal`, \
|
|
|
|
`expanded`, `typed`, `identified`, \
|
|
|
|
or `expanded,identified`");
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
2012-03-12 12:19:35 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-10-11 18:54:31 -05:00
|
|
|
// rustc command line options
|
2013-01-29 17:16:07 -06:00
|
|
|
pub fn optgroups() -> ~[getopts::groups::OptGroup] {
|
2012-10-11 18:54:31 -05:00
|
|
|
~[
|
2013-04-28 19:57:49 -05:00
|
|
|
optflag("", "bin", "Compile an executable crate (default)"),
|
|
|
|
optflag("c", "", "Compile and assemble, but do not link"),
|
|
|
|
optmulti("", "cfg", "Configure the compilation
|
|
|
|
environment", "SPEC"),
|
|
|
|
optflag("", "emit-llvm",
|
2013-08-05 17:49:01 -05:00
|
|
|
"Produce an LLVM assembly file if used with -S option;
|
|
|
|
produce an LLVM bitcode file otherwise"),
|
2013-04-28 19:57:49 -05:00
|
|
|
optflag("h", "help","Display this message"),
|
|
|
|
optmulti("L", "", "Add a directory to the library search path",
|
|
|
|
"PATH"),
|
|
|
|
optflag("", "lib", "Compile a library crate"),
|
2013-05-02 16:12:47 -05:00
|
|
|
optopt("", "linker", "Program to use for linking instead of the default.", "LINKER"),
|
2013-05-02 11:46:58 -05:00
|
|
|
optmulti("", "link-args", "FLAGS is a space-separated list of flags
|
2013-04-28 19:57:49 -05:00
|
|
|
passed to the linker", "FLAGS"),
|
|
|
|
optflag("", "ls", "List the symbols defined by a library crate"),
|
|
|
|
optflag("", "no-trans",
|
|
|
|
"Run all passes except translation; no output"),
|
|
|
|
optflag("O", "", "Equivalent to --opt-level=2"),
|
|
|
|
optopt("o", "", "Write output to <filename>", "FILENAME"),
|
|
|
|
optopt("", "opt-level",
|
|
|
|
"Optimize with possible levels 0-3", "LEVEL"),
|
2013-05-29 03:08:20 -05:00
|
|
|
optopt("", "passes", "Comma or space separated list of pass names to use. \
|
|
|
|
Overrides the default passes for optimization levels,\n\
|
|
|
|
a value of \"list\" will list the available passes.", "NAMES"),
|
2013-04-28 19:57:49 -05:00
|
|
|
optopt( "", "out-dir",
|
|
|
|
"Write output to compiler-chosen filename
|
|
|
|
in <dir>", "DIR"),
|
|
|
|
optflag("", "parse-only",
|
|
|
|
"Parse only; do not compile, assemble, or link"),
|
|
|
|
optflagopt("", "pretty",
|
|
|
|
"Pretty-print the input instead of compiling;
|
2012-10-11 18:54:31 -05:00
|
|
|
valid types are: normal (un-annotated source),
|
|
|
|
expanded (crates expanded),
|
|
|
|
typed (crates expanded, with type annotations),
|
|
|
|
or identified (fully parenthesized,
|
2013-04-28 19:57:49 -05:00
|
|
|
AST nodes and blocks with IDs)", "TYPE"),
|
|
|
|
optflag("S", "", "Compile only; do not assemble or link"),
|
|
|
|
optflag("", "save-temps",
|
|
|
|
"Write intermediate files (.bc, .opt.bc, .o)
|
2012-10-11 18:54:31 -05:00
|
|
|
in addition to normal output"),
|
2013-04-28 19:57:49 -05:00
|
|
|
optopt("", "sysroot",
|
|
|
|
"Override the system root", "PATH"),
|
|
|
|
optflag("", "test", "Build a test harness"),
|
|
|
|
optopt("", "target",
|
|
|
|
"Target triple cpu-manufacturer-kernel[-os]
|
2013-03-28 15:44:46 -05:00
|
|
|
to compile for (see chapter 3.4 of http://www.sourceware.org/autobook/
|
2013-08-08 21:16:00 -05:00
|
|
|
for details)", "TRIPLE"),
|
|
|
|
optopt("", "target-cpu",
|
|
|
|
"Select target processor (llc -mcpu=help
|
|
|
|
for details)", "CPU"),
|
2013-04-28 19:57:49 -05:00
|
|
|
optopt("", "target-feature",
|
|
|
|
"Target specific attributes (llc -mattr=help
|
2013-08-08 21:16:00 -05:00
|
|
|
for details)", "FEATURE"),
|
2013-04-28 19:57:49 -05:00
|
|
|
optopt("", "android-cross-path",
|
|
|
|
"The path to the Android NDK", "PATH"),
|
2013-05-24 14:32:30 -05:00
|
|
|
optflagopt("W", "warn",
|
2013-04-28 19:57:49 -05:00
|
|
|
"Set lint warnings", "OPT"),
|
|
|
|
optmulti("A", "allow",
|
|
|
|
"Set lint allowed", "OPT"),
|
|
|
|
optmulti("D", "deny",
|
|
|
|
"Set lint denied", "OPT"),
|
|
|
|
optmulti("F", "forbid",
|
|
|
|
"Set lint forbidden", "OPT"),
|
|
|
|
optmulti("Z", "", "Set internal debugging options", "FLAG"),
|
|
|
|
optflag( "v", "version",
|
|
|
|
"Print version info and exit"),
|
2012-10-11 18:54:31 -05:00
|
|
|
]
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
pub struct OutputFilenames {
|
|
|
|
out_filename: Path,
|
|
|
|
obj_filename: Path
|
|
|
|
}
|
2012-01-17 09:42:45 -06:00
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
pub fn build_output_filenames(input: &input,
|
2013-01-29 17:16:07 -06:00
|
|
|
odir: &Option<Path>,
|
|
|
|
ofile: &Option<Path>,
|
2013-07-19 06:51:37 -05:00
|
|
|
attrs: &[ast::Attribute],
|
2013-01-29 17:16:07 -06:00
|
|
|
sess: Session)
|
2013-07-27 01:49:38 -05:00
|
|
|
-> ~OutputFilenames {
|
2012-06-14 14:40:49 -05:00
|
|
|
let obj_path;
|
|
|
|
let out_path;
|
2012-01-12 10:59:49 -06:00
|
|
|
let sopts = sess.opts;
|
2011-12-20 04:17:13 -06:00
|
|
|
let stop_after_codegen =
|
2012-09-18 13:46:39 -05:00
|
|
|
sopts.output_type != link::output_type_exe ||
|
2013-02-19 01:40:42 -06:00
|
|
|
sopts.is_static && *sess.building_library;
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
let obj_suffix =
|
2012-08-06 14:34:08 -05:00
|
|
|
match sopts.output_type {
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_none => ~"none",
|
|
|
|
link::output_type_bitcode => ~"bc",
|
|
|
|
link::output_type_assembly => ~"s",
|
|
|
|
link::output_type_llvm_assembly => ~"ll",
|
2011-12-20 04:17:13 -06:00
|
|
|
// Object and exe output both use the '.o' extension here
|
2012-09-18 13:46:39 -05:00
|
|
|
link::output_type_object | link::output_type_exe => ~"o"
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
|
|
|
|
2012-08-24 17:28:43 -05:00
|
|
|
match *ofile {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2013-05-27 19:45:16 -05:00
|
|
|
// "-" as input file will cause the parser to read from stdin so we
|
|
|
|
// have to make up a name
|
|
|
|
// We want to toss everything after the final '.'
|
|
|
|
let dirpath = match *odir {
|
2013-07-02 14:47:32 -05:00
|
|
|
Some(ref d) => (*d).clone(),
|
2013-05-27 19:45:16 -05:00
|
|
|
None => match *input {
|
|
|
|
str_input(_) => os::getcwd(),
|
|
|
|
file_input(ref ifile) => (*ifile).dir_path()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut stem = match *input {
|
2013-08-03 18:59:24 -05:00
|
|
|
file_input(ref ifile) => (*ifile).filestem().unwrap().to_managed(),
|
2013-06-12 12:02:55 -05:00
|
|
|
str_input(_) => @"rust_out"
|
2013-05-27 19:45:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// If a linkage name meta is present, we use it as the link name
|
|
|
|
let linkage_metas = attr::find_linkage_metas(attrs);
|
|
|
|
if !linkage_metas.is_empty() {
|
|
|
|
// But if a linkage meta is present, that overrides
|
2013-08-09 22:49:29 -05:00
|
|
|
let maybe_name = linkage_metas.iter().find(|m| "name" == m.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
match maybe_name.chain(|m| m.value_str()) {
|
|
|
|
Some(s) => stem = s,
|
|
|
|
_ => ()
|
2013-05-27 19:45:16 -05:00
|
|
|
}
|
|
|
|
// If the name is missing, we just default to the filename
|
|
|
|
// version
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-05-27 19:45:16 -05:00
|
|
|
if *sess.building_library {
|
|
|
|
out_path = dirpath.push(os::dll_filename(stem));
|
|
|
|
obj_path = dirpath.push(stem).with_filetype(obj_suffix);
|
|
|
|
} else {
|
|
|
|
out_path = dirpath.push(stem);
|
|
|
|
obj_path = dirpath.push(stem).with_filetype(obj_suffix);
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-12-04 12:50:00 -06:00
|
|
|
Some(ref out_file) => {
|
2013-07-02 14:47:32 -05:00
|
|
|
out_path = (*out_file).clone();
|
2011-12-20 04:17:13 -06:00
|
|
|
obj_path = if stop_after_codegen {
|
2013-07-02 14:47:32 -05:00
|
|
|
(*out_file).clone()
|
2011-12-20 04:17:13 -06:00
|
|
|
} else {
|
2012-12-04 12:50:00 -06:00
|
|
|
(*out_file).with_filetype(obj_suffix)
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
if *sess.building_library {
|
2013-07-30 17:40:19 -05:00
|
|
|
sess.warn("ignoring specified output filename for library.");
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
if *odir != None {
|
2013-05-19 00:07:44 -05:00
|
|
|
sess.warn("ignoring --out-dir flag due to -o flag.");
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
|
2013-07-27 01:49:38 -05:00
|
|
|
~OutputFilenames {
|
2013-02-19 01:40:42 -06:00
|
|
|
out_filename: out_path,
|
|
|
|
obj_filename: obj_path
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn early_error(emitter: diagnostic::Emitter, msg: ~str) -> ! {
|
2012-08-20 14:23:37 -05:00
|
|
|
emitter(None, msg, diagnostic::fatal);
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn list_metadata(sess: Session, path: &Path, out: @io::Writer) {
|
2012-05-22 19:16:26 -05:00
|
|
|
metadata::loader::list_file_metadata(
|
2013-05-14 19:27:27 -05:00
|
|
|
token::get_ident_interner(),
|
2012-05-22 19:16:26 -05:00
|
|
|
session::sess_os_to_meta_os(sess.targ_cfg.os), path, out);
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-04-15 10:08:52 -05:00
|
|
|
mod test {
|
2013-05-22 21:59:22 -05:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use driver::driver::{build_configuration, build_session};
|
2013-08-13 19:06:27 -05:00
|
|
|
use driver::driver::{build_session_options, optgroups};
|
2013-01-08 21:37:25 -06:00
|
|
|
|
2013-05-18 14:39:17 -05:00
|
|
|
use extra::getopts::groups::getopts;
|
|
|
|
use extra::getopts;
|
2012-12-27 17:49:26 -06:00
|
|
|
use syntax::attr;
|
|
|
|
use syntax::diagnostic;
|
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
// When the user supplies --test we should implicitly supply --cfg test
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_switch_implies_cfg_test() {
|
2012-07-31 18:38:41 -05:00
|
|
|
let matches =
|
2013-05-27 18:04:00 -05:00
|
|
|
&match getopts([~"--test"], optgroups()) {
|
2013-05-29 18:59:33 -05:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => fail!("test_switch_implies_cfg_test: %s", getopts::fail_str(f))
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-08-29 21:24:43 -05:00
|
|
|
let sessopts = build_session_options(
|
2013-06-12 12:02:55 -05:00
|
|
|
@"rustc", matches, diagnostic::emit);
|
2012-03-21 17:56:20 -05:00
|
|
|
let sess = build_session(sessopts, diagnostic::emit);
|
2013-08-13 19:06:27 -05:00
|
|
|
let cfg = build_configuration(sess);
|
2013-05-23 11:39:10 -05:00
|
|
|
assert!((attr::contains_name(cfg, "test")));
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the user supplies --test and --cfg test, don't implicitly add
|
|
|
|
// another --cfg test
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_switch_implies_cfg_test_unless_cfg_test() {
|
2012-07-31 18:38:41 -05:00
|
|
|
let matches =
|
2013-05-23 11:39:10 -05:00
|
|
|
&match getopts([~"--test", ~"--cfg=test"], optgroups()) {
|
2013-05-29 18:59:33 -05:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
2013-05-09 06:52:07 -05:00
|
|
|
fail!("test_switch_implies_cfg_test_unless_cfg_test: %s", getopts::fail_str(f));
|
2012-07-14 00:57:48 -05:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-08-29 21:24:43 -05:00
|
|
|
let sessopts = build_session_options(
|
2013-06-12 12:02:55 -05:00
|
|
|
@"rustc", matches, diagnostic::emit);
|
2012-03-21 17:56:20 -05:00
|
|
|
let sess = build_session(sessopts, diagnostic::emit);
|
2013-08-13 19:06:27 -05:00
|
|
|
let cfg = build_configuration(sess);
|
2013-07-19 06:51:37 -05:00
|
|
|
let mut test_items = cfg.iter().filter(|m| "test" == m.name());
|
|
|
|
assert!(test_items.next().is_some());
|
|
|
|
assert!(test_items.next().is_none());
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|