]> git.lizzy.rs Git - rust.git/blob - src/librustc/lib.rs
2e3b9be5f9b86ec19c18d1a9f1554700e29dce6f
[rust.git] / src / librustc / lib.rs
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.
4 //
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.
10
11 /*!
12
13 The Rust compiler.
14
15 # Note
16
17 This API is completely unstable and subject to change.
18
19 */
20
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")];
29
30 #[feature(macro_rules, globs, struct_variant, managed_boxes)];
31 #[feature(quote)];
32
33 #[allow(visible_private_types)];
34
35 extern crate extra;
36 extern crate flate;
37 extern crate arena;
38 extern crate syntax;
39 extern crate serialize;
40 extern crate sync;
41 extern crate getopts;
42 extern crate collections;
43 extern crate time;
44
45 use back::link;
46 use driver::session;
47 use middle::lint;
48
49 use d = driver::driver;
50
51 use std::cmp;
52 use std::io;
53 use std::os;
54 use std::str;
55 use std::task;
56 use std::vec;
57 use syntax::ast;
58 use syntax::diagnostic::Emitter;
59 use syntax::diagnostic;
60 use syntax::parse;
61
62 pub mod middle {
63     pub mod trans;
64     pub mod ty;
65     pub mod ty_fold;
66     pub mod subst;
67     pub mod resolve;
68     pub mod resolve_lifetime;
69     pub mod typeck;
70     pub mod check_loop;
71     pub mod check_match;
72     pub mod check_const;
73     pub mod check_static;
74     pub mod lint;
75     pub mod borrowck;
76     pub mod dataflow;
77     pub mod mem_categorization;
78     pub mod liveness;
79     pub mod kind;
80     pub mod freevars;
81     pub mod pat_util;
82     pub mod region;
83     pub mod const_eval;
84     pub mod astencode;
85     pub mod lang_items;
86     pub mod privacy;
87     pub mod moves;
88     pub mod entry;
89     pub mod effect;
90     pub mod reachable;
91     pub mod graph;
92     pub mod cfg;
93     pub mod dead;
94 }
95
96 pub mod front {
97     pub mod config;
98     pub mod test;
99     pub mod std_inject;
100     pub mod assign_node_ids_and_map;
101     pub mod feature_gate;
102     pub mod show_span;
103 }
104
105 pub mod back {
106     pub mod abi;
107     pub mod archive;
108     pub mod arm;
109     pub mod link;
110     pub mod lto;
111     pub mod mips;
112     pub mod rpath;
113     pub mod svh;
114     pub mod target_strs;
115     pub mod x86;
116     pub mod x86_64;
117 }
118
119 pub mod metadata;
120
121 pub mod driver;
122
123 pub mod util {
124     pub mod common;
125     pub mod ppaux;
126     pub mod sha2;
127 }
128
129 pub mod lib {
130     pub mod llvm;
131     pub mod llvmdeps;
132 }
133
134 pub fn version(argv0: &str) {
135     let vers = match option_env!("CFG_VERSION") {
136         Some(vers) => vers,
137         None => "unknown version"
138     };
139     println!("{} {}", argv0, vers);
140     println!("host: {}", d::host_triple());
141 }
142
143 pub fn usage(argv0: &str) {
144     let message = format!("Usage: {} [OPTIONS] INPUT", argv0);
145     println!("{}\n\
146 Additional help:
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()));
151 }
152
153 pub fn describe_warnings() {
154     println!("
155 Available lint options:
156     -W <foo>           Warn about <foo>
157     -A <foo>           Allow <foo>
158     -D <foo>           Deny <foo>
159     -F <foo>           Forbid <foo> (deny, and deny all overrides)
160 ");
161
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)]>();
166     lint_dict.sort();
167
168     let mut max_key = 0;
169     for &(_, name) in lint_dict.iter() {
170         max_key = cmp::max(name.len(), max_key);
171     }
172     fn padded(max: uint, s: &str) -> ~str {
173         " ".repeat(max - s.len()) + s
174     }
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),
185                  spec.desc);
186     }
187     println!("");
188 }
189
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() {
194         match *tuple {
195             (ref name, ref desc, _) => {
196                 println!("    -Z {:>20s} -- {}", *name, *desc);
197             }
198         }
199     }
200 }
201
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) {
209             (25, "")
210         } else {
211             (21, "=val")
212         };
213         println!("    -C {:>width$s}{} -- {}", name.replace("_", "-"),
214                  extra, desc, width=width);
215     }
216 }
217
218 pub fn run_compiler(args: &[~str]) {
219     let mut args = args.to_owned();
220     let binary = args.shift().unwrap();
221
222     if args.is_empty() { usage(binary); return; }
223
224     let matches =
225         &match getopts::getopts(args, d::optgroups()) {
226           Ok(m) => m,
227           Err(f) => {
228             d::early_error(f.to_err_msg());
229           }
230         };
231
232     if matches.opt_present("h") || matches.opt_present("help") {
233         usage(binary);
234         return;
235     }
236
237     let lint_flags = vec::append(matches.opt_strs("W"),
238                                  matches.opt_strs("warn"));
239     if lint_flags.iter().any(|x| x == &~"help") {
240         describe_warnings();
241         return;
242     }
243
244     let r = matches.opt_strs("Z");
245     if r.iter().any(|x| x == &~"help") {
246         describe_debug_flags();
247         return;
248     }
249
250     let cg_flags = matches.opt_strs("C");
251     if cg_flags.iter().any(|x| x == &~"help") {
252         describe_codegen_flags();
253         return;
254     }
255
256     if cg_flags.contains(&~"passes=list") {
257         unsafe { lib::llvm::llvm::LLVMRustPrintPasses(); }
258         return;
259     }
260
261     if matches.opt_present("v") || matches.opt_present("version") {
262         version(binary);
263         return;
264     }
265     let (input, input_file_path) = match matches.free.len() {
266       0u => d::early_error("no input filename given"),
267       1u => {
268         let ifile = matches.free[0].as_slice();
269         if ifile == "-" {
270             let contents = io::stdin().read_to_end().unwrap();
271             let src = str::from_utf8_owned(contents).unwrap();
272             (d::StrInput(src), None)
273         } else {
274             (d::FileInput(Path::new(ifile)), Some(Path::new(ifile)))
275         }
276       }
277       _ => d::early_error("multiple input filenames provided")
278     };
279
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)
287     });
288     match pretty {
289       Some::<d::PpMode>(ppm) => {
290         d::pretty_print_input(sess, cfg, &input, ppm);
291         return;
292       }
293       None::<d::PpMode> => {/* continue */ }
294     }
295     let ls = matches.opt_present("ls");
296     if ls {
297         match input {
298           d::FileInput(ref ifile) => {
299             let mut stdout = io::stdout();
300             d::list_metadata(sess, &(*ifile), &mut stdout).unwrap();
301           }
302           d::StrInput(_) => {
303             d::early_error("can not list metadata for stdin");
304           }
305         }
306         return;
307     }
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,
313                                                   attrs, sess);
314         let id = link::find_crate_id(attrs, &t_outputs);
315
316         if crate_id {
317             println!("{}", id.to_str());
318         }
319         if crate_name {
320             println!("{}", id.name);
321         }
322         if crate_file_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());
328             }
329         }
330
331         return;
332     }
333
334     d::compile_input(sess, cfg, &input, &odir, &ofile);
335 }
336
337 fn parse_crate_attrs(sess: session::Session,
338                      input: &d::Input) -> ~[ast::Attribute] {
339     match *input {
340         d::FileInput(ref ifile) => {
341             parse::parse_crate_attrs_from_file(ifile, ~[], sess.parse_sess)
342         }
343         d::StrInput(ref src) => {
344             parse::parse_crate_attrs_from_source_str(d::anon_src(),
345                                                      (*src).clone(),
346                                                      ~[],
347                                                      sess.parse_sess)
348         }
349     }
350 }
351
352 /// Run a procedure which will detect failures in the compiler and print nicer
353 /// error messages rather than just failing the test.
354 ///
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.
361     #[cfg(rtopt)]
362     static STACK_SIZE: uint = 6000000;  // 6MB
363     #[cfg(not(rtopt))]
364     static STACK_SIZE: uint = 20000000; // 20MB
365
366     let mut task_builder = task::task().named("rustc");
367
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);
372     }
373
374     let (p, c) = Chan::new();
375     let w = io::ChanWriter::new(c);
376     let mut r = io::PortReader::new(p);
377
378     match task_builder.try(proc() {
379         io::stdio::set_stderr(~w as ~io::Writer);
380         f()
381     }) {
382         Ok(()) => { /* fallthrough */ }
383         Err(value) => {
384             // Task failed without emitting a fatal diagnostic
385             if !value.is::<diagnostic::FatalError>() {
386                 let mut emitter = diagnostic::EmitterWriter::stderr();
387                 emitter.emit(
388                     None,
389                     diagnostic::ice_msg("unexpected failure"),
390                     diagnostic::Error);
391
392                 let xs = [
393                     ~"the compiler hit an unexpected failure path. \
394                      this is a bug",
395                 ];
396                 for note in xs.iter() {
397                     emitter.emit(None, *note, diagnostic::Note)
398                 }
399
400                 println!("{}", r.read_to_str());
401             }
402
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);
407             fail!();
408         }
409     }
410 }
411
412 pub fn main() {
413     std::os::set_exit_status(main_args(std::os::args()));
414 }
415
416 pub fn main_args(args: &[~str]) -> int {
417     let owned_args = args.to_owned();
418     monitor(proc() run_compiler(owned_args));
419     0
420 }