2010-06-23 21:03:09 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
// -*- rust -*-
|
2011-06-27 14:18:32 -07:00
|
|
|
import metadata::creader;
|
2011-07-07 18:25:56 -07:00
|
|
|
import metadata::cstore;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::parse::parser;
|
|
|
|
|
import syntax::parse::token;
|
|
|
|
|
import syntax::ast;
|
|
|
|
|
import syntax::codemap;
|
2011-06-30 17:29:54 -07:00
|
|
|
import front::attr;
|
2011-05-12 17:24:54 +02:00
|
|
|
import middle::trans;
|
|
|
|
|
import middle::resolve;
|
2011-07-18 19:14:01 -07:00
|
|
|
import middle::freevars;
|
2011-07-27 17:49:00 -07:00
|
|
|
import middle::kind;
|
2011-05-12 17:24:54 +02:00
|
|
|
import middle::ty;
|
|
|
|
|
import middle::typeck;
|
2011-05-14 19:02:30 -07:00
|
|
|
import middle::tstate::ck;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::print::pp;
|
|
|
|
|
import syntax::print::pprust;
|
|
|
|
|
import util::ppaux;
|
2011-05-13 16:47:37 -04:00
|
|
|
import back::link;
|
2011-05-12 17:24:54 +02:00
|
|
|
import lib::llvm;
|
|
|
|
|
import util::common;
|
|
|
|
|
import std::fs;
|
|
|
|
|
import std::map::mk_hashmap;
|
|
|
|
|
import std::option;
|
|
|
|
|
import std::option::some;
|
|
|
|
|
import std::option::none;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::str;
|
|
|
|
|
import std::vec;
|
2011-07-05 10:38:35 +02:00
|
|
|
import std::int;
|
2011-07-12 10:59:18 -07:00
|
|
|
import std::ioivec;
|
2011-05-16 09:01:36 -06:00
|
|
|
import std::run;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::getopts;
|
|
|
|
|
import std::getopts::optopt;
|
|
|
|
|
import std::getopts::optmulti;
|
|
|
|
|
import std::getopts::optflag;
|
2011-06-01 18:31:31 -07:00
|
|
|
import std::getopts::optflagopt;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::getopts::opt_present;
|
2011-05-13 16:47:37 -04:00
|
|
|
import back::link::output_type;
|
2011-05-12 17:24:54 +02:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
tag pp_mode { ppm_normal; ppm_typed; ppm_identified; }
|
2011-03-01 15:57:55 -08:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn default_configuration(sess: session::session, argv0: str, input: str) ->
|
|
|
|
|
ast::crate_cfg {
|
|
|
|
|
let libc =
|
|
|
|
|
alt sess.get_targ_cfg().os {
|
|
|
|
|
session::os_win32. { "msvcrt.dll" }
|
|
|
|
|
session::os_macos. { "libc.dylib" }
|
|
|
|
|
session::os_linux. { "libc.so.6" }
|
|
|
|
|
_ { "libc.so" }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-06-30 17:29:54 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let mk = attr::mk_name_value_item_str;
|
2011-06-30 17:29:54 -07:00
|
|
|
|
2011-07-05 17:57:34 -07:00
|
|
|
ret ~[ // Target bindings.
|
2011-07-27 14:19:39 +02:00
|
|
|
mk("target_os", std::os::target_os()), mk("target_arch", "x86"),
|
2011-07-05 17:57:34 -07:00
|
|
|
mk("target_libc", libc),
|
|
|
|
|
// Build bindings.
|
2011-07-27 14:19:39 +02:00
|
|
|
mk("build_compiler", argv0), mk("build_input", input)];
|
2011-03-01 15:57:55 -08:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn build_configuration(sess: session::session, argv0: str, input: str) ->
|
|
|
|
|
ast::crate_cfg {
|
2011-07-01 15:30:25 -07:00
|
|
|
// Combine the configuration requested by the session (command line) with
|
2011-07-13 18:13:19 -07:00
|
|
|
// some default and generated configuration items
|
2011-07-27 14:19:39 +02:00
|
|
|
let default_cfg = default_configuration(sess, argv0, input);
|
|
|
|
|
let user_cfg = sess.get_opts().cfg;
|
2011-07-27 14:48:34 +02:00
|
|
|
// If the user wants a test runner, then add the test cfg
|
|
|
|
|
let gen_cfg =
|
2011-07-27 14:19:39 +02:00
|
|
|
{
|
|
|
|
|
if sess.get_opts().test && !attr::contains_name(user_cfg, "test")
|
|
|
|
|
{
|
|
|
|
|
~[attr::mk_word_item("test")]
|
|
|
|
|
} else { ~[] }
|
|
|
|
|
};
|
2011-07-13 18:13:19 -07:00
|
|
|
ret user_cfg + gen_cfg + default_cfg;
|
2011-07-01 15:30:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
|
2011-07-27 14:19:39 +02:00
|
|
|
fn parse_cfgspecs(cfgspecs: &vec[str]) -> ast::crate_cfg {
|
2011-07-01 15:30:25 -07:00
|
|
|
// 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.
|
2011-07-27 14:19:39 +02:00
|
|
|
let words = ~[];
|
|
|
|
|
for s: str in cfgspecs { words += ~[attr::mk_word_item(s)]; }
|
2011-07-05 17:57:34 -07:00
|
|
|
ret words;
|
2011-07-01 15:30:25 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn parse_input(sess: session::session, cfg: &ast::crate_cfg, input: str) ->
|
|
|
|
|
@ast::crate {
|
|
|
|
|
ret if str::ends_with(input, ".rc") {
|
|
|
|
|
parser::parse_crate_from_crate_file(input, cfg,
|
|
|
|
|
sess.get_parse_sess())
|
2011-06-15 11:19:50 -07:00
|
|
|
} else if (str::ends_with(input, ".rs")) {
|
2011-07-27 14:19:39 +02:00
|
|
|
parser::parse_crate_from_source_file(input, cfg,
|
|
|
|
|
sess.get_parse_sess())
|
2011-07-29 20:51:18 +02:00
|
|
|
} else { sess.fatal("unknown input file type: " + input) };
|
2011-01-03 20:41:11 -08:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn time[T](do_it: bool, what: str, thunk: fn() -> T ) -> T {
|
|
|
|
|
if !do_it { ret thunk(); }
|
2011-07-22 12:03:10 +02:00
|
|
|
let start = std::time::precise_time_s();
|
2011-07-27 14:19:39 +02:00
|
|
|
let rv = thunk();
|
2011-07-22 12:03:10 +02:00
|
|
|
let end = std::time::precise_time_s();
|
|
|
|
|
log_err #fmt("time: %s took %s s", what,
|
|
|
|
|
common::float_to_str(end - start, 3u));
|
2011-04-29 11:55:20 -07:00
|
|
|
ret rv;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
|
|
|
|
|
output: str) {
|
|
|
|
|
let time_passes = sess.get_opts().time_passes;
|
|
|
|
|
let crate =
|
2011-07-05 11:48:19 +02:00
|
|
|
time(time_passes, "parsing", bind parse_input(sess, cfg, input));
|
2011-07-27 14:19:39 +02:00
|
|
|
if sess.get_opts().output_type == link::output_type_none { ret; }
|
|
|
|
|
crate =
|
|
|
|
|
time(time_passes, "configuration",
|
|
|
|
|
bind front::config::strip_unconfigured_items(crate));
|
|
|
|
|
if sess.get_opts().test {
|
|
|
|
|
crate =
|
|
|
|
|
time(time_passes, "building test harness",
|
|
|
|
|
bind front::test::modify_for_testing(crate));
|
2011-07-06 14:29:50 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
crate =
|
|
|
|
|
time(time_passes, "expansion",
|
|
|
|
|
bind syntax::ext::expand::expand_crate(sess, crate));
|
2011-07-06 15:22:23 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let ast_map =
|
|
|
|
|
time(time_passes, "ast indexing",
|
|
|
|
|
bind middle::ast_map::map_crate(*crate));
|
2011-07-07 21:43:26 -07:00
|
|
|
time(time_passes, "external crate/lib resolution",
|
|
|
|
|
bind creader::read_crates(sess, *crate));
|
2011-07-27 14:19:39 +02:00
|
|
|
let d =
|
2011-06-15 11:19:50 -07:00
|
|
|
time(time_passes, "resolution",
|
2011-06-20 09:59:16 +02:00
|
|
|
bind resolve::resolve_crate(sess, ast_map, crate));
|
2011-07-27 14:19:39 +02:00
|
|
|
let freevars =
|
2011-07-18 19:14:01 -07:00
|
|
|
time(time_passes, "freevar finding",
|
2011-07-19 17:52:34 -07:00
|
|
|
bind freevars::annotate_freevars(sess, d, crate));
|
2011-07-27 14:19:39 +02:00
|
|
|
let ty_cx = ty::mk_ctxt(sess, d, ast_map, freevars);
|
2011-05-19 15:47:15 -07:00
|
|
|
time[()](time_passes, "typechecking",
|
|
|
|
|
bind typeck::check_crate(ty_cx, crate));
|
2011-07-25 13:45:09 +02:00
|
|
|
time[()](time_passes, "alt checking",
|
|
|
|
|
bind middle::check_alt::check_crate(ty_cx, crate));
|
2011-07-27 14:19:39 +02:00
|
|
|
if sess.get_opts().run_typestate {
|
2011-05-18 18:04:07 -07:00
|
|
|
time(time_passes, "typestate checking",
|
2011-05-19 15:47:15 -07:00
|
|
|
bind middle::tstate::ck::check_crate(ty_cx, crate));
|
2011-04-29 12:16:14 -07:00
|
|
|
}
|
2011-06-06 15:22:13 +02:00
|
|
|
time(time_passes, "alias checking",
|
2011-07-21 15:32:31 -07:00
|
|
|
bind middle::alias::check_crate(ty_cx, crate));
|
2011-07-27 17:49:00 -07:00
|
|
|
time[()](time_passes, "kind checking",
|
|
|
|
|
bind kind::check_crate(ty_cx, crate));
|
2011-07-27 14:19:39 +02:00
|
|
|
let llmod =
|
2011-05-31 18:24:06 -07:00
|
|
|
time[llvm::llvm::ModuleRef](time_passes, "translation",
|
2011-07-27 14:19:39 +02:00
|
|
|
bind trans::trans_crate(sess, crate,
|
|
|
|
|
ty_cx, output,
|
|
|
|
|
ast_map));
|
2011-05-02 17:45:07 -07:00
|
|
|
time[()](time_passes, "LLVM passes",
|
2011-05-13 16:47:37 -04:00
|
|
|
bind link::write::run_passes(sess, llmod, output));
|
2010-10-22 11:47:28 -07:00
|
|
|
}
|
2010-07-13 14:26:59 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
|
|
|
|
|
ppm: pp_mode, expand: bool) {
|
|
|
|
|
fn ann_paren_for_expr(node: &pprust::ann_node) {
|
|
|
|
|
alt node { pprust::node_expr(s, expr) { pprust::popen(s); } _ { } }
|
2011-07-05 10:38:35 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
_ { }
|
2011-07-05 10:38:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
_ { }
|
2011-07-05 10:38:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let crate = parse_input(sess, cfg, input);
|
|
|
|
|
if expand { crate = syntax::ext::expand::expand_crate(sess, crate); }
|
|
|
|
|
let ann;
|
|
|
|
|
alt ppm {
|
|
|
|
|
ppm_typed. {
|
|
|
|
|
let amap = middle::ast_map::map_crate(*crate);
|
|
|
|
|
let d = resolve::resolve_crate(sess, amap, crate);
|
|
|
|
|
let freevars = freevars::annotate_freevars(sess, d, crate);
|
|
|
|
|
let ty_cx = ty::mk_ctxt(sess, d, amap, freevars);
|
|
|
|
|
typeck::check_crate(ty_cx, crate);
|
|
|
|
|
ann = {pre: ann_paren_for_expr, post: bind ann_typed_post(ty_cx, _)};
|
|
|
|
|
}
|
|
|
|
|
ppm_identified. {
|
|
|
|
|
ann = {pre: ann_paren_for_expr, post: ann_identified_post};
|
|
|
|
|
}
|
|
|
|
|
ppm_normal. { ann = pprust::no_ann(); }
|
2011-05-17 17:42:23 -07:00
|
|
|
}
|
2011-07-25 14:04:38 -07:00
|
|
|
pprust::print_crate(sess.get_codemap(), crate, input,
|
2011-07-27 14:19:39 +02:00
|
|
|
ioivec::file_reader(input), ioivec::stdout(), ann);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn version(argv0: str) {
|
|
|
|
|
let vers = "unknown version";
|
|
|
|
|
let env_vers = #env("CFG_VERSION");
|
|
|
|
|
if str::byte_len(env_vers) != 0u { vers = env_vers; }
|
2011-07-12 10:59:18 -07:00
|
|
|
ioivec::stdout().write_str(#fmt("%s %s\n", argv0, vers));
|
2011-05-05 12:03:23 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn usage(argv0: str) {
|
2011-07-12 10:59:18 -07:00
|
|
|
ioivec::stdout().write_str(#fmt("usage: %s [options] <input>\n", argv0) +
|
2011-07-27 14:19:39 +02:00
|
|
|
"
|
2011-04-19 12:02:06 +02:00
|
|
|
options:
|
|
|
|
|
|
2011-05-05 12:03:23 -07:00
|
|
|
-h --help display this message
|
|
|
|
|
-v --version print version info and exit
|
|
|
|
|
|
2011-04-19 12:02:06 +02:00
|
|
|
-o <filename> write output to <filename>
|
2011-04-26 20:32:08 +02:00
|
|
|
--glue generate glue.bc file
|
2011-07-07 14:42:18 -04:00
|
|
|
--lib compile a library crate
|
|
|
|
|
--static use or produce static libraries
|
2011-06-01 18:31:31 -07:00
|
|
|
--pretty [type] pretty-print the input instead of compiling
|
2011-07-22 11:50:01 -07:00
|
|
|
--expand [type] expand and pretty-print the input instead of compiling
|
2011-04-26 20:32:08 +02:00
|
|
|
--ls list the symbols defined by a crate file
|
2011-04-19 12:02:06 +02:00
|
|
|
-L <path> add a directory to the library search path
|
2011-04-26 20:32:08 +02:00
|
|
|
--noverify suppress LLVM verification step (slight speedup)
|
2011-05-03 15:50:56 -07:00
|
|
|
--depend print dependencies, in makefile-rule form
|
2011-04-26 20:32:08 +02:00
|
|
|
--parse-only parse only; do not compile, assemble, or link
|
2011-05-04 15:36:42 -07:00
|
|
|
-g produce debug info
|
2011-05-19 12:46:10 -07:00
|
|
|
--OptLevel= optimize with possible levels 0-3
|
|
|
|
|
-O equivalent to --OptLevel=2
|
2011-04-25 14:08:12 -07:00
|
|
|
-S compile only; do not assemble or link
|
|
|
|
|
-c compile and assemble, but do not link
|
2011-05-16 12:14:24 -06:00
|
|
|
--emit-llvm produce an LLVM bitcode file
|
2011-04-26 20:32:08 +02:00
|
|
|
--save-temps write intermediate files in addition to normal output
|
2011-05-12 15:42:12 -07:00
|
|
|
--stats gather and report various compilation statistics
|
2011-07-01 15:30:25 -07:00
|
|
|
--cfg [cfgspec] configure the compilation environment
|
2011-04-29 11:55:20 -07:00
|
|
|
--time-passes time the individual phases of the compiler
|
2011-05-10 16:10:08 -07:00
|
|
|
--time-llvm-passes time the individual phases of the LLVM backend
|
2011-05-04 19:27:54 -07:00
|
|
|
--sysroot <path> override the system root (default: rustc's directory)
|
2011-07-06 14:29:50 -07:00
|
|
|
--no-typestate don't run the typestate pass (unsafe!)
|
2011-07-15 11:38:16 -07:00
|
|
|
--test build test harness
|
|
|
|
|
--dps translate via destination-passing style (experimental)
|
|
|
|
|
|
|
|
|
|
");
|
2010-10-22 11:47:28 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn get_os(triple: str) -> session::os {
|
|
|
|
|
ret if str::find(triple, "win32") >= 0 ||
|
|
|
|
|
str::find(triple, "mingw32") >= 0 {
|
2011-06-15 11:19:50 -07:00
|
|
|
session::os_win32
|
|
|
|
|
} else if (str::find(triple, "darwin") >= 0) {
|
|
|
|
|
session::os_macos
|
|
|
|
|
} else if (str::find(triple, "linux") >= 0) {
|
|
|
|
|
session::os_linux
|
|
|
|
|
} else { log_err "Unknown operating system!"; fail };
|
2011-05-06 07:59:33 -06:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn get_arch(triple: str) -> session::arch {
|
|
|
|
|
ret if str::find(triple, "i386") >= 0 || str::find(triple, "i486") >= 0 ||
|
|
|
|
|
str::find(triple, "i586") >= 0 ||
|
|
|
|
|
str::find(triple, "i686") >= 0 ||
|
|
|
|
|
str::find(triple, "i786") >= 0 {
|
2011-06-15 11:19:50 -07:00
|
|
|
session::arch_x86
|
|
|
|
|
} else if (str::find(triple, "x86_64") >= 0) {
|
|
|
|
|
session::arch_x64
|
|
|
|
|
} else if (str::find(triple, "arm") >= 0 ||
|
|
|
|
|
str::find(triple, "xscale") >= 0) {
|
|
|
|
|
session::arch_arm
|
|
|
|
|
} else { log_err "Unknown architecture! " + triple; fail };
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn get_default_sysroot(binary: str) -> str {
|
|
|
|
|
let dirname = fs::dirname(binary);
|
|
|
|
|
if str::eq(dirname, binary) { ret "."; }
|
2011-05-04 19:27:54 -07:00
|
|
|
ret dirname;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-22 16:41:32 -04:00
|
|
|
fn build_target_config() -> @session::config {
|
2011-07-27 14:19:39 +02:00
|
|
|
let triple: str =
|
2011-05-17 20:41:41 +02:00
|
|
|
std::str::rustrt::str_from_cstr(llvm::llvm::LLVMRustGetHostTriple());
|
2011-07-27 14:19:39 +02:00
|
|
|
let target_cfg: @session::config =
|
|
|
|
|
@{os: get_os(triple),
|
|
|
|
|
arch: get_arch(triple),
|
|
|
|
|
int_type: ast::ty_i32,
|
|
|
|
|
uint_type: ast::ty_u32,
|
|
|
|
|
float_type: ast::ty_f64};
|
2011-05-22 16:41:32 -04:00
|
|
|
ret target_cfg;
|
|
|
|
|
}
|
2011-05-05 12:03:23 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn build_session_options(binary: str, match: getopts::match, binary_dir: str)
|
|
|
|
|
-> @session::options {
|
|
|
|
|
let library = opt_present(match, "lib");
|
|
|
|
|
let static = opt_present(match, "static");
|
2011-07-14 17:26:10 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let library_search_paths = ~[binary_dir + "/lib"];
|
2011-07-14 17:26:10 -07:00
|
|
|
// FIXME: Remove this vec->ivec conversion.
|
2011-07-27 14:19:39 +02:00
|
|
|
let lsp_vec = getopts::opt_strs(match, "L");
|
|
|
|
|
for lsp: str in lsp_vec { library_search_paths += ~[lsp]; }
|
2011-07-14 17:26:10 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let output_type =
|
|
|
|
|
if opt_present(match, "parse-only") {
|
2011-06-15 11:19:50 -07:00
|
|
|
link::output_type_none
|
|
|
|
|
} 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 };
|
2011-07-27 14:19:39 +02:00
|
|
|
let verify = !opt_present(match, "noverify");
|
|
|
|
|
let save_temps = opt_present(match, "save-temps");
|
|
|
|
|
let debuginfo = opt_present(match, "g");
|
|
|
|
|
let stats = opt_present(match, "stats");
|
|
|
|
|
let time_passes = opt_present(match, "time-passes");
|
|
|
|
|
let time_llvm_passes = opt_present(match, "time-llvm-passes");
|
|
|
|
|
let run_typestate = !opt_present(match, "no-typestate");
|
|
|
|
|
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
|
|
|
|
|
let opt_level: uint =
|
|
|
|
|
if opt_present(match, "O") {
|
|
|
|
|
if opt_present(match, "OptLevel") {
|
2011-06-15 11:19:50 -07:00
|
|
|
log_err "error: -O and --OptLevel both provided";
|
|
|
|
|
fail;
|
2011-05-19 12:46:10 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
2u
|
|
|
|
|
} else if (opt_present(match, "OptLevel")) {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt getopts::opt_str(match, "OptLevel") {
|
|
|
|
|
"0" { 0u }
|
|
|
|
|
"1" { 1u }
|
|
|
|
|
"2" { 2u }
|
|
|
|
|
"3" { 3u }
|
|
|
|
|
_ {
|
|
|
|
|
log_err "error: optimization level needs " +
|
|
|
|
|
"to be between 0-3";
|
|
|
|
|
fail
|
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
}
|
|
|
|
|
} else { 0u };
|
2011-07-27 14:19:39 +02:00
|
|
|
let sysroot =
|
|
|
|
|
alt sysroot_opt {
|
|
|
|
|
none. { get_default_sysroot(binary) }
|
|
|
|
|
some(s) { s }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
|
|
|
|
|
let test = opt_present(match, "test");
|
|
|
|
|
let dps = opt_present(match, "dps");
|
|
|
|
|
let sopts: @session::options =
|
|
|
|
|
@{library: library,
|
|
|
|
|
static: static,
|
|
|
|
|
optimize: opt_level,
|
|
|
|
|
debuginfo: debuginfo,
|
|
|
|
|
verify: verify,
|
|
|
|
|
run_typestate: run_typestate,
|
|
|
|
|
save_temps: save_temps,
|
|
|
|
|
stats: stats,
|
|
|
|
|
time_passes: time_passes,
|
|
|
|
|
time_llvm_passes: time_llvm_passes,
|
|
|
|
|
output_type: output_type,
|
|
|
|
|
library_search_paths: library_search_paths,
|
|
|
|
|
sysroot: sysroot,
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
test: test,
|
|
|
|
|
dps: dps};
|
2011-05-22 16:41:32 -04:00
|
|
|
ret sopts;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn build_session(sopts: @session::options) -> session::session {
|
|
|
|
|
let target_cfg = build_target_config();
|
|
|
|
|
let cstore = cstore::mk_cstore();
|
2011-07-07 18:25:56 -07:00
|
|
|
ret session::session(target_cfg, sopts, cstore,
|
2011-07-27 14:19:39 +02:00
|
|
|
@{cm: codemap::new_codemap(), mutable next_id: 0},
|
2011-07-13 17:26:06 -07:00
|
|
|
none, 0u);
|
2011-05-22 16:41:32 -04:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn parse_pretty(sess: session::session, name: &str) -> pp_mode {
|
|
|
|
|
if str::eq(name, "normal") {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret ppm_normal;
|
|
|
|
|
} else if (str::eq(name, "typed")) {
|
|
|
|
|
ret ppm_typed;
|
|
|
|
|
} else if (str::eq(name, "identified")) { ret ppm_identified; }
|
2011-07-27 14:19:39 +02:00
|
|
|
sess.fatal("argument to `pretty` or `expand` must be one of `normal`, " +
|
|
|
|
|
"`typed`, or `identified`");
|
2011-06-01 18:31:31 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-13 18:13:19 -07:00
|
|
|
fn opts() -> vec[getopts::opt] {
|
|
|
|
|
ret [optflag("h"), optflag("help"), optflag("v"), optflag("version"),
|
2011-06-15 11:19:50 -07:00
|
|
|
optflag("glue"), optflag("emit-llvm"), optflagopt("pretty"),
|
2011-07-22 11:50:01 -07:00
|
|
|
optflagopt("expand"), optflag("ls"), optflag("parse-only"),
|
|
|
|
|
optflag("O"), optopt("OptLevel"), optmulti("L"), optflag("S"),
|
2011-06-15 11:19:50 -07:00
|
|
|
optflag("c"), optopt("o"), optflag("g"), optflag("save-temps"),
|
|
|
|
|
optopt("sysroot"), optflag("stats"), optflag("time-passes"),
|
|
|
|
|
optflag("time-llvm-passes"), optflag("no-typestate"),
|
2011-07-07 14:42:18 -04:00
|
|
|
optflag("noverify"), optmulti("cfg"), optflag("test"),
|
2011-07-15 11:38:16 -07:00
|
|
|
optflag("lib"), optflag("static"), optflag("dps")];
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn main(args: vec[str]) {
|
|
|
|
|
let binary = vec::shift(args);
|
|
|
|
|
let binary_dir = fs::dirname(binary);
|
|
|
|
|
let match =
|
|
|
|
|
alt getopts::getopts(args, opts()) {
|
|
|
|
|
getopts::success(m) { m }
|
|
|
|
|
getopts::failure(f) {
|
|
|
|
|
log_err #fmt("error: %s", getopts::fail_str(f));
|
|
|
|
|
fail
|
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
if opt_present(match, "h") || opt_present(match, "help") {
|
2011-05-22 16:41:32 -04:00
|
|
|
usage(binary);
|
|
|
|
|
ret;
|
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if opt_present(match, "v") || opt_present(match, "version") {
|
2011-05-22 16:41:32 -04:00
|
|
|
version(binary);
|
|
|
|
|
ret;
|
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let sopts = build_session_options(binary, match, binary_dir);
|
|
|
|
|
let sess = build_session(sopts);
|
|
|
|
|
let n_inputs = vec::len[str](match.free);
|
|
|
|
|
let output_file = getopts::opt_maybe_str(match, "o");
|
|
|
|
|
let glue = opt_present(match, "glue");
|
|
|
|
|
if glue {
|
|
|
|
|
if n_inputs > 0u {
|
2011-06-18 22:41:20 -07:00
|
|
|
sess.fatal("No input files allowed with --glue.");
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let out = option::from_maybe[str]("glue.bc", output_file);
|
2011-05-12 17:24:54 +02:00
|
|
|
middle::trans::make_common_glue(sess, out);
|
2011-04-26 20:32:08 +02:00
|
|
|
ret;
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if n_inputs == 0u {
|
2011-06-18 22:41:20 -07:00
|
|
|
sess.fatal("No input filename given.");
|
2011-04-26 20:32:08 +02:00
|
|
|
} else if (n_inputs > 1u) {
|
2011-06-18 22:41:20 -07:00
|
|
|
sess.fatal("Multiple input filenames provided.");
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let ifile = match.free.(0);
|
|
|
|
|
let saved_out_filename: str = "";
|
|
|
|
|
let cfg = build_configuration(sess, binary, ifile);
|
|
|
|
|
let expand =
|
2011-07-22 11:50:01 -07:00
|
|
|
option::map[str,
|
|
|
|
|
pp_mode](bind parse_pretty(sess, _),
|
|
|
|
|
getopts::opt_default(match, "expand", "normal"));
|
2011-07-27 14:19:39 +02:00
|
|
|
alt expand {
|
|
|
|
|
some[pp_mode](ppm) {
|
|
|
|
|
pretty_print_input(sess, cfg, ifile, ppm, true);
|
|
|
|
|
ret;
|
|
|
|
|
}
|
|
|
|
|
none[pp_mode]. {/* continue */ }
|
2011-07-22 11:50:01 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let pretty =
|
2011-06-15 11:19:50 -07:00
|
|
|
option::map[str,
|
|
|
|
|
pp_mode](bind parse_pretty(sess, _),
|
|
|
|
|
getopts::opt_default(match, "pretty", "normal"));
|
2011-07-27 14:19:39 +02:00
|
|
|
alt pretty {
|
|
|
|
|
some[pp_mode](ppm) {
|
|
|
|
|
pretty_print_input(sess, cfg, ifile, ppm, false);
|
|
|
|
|
ret;
|
|
|
|
|
}
|
|
|
|
|
none[pp_mode]. {/* continue */ }
|
2011-06-01 18:31:31 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let ls = opt_present(match, "ls");
|
|
|
|
|
if ls {
|
2011-07-12 10:59:18 -07:00
|
|
|
metadata::creader::list_file_metadata(ifile, ioivec::stdout());
|
2011-05-26 15:22:08 +00:00
|
|
|
ret;
|
2011-06-01 18:38:00 -07:00
|
|
|
}
|
2011-07-08 15:03:48 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let stop_after_codegen =
|
|
|
|
|
sopts.output_type != link::output_type_exe ||
|
|
|
|
|
sopts.static && sopts.library;
|
|
|
|
|
|
|
|
|
|
alt output_file {
|
|
|
|
|
none. {
|
|
|
|
|
let parts: vec[str] = str::split(ifile, '.' as u8);
|
|
|
|
|
vec::pop[str](parts);
|
|
|
|
|
saved_out_filename = parts.(0);
|
|
|
|
|
alt sopts.output_type {
|
|
|
|
|
link::output_type_none. { parts += ["pp"]; }
|
|
|
|
|
link::output_type_bitcode. { parts += ["bc"]; }
|
|
|
|
|
link::output_type_assembly. { parts += ["s"]; }
|
|
|
|
|
|
|
|
|
|
// Object and exe output both use the '.o' extension here
|
|
|
|
|
link::output_type_object. {
|
|
|
|
|
parts += ["o"];
|
|
|
|
|
}
|
|
|
|
|
link::output_type_exe. { parts += ["o"]; }
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let ofile = str::connect(parts, ".");
|
|
|
|
|
compile_input(sess, cfg, ifile, ofile);
|
|
|
|
|
}
|
|
|
|
|
some(ofile) {
|
|
|
|
|
// FIXME: what about windows? This will create a foo.exe.o.
|
|
|
|
|
|
|
|
|
|
saved_out_filename = ofile;
|
|
|
|
|
let temp_filename;
|
|
|
|
|
if !stop_after_codegen {
|
|
|
|
|
temp_filename = ofile + ".o";
|
|
|
|
|
} else { temp_filename = ofile; }
|
|
|
|
|
compile_input(sess, cfg, ifile, temp_filename);
|
|
|
|
|
}
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-05-16 09:01:36 -06:00
|
|
|
|
|
|
|
|
// If the user wants an exe generated we need to invoke
|
|
|
|
|
// gcc to link the object file with some libs
|
2011-06-01 18:31:31 -07:00
|
|
|
//
|
|
|
|
|
// TODO: Factor this out of main.
|
2011-07-27 14:19:39 +02:00
|
|
|
if stop_after_codegen { ret; }
|
2011-06-20 17:36:15 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let glu: str = binary_dir + "/lib/glue.o";
|
|
|
|
|
let main: str = binary_dir + "/lib/main.o";
|
|
|
|
|
let stage: str = "-L" + binary_dir + "/lib";
|
|
|
|
|
let prog: str = "gcc";
|
2011-07-07 14:59:18 -04:00
|
|
|
// The invocations of gcc share some flags across platforms
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let gcc_args: vec[str] =
|
|
|
|
|
[stage, "-Lrt", "-lrustrt", glu, "-m32", "-o", saved_out_filename,
|
|
|
|
|
saved_out_filename + ".o"];
|
|
|
|
|
let lib_cmd;
|
2011-07-07 14:59:18 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let os = sess.get_targ_cfg().os;
|
|
|
|
|
if os == session::os_macos {
|
|
|
|
|
lib_cmd = "-dynamiclib";
|
|
|
|
|
} else { lib_cmd = "-shared"; }
|
2011-07-04 13:22:23 -04:00
|
|
|
|
2011-07-07 14:59:18 -04:00
|
|
|
// Converts a library file name into a gcc -l argument
|
2011-07-27 14:19:39 +02:00
|
|
|
fn unlib(config: @session::config, filename: str) -> str {
|
|
|
|
|
let rmlib =
|
|
|
|
|
bind fn (config: @session::config, filename: str) -> str {
|
|
|
|
|
if config.os == session::os_macos ||
|
|
|
|
|
config.os == session::os_linux &&
|
|
|
|
|
str::find(filename, "lib") == 0 {
|
|
|
|
|
ret str::slice(filename, 3u,
|
|
|
|
|
str::byte_len(filename));
|
|
|
|
|
} else { ret filename; }
|
|
|
|
|
}(config, _);
|
|
|
|
|
fn rmext(filename: str) -> str {
|
|
|
|
|
let parts = str::split(filename, '.' as u8);
|
2011-07-07 14:59:18 -04:00
|
|
|
vec::pop(parts);
|
|
|
|
|
ret str::connect(parts, ".");
|
2011-06-29 11:12:13 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret alt config.os {
|
|
|
|
|
session::os_macos. { rmext(rmlib(filename)) }
|
|
|
|
|
session::os_linux. { rmext(rmlib(filename)) }
|
|
|
|
|
_ { rmext(filename) }
|
|
|
|
|
};
|
2011-07-07 14:59:18 -04:00
|
|
|
}
|
2011-06-27 15:24:44 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let cstore = sess.get_cstore();
|
|
|
|
|
for cratepath: str in cstore::get_used_crate_files(cstore) {
|
|
|
|
|
if str::ends_with(cratepath, ".rlib") {
|
2011-07-08 15:03:48 -04:00
|
|
|
gcc_args += [cratepath];
|
|
|
|
|
cont;
|
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let dir = fs::dirname(cratepath);
|
|
|
|
|
if dir != "" { gcc_args += ["-L" + dir]; }
|
|
|
|
|
let libarg = unlib(sess.get_targ_cfg(), fs::basename(cratepath));
|
2011-07-07 14:59:18 -04:00
|
|
|
gcc_args += ["-l" + libarg];
|
|
|
|
|
}
|
2011-06-20 17:36:15 -04:00
|
|
|
|
2011-07-14 17:26:10 -07:00
|
|
|
// FIXME: Remove this ivec->vec conversion.
|
2011-07-27 14:19:39 +02:00
|
|
|
let ula = cstore::get_used_link_args(cstore);
|
|
|
|
|
for arg: str in ula { gcc_args += [arg]; }
|
2011-07-14 17:26:10 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let used_libs = cstore::get_used_libraries(cstore);
|
|
|
|
|
for l: str in used_libs { gcc_args += ["-l" + l]; }
|
2011-06-16 16:55:46 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if sopts.library {
|
2011-07-07 14:59:18 -04:00
|
|
|
gcc_args += [lib_cmd];
|
|
|
|
|
} else {
|
|
|
|
|
// FIXME: why do we hardcode -lm?
|
|
|
|
|
gcc_args += ["-lm", main];
|
|
|
|
|
}
|
|
|
|
|
// We run 'gcc' here
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let err_code = run::run_program(prog, gcc_args);
|
|
|
|
|
if 0 != err_code {
|
2011-07-07 14:59:18 -04:00
|
|
|
sess.err(#fmt("linking with gcc failed with code %d", err_code));
|
|
|
|
|
sess.note(#fmt("gcc arguments: %s", str::connect(gcc_args, " ")));
|
|
|
|
|
sess.abort_if_errors();
|
|
|
|
|
}
|
|
|
|
|
// Clean up on Darwin
|
2011-05-16 09:01:36 -06:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if sess.get_targ_cfg().os == session::os_macos {
|
2011-07-07 14:59:18 -04:00
|
|
|
run::run_program("dsymutil", [saved_out_filename]);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-07-07 14:59:18 -04:00
|
|
|
// Remove the temporary object file if we aren't saving temps
|
2011-07-27 14:19:39 +02:00
|
|
|
if !sopts.save_temps {
|
2011-07-07 14:59:18 -04:00
|
|
|
run::run_program("rm", [saved_out_filename + ".o"]);
|
2011-05-16 09:01:36 -06:00
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
2011-07-13 18:13:19 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
|
|
|
|
|
import std::ivec;
|
|
|
|
|
|
|
|
|
|
// When the user supplies --test we should implicitly supply --cfg test
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_switch_implies_cfg_test() {
|
2011-07-27 14:19:39 +02:00
|
|
|
let match =
|
|
|
|
|
alt getopts::getopts(["--test"], opts()) {
|
|
|
|
|
getopts::success(m) { m }
|
|
|
|
|
};
|
|
|
|
|
let sessopts = build_session_options("whatever", match, "whatever");
|
|
|
|
|
let sess = build_session(sessopts);
|
|
|
|
|
let cfg = build_configuration(sess, "whatever", "whatever");
|
|
|
|
|
assert (attr::contains_name(cfg, "test"));
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() {
|
2011-07-27 14:19:39 +02:00
|
|
|
let match =
|
|
|
|
|
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
|
|
|
|
|
getopts::success(m) { m }
|
|
|
|
|
};
|
|
|
|
|
let sessopts = build_session_options("whatever", match, "whatever");
|
|
|
|
|
let sess = build_session(sessopts);
|
|
|
|
|
let cfg = build_configuration(sess, "whatever", "whatever");
|
|
|
|
|
let test_items = attr::find_meta_items_by_name(cfg, "test");
|
|
|
|
|
assert (ivec::len(test_items) == 1u);
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 10:29:23 -07:00
|
|
|
// Local Variables:
|
|
|
|
|
// mode: rust
|
|
|
|
|
// fill-column: 78;
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// c-basic-offset: 4
|
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 15:07:27 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-08-12 10:29:23 -07:00
|
|
|
// End:
|