]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/driver.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustc_driver / driver.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 use rustc::session::Session;
12 use rustc::session::config::{self, Input, OutputFilenames};
13 use rustc::session::search_paths::PathKind;
14 use rustc::lint;
15 use rustc::metadata::creader;
16 use rustc::middle::{stability, ty, reachable};
17 use rustc::middle::dependency_format;
18 use rustc::middle;
19 use rustc::plugin::load::Plugins;
20 use rustc::plugin::registry::Registry;
21 use rustc::plugin;
22 use rustc::util::common::time;
23 use rustc_borrowck as borrowck;
24 use rustc_resolve as resolve;
25 use rustc_trans::back::link;
26 use rustc_trans::back::write;
27 use rustc_trans::save;
28 use rustc_trans::trans;
29 use rustc_typeck as typeck;
30
31 use serialize::json;
32
33 use std::io;
34 use std::io::fs;
35 use std::os;
36 use syntax::ast;
37 use syntax::ast_map;
38 use syntax::attr;
39 use syntax::attr::{AttrMetaMethods};
40 use syntax::diagnostics;
41 use syntax::parse;
42 use syntax::parse::token;
43 use syntax;
44
45 pub fn compile_input(sess: Session,
46                      cfg: ast::CrateConfig,
47                      input: &Input,
48                      outdir: &Option<Path>,
49                      output: &Option<Path>,
50                      addl_plugins: Option<Plugins>) {
51     // We need nested scopes here, because the intermediate results can keep
52     // large chunks of memory alive and we want to free them as soon as
53     // possible to keep the peak memory usage low
54     let (outputs, trans, sess) = {
55         let (outputs, expanded_crate, id) = {
56             let krate = phase_1_parse_input(&sess, cfg, input);
57             if stop_after_phase_1(&sess) { return; }
58             let outputs = build_output_filenames(input,
59                                                  outdir,
60                                                  output,
61                                                  krate.attrs[],
62                                                  &sess);
63             let id = link::find_crate_name(Some(&sess), krate.attrs[],
64                                            input);
65             let expanded_crate
66                 = match phase_2_configure_and_expand(&sess, krate, id[],
67                                                      addl_plugins) {
68                     None => return,
69                     Some(k) => k
70                 };
71
72             (outputs, expanded_crate, id)
73         };
74
75         let mut forest = ast_map::Forest::new(expanded_crate);
76         let ast_map = assign_node_ids_and_map(&sess, &mut forest);
77
78         write_out_deps(&sess, input, &outputs, id[]);
79
80         if stop_after_phase_2(&sess) { return; }
81
82         let arenas = ty::CtxtArenas::new();
83         let analysis = phase_3_run_analysis_passes(sess, ast_map, &arenas, id);
84         phase_save_analysis(&analysis.ty_cx.sess, analysis.ty_cx.map.krate(), &analysis, outdir);
85
86         if log_enabled!(::log::INFO) {
87             println!("Pre-trans");
88             analysis.ty_cx.print_debug_stats();
89         }
90
91         if stop_after_phase_3(&analysis.ty_cx.sess) { return; }
92         let (tcx, trans) = phase_4_translate_to_llvm(analysis);
93
94         if log_enabled!(::log::INFO) {
95             println!("Post-trans");
96             tcx.print_debug_stats();
97         }
98
99         // Discard interned strings as they are no longer required.
100         token::get_ident_interner().clear();
101
102         (outputs, trans, tcx.sess)
103     };
104     phase_5_run_llvm_passes(&sess, &trans, &outputs);
105     if stop_after_phase_5(&sess) { return; }
106     phase_6_link_output(&sess, &trans, &outputs);
107 }
108
109 /// The name used for source code that doesn't originate in a file
110 /// (e.g. source from stdin or a string)
111 pub fn anon_src() -> String {
112     "<anon>".to_string()
113 }
114
115 pub fn source_name(input: &Input) -> String {
116     match *input {
117         // FIXME (#9639): This needs to handle non-utf8 paths
118         Input::File(ref ifile) => ifile.as_str().unwrap().to_string(),
119         Input::Str(_) => anon_src()
120     }
121 }
122
123 pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
124     -> ast::Crate {
125     // These may be left in an incoherent state after a previous compile.
126     // `clear_tables` and `get_ident_interner().clear()` can be used to free
127     // memory, but they do not restore the initial state.
128     syntax::ext::mtwt::reset_tables();
129     token::reset_ident_interner();
130
131     let krate = time(sess.time_passes(), "parsing", (), |_| {
132         match *input {
133             Input::File(ref file) => {
134                 parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
135             }
136             Input::Str(ref src) => {
137                 parse::parse_crate_from_source_str(anon_src().to_string(),
138                                                    src.to_string(),
139                                                    cfg.clone(),
140                                                    &sess.parse_sess)
141             }
142         }
143     });
144
145     if sess.opts.debugging_opts & config::AST_JSON_NOEXPAND != 0 {
146         println!("{}", json::as_json(&krate));
147     }
148
149     if sess.show_span() {
150         syntax::show_span::run(sess.diagnostic(), &krate);
151     }
152
153     krate
154 }
155
156 // For continuing compilation after a parsed crate has been
157 // modified
158
159 /// Run the "early phases" of the compiler: initial `cfg` processing,
160 /// loading compiler plugins (including those from `addl_plugins`),
161 /// syntax expansion, secondary `cfg` expansion, synthesis of a test
162 /// harness if one is to be provided and injection of a dependency on the
163 /// standard library and prelude.
164 ///
165 /// Returns `None` if we're aborting after handling -W help.
166 pub fn phase_2_configure_and_expand(sess: &Session,
167                                     mut krate: ast::Crate,
168                                     crate_name: &str,
169                                     addl_plugins: Option<Plugins>)
170                                     -> Option<ast::Crate> {
171     let time_passes = sess.time_passes();
172
173     *sess.crate_types.borrow_mut() =
174         collect_crate_types(sess, krate.attrs[]);
175     *sess.crate_metadata.borrow_mut() =
176         collect_crate_metadata(sess, krate.attrs[]);
177
178     time(time_passes, "recursion limit", (), |_| {
179         middle::recursion_limit::update_recursion_limit(sess, &krate);
180     });
181
182     // strip before expansion to allow macros to depend on
183     // configuration variables e.g/ in
184     //
185     //   #[macro_escape] #[cfg(foo)]
186     //   mod bar { macro_rules! baz!(() => {{}}) }
187     //
188     // baz! should not use this definition unless foo is enabled.
189
190     time(time_passes, "gated macro checking", (), |_| {
191         let (features, unknown_features) =
192             syntax::feature_gate::check_crate_macros(sess.codemap(),
193                                                      &sess.parse_sess.span_diagnostic,
194                                                      &krate);
195         for uf in unknown_features.iter() {
196             sess.add_lint(lint::builtin::UNKNOWN_FEATURES,
197                           ast::CRATE_NODE_ID,
198                           *uf,
199                           "unknown feature".to_string());
200         }
201
202         // these need to be set "early" so that expansion sees `quote` if enabled.
203         *sess.features.borrow_mut() = features;
204         sess.abort_if_errors();
205     });
206
207     krate = time(time_passes, "configuration 1", krate, |krate|
208                  syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
209
210     krate = time(time_passes, "crate injection", krate, |krate|
211                  syntax::std_inject::maybe_inject_crates_ref(krate,
212                                                              sess.opts.alt_std_name.clone()));
213
214     let mut addl_plugins = Some(addl_plugins);
215     let Plugins { macros, registrars }
216         = time(time_passes, "plugin loading", (), |_|
217                plugin::load::load_plugins(sess, &krate, addl_plugins.take().unwrap()));
218
219     let mut registry = Registry::new(&krate);
220
221     time(time_passes, "plugin registration", (), |_| {
222         if sess.features.borrow().rustc_diagnostic_macros {
223             registry.register_macro("__diagnostic_used",
224                 diagnostics::plugin::expand_diagnostic_used);
225             registry.register_macro("__register_diagnostic",
226                 diagnostics::plugin::expand_register_diagnostic);
227             registry.register_macro("__build_diagnostic_array",
228                 diagnostics::plugin::expand_build_diagnostic_array);
229         }
230
231         for &registrar in registrars.iter() {
232             registrar(&mut registry);
233         }
234     });
235
236     let Registry { syntax_exts, lint_passes, lint_groups, .. } = registry;
237
238     {
239         let mut ls = sess.lint_store.borrow_mut();
240         for pass in lint_passes.into_iter() {
241             ls.register_pass(Some(sess), true, pass);
242         }
243
244         for (name, to) in lint_groups.into_iter() {
245             ls.register_group(Some(sess), true, name, to);
246         }
247     }
248
249     // Lint plugins are registered; now we can process command line flags.
250     if sess.opts.describe_lints {
251         super::describe_lints(&*sess.lint_store.borrow(), true);
252         return None;
253     }
254     sess.lint_store.borrow_mut().process_command_line(sess);
255
256     // Abort if there are errors from lint processing or a plugin registrar.
257     sess.abort_if_errors();
258
259     krate = time(time_passes, "expansion", (krate, macros, syntax_exts),
260         |(krate, macros, syntax_exts)| {
261             // Windows dlls do not have rpaths, so they don't know how to find their
262             // dependencies. It's up to us to tell the system where to find all the
263             // dependent dlls. Note that this uses cfg!(windows) as opposed to
264             // targ_cfg because syntax extensions are always loaded for the host
265             // compiler, not for the target.
266             let mut _old_path = String::new();
267             if cfg!(windows) {
268                 _old_path = os::getenv("PATH").unwrap_or(_old_path);
269                 let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
270                 new_path.extend(os::split_paths(_old_path[]).into_iter());
271                 os::setenv("PATH", os::join_paths(new_path[]).unwrap());
272             }
273             let cfg = syntax::ext::expand::ExpansionConfig {
274                 crate_name: crate_name.to_string(),
275                 deriving_hash_type_parameter: sess.features.borrow().default_type_params,
276                 enable_quotes: sess.features.borrow().quote,
277                 recursion_limit: sess.recursion_limit.get(),
278             };
279             let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
280                                               cfg,
281                                               macros,
282                                               syntax_exts,
283                                               krate);
284             if cfg!(windows) {
285                 os::setenv("PATH", _old_path);
286             }
287             ret
288         }
289     );
290
291     // Needs to go *after* expansion to be able to check the results of macro expansion.
292     time(time_passes, "complete gated feature checking", (), |_| {
293         syntax::feature_gate::check_crate(sess.codemap(),
294                                           &sess.parse_sess.span_diagnostic,
295                                           &krate);
296         sess.abort_if_errors();
297     });
298
299     // JBC: make CFG processing part of expansion to avoid this problem:
300
301     // strip again, in case expansion added anything with a #[cfg].
302     krate = time(time_passes, "configuration 2", krate, |krate|
303                  syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
304
305     krate = time(time_passes, "maybe building test harness", krate, |krate|
306                  syntax::test::modify_for_testing(&sess.parse_sess,
307                                                   &sess.opts.cfg,
308                                                   krate,
309                                                   sess.diagnostic()));
310
311     krate = time(time_passes, "prelude injection", krate, |krate|
312                  syntax::std_inject::maybe_inject_prelude(krate));
313
314     time(time_passes, "checking that all macro invocations are gone", &krate, |krate|
315          syntax::ext::expand::check_for_macros(&sess.parse_sess, krate));
316
317     Some(krate)
318 }
319
320 pub fn assign_node_ids_and_map<'ast>(sess: &Session,
321                                      forest: &'ast mut ast_map::Forest)
322                                      -> ast_map::Map<'ast> {
323     struct NodeIdAssigner<'a> {
324         sess: &'a Session
325     }
326
327     impl<'a> ast_map::FoldOps for NodeIdAssigner<'a> {
328         fn new_id(&self, old_id: ast::NodeId) -> ast::NodeId {
329             assert_eq!(old_id, ast::DUMMY_NODE_ID);
330             self.sess.next_node_id()
331         }
332     }
333
334     let map = time(sess.time_passes(), "assigning node ids and indexing ast", forest, |forest|
335                    ast_map::map_crate(forest, NodeIdAssigner { sess: sess }));
336
337     if sess.opts.debugging_opts & config::AST_JSON != 0 {
338         println!("{}", json::as_json(map.krate()));
339     }
340
341     map
342 }
343
344 /// Run the resolution, typechecking, region checking and other
345 /// miscellaneous analysis passes on the crate. Return various
346 /// structures carrying the results of the analysis.
347 pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
348                                          ast_map: ast_map::Map<'tcx>,
349                                          arenas: &'tcx ty::CtxtArenas<'tcx>,
350                                          name: String) -> ty::CrateAnalysis<'tcx> {
351     let time_passes = sess.time_passes();
352     let krate = ast_map.krate();
353
354     time(time_passes, "external crate/lib resolution", (), |_|
355          creader::read_crates(&sess, krate));
356
357     let lang_items = time(time_passes, "language item collection", (), |_|
358                           middle::lang_items::collect_language_items(krate, &sess));
359
360     let make_glob_map = if save_analysis(&sess) {
361         resolve::MakeGlobMap::Yes
362     } else {
363         resolve::MakeGlobMap::No
364     };
365     let resolve::CrateMap {
366         def_map,
367         freevars,
368         capture_mode_map,
369         export_map,
370         trait_map,
371         external_exports,
372         last_private_map,
373         glob_map,
374     } =
375         time(time_passes, "resolution", (),
376              |_| resolve::resolve_crate(&sess,
377                                         &ast_map,
378                                         &lang_items,
379                                         krate,
380                                         make_glob_map));
381
382     // Discard MTWT tables that aren't required past resolution.
383     syntax::ext::mtwt::clear_tables();
384
385     let named_region_map = time(time_passes, "lifetime resolution", (),
386                                 |_| middle::resolve_lifetime::krate(&sess, krate, &def_map));
387
388     time(time_passes, "looking for entry point", (),
389          |_| middle::entry::find_entry_point(&sess, &ast_map));
390
391     sess.plugin_registrar_fn.set(
392         time(time_passes, "looking for plugin registrar", (), |_|
393             plugin::build::find_plugin_registrar(
394                 sess.diagnostic(), krate)));
395
396     let region_map = time(time_passes, "region resolution", (), |_|
397                           middle::region::resolve_crate(&sess, krate));
398
399     time(time_passes, "loop checking", (), |_|
400          middle::check_loop::check_crate(&sess, krate));
401
402     let stability_index = time(time_passes, "stability index", (), |_|
403                                stability::Index::build(krate));
404
405     time(time_passes, "static item recursion checking", (), |_|
406          middle::check_static_recursion::check_crate(&sess, krate, &def_map, &ast_map));
407
408     let ty_cx = ty::mk_ctxt(sess,
409                             arenas,
410                             def_map,
411                             named_region_map,
412                             ast_map,
413                             freevars,
414                             capture_mode_map,
415                             region_map,
416                             lang_items,
417                             stability_index);
418
419     // passes are timed inside typeck
420     typeck::check_crate(&ty_cx, trait_map);
421
422     time(time_passes, "check static items", (), |_|
423          middle::check_static::check_crate(&ty_cx));
424
425     // These next two const passes can probably be merged
426     time(time_passes, "const marking", (), |_|
427          middle::const_eval::process_crate(&ty_cx));
428
429     time(time_passes, "const checking", (), |_|
430          middle::check_const::check_crate(&ty_cx));
431
432     let maps = (external_exports, last_private_map);
433     let (exported_items, public_items) =
434             time(time_passes, "privacy checking", maps, |(a, b)|
435                  middle::privacy::check_crate(&ty_cx, &export_map, a, b));
436
437     time(time_passes, "intrinsic checking", (), |_|
438          middle::intrinsicck::check_crate(&ty_cx));
439
440     time(time_passes, "effect checking", (), |_|
441          middle::effect::check_crate(&ty_cx));
442
443     time(time_passes, "match checking", (), |_|
444          middle::check_match::check_crate(&ty_cx));
445
446     time(time_passes, "liveness checking", (), |_|
447          middle::liveness::check_crate(&ty_cx));
448
449     time(time_passes, "borrow checking", (), |_|
450          borrowck::check_crate(&ty_cx));
451
452     time(time_passes, "rvalue checking", (), |_|
453          middle::check_rvalues::check_crate(&ty_cx, krate));
454
455     // Avoid overwhelming user with errors if type checking failed.
456     // I'm not sure how helpful this is, to be honest, but it avoids a
457     // lot of annoying errors in the compile-fail tests (basically,
458     // lint warnings and so on -- kindck used to do this abort, but
459     // kindck is gone now). -nmatsakis
460     ty_cx.sess.abort_if_errors();
461
462     let reachable_map =
463         time(time_passes, "reachability checking", (), |_|
464              reachable::find_reachable(&ty_cx, &exported_items));
465
466     time(time_passes, "death checking", (), |_| {
467         middle::dead::check_crate(&ty_cx,
468                                   &exported_items,
469                                   &reachable_map)
470     });
471
472     time(time_passes, "lint checking", (), |_|
473          lint::check_crate(&ty_cx, &exported_items));
474
475     ty::CrateAnalysis {
476         export_map: export_map,
477         ty_cx: ty_cx,
478         exported_items: exported_items,
479         public_items: public_items,
480         reachable: reachable_map,
481         name: name,
482         glob_map: glob_map,
483     }
484 }
485
486 fn save_analysis(sess: &Session) -> bool {
487     (sess.opts.debugging_opts & config::SAVE_ANALYSIS) != 0
488 }
489
490 pub fn phase_save_analysis(sess: &Session,
491                            krate: &ast::Crate,
492                            analysis: &ty::CrateAnalysis,
493                            odir: &Option<Path>) {
494     if !save_analysis(sess) {
495         return;
496     }
497     time(sess.time_passes(), "save analysis", krate, |krate|
498          save::process_crate(sess, krate, analysis, odir));
499 }
500
501 /// Run the translation phase to LLVM, after which the AST and analysis can
502 /// be discarded.
503 pub fn phase_4_translate_to_llvm<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
504                                        -> (ty::ctxt<'tcx>, trans::CrateTranslation) {
505     let time_passes = analysis.ty_cx.sess.time_passes();
506
507     time(time_passes, "resolving dependency formats", (), |_|
508          dependency_format::calculate(&analysis.ty_cx));
509
510     // Option dance to work around the lack of stack once closures.
511     time(time_passes, "translation", analysis, |analysis|
512          trans::trans_crate(analysis))
513 }
514
515 /// Run LLVM itself, producing a bitcode file, assembly file or object file
516 /// as a side effect.
517 pub fn phase_5_run_llvm_passes(sess: &Session,
518                                trans: &trans::CrateTranslation,
519                                outputs: &OutputFilenames) {
520     if sess.opts.cg.no_integrated_as {
521         let output_type = config::OutputTypeAssembly;
522
523         time(sess.time_passes(), "LLVM passes", (), |_|
524             write::run_passes(sess, trans, &[output_type], outputs));
525
526         write::run_assembler(sess, outputs);
527
528         // Remove assembly source, unless --save-temps was specified
529         if !sess.opts.cg.save_temps {
530             fs::unlink(&outputs.temp_path(config::OutputTypeAssembly)).unwrap();
531         }
532     } else {
533         time(sess.time_passes(), "LLVM passes", (), |_|
534             write::run_passes(sess,
535                               trans,
536                               sess.opts.output_types[],
537                               outputs));
538     }
539
540     sess.abort_if_errors();
541 }
542
543 /// Run the linker on any artifacts that resulted from the LLVM run.
544 /// This should produce either a finished executable or library.
545 pub fn phase_6_link_output(sess: &Session,
546                            trans: &trans::CrateTranslation,
547                            outputs: &OutputFilenames) {
548     let old_path = os::getenv("PATH").unwrap_or_else(||String::new());
549     let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
550     new_path.extend(os::split_paths(old_path[]).into_iter());
551     os::setenv("PATH", os::join_paths(new_path[]).unwrap());
552
553     time(sess.time_passes(), "linking", (), |_|
554          link::link_binary(sess,
555                            trans,
556                            outputs,
557                            trans.link.crate_name[]));
558
559     os::setenv("PATH", old_path);
560 }
561
562 pub fn stop_after_phase_3(sess: &Session) -> bool {
563    if sess.opts.no_trans {
564         debug!("invoked with --no-trans, returning early from compile_input");
565         return true;
566     }
567     return false;
568 }
569
570 pub fn stop_after_phase_1(sess: &Session) -> bool {
571     if sess.opts.parse_only {
572         debug!("invoked with --parse-only, returning early from compile_input");
573         return true;
574     }
575     if sess.show_span() {
576         return true;
577     }
578     return sess.opts.debugging_opts & config::AST_JSON_NOEXPAND != 0;
579 }
580
581 pub fn stop_after_phase_2(sess: &Session) -> bool {
582     if sess.opts.no_analysis {
583         debug!("invoked with --no-analysis, returning early from compile_input");
584         return true;
585     }
586     return sess.opts.debugging_opts & config::AST_JSON != 0;
587 }
588
589 pub fn stop_after_phase_5(sess: &Session) -> bool {
590     if !sess.opts.output_types.iter().any(|&i| i == config::OutputTypeExe) {
591         debug!("not building executable, returning early from compile_input");
592         return true;
593     }
594     return false;
595 }
596
597 fn escape_dep_filename(filename: &str) -> String {
598     // Apparently clang and gcc *only* escape spaces:
599     // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
600     filename.replace(" ", "\\ ")
601 }
602
603 fn write_out_deps(sess: &Session,
604                   input: &Input,
605                   outputs: &OutputFilenames,
606                   id: &str) {
607
608     let mut out_filenames = Vec::new();
609     for output_type in sess.opts.output_types.iter() {
610         let file = outputs.path(*output_type);
611         match *output_type {
612             config::OutputTypeExe => {
613                 for output in sess.crate_types.borrow().iter() {
614                     let p = link::filename_for_input(sess, *output,
615                                                      id, &file);
616                     out_filenames.push(p);
617                 }
618             }
619             _ => { out_filenames.push(file); }
620         }
621     }
622
623     // Write out dependency rules to the dep-info file if requested with
624     // --dep-info
625     let deps_filename = match sess.opts.write_dependency_info {
626         // Use filename from --dep-file argument if given
627         (true, Some(ref filename)) => filename.clone(),
628         // Use default filename: crate source filename with extension replaced
629         // by ".d"
630         (true, None) => match *input {
631             Input::File(..) => outputs.with_extension("d"),
632             Input::Str(..) => {
633                 sess.warn("can not write --dep-info without a filename \
634                            when compiling stdin.");
635                 return
636             },
637         },
638         _ => return,
639     };
640
641     let result = (|| -> io::IoResult<()> {
642         // Build a list of files used to compile the output and
643         // write Makefile-compatible dependency rules
644         let files: Vec<String> = sess.codemap().files.borrow()
645                                    .iter().filter(|fmap| fmap.is_real_file())
646                                    .map(|fmap| escape_dep_filename(fmap.name[]))
647                                    .collect();
648         let mut file = try!(io::File::create(&deps_filename));
649         for path in out_filenames.iter() {
650             try!(write!(&mut file as &mut Writer,
651                           "{}: {}\n\n", path.display(), files.connect(" ")));
652         }
653         Ok(())
654     })();
655
656     match result {
657         Ok(()) => {}
658         Err(e) => {
659             sess.fatal(format!("error writing dependencies to `{}`: {}",
660                                deps_filename.display(), e)[]);
661         }
662     }
663 }
664
665 pub fn collect_crate_types(session: &Session,
666                            attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
667     // Unconditionally collect crate types from attributes to make them used
668     let attr_types: Vec<config::CrateType> = attrs.iter().filter_map(|a| {
669         if a.check_name("crate_type") {
670             match a.value_str() {
671                 Some(ref n) if *n == "rlib" => {
672                     Some(config::CrateTypeRlib)
673                 }
674                 Some(ref n) if *n == "dylib" => {
675                     Some(config::CrateTypeDylib)
676                 }
677                 Some(ref n) if *n == "lib" => {
678                     Some(config::default_lib_output())
679                 }
680                 Some(ref n) if *n == "staticlib" => {
681                     Some(config::CrateTypeStaticlib)
682                 }
683                 Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
684                 Some(_) => {
685                     session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
686                                      ast::CRATE_NODE_ID,
687                                      a.span,
688                                      "invalid `crate_type` \
689                                       value".to_string());
690                     None
691                 }
692                 _ => {
693                     session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
694                                      ast::CRATE_NODE_ID,
695                                      a.span,
696                                      "`crate_type` requires a \
697                                       value".to_string());
698                     None
699                 }
700             }
701         } else {
702             None
703         }
704     }).collect();
705
706     // If we're generating a test executable, then ignore all other output
707     // styles at all other locations
708     if session.opts.test {
709         return vec!(config::CrateTypeExecutable)
710     }
711
712     // Only check command line flags if present. If no types are specified by
713     // command line, then reuse the empty `base` Vec to hold the types that
714     // will be found in crate attributes.
715     let mut base = session.opts.crate_types.clone();
716     if base.len() == 0 {
717         base.extend(attr_types.into_iter());
718         if base.len() == 0 {
719             base.push(link::default_output_for_target(session));
720         }
721         base.sort();
722         base.dedup();
723     }
724
725     base.into_iter().filter(|crate_type| {
726         let res = !link::invalid_output_for_target(session, *crate_type);
727
728         if !res {
729             session.warn(format!("dropping unsupported crate type `{}` \
730                                    for target `{}`",
731                                  *crate_type, session.opts.target_triple)[]);
732         }
733
734         res
735     }).collect()
736 }
737
738 pub fn collect_crate_metadata(session: &Session,
739                               _attrs: &[ast::Attribute]) -> Vec<String> {
740     session.opts.cg.metadata.clone()
741 }
742
743 pub fn build_output_filenames(input: &Input,
744                               odir: &Option<Path>,
745                               ofile: &Option<Path>,
746                               attrs: &[ast::Attribute],
747                               sess: &Session)
748                            -> OutputFilenames {
749     match *ofile {
750         None => {
751             // "-" as input file will cause the parser to read from stdin so we
752             // have to make up a name
753             // We want to toss everything after the final '.'
754             let dirpath = match *odir {
755                 Some(ref d) => d.clone(),
756                 None => Path::new(".")
757             };
758
759             // If a crate name is present, we use it as the link name
760             let stem = sess.opts.crate_name.clone().or_else(|| {
761                 attr::find_crate_name(attrs).map(|n| n.get().to_string())
762             }).unwrap_or(input.filestem());
763
764             OutputFilenames {
765                 out_directory: dirpath,
766                 out_filestem: stem,
767                 single_output_file: None,
768                 extra: sess.opts.cg.extra_filename.clone(),
769             }
770         }
771
772         Some(ref out_file) => {
773             let ofile = if sess.opts.output_types.len() > 1 {
774                 sess.warn("ignoring specified output filename because multiple \
775                            outputs were requested");
776                 None
777             } else {
778                 Some(out_file.clone())
779             };
780             if *odir != None {
781                 sess.warn("ignoring --out-dir flag due to -o flag.");
782             }
783             OutputFilenames {
784                 out_directory: out_file.dir_path(),
785                 out_filestem: out_file.filestem_str().unwrap().to_string(),
786                 single_output_file: ofile,
787                 extra: sess.opts.cg.extra_filename.clone(),
788             }
789         }
790     }
791 }