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)];
27 extern mod core(name = "std");
28 extern mod extra(name = "extra");
31 extern mod std(name = "std", vers = "0.7-pre");
35 use driver::driver::{host_triple, optgroups, early_error};
36 use driver::driver::{str_input, file_input, build_session_options};
37 use driver::driver::{build_session, build_configuration, parse_pretty};
38 use driver::driver::{pp_mode, pretty_print_input, list_metadata};
39 use driver::driver::{compile_input};
50 use extra::getopts::{groups, opt_present};
53 use syntax::diagnostic;
56 #[path = "trans/mod.rs"]
61 #[path = "typeck/mod.rs"]
67 #[path = "borrowck/mod.rs"]
70 pub mod mem_categorization;
104 #[path = "metadata/mod.rs"]
107 #[path = "driver/mod.rs"]
120 // A curious inner module that allows ::std::foo to be available in here for
127 pub use core::to_bytes;
128 pub use core::unstable;
129 pub use extra::serialize;
132 pub fn version(argv0: &str) {
133 let mut vers = ~"unknown version";
134 let env_vers = env!("CFG_VERSION");
135 if env_vers.len() != 0 { vers = env_vers.to_owned(); }
136 io::println(fmt!("%s %s", argv0, vers));
137 io::println(fmt!("host: %s", host_triple()));
140 pub fn usage(argv0: &str) {
141 let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
142 io::println(fmt!("%s\
144 -W help Print 'lint' options and default settings
145 -Z help Print internal options for debugging rustc\n",
146 groups::usage(message, optgroups())));
149 pub fn describe_warnings() {
151 Available lint options:
152 -W <foo> Warn about <foo>
155 -F <foo> Forbid <foo> (deny, and deny all overrides)
158 let lint_dict = lint::get_lint_dict();
160 for lint_dict.each_key |k| { max_key = uint::max(k.len(), max_key); }
161 fn padded(max: uint, s: &str) -> ~str {
162 str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
164 io::println(fmt!("\nAvailable lint checks:\n"));
165 io::println(fmt!(" %s %7.7s %s",
166 padded(max_key, "name"), "default", "meaning"));
167 io::println(fmt!(" %s %7.7s %s\n",
168 padded(max_key, "----"), "-------", "-------"));
169 for lint_dict.each |k, v| {
170 let k = k.replace("_", "-");
171 io::println(fmt!(" %s %7.7s %s",
174 lint::allow => ~"allow",
175 lint::warn => ~"warn",
176 lint::deny => ~"deny",
177 lint::forbid => ~"forbid"
184 pub fn describe_debug_flags() {
185 io::println(fmt!("\nAvailable debug options:\n"));
186 let r = session::debugging_opts_map();
187 for r.iter().advance |pair| {
188 let (name, desc, _) = /*bad*/copy *pair;
189 io::println(fmt!(" -Z %-20s -- %s", name, desc));
193 pub fn run_compiler(args: &~[~str], demitter: diagnostic::Emitter) {
194 // Don't display log spew by default. Can override with RUST_LOG.
195 ::core::logging::console_off();
197 let mut args = /*bad*/copy *args;
198 let binary = args.shift().to_managed();
200 if args.is_empty() { usage(binary); return; }
203 &match getopts::groups::getopts(args, optgroups()) {
206 early_error(demitter, getopts::fail_str(f));
210 if opt_present(matches, "h") || opt_present(matches, "help") {
215 // Display the available lint options if "-W help" or only "-W" is given.
216 let lint_flags = vec::append(getopts::opt_strs(matches, "W"),
217 getopts::opt_strs(matches, "warn"));
219 let show_lint_options = lint_flags.iter().any_(|x| x == &~"help") ||
220 (opt_present(matches, "W") && lint_flags.is_empty());
222 if show_lint_options {
227 let r = getopts::opt_strs(matches, "Z");
228 if r.iter().any_(|x| x == &~"help") {
229 describe_debug_flags();
233 if getopts::opt_maybe_str(matches, "passes") == Some(~"list") {
234 back::passes::list_passes();
238 if opt_present(matches, "v") || opt_present(matches, "version") {
242 let input = match matches.free.len() {
243 0u => early_error(demitter, ~"no input filename given"),
245 let ifile = matches.free[0].as_slice();
247 let src = str::from_bytes(io::stdin().read_whole_stream());
248 str_input(src.to_managed())
250 file_input(Path(ifile))
253 _ => early_error(demitter, ~"multiple input filenames provided")
256 let sopts = build_session_options(binary, matches, demitter);
257 let sess = build_session(sopts, demitter);
258 let odir = getopts::opt_maybe_str(matches, "out-dir");
259 let odir = odir.map(|o| Path(*o));
260 let ofile = getopts::opt_maybe_str(matches, "o");
261 let ofile = ofile.map(|o| Path(*o));
262 let cfg = build_configuration(sess, binary, &input);
263 let pretty = getopts::opt_default(matches, "pretty", "normal").map(
264 |a| parse_pretty(sess, *a));
266 Some::<pp_mode>(ppm) => {
267 pretty_print_input(sess, cfg, &input, ppm);
270 None::<pp_mode> => {/* continue */ }
272 let ls = opt_present(matches, "ls");
275 file_input(ref ifile) => {
276 list_metadata(sess, &(*ifile), io::stdout());
279 early_error(demitter, ~"can not list metadata for stdin");
285 compile_input(sess, cfg, &input, &odir, &ofile);
289 pub enum monitor_msg {
295 This is a sanity check that any failure of the compiler is performed
296 through the diagnostic module and reported properly - we shouldn't be calling
297 plain-old-fail on any execution path that might be taken. Since we have
298 console logging off by default, hitting a plain fail statement would make the
299 compiler silently exit, which would be terrible.
301 This method wraps the compiler in a subtask and injects a function into the
302 diagnostic emitter which records when we hit a fatal error. If the task
303 fails without recording a fatal error then we've encountered a compiler
304 bug and need to present an error.
306 pub fn monitor(f: ~fn(diagnostic::Emitter)) {
308 let (p, ch) = stream();
309 let ch = SharedChan::new(ch);
310 let ch_capture = ch.clone();
311 match do task::try || {
312 let ch = ch_capture.clone();
313 let ch_capture = ch.clone();
314 // The 'diagnostics emitter'. Every error, warning, etc. should
315 // go through this function.
316 let demitter: @fn(Option<(@codemap::CodeMap, codemap::span)>,
320 if lvl == diagnostic::fatal {
321 ch_capture.send(fatal);
323 diagnostic::emit(cmsp, msg, lvl);
327 ch: SharedChan<monitor_msg>,
330 impl Drop for finally {
331 fn drop(&self) { self.ch.send(done); }
334 let _finally = finally { ch: ch };
338 result::Ok(_) => { /* fallthrough */ }
340 // Task failed without emitting a fatal diagnostic
341 if p.recv() == done {
344 diagnostic::ice_msg("unexpected failure"),
348 ~"the compiler hit an unexpected failure path. \
350 ~"try running with RUST_LOG=rustc=1,::rt::backtrace \
351 to get further details and report the results \
352 to github.com/mozilla/rust/issues"
354 for xs.iter().advance |note| {
355 diagnostic::emit(None, *note, diagnostic::note)
358 // Fail so the process returns a failure code
365 let args = os::args();
366 do monitor |demitter| {
367 run_compiler(&args, demitter);