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