2012-04-05 19:30:26 -05:00
|
|
|
#[no_core];
|
|
|
|
|
|
|
|
use core(vers = "0.2");
|
|
|
|
use std(vers = "0.2");
|
|
|
|
use rustc(vers = "0.2");
|
2012-05-13 19:12:56 -05:00
|
|
|
use syntax(name = "rustsyntax", vers = "0.2");
|
2012-04-05 19:30:26 -05:00
|
|
|
|
|
|
|
import core::*;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
// -*- rust -*-
|
2011-12-15 18:11:29 -06:00
|
|
|
import result::{ok, err};
|
2012-03-12 22:04:27 -05:00
|
|
|
import std::getopts;
|
2012-04-26 15:26:45 -05:00
|
|
|
import std::map::hashmap;
|
2011-12-20 04:17:13 -06:00
|
|
|
import getopts::{opt_present};
|
|
|
|
import rustc::driver::driver::*;
|
2012-05-13 19:12:56 -05:00
|
|
|
import syntax::codemap;
|
2012-05-17 23:53:49 -05:00
|
|
|
import rustc::driver::{diagnostic, session};
|
2012-04-26 15:26:45 -05:00
|
|
|
import rustc::middle::lint;
|
2012-05-09 21:41:24 -05:00
|
|
|
import io::reader_util;
|
2011-03-04 00:22:43 -06:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn version(argv0: str) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut vers = "unknown version";
|
2011-09-01 19:37:20 -05:00
|
|
|
let env_vers = #env["CFG_VERSION"];
|
2012-02-23 03:44:04 -06:00
|
|
|
if str::len(env_vers) != 0u { vers = env_vers; }
|
2012-04-26 15:26:45 -05:00
|
|
|
io::println(#fmt("%s %s", argv0, vers));
|
|
|
|
io::println(#fmt("host: %s", host_triple()));
|
2011-05-05 14:03:23 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn usage(argv0: str) {
|
2012-04-26 15:26:45 -05:00
|
|
|
io::println(#fmt("Usage: %s [options] <input>\n", argv0) +
|
|
|
|
"
|
2012-01-30 21:02:20 -06:00
|
|
|
Options:
|
2011-04-19 05:02:06 -05:00
|
|
|
|
2012-01-30 21:02:20 -06:00
|
|
|
--bin Compile an executable crate (default)
|
|
|
|
-c Compile and assemble, but do not link
|
|
|
|
--cfg <cfgspec> Configure the compilation environment
|
|
|
|
--emit-llvm Produce an LLVM bitcode file
|
|
|
|
-g Produce debug info
|
|
|
|
--gc Garbage collect shared data (experimental/temporary)
|
|
|
|
-h --help Display this message
|
|
|
|
-L <path> Add a directory to the library search path
|
|
|
|
--lib Compile a library crate
|
|
|
|
--ls List the symbols defined by a compiled library crate
|
|
|
|
--no-trans Run all passes except translation; no output
|
|
|
|
-O Equivalent to --opt-level=2
|
|
|
|
-o <filename> Write output to <filename>
|
|
|
|
--opt-level <lvl> Optimize with possible levels 0-3
|
|
|
|
--out-dir <dir> Write output to compiler-chosen filename in <dir>
|
|
|
|
--parse-only Parse only; do not compile, assemble, or link
|
|
|
|
--pretty [type] Pretty-print the input instead of compiling;
|
2012-04-13 13:59:17 -05:00
|
|
|
valid types are: normal (un-annotated source),
|
2012-01-30 21:02:20 -06:00
|
|
|
expanded (crates expanded), typed (crates expanded,
|
|
|
|
with type annotations), or identified (fully
|
|
|
|
parenthesized, AST nodes and blocks with IDs)
|
|
|
|
-S Compile only; do not assemble or link
|
|
|
|
--save-temps Write intermediate files (.bc, .opt.bc, .o)
|
|
|
|
in addition to normal output
|
|
|
|
--static Use or produce static libraries or binaries
|
|
|
|
--stats Print compilation statistics
|
|
|
|
--sysroot <path> Override the system root
|
|
|
|
--test Build a test harness
|
|
|
|
--target <triple> Target cpu-manufacturer-kernel[-os] to compile for
|
|
|
|
(default: host triple)
|
|
|
|
(see http://sources.redhat.com/autobook/autobook/
|
|
|
|
autobook_17.html for detail)
|
2011-05-05 14:03:23 -05:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
-W <foo> enable warning <foo>
|
|
|
|
-W no-<foo> disable warning <foo>
|
|
|
|
-W err-<foo> enable warning <foo> as an error
|
2012-04-26 15:26:45 -05:00
|
|
|
-W help Print available warnings and default settings
|
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
-Z help list internal options for debugging rustc
|
2011-07-15 13:38:16 -05:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
-v --version Print version info and exit
|
2011-08-28 02:24:28 -05:00
|
|
|
");
|
2010-10-22 13:47:28 -05:00
|
|
|
}
|
|
|
|
|
2012-04-26 15:26:45 -05:00
|
|
|
fn describe_warnings() {
|
|
|
|
let lint_dict = lint::get_lint_dict();
|
|
|
|
let mut max_key = 0u;
|
|
|
|
for lint_dict.each_key {|k| max_key = uint::max(k.len(), max_key); }
|
|
|
|
fn padded(max: uint, s: str) -> str {
|
|
|
|
str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
|
|
|
|
}
|
|
|
|
io::println(#fmt("\nAvailable warnings:\n"));
|
|
|
|
io::println(#fmt(" %s %7.7s %s",
|
|
|
|
padded(max_key, "name"), "default", "meaning"));
|
|
|
|
io::println(#fmt(" %s %7.7s %s\n",
|
|
|
|
padded(max_key, "----"), "-------", "-------"));
|
|
|
|
for lint_dict.each {|k, v|
|
|
|
|
let k = str::replace(k, "_", "-");
|
|
|
|
io::println(#fmt(" %s %7.7s %s",
|
|
|
|
padded(max_key, k),
|
|
|
|
alt v.default { lint::warn { "warn" }
|
|
|
|
lint::error { "error" }
|
|
|
|
lint::ignore { "ignore" } },
|
|
|
|
v.desc));
|
|
|
|
}
|
|
|
|
io::println("");
|
|
|
|
}
|
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
fn describe_debug_flags() {
|
|
|
|
io::println(#fmt("\nAvailable debug options:\n"));
|
|
|
|
for session::debugging_opts_map().each { |pair|
|
|
|
|
let (name, desc, _) = pair;
|
|
|
|
io::println(#fmt(" -Z%-20s -- %s", name, desc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-14 02:35:46 -06:00
|
|
|
fn run_compiler(args: [str], demitter: diagnostic::emitter) {
|
2012-01-13 15:21:19 -06:00
|
|
|
// Don't display log spew by default. Can override with RUST_LOG.
|
|
|
|
logging::console_off();
|
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut args = args;
|
|
|
|
let binary = vec::shift(args);
|
2011-12-30 17:26:49 -06:00
|
|
|
|
|
|
|
if vec::len(args) == 0u { usage(binary); ret; }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let match =
|
2011-08-27 19:46:13 -05:00
|
|
|
alt getopts::getopts(args, opts()) {
|
2011-12-15 18:11:29 -06:00
|
|
|
ok(m) { m }
|
|
|
|
err(f) {
|
2012-01-14 01:18:01 -06:00
|
|
|
early_error(demitter, getopts::fail_str(f))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
};
|
2012-04-26 15:26:45 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
if opt_present(match, "h") || opt_present(match, "help") {
|
2011-05-22 15:41:32 -05:00
|
|
|
usage(binary);
|
|
|
|
ret;
|
|
|
|
}
|
2012-04-26 15:26:45 -05:00
|
|
|
|
|
|
|
let lint_flags = (getopts::opt_strs(match, "W")
|
|
|
|
+ getopts::opt_strs(match, "warn"));
|
|
|
|
if lint_flags.contains("help") {
|
|
|
|
describe_warnings();
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
if getopts::opt_strs(match, "Z").contains("help") {
|
|
|
|
describe_debug_flags();
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
if opt_present(match, "v") || opt_present(match, "version") {
|
2011-05-22 15:41:32 -05:00
|
|
|
version(binary);
|
|
|
|
ret;
|
|
|
|
}
|
2012-05-09 21:41:24 -05:00
|
|
|
let input = alt vec::len(match.free) {
|
2012-03-17 22:45:03 -05:00
|
|
|
0u { early_error(demitter, "no input filename given") }
|
2012-05-09 21:41:24 -05:00
|
|
|
1u {
|
|
|
|
let ifile = match.free[0];
|
|
|
|
if ifile == "-" {
|
|
|
|
let src = str::from_bytes(io::stdin().read_whole_stream());
|
|
|
|
str_input(src)
|
|
|
|
} else {
|
|
|
|
file_input(ifile)
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 18:27:27 -06:00
|
|
|
_ { early_error(demitter, "multiple input filenames provided") }
|
2011-10-28 13:57:01 -05:00
|
|
|
};
|
|
|
|
|
2012-01-14 01:18:01 -06:00
|
|
|
let sopts = build_session_options(match, demitter);
|
2012-03-21 17:56:20 -05:00
|
|
|
let sess = build_session(sopts, demitter);
|
2011-12-15 15:52:33 -06:00
|
|
|
let odir = getopts::opt_maybe_str(match, "out-dir");
|
2011-10-28 13:17:07 -05:00
|
|
|
let ofile = getopts::opt_maybe_str(match, "o");
|
2012-05-09 21:41:24 -05:00
|
|
|
let cfg = build_configuration(sess, binary, input);
|
2011-07-27 07:19:39 -05:00
|
|
|
let pretty =
|
2011-12-16 08:27:50 -06:00
|
|
|
option::map(getopts::opt_default(match, "pretty",
|
|
|
|
"normal"),
|
|
|
|
bind parse_pretty(sess, _));
|
2011-07-27 07:19:39 -05:00
|
|
|
alt pretty {
|
2012-05-09 21:41:24 -05:00
|
|
|
some::<pp_mode>(ppm) { pretty_print_input(sess, cfg, input, ppm); ret; }
|
2012-01-19 03:03:57 -06:00
|
|
|
none::<pp_mode> {/* continue */ }
|
2011-06-01 20:31:31 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
let ls = opt_present(match, "ls");
|
2011-10-12 14:29:08 -05:00
|
|
|
if ls {
|
2012-05-09 21:41:24 -05:00
|
|
|
alt input {
|
|
|
|
file_input(ifile) {
|
|
|
|
list_metadata(sess, ifile, io::stdout());
|
|
|
|
}
|
|
|
|
str_input(_) {
|
|
|
|
early_error(demitter, "can not list metadata for stdin");
|
|
|
|
}
|
|
|
|
}
|
2011-10-12 14:29:08 -05:00
|
|
|
ret;
|
|
|
|
}
|
2011-07-08 14:03:48 -05:00
|
|
|
|
2012-05-09 21:41:24 -05:00
|
|
|
compile_input(sess, cfg, input, odir, ofile);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2011-07-13 20:13:19 -05:00
|
|
|
|
2012-01-14 02:35:46 -06:00
|
|
|
/*
|
|
|
|
This is a sanity check that any failure of the compiler is performed
|
|
|
|
through the diagnostic module and reported properly - we shouldn't be calling
|
|
|
|
plain-old-fail on any execution path that might be taken. Since we have
|
|
|
|
console logging off by default, hitting a plain fail statement would make the
|
|
|
|
compiler silently exit, which would be terrible.
|
|
|
|
|
|
|
|
This method wraps the compiler in a subtask and injects a function into the
|
|
|
|
diagnostic emitter which records when we hit a fatal error. If the task
|
|
|
|
fails without recording a fatal error then we've encountered a compiler
|
|
|
|
bug and need to present an error.
|
|
|
|
*/
|
|
|
|
fn monitor(f: fn~(diagnostic::emitter)) {
|
2012-01-19 16:24:03 -06:00
|
|
|
enum monitor_msg {
|
2012-01-19 19:56:05 -06:00
|
|
|
fatal,
|
|
|
|
done,
|
2012-01-14 02:35:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let p = comm::port();
|
|
|
|
let ch = comm::chan(p);
|
|
|
|
|
|
|
|
alt task::try {||
|
|
|
|
|
|
|
|
// The 'diagnostics emitter'. Every error, warning, etc. should
|
|
|
|
// go through this function.
|
|
|
|
let demitter = fn@(cmsp: option<(codemap::codemap, codemap::span)>,
|
|
|
|
msg: str, lvl: diagnostic::level) {
|
|
|
|
if lvl == diagnostic::fatal {
|
|
|
|
comm::send(ch, fatal);
|
|
|
|
}
|
|
|
|
diagnostic::emit(cmsp, msg, lvl);
|
|
|
|
};
|
|
|
|
|
|
|
|
resource finally(ch: comm::chan<monitor_msg>) {
|
|
|
|
comm::send(ch, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _finally = finally(ch);
|
|
|
|
|
|
|
|
f(demitter)
|
|
|
|
} {
|
|
|
|
result::ok(_) { /* fallthrough */ }
|
|
|
|
result::err(_) {
|
|
|
|
// Task failed without emitting a fatal diagnostic
|
|
|
|
if comm::recv(p) == done {
|
|
|
|
diagnostic::emit(
|
|
|
|
none,
|
|
|
|
diagnostic::ice_msg("unexpected failure"),
|
|
|
|
diagnostic::error);
|
2012-04-28 18:38:06 -05:00
|
|
|
|
|
|
|
for [
|
|
|
|
|
|
|
|
"the compiler hit an unexpected failure path. \
|
|
|
|
this is a bug",
|
|
|
|
"try running with RUST_LOG=rustc=0,::rt::backtrace \
|
|
|
|
to get further details and report the results \
|
|
|
|
to github.com/mozilla/rust/issues"
|
|
|
|
|
|
|
|
].each {|note|
|
|
|
|
diagnostic::emit(none, note, diagnostic::note)
|
|
|
|
}
|
2012-01-14 02:35:46 -06:00
|
|
|
}
|
|
|
|
// Fail so the process returns a failure code
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main(args: [str]) {
|
|
|
|
monitor {|demitter|
|
|
|
|
run_compiler(args, demitter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// End:
|