1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #[link(name = "rustc",
13 uuid = "0ce89b41-2f92-459e-bbc1-8f5fe32f16cf",
14 url = "https://github.com/mozilla/rust/tree/master/src/rustc")];
16 #[comment = "The Rust compiler"];
17 #[license = "MIT/ASL2"];
18 #[crate_type = "lib"];
20 #[allow(non_implicitly_copyable_typarams)];
21 #[allow(non_camel_case_types)];
22 #[deny(deprecated_pattern)];
23 #[deny(deprecated_mode)];
25 extern mod std(vers = "0.7-pre");
26 extern mod syntax(vers = "0.7-pre");
29 use driver::driver::{host_triple, optgroups, early_error};
30 use driver::driver::{str_input, file_input, build_session_options};
31 use driver::driver::{build_session, build_configuration, parse_pretty};
32 use driver::driver::{pp_mode, pretty_print_input, list_metadata};
33 use driver::driver::{compile_input};
37 use std::getopts::{groups, opt_present};
40 use syntax::diagnostic;
80 #[path = "typeck/mod.rs"]
86 #[path = "borrowck/mod.rs"]
88 pub mod mem_categorization;
105 pub mod intrinsic_inject;
120 #[path = "metadata/mod.rs"]
123 #[path = "driver/mod.rs"]
135 pub fn version(argv0: &str) {
136 let mut vers = ~"unknown version";
137 let env_vers = env!("CFG_VERSION");
138 if env_vers.len() != 0 { vers = env_vers; }
139 io::println(fmt!("%s %s", argv0, vers));
140 io::println(fmt!("host: %s", host_triple()));
143 pub fn usage(argv0: &str) {
144 let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
145 io::println(groups::usage(message, optgroups()) +
147 -W help Print 'lint' options and default settings
148 -Z help Print internal options for debugging rustc
152 pub fn describe_warnings() {
154 Available lint options:
155 -W <foo> Warn about <foo>
158 -F <foo> Forbid <foo> (deny, and deny all overrides)
161 let lint_dict = lint::get_lint_dict();
163 for lint_dict.each_key |k| { max_key = uint::max(k.len(), max_key); }
164 fn padded(max: uint, s: &str) -> ~str {
165 str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
167 io::println(fmt!("\nAvailable lint checks:\n"));
168 io::println(fmt!(" %s %7.7s %s",
169 padded(max_key, ~"name"), ~"default", ~"meaning"));
170 io::println(fmt!(" %s %7.7s %s\n",
171 padded(max_key, ~"----"), ~"-------", ~"-------"));
172 for lint_dict.each |k, v| {
173 let k = str::replace(*k, ~"_", ~"-");
174 io::println(fmt!(" %s %7.7s %s",
177 lint::allow => ~"allow",
178 lint::warn => ~"warn",
179 lint::deny => ~"deny",
180 lint::forbid => ~"forbid"
187 pub fn describe_debug_flags() {
188 io::println(fmt!("\nAvailable debug options:\n"));
189 for session::debugging_opts_map().each |pair| {
190 let (name, desc, _) = /*bad*/copy *pair;
191 io::println(fmt!(" -Z %-20s -- %s", name, desc));
195 pub fn run_compiler(args: &~[~str], demitter: diagnostic::Emitter) {
196 // Don't display log spew by default. Can override with RUST_LOG.
197 ::core::logging::console_off();
199 let mut args = /*bad*/copy *args;
200 let binary = @args.shift();
202 if args.is_empty() { usage(*binary); return; }
205 &match getopts::groups::getopts(args, optgroups()) {
208 early_error(demitter, getopts::fail_str(f));
212 if opt_present(matches, ~"h") || opt_present(matches, ~"help") {
217 let lint_flags = vec::append(getopts::opt_strs(matches, ~"W"),
218 getopts::opt_strs(matches, ~"warn"));
219 if lint_flags.contains(&~"help") {
224 if getopts::opt_strs(matches, ~"Z").contains(&~"help") {
225 describe_debug_flags();
229 if opt_present(matches, ~"v") || opt_present(matches, ~"version") {
233 let input = match vec::len(matches.free) {
234 0u => early_error(demitter, ~"no input filename given"),
236 let ifile = /*bad*/copy matches.free[0];
238 let src = str::from_bytes(io::stdin().read_whole_stream());
241 file_input(Path(ifile))
244 _ => early_error(demitter, ~"multiple input filenames provided")
247 let sopts = build_session_options(binary, matches, demitter);
248 let sess = build_session(sopts, demitter);
249 let odir = getopts::opt_maybe_str(matches, ~"out-dir");
250 let odir = odir.map(|o| Path(*o));
251 let ofile = getopts::opt_maybe_str(matches, ~"o");
252 let ofile = ofile.map(|o| Path(*o));
253 let cfg = build_configuration(sess, binary, &input);
254 let pretty = getopts::opt_default(matches, ~"pretty", "normal").map(
255 |a| parse_pretty(sess, *a));
257 Some::<pp_mode>(ppm) => {
258 pretty_print_input(sess, cfg, &input, ppm);
261 None::<pp_mode> => {/* continue */ }
263 let ls = opt_present(matches, ~"ls");
266 file_input(ref ifile) => {
267 list_metadata(sess, &(*ifile), io::stdout());
270 early_error(demitter, ~"can not list metadata for stdin");
276 compile_input(sess, cfg, &input, &odir, &ofile);
280 pub enum monitor_msg {
286 This is a sanity check that any failure of the compiler is performed
287 through the diagnostic module and reported properly - we shouldn't be calling
288 plain-old-fail on any execution path that might be taken. Since we have
289 console logging off by default, hitting a plain fail statement would make the
290 compiler silently exit, which would be terrible.
292 This method wraps the compiler in a subtask and injects a function into the
293 diagnostic emitter which records when we hit a fatal error. If the task
294 fails without recording a fatal error then we've encountered a compiler
295 bug and need to present an error.
297 pub fn monitor(f: ~fn(diagnostic::Emitter)) {
299 let (p, ch) = stream();
300 let ch = SharedChan::new(ch);
301 let ch_capture = ch.clone();
302 match do task::try || {
303 let ch = ch_capture.clone();
304 let ch_capture = ch.clone();
305 // The 'diagnostics emitter'. Every error, warning, etc. should
306 // go through this function.
307 let demitter: @fn(Option<(@codemap::CodeMap, codemap::span)>,
311 if lvl == diagnostic::fatal {
312 ch_capture.send(fatal);
314 diagnostic::emit(cmsp, msg, lvl);
318 ch: SharedChan<monitor_msg>,
321 impl Drop for finally {
322 fn finalize(&self) { self.ch.send(done); }
325 let _finally = finally { ch: ch };
329 result::Ok(_) => { /* fallthrough */ }
331 // Task failed without emitting a fatal diagnostic
332 if p.recv() == done {
335 diagnostic::ice_msg(~"unexpected failure"),
339 ~"the compiler hit an unexpected failure path. \
341 ~"try running with RUST_LOG=rustc=1,::rt::backtrace \
342 to get further details and report the results \
343 to github.com/mozilla/rust/issues"
345 diagnostic::emit(None, *note, diagnostic::note)
348 // Fail so the process returns a failure code
355 let args = os::args();
356 do monitor |demitter| {
357 run_compiler(&args, demitter);