1 // Copyright 2012-2015 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.
12 use rustc::front::map as hir_map;
14 use rustc_mir::mir_map::MirMap;
15 use rustc::session::Session;
16 use rustc::session::config::{self, Input, OutputFilenames, OutputType};
17 use rustc::session::search_paths::PathKind;
19 use rustc::middle::{stability, ty, reachable};
20 use rustc::middle::dependency_format;
22 use rustc::util::common::time;
23 use rustc_borrowck as borrowck;
24 use rustc_resolve as resolve;
25 use rustc_metadata::macro_import;
26 use rustc_metadata::creader::LocalCrateReader;
27 use rustc_metadata::cstore::CStore;
28 use rustc_trans::back::link;
29 use rustc_trans::back::write;
30 use rustc_trans::trans;
31 use rustc_typeck as typeck;
33 use rustc_plugin::registry::Registry;
34 use rustc_plugin as plugin;
36 use rustc_front::lowering::{lower_crate, LoweringContext};
37 use rustc_passes::{no_asm, loops, consts, const_fn, rvalues, static_recursion};
38 use super::Compilation;
42 use std::collections::HashMap;
44 use std::ffi::{OsString, OsStr};
46 use std::io::{self, Write};
47 use std::path::{Path, PathBuf};
48 use syntax::ast::{self, NodeIdAssigner};
50 use syntax::attr::AttrMetaMethods;
51 use syntax::diagnostics;
52 use syntax::fold::Folder;
54 use syntax::parse::token;
55 use syntax::util::node_count::NodeCounter;
60 pub fn compile_input(sess: Session,
62 cfg: ast::CrateConfig,
64 outdir: &Option<PathBuf>,
65 output: &Option<PathBuf>,
66 addl_plugins: Option<Vec<String>>,
67 control: CompileController) {
68 macro_rules! controller_entry_point{($point: ident, $tsess: expr, $make_state: expr) => ({
69 let state = $make_state;
70 (control.$point.callback)(state);
72 $tsess.abort_if_errors();
73 if control.$point.stop == Compilation::Stop {
78 // We need nested scopes here, because the intermediate results can keep
79 // large chunks of memory alive and we want to free them as soon as
80 // possible to keep the peak memory usage low
82 let (outputs, expanded_crate, id) = {
83 let krate = phase_1_parse_input(&sess, cfg, input);
85 controller_entry_point!(after_parse,
87 CompileState::state_after_parse(input, &sess, outdir, &krate));
89 let outputs = build_output_filenames(input, outdir, output, &krate.attrs, &sess);
90 let id = link::find_crate_name(Some(&sess), &krate.attrs, input);
91 let expanded_crate = match phase_2_configure_and_expand(&sess,
100 (outputs, expanded_crate, id)
103 controller_entry_point!(after_expand,
105 CompileState::state_after_expand(input,
111 let expanded_crate = assign_node_ids(&sess, expanded_crate);
113 let lcx = LoweringContext::new(&sess, Some(&expanded_crate));
114 let mut hir_forest = time(sess.time_passes(),
115 "lowering ast -> hir",
116 || hir_map::Forest::new(lower_crate(&lcx, &expanded_crate)));
118 // Discard MTWT tables that aren't required past lowering to HIR.
119 if !sess.opts.debugging_opts.keep_mtwt_tables &&
120 !sess.opts.debugging_opts.save_analysis {
121 syntax::ext::mtwt::clear_tables();
124 let arenas = ty::CtxtArenas::new();
125 let hir_map = make_map(&sess, &mut hir_forest);
127 write_out_deps(&sess, &outputs, &id);
129 controller_entry_point!(after_write_deps,
131 CompileState::state_after_write_deps(input,
140 time(sess.time_passes(), "attribute checking", || {
141 front::check_attr::check_crate(&sess, &expanded_crate);
144 time(sess.time_passes(),
146 || lint::check_ast_crate(&sess, &expanded_crate));
148 let opt_crate = if sess.opts.debugging_opts.keep_ast ||
149 sess.opts.debugging_opts.save_analysis {
150 Some(&expanded_crate)
152 drop(expanded_crate);
156 phase_3_run_analysis_passes(&sess,
161 control.make_glob_map,
162 |tcx, mir_map, analysis| {
166 CompileState::state_after_analysis(input,
176 (control.after_analysis.callback)(state);
178 tcx.sess.abort_if_errors();
179 if control.after_analysis.stop == Compilation::Stop {
184 if log_enabled!(::log::INFO) {
185 println!("Pre-trans");
186 tcx.print_debug_stats();
188 let trans = phase_4_translate_to_llvm(tcx,
192 if log_enabled!(::log::INFO) {
193 println!("Post-trans");
194 tcx.print_debug_stats();
197 // Discard interned strings as they are no longer required.
198 token::get_ident_interner().clear();
204 let (outputs, trans) = if let Ok(out) = result {
210 phase_5_run_llvm_passes(&sess, &trans, &outputs);
212 controller_entry_point!(after_llvm,
214 CompileState::state_after_llvm(input, &sess, outdir, &trans));
216 phase_6_link_output(&sess, &trans, &outputs);
219 /// The name used for source code that doesn't originate in a file
220 /// (e.g. source from stdin or a string)
221 pub fn anon_src() -> String {
225 pub fn source_name(input: &Input) -> String {
227 // FIXME (#9639): This needs to handle non-utf8 paths
228 Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
229 Input::Str(_) => anon_src(),
233 /// CompileController is used to customise compilation, it allows compilation to
234 /// be stopped and/or to call arbitrary code at various points in compilation.
235 /// It also allows for various flags to be set to influence what information gets
236 /// collected during compilation.
238 /// This is a somewhat higher level controller than a Session - the Session
239 /// controls what happens in each phase, whereas the CompileController controls
240 /// whether a phase is run at all and whether other code (from outside the
241 /// the compiler) is run between phases.
243 /// Note that if compilation is set to stop and a callback is provided for a
244 /// given entry point, the callback is called before compilation is stopped.
246 /// Expect more entry points to be added in the future.
247 pub struct CompileController<'a> {
248 pub after_parse: PhaseController<'a>,
249 pub after_expand: PhaseController<'a>,
250 pub after_write_deps: PhaseController<'a>,
251 pub after_analysis: PhaseController<'a>,
252 pub after_llvm: PhaseController<'a>,
254 pub make_glob_map: resolve::MakeGlobMap,
257 impl<'a> CompileController<'a> {
258 pub fn basic() -> CompileController<'a> {
260 after_parse: PhaseController::basic(),
261 after_expand: PhaseController::basic(),
262 after_write_deps: PhaseController::basic(),
263 after_analysis: PhaseController::basic(),
264 after_llvm: PhaseController::basic(),
265 make_glob_map: resolve::MakeGlobMap::No,
270 pub struct PhaseController<'a> {
271 pub stop: Compilation,
272 pub callback: Box<Fn(CompileState) -> () + 'a>,
275 impl<'a> PhaseController<'a> {
276 pub fn basic() -> PhaseController<'a> {
278 stop: Compilation::Continue,
279 callback: box |_| {},
284 /// State that is passed to a callback. What state is available depends on when
285 /// during compilation the callback is made. See the various constructor methods
286 /// (`state_*`) in the impl to see which data is provided for any given entry point.
287 pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
288 pub input: &'a Input,
289 pub session: &'a Session,
290 pub cfg: Option<&'a ast::CrateConfig>,
291 pub krate: Option<&'a ast::Crate>,
292 pub crate_name: Option<&'a str>,
293 pub output_filenames: Option<&'a OutputFilenames>,
294 pub out_dir: Option<&'a Path>,
295 pub expanded_crate: Option<&'a ast::Crate>,
296 pub hir_crate: Option<&'a hir::Crate>,
297 pub ast_map: Option<&'a hir_map::Map<'ast>>,
298 pub mir_map: Option<&'a MirMap<'tcx>>,
299 pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
300 pub tcx: Option<&'a ty::ctxt<'tcx>>,
301 pub lcx: Option<&'a LoweringContext<'a>>,
302 pub trans: Option<&'a trans::CrateTranslation>,
305 impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
306 fn empty(input: &'a Input,
307 session: &'a Session,
308 out_dir: &'a Option<PathBuf>)
309 -> CompileState<'a, 'ast, 'tcx> {
313 out_dir: out_dir.as_ref().map(|s| &**s),
317 output_filenames: None,
318 expanded_crate: None,
329 fn state_after_parse(input: &'a Input,
330 session: &'a Session,
331 out_dir: &'a Option<PathBuf>,
332 krate: &'a ast::Crate)
333 -> CompileState<'a, 'ast, 'tcx> {
334 CompileState { krate: Some(krate), ..CompileState::empty(input, session, out_dir) }
337 fn state_after_expand(input: &'a Input,
338 session: &'a Session,
339 out_dir: &'a Option<PathBuf>,
340 expanded_crate: &'a ast::Crate,
342 -> CompileState<'a, 'ast, 'tcx> {
344 crate_name: Some(crate_name),
345 expanded_crate: Some(expanded_crate),
346 ..CompileState::empty(input, session, out_dir)
350 fn state_after_write_deps(input: &'a Input,
351 session: &'a Session,
352 out_dir: &'a Option<PathBuf>,
353 hir_map: &'a hir_map::Map<'ast>,
354 krate: &'a ast::Crate,
355 hir_crate: &'a hir::Crate,
357 lcx: &'a LoweringContext<'a>)
358 -> CompileState<'a, 'ast, 'tcx> {
360 crate_name: Some(crate_name),
361 ast_map: Some(hir_map),
363 hir_crate: Some(hir_crate),
365 ..CompileState::empty(input, session, out_dir)
369 fn state_after_analysis(input: &'a Input,
370 session: &'a Session,
371 out_dir: &'a Option<PathBuf>,
372 krate: Option<&'a ast::Crate>,
373 hir_crate: &'a hir::Crate,
374 analysis: &'a ty::CrateAnalysis,
375 mir_map: &'a MirMap<'tcx>,
376 tcx: &'a ty::ctxt<'tcx>,
377 lcx: &'a LoweringContext<'a>,
379 -> CompileState<'a, 'ast, 'tcx> {
381 analysis: Some(analysis),
382 mir_map: Some(mir_map),
385 hir_crate: Some(hir_crate),
387 crate_name: Some(crate_name),
388 ..CompileState::empty(input, session, out_dir)
393 fn state_after_llvm(input: &'a Input,
394 session: &'a Session,
395 out_dir: &'a Option<PathBuf>,
396 trans: &'a trans::CrateTranslation)
397 -> CompileState<'a, 'ast, 'tcx> {
398 CompileState { trans: Some(trans), ..CompileState::empty(input, session, out_dir) }
402 pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input) -> ast::Crate {
403 // These may be left in an incoherent state after a previous compile.
404 // `clear_tables` and `get_ident_interner().clear()` can be used to free
405 // memory, but they do not restore the initial state.
406 syntax::ext::mtwt::reset_tables();
407 token::reset_ident_interner();
409 let krate = time(sess.time_passes(), "parsing", || {
411 Input::File(ref file) => {
412 parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
414 Input::Str(ref src) => {
415 parse::parse_crate_from_source_str(anon_src().to_string(),
423 if sess.opts.debugging_opts.ast_json_noexpand {
424 println!("{}", json::as_json(&krate));
427 if sess.opts.debugging_opts.input_stats {
428 println!("Lines of code: {}", sess.codemap().count_lines());
429 println!("Pre-expansion node count: {}", count_nodes(&krate));
432 if let Some(ref s) = sess.opts.debugging_opts.show_span {
433 syntax::show_span::run(sess.diagnostic(), s, &krate);
439 fn count_nodes(krate: &ast::Crate) -> usize {
440 let mut counter = NodeCounter::new();
441 visit::walk_crate(&mut counter, krate);
445 // For continuing compilation after a parsed crate has been
448 /// Run the "early phases" of the compiler: initial `cfg` processing,
449 /// loading compiler plugins (including those from `addl_plugins`),
450 /// syntax expansion, secondary `cfg` expansion, synthesis of a test
451 /// harness if one is to be provided and injection of a dependency on the
452 /// standard library and prelude.
454 /// Returns `None` if we're aborting after handling -W help.
455 pub fn phase_2_configure_and_expand(sess: &Session,
457 mut krate: ast::Crate,
459 addl_plugins: Option<Vec<String>>)
460 -> Option<ast::Crate> {
461 let time_passes = sess.time_passes();
463 // strip before anything else because crate metadata may use #[cfg_attr]
464 // and so macros can depend on configuration variables, such as
466 // #[macro_use] #[cfg(foo)]
467 // mod bar { macro_rules! baz!(() => {{}}) }
469 // baz! should not use this definition unless foo is enabled.
471 let mut feature_gated_cfgs = vec![];
472 krate = time(time_passes, "configuration 1", || {
473 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
476 *sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
477 *sess.crate_metadata.borrow_mut() = collect_crate_metadata(sess, &krate.attrs);
479 time(time_passes, "recursion limit", || {
480 middle::recursion_limit::update_recursion_limit(sess, &krate);
483 time(time_passes, "gated macro checking", || {
484 let features = syntax::feature_gate::check_crate_macros(sess.codemap(),
485 &sess.parse_sess.span_diagnostic,
488 // these need to be set "early" so that expansion sees `quote` if enabled.
489 *sess.features.borrow_mut() = features;
490 sess.abort_if_errors();
494 krate = time(time_passes, "crate injection", || {
495 syntax::std_inject::maybe_inject_crates_ref(krate, sess.opts.alt_std_name.clone())
498 let macros = time(time_passes,
500 || macro_import::read_macro_defs(sess, &cstore, &krate));
502 let mut addl_plugins = Some(addl_plugins);
503 let registrars = time(time_passes, "plugin loading", || {
504 plugin::load::load_plugins(sess, &cstore, &krate, addl_plugins.take().unwrap())
507 let mut registry = Registry::new(sess, &krate);
509 time(time_passes, "plugin registration", || {
510 if sess.features.borrow().rustc_diagnostic_macros {
511 registry.register_macro("__diagnostic_used",
512 diagnostics::plugin::expand_diagnostic_used);
513 registry.register_macro("__register_diagnostic",
514 diagnostics::plugin::expand_register_diagnostic);
515 registry.register_macro("__build_diagnostic_array",
516 diagnostics::plugin::expand_build_diagnostic_array);
519 for registrar in registrars {
520 registry.args_hidden = Some(registrar.args);
521 (registrar.fun)(&mut registry);
525 let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
526 llvm_passes, attributes, .. } = registry;
529 let mut ls = sess.lint_store.borrow_mut();
530 for pass in early_lint_passes {
531 ls.register_early_pass(Some(sess), true, pass);
533 for pass in late_lint_passes {
534 ls.register_late_pass(Some(sess), true, pass);
537 for (name, to) in lint_groups {
538 ls.register_group(Some(sess), true, name, to);
541 *sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
542 *sess.plugin_attributes.borrow_mut() = attributes.clone();
545 // Lint plugins are registered; now we can process command line flags.
546 if sess.opts.describe_lints {
547 super::describe_lints(&*sess.lint_store.borrow(), true);
550 sess.lint_store.borrow_mut().process_command_line(sess);
552 // Abort if there are errors from lint processing or a plugin registrar.
553 sess.abort_if_errors();
555 krate = time(time_passes, "expansion", || {
556 // Windows dlls do not have rpaths, so they don't know how to find their
557 // dependencies. It's up to us to tell the system where to find all the
558 // dependent dlls. Note that this uses cfg!(windows) as opposed to
559 // targ_cfg because syntax extensions are always loaded for the host
560 // compiler, not for the target.
561 let mut _old_path = OsString::new();
563 _old_path = env::var_os("PATH").unwrap_or(_old_path);
564 let mut new_path = sess.host_filesearch(PathKind::All)
565 .get_dylib_search_paths();
566 new_path.extend(env::split_paths(&_old_path));
567 env::set_var("PATH", &env::join_paths(new_path).unwrap());
569 let features = sess.features.borrow();
570 let cfg = syntax::ext::expand::ExpansionConfig {
571 crate_name: crate_name.to_string(),
572 features: Some(&features),
573 recursion_limit: sess.recursion_limit.get(),
574 trace_mac: sess.opts.debugging_opts.trace_macros,
576 let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
577 krate.config.clone(),
579 &mut feature_gated_cfgs);
580 syntax_ext::register_builtins(&mut ecx.syntax_env);
581 let (ret, macro_names) = syntax::ext::expand::expand_crate(ecx,
586 env::set_var("PATH", &_old_path);
588 *sess.available_macros.borrow_mut() = macro_names;
592 // Needs to go *after* expansion to be able to check the results
593 // of macro expansion. This runs before #[cfg] to try to catch as
594 // much as possible (e.g. help the programmer avoid platform
595 // specific differences)
596 time(time_passes, "complete gated feature checking 1", || {
597 let features = syntax::feature_gate::check_crate(sess.codemap(),
598 &sess.parse_sess.span_diagnostic,
601 sess.opts.unstable_features);
602 *sess.features.borrow_mut() = features;
603 sess.abort_if_errors();
606 // JBC: make CFG processing part of expansion to avoid this problem:
608 // strip again, in case expansion added anything with a #[cfg].
609 krate = time(time_passes, "configuration 2", || {
610 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
613 time(time_passes, "gated configuration checking", || {
614 let features = sess.features.borrow();
615 feature_gated_cfgs.sort();
616 feature_gated_cfgs.dedup();
617 for cfg in &feature_gated_cfgs {
618 cfg.check_and_emit(sess.diagnostic(), &features, sess.codemap());
622 krate = time(time_passes, "maybe building test harness", || {
623 syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
626 krate = time(time_passes,
628 || syntax::std_inject::maybe_inject_prelude(&sess.parse_sess, krate));
631 "checking that all macro invocations are gone",
632 || syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
635 "checking for inline asm in case the target doesn't support it",
636 || no_asm::check_crate(sess, &krate));
638 // One final feature gating of the true AST that gets compiled
639 // later, to make sure we've got everything (e.g. configuration
640 // can insert new attributes via `cfg_attr`)
641 time(time_passes, "complete gated feature checking 2", || {
642 let features = syntax::feature_gate::check_crate(sess.codemap(),
643 &sess.parse_sess.span_diagnostic,
646 sess.opts.unstable_features);
647 *sess.features.borrow_mut() = features;
648 sess.abort_if_errors();
652 "const fn bodies and arguments",
653 || const_fn::check_crate(sess, &krate));
655 if sess.opts.debugging_opts.input_stats {
656 println!("Post-expansion node count: {}", count_nodes(&krate));
662 pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
663 struct NodeIdAssigner<'a> {
667 impl<'a> Folder for NodeIdAssigner<'a> {
668 fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
669 assert_eq!(old_id, ast::DUMMY_NODE_ID);
670 self.sess.next_node_id()
674 let krate = time(sess.time_passes(),
675 "assigning node ids",
676 || NodeIdAssigner { sess: sess }.fold_crate(krate));
678 if sess.opts.debugging_opts.ast_json {
679 println!("{}", json::as_json(&krate));
685 pub fn make_map<'ast>(sess: &Session,
686 forest: &'ast mut hir_map::Forest)
687 -> hir_map::Map<'ast> {
688 // Construct the HIR map
689 time(sess.time_passes(),
691 move || hir_map::map_crate(forest))
694 /// Run the resolution, typechecking, region checking and other
695 /// miscellaneous analysis passes on the crate. Return various
696 /// structures carrying the results of the analysis.
697 pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
699 hir_map: hir_map::Map<'tcx>,
700 arenas: &'tcx ty::CtxtArenas<'tcx>,
702 make_glob_map: resolve::MakeGlobMap,
705 where F: for<'a> FnOnce(&'a ty::ctxt<'tcx>, MirMap<'tcx>, ty::CrateAnalysis) -> R
707 let time_passes = sess.time_passes();
708 let krate = hir_map.krate();
711 "external crate/lib resolution",
712 || LocalCrateReader::new(sess, cstore, &hir_map).read_crates(krate));
714 let lang_items = time(time_passes,
715 "language item collection",
716 || middle::lang_items::collect_language_items(&sess, &hir_map));
718 let resolve::CrateMap {
725 } = time(time_passes,
727 || resolve::resolve_crate(sess, &hir_map, make_glob_map));
729 let named_region_map = time(time_passes,
730 "lifetime resolution",
731 || middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));
734 "looking for entry point",
735 || middle::entry::find_entry_point(sess, &hir_map));
737 sess.plugin_registrar_fn.set(time(time_passes, "looking for plugin registrar", || {
738 plugin::build::find_plugin_registrar(sess.diagnostic(), krate)
741 let region_map = time(time_passes,
743 || middle::region::resolve_crate(sess, krate));
747 || loops::check_crate(sess, krate));
750 "static item recursion checking",
751 || static_recursion::check_crate(sess, krate, &def_map.borrow(), &hir_map));
753 ty::ctxt::create_and_enter(sess,
761 stability::Index::new(krate),
763 // passes are timed inside typeck
764 typeck::check_crate(tcx, trait_map);
768 || consts::check_crate(tcx));
771 time(time_passes, "privacy checking", || {
772 rustc_privacy::check_crate(tcx,
777 // Do not move this check past lint
778 time(time_passes, "stability index", || {
779 tcx.stability.borrow_mut().build(tcx, krate, &access_levels)
783 "intrinsic checking",
784 || middle::intrinsicck::check_crate(tcx));
788 || middle::effect::check_crate(tcx));
792 || middle::check_match::check_crate(tcx));
797 || mir::mir_map::build_mir_for_crate(tcx));
801 || middle::liveness::check_crate(tcx));
805 || borrowck::check_crate(tcx));
809 || rvalues::check_crate(tcx));
811 // Avoid overwhelming user with errors if type checking failed.
812 // I'm not sure how helpful this is, to be honest, but it avoids
814 // lot of annoying errors in the compile-fail tests (basically,
815 // lint warnings and so on -- kindck used to do this abort, but
816 // kindck is gone now). -nmatsakis
817 tcx.sess.abort_if_errors();
821 "reachability checking",
822 || reachable::find_reachable(tcx, &access_levels));
824 time(time_passes, "death checking", || {
825 middle::dead::check_crate(tcx, &access_levels);
828 let ref lib_features_used =
830 "stability checking",
831 || stability::check_unstable_api_usage(tcx));
833 time(time_passes, "unused lib feature checking", || {
834 stability::check_unused_or_stable_features(&tcx.sess,
840 || lint::check_crate(tcx, &access_levels));
842 // The above three passes generate errors w/o aborting
843 tcx.sess.abort_if_errors();
848 export_map: export_map,
849 access_levels: access_levels,
850 reachable: reachable_map,
857 /// Run the translation phase to LLVM, after which the AST and analysis can
859 pub fn phase_4_translate_to_llvm<'tcx>(tcx: &ty::ctxt<'tcx>,
860 mut mir_map: MirMap<'tcx>,
861 analysis: ty::CrateAnalysis)
862 -> trans::CrateTranslation {
863 let time_passes = tcx.sess.time_passes();
866 "resolving dependency formats",
867 || dependency_format::calculate(&tcx.sess));
870 "erasing regions from MIR",
871 || mir::transform::erase_regions::erase_regions(tcx, &mut mir_map));
873 // Option dance to work around the lack of stack once closures.
876 move || trans::trans_crate(tcx, &mir_map, analysis))
879 /// Run LLVM itself, producing a bitcode file, assembly file or object file
880 /// as a side effect.
881 pub fn phase_5_run_llvm_passes(sess: &Session,
882 trans: &trans::CrateTranslation,
883 outputs: &OutputFilenames) {
884 if sess.opts.cg.no_integrated_as {
885 let mut map = HashMap::new();
886 map.insert(OutputType::Assembly, None);
887 time(sess.time_passes(),
889 || write::run_passes(sess, trans, &map, outputs));
891 write::run_assembler(sess, outputs);
893 // Remove assembly source, unless --save-temps was specified
894 if !sess.opts.cg.save_temps {
895 fs::remove_file(&outputs.temp_path(OutputType::Assembly)).unwrap();
898 time(sess.time_passes(),
900 || write::run_passes(sess, trans, &sess.opts.output_types, outputs));
903 sess.abort_if_errors();
906 /// Run the linker on any artifacts that resulted from the LLVM run.
907 /// This should produce either a finished executable or library.
908 pub fn phase_6_link_output(sess: &Session,
909 trans: &trans::CrateTranslation,
910 outputs: &OutputFilenames) {
911 time(sess.time_passes(),
913 || link::link_binary(sess, trans, outputs, &trans.link.crate_name));
916 fn escape_dep_filename(filename: &str) -> String {
917 // Apparently clang and gcc *only* escape spaces:
918 // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
919 filename.replace(" ", "\\ ")
922 fn write_out_deps(sess: &Session, outputs: &OutputFilenames, id: &str) {
923 let mut out_filenames = Vec::new();
924 for output_type in sess.opts.output_types.keys() {
925 let file = outputs.path(*output_type);
928 for output in sess.crate_types.borrow().iter() {
929 let p = link::filename_for_input(sess, *output, id, outputs);
930 out_filenames.push(p);
934 out_filenames.push(file);
939 // Write out dependency rules to the dep-info file if requested
940 if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
943 let deps_filename = outputs.path(OutputType::DepInfo);
946 (|| -> io::Result<()> {
947 // Build a list of files used to compile the output and
948 // write Makefile-compatible dependency rules
949 let files: Vec<String> = sess.codemap()
953 .filter(|fmap| fmap.is_real_file())
954 .filter(|fmap| !fmap.is_imported())
955 .map(|fmap| escape_dep_filename(&fmap.name))
957 let mut file = try!(fs::File::create(&deps_filename));
958 for path in &out_filenames {
959 try!(write!(file, "{}: {}\n\n", path.display(), files.join(" ")));
962 // Emit a fake target for each input file to the compilation. This
963 // prevents `make` from spitting out an error if a file is later
964 // deleted. For more info see #28735
966 try!(writeln!(file, "{}:", path));
974 sess.fatal(&format!("error writing dependencies to `{}`: {}",
975 deps_filename.display(),
981 pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
982 // Unconditionally collect crate types from attributes to make them used
983 let attr_types: Vec<config::CrateType> =
986 if a.check_name("crate_type") {
987 match a.value_str() {
988 Some(ref n) if *n == "rlib" => {
989 Some(config::CrateTypeRlib)
991 Some(ref n) if *n == "dylib" => {
992 Some(config::CrateTypeDylib)
994 Some(ref n) if *n == "lib" => {
995 Some(config::default_lib_output())
997 Some(ref n) if *n == "staticlib" => {
998 Some(config::CrateTypeStaticlib)
1000 Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
1002 session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
1005 "invalid `crate_type` value".to_string());
1009 session.struct_span_err(a.span, "`crate_type` requires a value")
1010 .note("for example: `#![crate_type=\"lib\"]`")
1021 // If we're generating a test executable, then ignore all other output
1022 // styles at all other locations
1023 if session.opts.test {
1024 return vec![config::CrateTypeExecutable];
1027 // Only check command line flags if present. If no types are specified by
1028 // command line, then reuse the empty `base` Vec to hold the types that
1029 // will be found in crate attributes.
1030 let mut base = session.opts.crate_types.clone();
1031 if base.is_empty() {
1032 base.extend(attr_types);
1033 if base.is_empty() {
1034 base.push(link::default_output_for_target(session));
1041 .filter(|crate_type| {
1042 let res = !link::invalid_output_for_target(session, *crate_type);
1045 session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",
1047 session.opts.target_triple));
1055 pub fn collect_crate_metadata(session: &Session, _attrs: &[ast::Attribute]) -> Vec<String> {
1056 session.opts.cg.metadata.clone()
1059 pub fn build_output_filenames(input: &Input,
1060 odir: &Option<PathBuf>,
1061 ofile: &Option<PathBuf>,
1062 attrs: &[ast::Attribute],
1064 -> OutputFilenames {
1067 // "-" as input file will cause the parser to read from stdin so we
1068 // have to make up a name
1069 // We want to toss everything after the final '.'
1070 let dirpath = match *odir {
1071 Some(ref d) => d.clone(),
1072 None => PathBuf::new(),
1075 // If a crate name is present, we use it as the link name
1076 let stem = sess.opts
1079 .or_else(|| attr::find_crate_name(attrs).map(|n| n.to_string()))
1080 .unwrap_or(input.filestem());
1083 out_directory: dirpath,
1085 single_output_file: None,
1086 extra: sess.opts.cg.extra_filename.clone(),
1087 outputs: sess.opts.output_types.clone(),
1091 Some(ref out_file) => {
1092 let unnamed_output_types = sess.opts
1095 .filter(|a| a.is_none())
1097 let ofile = if unnamed_output_types > 1 {
1098 sess.warn("ignoring specified output filename because multiple outputs were \
1102 Some(out_file.clone())
1105 sess.warn("ignoring --out-dir flag due to -o flag.");
1108 let cur_dir = Path::new("");
1111 out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
1112 out_filestem: out_file.file_stem()
1113 .unwrap_or(OsStr::new(""))
1117 single_output_file: ofile,
1118 extra: sess.opts.cg.extra_filename.clone(),
1119 outputs: sess.opts.output_types.clone(),