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"];
23 use driver::driver::{host_triple, optgroups, early_error};
24 use driver::driver::{str_input, file_input, build_session_options};
25 use driver::driver::{build_session, build_configuration, parse_pretty};
26 use driver::driver::{pp_mode, pretty_print_input, list_metadata};
27 use driver::driver::{compile_input};
38 use extra::getopts::{groups, opt_present};
41 use syntax::diagnostic;
55 pub mod mem_categorization;
105 // A curious inner module that allows ::std::foo to be available in here for
114 pub use std::to_bytes;
115 pub use std::unstable;
116 pub use extra::serialize;
121 pub fn version(argv0: &str) {
122 let mut vers = ~"unknown version";
123 let env_vers = env!("CFG_VERSION");
124 if env_vers.len() != 0 { vers = env_vers.to_owned(); }
125 printfln!("%s %s", argv0, vers);
126 printfln!("host: %s", host_triple());
130 pub fn version(argv0: &str) {
131 let vers = match option_env!("CFG_VERSION") {
133 None => "unknown version"
135 printfln!("%s %s", argv0, vers);
136 printfln!("host: %s", host_triple());
139 pub fn usage(argv0: &str) {
140 let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
143 -W help Print 'lint' options and default settings
144 -Z help Print internal options for debugging rustc\n",
145 groups::usage(message, optgroups()));
148 pub fn describe_warnings() {
149 use extra::sort::Sort;
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();
159 let mut lint_dict = lint_dict.move_iter()
160 .map(|(k, v)| (v, k))
161 .collect::<~[(lint::LintSpec, &'static str)]>();
165 for &(_, name) in lint_dict.iter() {
166 max_key = num::max(name.len(), max_key);
168 fn padded(max: uint, s: &str) -> ~str {
169 str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
171 println("\nAvailable lint checks:\n");
172 printfln!(" %s %7.7s %s",
173 padded(max_key, "name"), "default", "meaning");
174 printfln!(" %s %7.7s %s\n",
175 padded(max_key, "----"), "-------", "-------");
176 for (spec, name) in lint_dict.move_iter() {
177 let name = name.replace("_", "-");
178 printfln!(" %s %7.7s %s",
179 padded(max_key, name),
180 lint::level_to_str(spec.default),
186 pub fn describe_debug_flags() {
187 println("\nAvailable debug options:\n");
188 let r = session::debugging_opts_map();
189 for tuple in r.iter() {
191 (ref name, ref desc, _) => {
192 printfln!(" -Z %-20s -- %s", *name, *desc);
198 pub fn run_compiler(args: &~[~str], demitter: diagnostic::Emitter) {
199 // Don't display log spew by default. Can override with RUST_LOG.
200 ::std::logging::console_off();
202 let mut args = (*args).clone();
203 let binary = args.shift().to_managed();
205 if args.is_empty() { usage(binary); return; }
208 &match getopts::groups::getopts(args, optgroups()) {
211 early_error(demitter, getopts::fail_str(f));
215 if opt_present(matches, "h") || opt_present(matches, "help") {
220 // Display the available lint options if "-W help" or only "-W" is given.
221 let lint_flags = vec::append(getopts::opt_strs(matches, "W"),
222 getopts::opt_strs(matches, "warn"));
224 let show_lint_options = lint_flags.iter().any(|x| x == &~"help") ||
225 (opt_present(matches, "W") && lint_flags.is_empty());
227 if show_lint_options {
232 let r = getopts::opt_strs(matches, "Z");
233 if r.iter().any(|x| x == &~"help") {
234 describe_debug_flags();
238 if getopts::opt_maybe_str(matches, "passes") == Some(~"list") {
239 back::passes::list_passes();
243 if opt_present(matches, "v") || opt_present(matches, "version") {
247 let input = match matches.free.len() {
248 0u => early_error(demitter, ~"no input filename given"),
250 let ifile = matches.free[0].as_slice();
252 let src = str::from_bytes(io::stdin().read_whole_stream());
253 str_input(src.to_managed())
255 file_input(Path(ifile))
258 _ => early_error(demitter, ~"multiple input filenames provided")
261 let sopts = build_session_options(binary, matches, demitter);
262 let sess = build_session(sopts, demitter);
263 let odir = getopts::opt_maybe_str(matches, "out-dir").map_move(|o| Path(o));
264 let ofile = getopts::opt_maybe_str(matches, "o").map_move(|o| Path(o));
265 let cfg = build_configuration(sess, binary, &input);
266 let pretty = do getopts::opt_default(matches, "pretty", "normal").map_move |a| {
267 parse_pretty(sess, a)
270 Some::<pp_mode>(ppm) => {
271 pretty_print_input(sess, cfg, &input, ppm);
274 None::<pp_mode> => {/* continue */ }
276 let ls = opt_present(matches, "ls");
279 file_input(ref ifile) => {
280 list_metadata(sess, &(*ifile), io::stdout());
283 early_error(demitter, ~"can not list metadata for stdin");
289 compile_input(sess, cfg, &input, &odir, &ofile);
293 pub enum monitor_msg {
299 This is a sanity check that any failure of the compiler is performed
300 through the diagnostic module and reported properly - we shouldn't be calling
301 plain-old-fail on any execution path that might be taken. Since we have
302 console logging off by default, hitting a plain fail statement would make the
303 compiler silently exit, which would be terrible.
305 This method wraps the compiler in a subtask and injects a function into the
306 diagnostic emitter which records when we hit a fatal error. If the task
307 fails without recording a fatal error then we've encountered a compiler
308 bug and need to present an error.
310 pub fn monitor(f: ~fn(diagnostic::Emitter)) {
313 // XXX: This is a hack for newsched since it doesn't support split stacks.
314 // rustc needs a lot of stack!
315 static STACK_SIZE: uint = 6000000;
317 let (p, ch) = stream();
318 let ch = SharedChan::new(ch);
319 let ch_capture = ch.clone();
320 let mut task_builder = task::task();
321 task_builder.supervised();
323 // XXX: Hacks on hacks. If the env is trying to override the stack size
324 // then *don't* set it explicitly.
325 if os::getenv("RUST_MIN_STACK").is_none() {
326 task_builder.opts.stack_size = Some(STACK_SIZE);
329 match do task_builder.try {
330 let ch = ch_capture.clone();
331 let ch_capture = ch.clone();
332 // The 'diagnostics emitter'. Every error, warning, etc. should
333 // go through this function.
334 let demitter: @fn(Option<(@codemap::CodeMap, codemap::span)>,
338 if lvl == diagnostic::fatal {
339 ch_capture.send(fatal);
341 diagnostic::emit(cmsp, msg, lvl);
345 ch: SharedChan<monitor_msg>,
348 impl Drop for finally {
349 fn drop(&self) { self.ch.send(done); }
352 let _finally = finally { ch: ch };
356 // Due reasons explain in #7732, if there was a jit execution context it
357 // must be consumed and passed along to our parent task.
358 back::link::jit::consume_engine()
360 result::Ok(_) => { /* fallthrough */ }
362 // Task failed without emitting a fatal diagnostic
363 if p.recv() == done {
366 diagnostic::ice_msg("unexpected failure"),
370 ~"the compiler hit an unexpected failure path. \
372 ~"try running with RUST_LOG=rustc=1,::rt::backtrace \
373 to get further details and report the results \
374 to github.com/mozilla/rust/issues"
376 for note in xs.iter() {
377 diagnostic::emit(None, *note, diagnostic::note)
380 // Fail so the process returns a failure code
387 let args = os::args();
388 do monitor |demitter| {
389 run_compiler(&args, demitter);