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.
17 This API is completely unstable and subject to change.
21 #[crate_id = "rustc#0.10-pre"];
22 #[comment = "The Rust compiler"];
23 #[license = "MIT/ASL2"];
24 #[crate_type = "dylib"];
25 #[crate_type = "rlib"];
26 #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
27 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
28 html_root_url = "http://static.rust-lang.org/doc/master")];
30 #[feature(macro_rules, globs, struct_variant, managed_boxes)];
33 #[allow(visible_private_types)];
39 extern crate serialize;
42 extern crate collections;
49 use d = driver::driver;
58 use syntax::diagnostic::Emitter;
59 use syntax::diagnostic;
68 pub mod resolve_lifetime;
77 pub mod mem_categorization;
100 pub mod assign_node_ids_and_map;
101 pub mod feature_gate;
134 pub fn version(argv0: &str) {
135 let vers = match option_env!("CFG_VERSION") {
137 None => "unknown version"
139 println!("{} {}", argv0, vers);
140 println!("host: {}", d::host_triple());
143 pub fn usage(argv0: &str) {
144 let message = format!("Usage: {} [OPTIONS] INPUT", argv0);
147 -C help Print codegen options
148 -W help Print 'lint' options and default settings
149 -Z help Print internal options for debugging rustc\n",
150 getopts::usage(message, d::optgroups()));
153 pub fn describe_warnings() {
155 Available lint options:
156 -W <foo> Warn about <foo>
159 -F <foo> Forbid <foo> (deny, and deny all overrides)
162 let lint_dict = lint::get_lint_dict();
163 let mut lint_dict = lint_dict.move_iter()
164 .map(|(k, v)| (v, k))
165 .collect::<~[(lint::LintSpec, &'static str)]>();
169 for &(_, name) in lint_dict.iter() {
170 max_key = cmp::max(name.len(), max_key);
172 fn padded(max: uint, s: &str) -> ~str {
173 " ".repeat(max - s.len()) + s
175 println!("\nAvailable lint checks:\n");
176 println!(" {} {:7.7s} {}",
177 padded(max_key, "name"), "default", "meaning");
178 println!(" {} {:7.7s} {}\n",
179 padded(max_key, "----"), "-------", "-------");
180 for (spec, name) in lint_dict.move_iter() {
181 let name = name.replace("_", "-");
182 println!(" {} {:7.7s} {}",
183 padded(max_key, name),
184 lint::level_to_str(spec.default),
190 pub fn describe_debug_flags() {
191 println!("\nAvailable debug options:\n");
192 let r = session::debugging_opts_map();
193 for tuple in r.iter() {
195 (ref name, ref desc, _) => {
196 println!(" -Z {:>20s} -- {}", *name, *desc);
202 pub fn describe_codegen_flags() {
203 println!("\nAvailable codegen options:\n");
204 let mut cg = session::basic_codegen_options();
205 for &(name, parser, desc) in session::CG_OPTIONS.iter() {
206 // we invoke the parser function on `None` to see if this option needs
207 // an argument or not.
208 let (width, extra) = if parser(&mut cg, None) {
213 println!(" -C {:>width$s}{} -- {}", name.replace("_", "-"),
214 extra, desc, width=width);
218 pub fn run_compiler(args: &[~str]) {
219 let mut args = args.to_owned();
220 let binary = args.shift().unwrap();
222 if args.is_empty() { usage(binary); return; }
225 &match getopts::getopts(args, d::optgroups()) {
228 d::early_error(f.to_err_msg());
232 if matches.opt_present("h") || matches.opt_present("help") {
237 let lint_flags = vec::append(matches.opt_strs("W"),
238 matches.opt_strs("warn"));
239 if lint_flags.iter().any(|x| x == &~"help") {
244 let r = matches.opt_strs("Z");
245 if r.iter().any(|x| x == &~"help") {
246 describe_debug_flags();
250 let cg_flags = matches.opt_strs("C");
251 if cg_flags.iter().any(|x| x == &~"help") {
252 describe_codegen_flags();
256 if cg_flags.contains(&~"passes=list") {
257 unsafe { lib::llvm::llvm::LLVMRustPrintPasses(); }
261 if matches.opt_present("v") || matches.opt_present("version") {
265 let (input, input_file_path) = match matches.free.len() {
266 0u => d::early_error("no input filename given"),
268 let ifile = matches.free[0].as_slice();
270 let contents = io::stdin().read_to_end().unwrap();
271 let src = str::from_utf8_owned(contents).unwrap();
272 (d::StrInput(src), None)
274 (d::FileInput(Path::new(ifile)), Some(Path::new(ifile)))
277 _ => d::early_error("multiple input filenames provided")
280 let sopts = d::build_session_options(matches);
281 let sess = d::build_session(sopts, input_file_path);
282 let odir = matches.opt_str("out-dir").map(|o| Path::new(o));
283 let ofile = matches.opt_str("o").map(|o| Path::new(o));
284 let cfg = d::build_configuration(sess);
285 let pretty = matches.opt_default("pretty", "normal").map(|a| {
286 d::parse_pretty(sess, a)
289 Some::<d::PpMode>(ppm) => {
290 d::pretty_print_input(sess, cfg, &input, ppm);
293 None::<d::PpMode> => {/* continue */ }
295 let ls = matches.opt_present("ls");
298 d::FileInput(ref ifile) => {
299 let mut stdout = io::stdout();
300 d::list_metadata(sess, &(*ifile), &mut stdout).unwrap();
303 d::early_error("can not list metadata for stdin");
308 let (crate_id, crate_name, crate_file_name) = sopts.print_metas;
309 // these nasty nested conditions are to avoid doing extra work
310 if crate_id || crate_name || crate_file_name {
311 let attrs = parse_crate_attrs(sess, &input);
312 let t_outputs = d::build_output_filenames(&input, &odir, &ofile,
314 let id = link::find_crate_id(attrs, &t_outputs);
317 println!("{}", id.to_str());
320 println!("{}", id.name);
323 let crate_types = session::collect_crate_types(&sess, attrs);
324 for &style in crate_types.iter() {
325 let fname = link::filename_for_input(&sess, style, &id,
326 &t_outputs.with_extension(""));
327 println!("{}", fname.filename_display());
334 d::compile_input(sess, cfg, &input, &odir, &ofile);
337 fn parse_crate_attrs(sess: session::Session,
338 input: &d::Input) -> ~[ast::Attribute] {
340 d::FileInput(ref ifile) => {
341 parse::parse_crate_attrs_from_file(ifile, ~[], sess.parse_sess)
343 d::StrInput(ref src) => {
344 parse::parse_crate_attrs_from_source_str(d::anon_src(),
352 /// Run a procedure which will detect failures in the compiler and print nicer
353 /// error messages rather than just failing the test.
355 /// The diagnostic emitter yielded to the procedure should be used for reporting
356 /// errors of the compiler.
357 pub fn monitor(f: proc()) {
358 // FIXME: This is a hack for newsched since it doesn't support split stacks.
359 // rustc needs a lot of stack! When optimizations are disabled, it needs
360 // even *more* stack than usual as well.
362 static STACK_SIZE: uint = 6000000; // 6MB
364 static STACK_SIZE: uint = 20000000; // 20MB
366 let mut task_builder = task::task().named("rustc");
368 // FIXME: Hacks on hacks. If the env is trying to override the stack size
369 // then *don't* set it explicitly.
370 if os::getenv("RUST_MIN_STACK").is_none() {
371 task_builder.opts.stack_size = Some(STACK_SIZE);
374 let (p, c) = Chan::new();
375 let w = io::ChanWriter::new(c);
376 let mut r = io::PortReader::new(p);
378 match task_builder.try(proc() {
379 io::stdio::set_stderr(~w as ~io::Writer);
382 Ok(()) => { /* fallthrough */ }
384 // Task failed without emitting a fatal diagnostic
385 if !value.is::<diagnostic::FatalError>() {
386 let mut emitter = diagnostic::EmitterWriter::stderr();
389 diagnostic::ice_msg("unexpected failure"),
393 ~"the compiler hit an unexpected failure path. \
396 for note in xs.iter() {
397 emitter.emit(None, *note, diagnostic::Note)
400 println!("{}", r.read_to_str());
403 // Fail so the process returns a failure code, but don't pollute the
404 // output with some unnecessary failure messages, we've already
405 // printed everything that we needed to.
406 io::stdio::set_stderr(~io::util::NullWriter as ~io::Writer);
413 std::os::set_exit_status(main_args(std::os::args()));
416 pub fn main_args(args: &[~str]) -> int {
417 let owned_args = args.to_owned();
418 monitor(proc() run_compiler(owned_args));