2011-12-20 04:17:13 -06:00
|
|
|
// -*- rust -*-
|
|
|
|
import metadata::{creader, cstore};
|
2012-01-12 10:59:49 -06:00
|
|
|
import session::session;
|
2012-04-18 01:34:48 -05:00
|
|
|
import syntax::parse;
|
2011-12-20 04:17:13 -06:00
|
|
|
import syntax::{ast, codemap};
|
2012-03-22 20:16:22 -05:00
|
|
|
import syntax::attr;
|
2012-04-26 18:02:01 -05:00
|
|
|
import middle::{trans, resolve, freevars, kind, ty, typeck,
|
2012-03-06 04:33:25 -06:00
|
|
|
last_use, lint};
|
2011-12-20 04:17:13 -06:00
|
|
|
import syntax::print::{pp, pprust};
|
|
|
|
import util::{ppaux, filesearch};
|
|
|
|
import back::link;
|
|
|
|
import result::{ok, err};
|
2012-03-12 22:04:27 -05:00
|
|
|
import std::getopts;
|
2012-01-19 12:53:20 -06:00
|
|
|
import io::{reader_util, writer_util};
|
2011-12-20 04:17:13 -06:00
|
|
|
import getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
|
|
|
|
import back::{x86, x86_64};
|
|
|
|
|
2012-03-12 12:19:35 -05:00
|
|
|
enum pp_mode {ppm_normal, ppm_expanded, ppm_typed, ppm_identified,
|
|
|
|
ppm_expanded_identified }
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn default_configuration(sess: session, argv0: str, input: str) ->
|
2011-12-20 04:17:13 -06:00
|
|
|
ast::crate_cfg {
|
2012-01-26 03:52:08 -06:00
|
|
|
let libc = alt sess.targ_cfg.os {
|
|
|
|
session::os_win32 { "msvcrt.dll" }
|
|
|
|
session::os_macos { "libc.dylib" }
|
|
|
|
session::os_linux { "libc.so.6" }
|
|
|
|
session::os_freebsd { "libc.so.7" }
|
2012-04-24 04:13:25 -05:00
|
|
|
// _ { "libc.so" }
|
2012-01-26 03:52:08 -06:00
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
let mk = attr::mk_name_value_item_str;
|
|
|
|
|
2012-01-12 10:59:49 -06:00
|
|
|
let arch = alt sess.targ_cfg.arch {
|
2012-01-19 03:03:57 -06:00
|
|
|
session::arch_x86 { "x86" }
|
|
|
|
session::arch_x86_64 { "x86_64" }
|
|
|
|
session::arch_arm { "arm" }
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
ret [ // Target bindings.
|
2012-03-12 22:04:27 -05:00
|
|
|
mk("target_os", os::sysname()),
|
2011-12-20 04:17:13 -06:00
|
|
|
mk("target_arch", arch),
|
|
|
|
mk("target_libc", libc),
|
|
|
|
// Build bindings.
|
|
|
|
mk("build_compiler", argv0),
|
|
|
|
mk("build_input", input)];
|
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn build_configuration(sess: session, argv0: str, input: str) ->
|
2011-12-20 04:17:13 -06:00
|
|
|
ast::crate_cfg {
|
|
|
|
// Combine the configuration requested by the session (command line) with
|
|
|
|
// some default and generated configuration items
|
|
|
|
let default_cfg = default_configuration(sess, argv0, input);
|
2012-01-12 10:59:49 -06:00
|
|
|
let user_cfg = sess.opts.cfg;
|
2011-12-20 04:17:13 -06:00
|
|
|
// If the user wants a test runner, then add the test cfg
|
|
|
|
let gen_cfg =
|
|
|
|
{
|
2012-01-12 10:59:49 -06:00
|
|
|
if sess.opts.test && !attr::contains_name(user_cfg, "test")
|
2011-12-20 04:17:13 -06:00
|
|
|
{
|
|
|
|
[attr::mk_word_item("test")]
|
|
|
|
} else { [] }
|
|
|
|
};
|
|
|
|
ret user_cfg + gen_cfg + default_cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
|
|
|
|
fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
|
|
|
|
// FIXME: It would be nice to use the parser to parse all varieties of
|
|
|
|
// meta_item here. At the moment we just support the meta_word variant.
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut words = [];
|
2012-04-06 13:01:43 -05:00
|
|
|
for cfgspecs.each {|s| words += [attr::mk_word_item(s)]; }
|
2011-12-20 04:17:13 -06:00
|
|
|
ret words;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn input_is_stdin(filename: str) -> bool { filename == "-" }
|
|
|
|
|
2012-01-17 10:44:38 -06:00
|
|
|
fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
|
2012-01-26 03:52:08 -06:00
|
|
|
-> @ast::crate {
|
|
|
|
if !input_is_stdin(input) {
|
2012-04-18 01:34:48 -05:00
|
|
|
parse::parse_crate_from_file(input, cfg, sess.parse_sess)
|
2012-01-17 09:42:45 -06:00
|
|
|
} else {
|
2012-04-30 13:52:07 -05:00
|
|
|
let src = @str::from_bytes(io::stdin().read_whole_stream());
|
2012-04-18 01:34:48 -05:00
|
|
|
parse::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
|
2012-01-26 03:52:08 -06:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
|
|
|
|
if !do_it { ret thunk(); }
|
|
|
|
let start = std::time::precise_time_s();
|
|
|
|
let rv = thunk();
|
|
|
|
let end = std::time::precise_time_s();
|
2012-01-19 12:53:20 -06:00
|
|
|
io::stdout().write_str(#fmt("time: %3.3f s\t%s\n",
|
|
|
|
end - start, what));
|
2011-12-20 04:17:13 -06:00
|
|
|
ret rv;
|
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
enum compile_upto {
|
2012-01-19 19:56:05 -06:00
|
|
|
cu_parse,
|
|
|
|
cu_expand,
|
|
|
|
cu_typeck,
|
|
|
|
cu_no_trans,
|
|
|
|
cu_everything,
|
2012-01-17 09:42:45 -06:00
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn compile_upto(sess: session, cfg: ast::crate_cfg,
|
|
|
|
input: str, upto: compile_upto,
|
2012-01-31 19:05:20 -06:00
|
|
|
outputs: option<output_filenames>)
|
|
|
|
-> {crate: @ast::crate, tcx: option<ty::ctxt>} {
|
2012-01-12 10:59:49 -06:00
|
|
|
let time_passes = sess.opts.time_passes;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut crate = time(time_passes, "parsing",
|
|
|
|
bind parse_input(sess, cfg, input));
|
2012-01-26 03:52:08 -06:00
|
|
|
if upto == cu_parse { ret {crate: crate, tcx: none}; }
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-17 16:37:39 -06:00
|
|
|
sess.building_library = session::building_library(
|
|
|
|
sess.opts.crate_type, crate, sess.opts.test);
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
crate =
|
|
|
|
time(time_passes, "configuration",
|
|
|
|
bind front::config::strip_unconfigured_items(crate));
|
2012-01-05 19:30:00 -06:00
|
|
|
crate =
|
|
|
|
time(time_passes, "maybe building test harness",
|
|
|
|
bind front::test::modify_for_testing(sess, crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
crate =
|
|
|
|
time(time_passes, "expansion",
|
2012-03-27 16:03:57 -05:00
|
|
|
bind syntax::ext::expand::expand_crate(
|
|
|
|
sess.parse_sess, sess.opts.cfg, crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-26 03:52:08 -06:00
|
|
|
if upto == cu_expand { ret {crate: crate, tcx: none}; }
|
2012-01-26 17:20:29 -06:00
|
|
|
|
2012-01-26 17:21:16 -06:00
|
|
|
crate =
|
|
|
|
time(time_passes, "core injection",
|
|
|
|
bind front::core_inject::maybe_inject_libcore_ref(sess, crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
let ast_map =
|
|
|
|
time(time_passes, "ast indexing",
|
2012-03-07 05:21:08 -06:00
|
|
|
bind middle::ast_map::map_crate(sess, *crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
time(time_passes, "external crate/lib resolution",
|
|
|
|
bind creader::read_crates(sess, *crate));
|
|
|
|
let {def_map, exp_map, impl_map} =
|
|
|
|
time(time_passes, "resolution",
|
|
|
|
bind resolve::resolve_crate(sess, ast_map, crate));
|
|
|
|
let freevars =
|
|
|
|
time(time_passes, "freevar finding",
|
|
|
|
bind freevars::annotate_freevars(def_map, crate));
|
2012-03-09 18:39:54 -06:00
|
|
|
let region_map =
|
|
|
|
time(time_passes, "region resolution",
|
2012-03-11 14:05:17 -05:00
|
|
|
bind middle::region::resolve_crate(sess, def_map, crate));
|
2012-03-09 18:39:54 -06:00
|
|
|
let ty_cx = ty::mk_ctxt(sess, def_map, ast_map, freevars, region_map);
|
2012-03-08 09:17:59 -06:00
|
|
|
let (method_map, vtable_map) =
|
2012-01-02 09:50:51 -06:00
|
|
|
time(time_passes, "typechecking",
|
|
|
|
bind typeck::check_crate(ty_cx, impl_map, crate));
|
2012-01-26 05:26:14 -06:00
|
|
|
time(time_passes, "const checking",
|
2012-04-04 18:12:57 -05:00
|
|
|
bind middle::check_const::check_crate(sess, crate, ast_map, def_map,
|
2012-04-04 17:02:25 -05:00
|
|
|
method_map, ty_cx));
|
2012-01-17 09:42:45 -06:00
|
|
|
|
2012-01-26 03:52:08 -06:00
|
|
|
if upto == cu_typeck { ret {crate: crate, tcx: some(ty_cx)}; }
|
2012-01-17 09:42:45 -06:00
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
time(time_passes, "block-use checking",
|
|
|
|
bind middle::block_use::check_crate(ty_cx, crate));
|
2012-03-26 05:39:20 -05:00
|
|
|
time(time_passes, "loop checking",
|
2012-03-26 09:09:27 -05:00
|
|
|
bind middle::check_loop::check_crate(ty_cx, crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
time(time_passes, "alt checking",
|
|
|
|
bind middle::check_alt::check_crate(ty_cx, crate));
|
|
|
|
time(time_passes, "typestate checking",
|
|
|
|
bind middle::tstate::ck::check_crate(ty_cx, crate));
|
2012-04-26 18:02:01 -05:00
|
|
|
let _root_map = time(
|
|
|
|
time_passes, "borrow checking",
|
|
|
|
bind middle::borrowck::check_crate(ty_cx, method_map, crate));
|
2012-02-14 17:21:53 -06:00
|
|
|
let mutbl_map =
|
2011-12-20 04:17:13 -06:00
|
|
|
time(time_passes, "mutability checking",
|
2012-02-15 13:25:39 -06:00
|
|
|
bind middle::mutbl::check_crate(ty_cx, crate));
|
2012-03-09 19:38:57 -06:00
|
|
|
time(time_passes, "region checking",
|
|
|
|
bind middle::regionck::check_crate(ty_cx, crate));
|
2011-12-20 04:17:13 -06:00
|
|
|
let (copy_map, ref_map) =
|
|
|
|
time(time_passes, "alias checking",
|
|
|
|
bind middle::alias::check_crate(ty_cx, crate));
|
2012-03-23 10:05:53 -05:00
|
|
|
let (last_uses, spill_map) = time(time_passes, "last use finding",
|
2011-12-20 04:17:13 -06:00
|
|
|
bind last_use::find_last_uses(crate, def_map, ref_map, ty_cx));
|
|
|
|
time(time_passes, "kind checking",
|
|
|
|
bind kind::check_crate(ty_cx, method_map, last_uses, crate));
|
2012-01-19 06:29:50 -06:00
|
|
|
|
2012-01-21 11:20:22 -06:00
|
|
|
lint::check_crate(ty_cx, crate, sess.opts.lint_opts, time_passes);
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-26 03:52:08 -06:00
|
|
|
if upto == cu_no_trans { ret {crate: crate, tcx: some(ty_cx)}; }
|
2012-01-17 09:42:45 -06:00
|
|
|
let outputs = option::get(outputs);
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
let maps = {mutbl_map: mutbl_map, copy_map: copy_map,
|
|
|
|
last_uses: last_uses, impl_map: impl_map,
|
2012-03-23 10:05:53 -05:00
|
|
|
method_map: method_map, vtable_map: vtable_map,
|
|
|
|
spill_map: spill_map};
|
2012-02-14 17:21:53 -06:00
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
let (llmod, link_meta) =
|
|
|
|
time(time_passes, "translation",
|
2012-01-27 06:17:06 -06:00
|
|
|
bind trans::base::trans_crate(
|
2012-02-14 17:21:53 -06:00
|
|
|
sess, crate, ty_cx, outputs.obj_filename,
|
2012-03-06 04:33:25 -06:00
|
|
|
exp_map, maps));
|
2011-12-20 04:17:13 -06:00
|
|
|
time(time_passes, "LLVM passes",
|
|
|
|
bind link::write::run_passes(sess, llmod, outputs.obj_filename));
|
|
|
|
|
|
|
|
let stop_after_codegen =
|
2012-01-12 10:59:49 -06:00
|
|
|
sess.opts.output_type != link::output_type_exe ||
|
|
|
|
sess.opts.static && sess.building_library;
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-01-26 03:52:08 -06:00
|
|
|
if stop_after_codegen { ret {crate: crate, tcx: some(ty_cx)}; }
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-03-05 19:44:11 -06:00
|
|
|
time(time_passes, "linking",
|
2011-12-20 04:17:13 -06:00
|
|
|
bind link::link_binary(sess, outputs.obj_filename,
|
|
|
|
outputs.out_filename, link_meta));
|
2012-01-26 03:52:08 -06:00
|
|
|
ret {crate: crate, tcx: some(ty_cx)};
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn compile_input(sess: session, cfg: ast::crate_cfg, input: str,
|
2012-01-31 19:05:20 -06:00
|
|
|
outdir: option<str>, output: option<str>) {
|
2012-01-17 09:42:45 -06:00
|
|
|
|
|
|
|
let upto = if sess.opts.parse_only { cu_parse }
|
|
|
|
else if sess.opts.no_trans { cu_no_trans }
|
|
|
|
else { cu_everything };
|
|
|
|
let outputs = build_output_filenames(input, outdir, output, sess);
|
|
|
|
compile_upto(sess, cfg, input, upto, some(outputs));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
|
2011-12-20 04:17:13 -06:00
|
|
|
ppm: pp_mode) {
|
|
|
|
fn ann_paren_for_expr(node: pprust::ann_node) {
|
|
|
|
alt node { pprust::node_expr(s, expr) { pprust::popen(s); } _ { } }
|
|
|
|
}
|
|
|
|
fn ann_typed_post(tcx: ty::ctxt, node: pprust::ann_node) {
|
|
|
|
alt node {
|
|
|
|
pprust::node_expr(s, expr) {
|
|
|
|
pp::space(s.s);
|
|
|
|
pp::word(s.s, "as");
|
|
|
|
pp::space(s.s);
|
|
|
|
pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
|
|
|
|
pprust::pclose(s);
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn ann_identified_post(node: pprust::ann_node) {
|
|
|
|
alt node {
|
|
|
|
pprust::node_item(s, item) {
|
|
|
|
pp::space(s.s);
|
|
|
|
pprust::synth_comment(s, int::to_str(item.id, 10u));
|
|
|
|
}
|
|
|
|
pprust::node_block(s, blk) {
|
|
|
|
pp::space(s.s);
|
|
|
|
pprust::synth_comment(s,
|
|
|
|
"block " + int::to_str(blk.node.id, 10u));
|
|
|
|
}
|
|
|
|
pprust::node_expr(s, expr) {
|
|
|
|
pp::space(s.s);
|
|
|
|
pprust::synth_comment(s, int::to_str(expr.id, 10u));
|
|
|
|
pprust::pclose(s);
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the pretty printer needs to make a pass over the source
|
|
|
|
// to collect comments and literals, and we need to support reading
|
|
|
|
// from stdin, we're going to just suck the source into a string
|
|
|
|
// so both the parser and pretty-printer can use it.
|
2012-01-17 09:42:45 -06:00
|
|
|
let upto = alt ppm {
|
2012-03-12 12:19:35 -05:00
|
|
|
ppm_expanded | ppm_expanded_identified { cu_expand }
|
2012-01-19 00:37:22 -06:00
|
|
|
ppm_typed { cu_typeck }
|
2012-01-17 09:42:45 -06:00
|
|
|
_ { cu_parse }
|
|
|
|
};
|
2012-01-26 03:52:08 -06:00
|
|
|
let {crate, tcx} = compile_upto(sess, cfg, input, upto, none);
|
2011-12-20 04:17:13 -06:00
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut ann: pprust::pp_ann = pprust::no_ann();
|
2011-12-20 04:17:13 -06:00
|
|
|
alt ppm {
|
2012-01-19 00:37:22 -06:00
|
|
|
ppm_typed {
|
2012-01-17 09:42:45 -06:00
|
|
|
ann = {pre: ann_paren_for_expr,
|
|
|
|
post: bind ann_typed_post(option::get(tcx), _)};
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2012-03-12 12:19:35 -05:00
|
|
|
ppm_identified | ppm_expanded_identified {
|
2011-12-20 04:17:13 -06:00
|
|
|
ann = {pre: ann_paren_for_expr, post: ann_identified_post};
|
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ppm_expanded | ppm_normal {}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
2012-01-26 03:52:08 -06:00
|
|
|
let src = codemap::get_filemap(sess.codemap, input).src;
|
2012-02-29 23:23:50 -06:00
|
|
|
io::with_str_reader(*src) { |rdr|
|
|
|
|
pprust::print_crate(sess.codemap, sess.span_diagnostic, crate, input,
|
|
|
|
rdr, io::stdout(), ann);
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:03:37 -06:00
|
|
|
fn get_os(triple: str) -> option<session::os> {
|
2012-02-13 00:00:56 -06:00
|
|
|
ret if str::contains(triple, "win32") ||
|
|
|
|
str::contains(triple, "mingw32") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::os_win32)
|
2012-02-13 00:00:56 -06:00
|
|
|
} else if str::contains(triple, "darwin") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::os_macos)
|
2012-02-13 00:00:56 -06:00
|
|
|
} else if str::contains(triple, "linux") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::os_linux)
|
2012-02-13 00:00:56 -06:00
|
|
|
} else if str::contains(triple, "freebsd") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::os_freebsd)
|
|
|
|
} else { none };
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:03:37 -06:00
|
|
|
fn get_arch(triple: str) -> option<session::arch> {
|
2012-02-13 00:00:56 -06:00
|
|
|
ret if str::contains(triple, "i386") || str::contains(triple, "i486") ||
|
|
|
|
str::contains(triple, "i586") ||
|
|
|
|
str::contains(triple, "i686") ||
|
|
|
|
str::contains(triple, "i786") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::arch_x86)
|
2012-02-13 00:00:56 -06:00
|
|
|
} else if str::contains(triple, "x86_64") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::arch_x86_64)
|
2012-02-13 00:00:56 -06:00
|
|
|
} else if str::contains(triple, "arm") ||
|
|
|
|
str::contains(triple, "xscale") {
|
2012-01-14 01:03:37 -06:00
|
|
|
some(session::arch_arm)
|
|
|
|
} else { none };
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:18:01 -06:00
|
|
|
fn build_target_config(sopts: @session::options,
|
|
|
|
demitter: diagnostic::emitter) -> @session::config {
|
2012-01-14 01:03:37 -06:00
|
|
|
let os = alt get_os(sopts.target_triple) {
|
|
|
|
some(os) { os }
|
2012-03-05 18:27:27 -06:00
|
|
|
none { early_error(demitter, "unknown operating system") }
|
2012-01-14 01:03:37 -06:00
|
|
|
};
|
|
|
|
let arch = alt get_arch(sopts.target_triple) {
|
|
|
|
some(arch) { arch }
|
2012-01-19 00:37:22 -06:00
|
|
|
none { early_error(demitter,
|
2012-03-05 18:27:27 -06:00
|
|
|
"unknown architecture: " + sopts.target_triple) }
|
2012-01-14 01:03:37 -06:00
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
let (int_type, uint_type, float_type) = alt arch {
|
2012-01-19 03:03:57 -06:00
|
|
|
session::arch_x86 {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
|
|
|
|
session::arch_x86_64 {(ast::ty_i64, ast::ty_u64, ast::ty_f64)}
|
|
|
|
session::arch_arm {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
|
|
|
let target_strs = alt arch {
|
2012-01-19 03:03:57 -06:00
|
|
|
session::arch_x86 {x86::get_target_strs(os)}
|
|
|
|
session::arch_x86_64 {x86_64::get_target_strs(os)}
|
|
|
|
session::arch_arm {x86::get_target_strs(os)}
|
2011-12-20 04:17:13 -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};
|
|
|
|
ret target_cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
// FIXME: Instead of grabbing the host triple we really should be
|
|
|
|
// grabbing (at compile time) the target triple that this rustc is
|
|
|
|
// built with and calling that (at runtime) the host triple.
|
|
|
|
let ht = #env("CFG_HOST_TRIPLE");
|
2012-01-29 20:33:08 -06:00
|
|
|
ret if ht != "" {
|
|
|
|
ht
|
|
|
|
} else {
|
|
|
|
fail "rustc built without CFG_HOST_TRIPLE"
|
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-14 01:18:01 -06:00
|
|
|
fn build_session_options(match: getopts::match,
|
|
|
|
demitter: diagnostic::emitter) -> @session::options {
|
2011-12-20 04:17:13 -06:00
|
|
|
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");
|
|
|
|
let no_trans = opt_present(match, "no-trans");
|
2012-04-12 19:30:52 -05:00
|
|
|
|
|
|
|
let lint_flags = (getopts::opt_strs(match, "W")
|
|
|
|
+ getopts::opt_strs(match, "warn"));
|
|
|
|
let lint_dict = lint::get_lint_dict();
|
|
|
|
let lint_opts = vec::map(lint_flags) {|flag|
|
|
|
|
alt lint::lookup_lint(lint_dict, flag) {
|
|
|
|
none { early_error(demitter, #fmt("unknown warning: %s", flag)) }
|
|
|
|
some(x) { x }
|
|
|
|
}
|
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
let output_type =
|
|
|
|
if parse_only || no_trans {
|
|
|
|
link::output_type_none
|
|
|
|
} else if opt_present(match, "S") && opt_present(match, "emit-llvm") {
|
|
|
|
link::output_type_llvm_assembly
|
|
|
|
} else if opt_present(match, "S") {
|
|
|
|
link::output_type_assembly
|
|
|
|
} else if opt_present(match, "c") {
|
|
|
|
link::output_type_object
|
|
|
|
} else if opt_present(match, "emit-llvm") {
|
|
|
|
link::output_type_bitcode
|
|
|
|
} else { link::output_type_exe };
|
|
|
|
let verify = !opt_present(match, "no-verify");
|
|
|
|
let save_temps = opt_present(match, "save-temps");
|
|
|
|
let extra_debuginfo = opt_present(match, "xg");
|
|
|
|
let debuginfo = opt_present(match, "g") || extra_debuginfo;
|
|
|
|
let stats = opt_present(match, "stats");
|
|
|
|
let time_passes = opt_present(match, "time-passes");
|
|
|
|
let time_llvm_passes = opt_present(match, "time-llvm-passes");
|
2012-03-22 15:44:20 -05:00
|
|
|
let count_llvm_insns = opt_present(match, "count-llvm-insns");
|
2011-12-20 04:17:13 -06:00
|
|
|
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
|
|
|
|
let target_opt = getopts::opt_maybe_str(match, "target");
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut no_asm_comments = getopts::opt_present(match, "no-asm-comments");
|
2012-04-17 12:05:28 -05:00
|
|
|
let debug_rustc = getopts::opt_present(match, "debug-rustc");
|
2012-04-26 18:02:01 -05:00
|
|
|
let borrowck = alt getopts::opt_maybe_str(match, "borrowck") {
|
|
|
|
none { 0u }
|
|
|
|
some("warn") { 1u }
|
|
|
|
some("err") { 2u }
|
|
|
|
some(_) {
|
|
|
|
early_error(demitter, "borrowck may be warn or err")
|
|
|
|
}
|
|
|
|
};
|
2011-12-20 04:17:13 -06:00
|
|
|
alt output_type {
|
|
|
|
// unless we're emitting huamn-readable assembly, omit comments.
|
2012-01-19 03:03:57 -06:00
|
|
|
link::output_type_llvm_assembly | link::output_type_assembly {}
|
2011-12-20 04:17:13 -06:00
|
|
|
_ { no_asm_comments = true; }
|
|
|
|
}
|
|
|
|
let opt_level: uint =
|
|
|
|
if opt_present(match, "O") {
|
|
|
|
if opt_present(match, "opt-level") {
|
2012-01-14 01:18:01 -06:00
|
|
|
early_error(demitter, "-O and --opt-level both provided");
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
2u
|
|
|
|
} else if opt_present(match, "opt-level") {
|
|
|
|
alt getopts::opt_str(match, "opt-level") {
|
|
|
|
"0" { 0u }
|
|
|
|
"1" { 1u }
|
|
|
|
"2" { 2u }
|
|
|
|
"3" { 3u }
|
|
|
|
_ {
|
2012-01-14 01:18:01 -06:00
|
|
|
early_error(demitter, "optimization level needs " +
|
2011-12-20 04:17:13 -06:00
|
|
|
"to be between 0-3")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { 0u };
|
|
|
|
let target =
|
|
|
|
alt target_opt {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { host_triple() }
|
2011-12-20 04:17:13 -06:00
|
|
|
some(s) { s }
|
|
|
|
};
|
|
|
|
|
|
|
|
let addl_lib_search_paths = getopts::opt_strs(match, "L");
|
|
|
|
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
|
|
|
|
let test = opt_present(match, "test");
|
|
|
|
let sopts: @session::options =
|
|
|
|
@{crate_type: crate_type,
|
|
|
|
static: static,
|
|
|
|
optimize: opt_level,
|
|
|
|
debuginfo: debuginfo,
|
|
|
|
extra_debuginfo: extra_debuginfo,
|
|
|
|
verify: verify,
|
2012-01-19 02:50:51 -06:00
|
|
|
lint_opts: lint_opts,
|
2011-12-20 04:17:13 -06:00
|
|
|
save_temps: save_temps,
|
|
|
|
stats: stats,
|
|
|
|
time_passes: time_passes,
|
2012-03-22 15:44:20 -05:00
|
|
|
count_llvm_insns: count_llvm_insns,
|
2011-12-20 04:17:13 -06:00
|
|
|
time_llvm_passes: time_llvm_passes,
|
|
|
|
output_type: output_type,
|
|
|
|
addl_lib_search_paths: addl_lib_search_paths,
|
|
|
|
maybe_sysroot: sysroot_opt,
|
|
|
|
target_triple: target,
|
|
|
|
cfg: cfg,
|
|
|
|
test: test,
|
|
|
|
parse_only: parse_only,
|
|
|
|
no_trans: no_trans,
|
2012-04-17 12:05:28 -05:00
|
|
|
no_asm_comments: no_asm_comments,
|
2012-04-26 18:02:01 -05:00
|
|
|
debug_rustc: debug_rustc,
|
|
|
|
borrowck: borrowck};
|
2011-12-20 04:17:13 -06:00
|
|
|
ret sopts;
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:56:20 -05:00
|
|
|
fn build_session(sopts: @session::options,
|
2012-01-17 09:42:45 -06:00
|
|
|
demitter: diagnostic::emitter) -> session {
|
2012-02-17 16:15:09 -06:00
|
|
|
let codemap = codemap::new_codemap();
|
|
|
|
let diagnostic_handler =
|
|
|
|
diagnostic::mk_handler(some(demitter));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn build_session_(
|
2012-03-21 17:56:20 -05:00
|
|
|
sopts: @session::options,
|
2012-02-17 16:15:09 -06:00
|
|
|
codemap: codemap::codemap,
|
|
|
|
demitter: diagnostic::emitter,
|
|
|
|
span_diagnostic_handler: diagnostic::span_handler
|
|
|
|
) -> session {
|
2012-01-14 01:18:01 -06:00
|
|
|
let target_cfg = build_target_config(sopts, demitter);
|
2011-12-20 04:17:13 -06:00
|
|
|
let cstore = cstore::mk_cstore();
|
|
|
|
let filesearch = filesearch::mk_filesearch(
|
|
|
|
sopts.maybe_sysroot,
|
|
|
|
sopts.target_triple,
|
|
|
|
sopts.addl_lib_search_paths);
|
2012-01-12 10:59:49 -06:00
|
|
|
@{targ_cfg: target_cfg,
|
|
|
|
opts: sopts,
|
|
|
|
cstore: cstore,
|
2012-01-13 19:14:52 -06:00
|
|
|
parse_sess: @{
|
|
|
|
cm: codemap,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut next_id: 1,
|
2012-01-24 23:42:54 -06:00
|
|
|
span_diagnostic: span_diagnostic_handler,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut chpos: 0u,
|
|
|
|
mut byte_pos: 0u
|
2012-01-13 19:14:52 -06:00
|
|
|
},
|
2012-01-12 10:59:49 -06:00
|
|
|
codemap: codemap,
|
|
|
|
// For a library crate, this is always none
|
2012-03-26 20:35:18 -05:00
|
|
|
mut main_fn: none,
|
2012-01-24 23:42:54 -06:00
|
|
|
span_diagnostic: span_diagnostic_handler,
|
2012-01-12 10:59:49 -06:00
|
|
|
filesearch: filesearch,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut building_library: false,
|
2012-03-21 17:56:20 -05:00
|
|
|
working_dir: os::getcwd()}
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn parse_pretty(sess: session, &&name: str) -> pp_mode {
|
2011-12-20 04:17:13 -06:00
|
|
|
if str::eq(name, "normal") {
|
|
|
|
ret ppm_normal;
|
|
|
|
} else if str::eq(name, "expanded") {
|
|
|
|
ret ppm_expanded;
|
|
|
|
} else if str::eq(name, "typed") {
|
|
|
|
ret ppm_typed;
|
2012-03-12 12:19:35 -05:00
|
|
|
} else if str::eq(name, "expanded,identified") {
|
|
|
|
ret ppm_expanded_identified;
|
|
|
|
} else if str::eq(name, "identified") {
|
|
|
|
ret ppm_identified;
|
|
|
|
}
|
2011-12-20 04:17:13 -06:00
|
|
|
sess.fatal("argument to `pretty` must be one of `normal`, `typed`, or " +
|
|
|
|
"`identified`");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn opts() -> [getopts::opt] {
|
|
|
|
ret [optflag("h"), optflag("help"), optflag("v"), optflag("version"),
|
|
|
|
optflag("emit-llvm"), optflagopt("pretty"),
|
|
|
|
optflag("ls"), optflag("parse-only"), optflag("no-trans"),
|
|
|
|
optflag("O"), optopt("opt-level"), optmulti("L"), optflag("S"),
|
|
|
|
optopt("o"), optopt("out-dir"), optflag("xg"),
|
|
|
|
optflag("c"), optflag("g"), optflag("save-temps"),
|
|
|
|
optopt("sysroot"), optopt("target"), optflag("stats"),
|
|
|
|
optflag("time-passes"), optflag("time-llvm-passes"),
|
2012-03-22 15:44:20 -05:00
|
|
|
optflag("count-llvm-insns"),
|
2011-12-20 04:17:13 -06:00
|
|
|
optflag("no-verify"),
|
2012-04-12 19:30:52 -05:00
|
|
|
|
|
|
|
optmulti("W"), optmulti("warn"),
|
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
optmulti("cfg"), optflag("test"),
|
|
|
|
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"),
|
|
|
|
optflag("no-asm-comments"),
|
2012-04-26 18:02:01 -05:00
|
|
|
optflag("debug-rustc"),
|
|
|
|
optopt("borrowck")];
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
type output_filenames = @{out_filename: str, obj_filename:str};
|
|
|
|
|
2011-12-20 04:17:13 -06:00
|
|
|
fn build_output_filenames(ifile: str,
|
2012-01-31 19:05:20 -06:00
|
|
|
odir: option<str>,
|
|
|
|
ofile: option<str>,
|
2012-01-17 09:42:45 -06:00
|
|
|
sess: session)
|
|
|
|
-> output_filenames {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut obj_path = "";
|
|
|
|
let mut out_path: str = "";
|
2012-01-12 10:59:49 -06:00
|
|
|
let sopts = sess.opts;
|
2011-12-20 04:17:13 -06:00
|
|
|
let stop_after_codegen =
|
|
|
|
sopts.output_type != link::output_type_exe ||
|
2012-01-12 10:59:49 -06:00
|
|
|
sopts.static && sess.building_library;
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
let obj_suffix =
|
|
|
|
alt sopts.output_type {
|
2012-01-19 00:37:22 -06:00
|
|
|
link::output_type_none { "none" }
|
|
|
|
link::output_type_bitcode { "bc" }
|
2012-01-19 03:03:57 -06:00
|
|
|
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-01-19 03:03:57 -06:00
|
|
|
link::output_type_object | link::output_type_exe {
|
2011-12-20 04:17:13 -06:00
|
|
|
"o"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
alt ofile {
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2011-12-20 04:17:13 -06: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 dirname = alt odir {
|
|
|
|
some(d) { d }
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2011-12-20 04:17:13 -06:00
|
|
|
if input_is_stdin(ifile) {
|
2012-03-12 22:04:27 -05:00
|
|
|
os::getcwd()
|
2011-12-20 04:17:13 -06:00
|
|
|
} else {
|
2012-03-12 22:04:27 -05:00
|
|
|
path::dirname(ifile)
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-18 20:38:54 -06:00
|
|
|
let base_filename = if !input_is_stdin(ifile) {
|
2012-03-12 22:04:27 -05:00
|
|
|
let (path, _) = path::splitext(ifile);
|
|
|
|
path::basename(path)
|
2011-12-20 04:17:13 -06:00
|
|
|
} else {
|
2012-01-18 20:38:54 -06:00
|
|
|
"rust_out"
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-03-12 22:04:27 -05:00
|
|
|
let base_path = path::connect(dirname, base_filename);
|
2011-12-20 04:17:13 -06:00
|
|
|
|
|
|
|
|
2012-01-12 10:59:49 -06:00
|
|
|
if sess.building_library {
|
2012-03-12 22:04:27 -05:00
|
|
|
let basename = path::basename(base_path);
|
|
|
|
let dylibname = os::dll_filename(basename);
|
|
|
|
out_path = path::connect(dirname, dylibname);
|
|
|
|
obj_path = path::connect(dirname, basename + "." + obj_suffix);
|
2011-12-20 04:17:13 -06:00
|
|
|
} else {
|
|
|
|
out_path = base_path;
|
|
|
|
obj_path = base_path + "." + obj_suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
some(out_file) {
|
|
|
|
out_path = out_file;
|
|
|
|
obj_path = if stop_after_codegen {
|
|
|
|
out_file
|
|
|
|
} else {
|
2012-03-12 22:04:27 -05:00
|
|
|
let (base, _) = path::splitext(out_file);
|
2011-12-20 04:17:13 -06:00
|
|
|
let modified = base + "." + obj_suffix;
|
|
|
|
modified
|
|
|
|
};
|
|
|
|
|
2012-01-12 10:59:49 -06:00
|
|
|
if sess.building_library {
|
2011-12-20 04:17:13 -06:00
|
|
|
// FIXME: We might want to warn here; we're actually not going to
|
|
|
|
// respect the user's choice of library name when it comes time to
|
|
|
|
// link, we'll be linking to lib<basename>-<hash>-<version>.so no
|
|
|
|
// matter what.
|
|
|
|
}
|
|
|
|
|
|
|
|
if odir != none {
|
2012-03-05 18:27:27 -06:00
|
|
|
sess.warn("ignoring --out-dir flag due to -o flag.");
|
2011-12-20 04:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret @{out_filename: out_path,
|
|
|
|
obj_filename: obj_path};
|
|
|
|
}
|
|
|
|
|
2012-01-14 01:18:01 -06:00
|
|
|
fn early_error(emitter: diagnostic::emitter, msg: str) -> ! {
|
|
|
|
emitter(none, msg, diagnostic::fatal);
|
2011-12-20 04:17:13 -06:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2012-01-17 09:42:45 -06:00
|
|
|
fn list_metadata(sess: session, path: str, out: io::writer) {
|
2011-12-20 04:17:13 -06:00
|
|
|
metadata::creader::list_file_metadata(sess, path, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
|
|
|
|
// When the user supplies --test we should implicitly supply --cfg test
|
|
|
|
#[test]
|
|
|
|
fn test_switch_implies_cfg_test() {
|
|
|
|
let match =
|
|
|
|
alt getopts::getopts(["--test"], opts()) {
|
|
|
|
ok(m) { m }
|
2012-01-30 23:00:57 -06:00
|
|
|
err(f) { fail "test_switch_implies_cfg_test: " +
|
|
|
|
getopts::fail_str(f); }
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-01-14 02:46:37 -06:00
|
|
|
let sessopts = build_session_options(match, diagnostic::emit);
|
2012-03-21 17:56:20 -05:00
|
|
|
let sess = build_session(sessopts, diagnostic::emit);
|
2011-12-20 04:17:13 -06:00
|
|
|
let cfg = build_configuration(sess, "whatever", "whatever");
|
|
|
|
assert (attr::contains_name(cfg, "test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the user supplies --test and --cfg test, don't implicitly add
|
|
|
|
// another --cfg test
|
|
|
|
#[test]
|
|
|
|
fn test_switch_implies_cfg_test_unless_cfg_test() {
|
|
|
|
let match =
|
|
|
|
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
|
|
|
|
ok(m) { m }
|
2012-01-30 23:00:57 -06:00
|
|
|
err(f) { fail "test_switch_implies_cfg_test_unless_cfg_test: " +
|
|
|
|
getopts::fail_str(f); }
|
2011-12-20 04:17:13 -06:00
|
|
|
};
|
2012-01-14 02:46:37 -06:00
|
|
|
let sessopts = build_session_options(match, diagnostic::emit);
|
2012-03-21 17:56:20 -05:00
|
|
|
let sess = build_session(sessopts, diagnostic::emit);
|
2011-12-20 04:17:13 -06:00
|
|
|
let cfg = build_configuration(sess, "whatever", "whatever");
|
|
|
|
let test_items = attr::find_meta_items_by_name(cfg, "test");
|
|
|
|
assert (vec::len(test_items) == 1u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|