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;
20 use rustc::metadata::creader::LocalCrateReader;
21 use rustc::middle::{stability, ty, reachable};
22 use rustc::middle::dependency_format;
24 use rustc::plugin::registry::Registry;
26 use rustc::util::nodemap::NodeMap;
27 use rustc::util::common::time;
28 use rustc_borrowck as borrowck;
29 use rustc_resolve as resolve;
30 use rustc_trans::back::link;
31 use rustc_trans::back::write;
32 use rustc_trans::trans;
33 use rustc_typeck as typeck;
36 use rustc_front::lowering::{lower_crate, LoweringContext};
37 use super::Compilation;
41 use std::collections::HashMap;
43 use std::ffi::{OsString, OsStr};
45 use std::io::{self, Write};
46 use std::path::{Path, PathBuf};
47 use syntax::ast::{self, NodeIdAssigner};
49 use syntax::attr::AttrMetaMethods;
50 use syntax::diagnostics;
51 use syntax::feature_gate::UnstableFeatures;
52 use syntax::fold::Folder;
54 use syntax::parse::token;
55 use syntax::util::node_count::NodeCounter;
59 pub fn compile_input(sess: Session,
60 cfg: ast::CrateConfig,
62 outdir: &Option<PathBuf>,
63 output: &Option<PathBuf>,
64 addl_plugins: Option<Vec<String>>,
65 control: CompileController) {
66 macro_rules! controller_entry_point{($point: ident, $tsess: expr, $make_state: expr) => ({
67 let state = $make_state;
68 (control.$point.callback)(state);
70 $tsess.abort_if_errors();
71 if control.$point.stop == Compilation::Stop {
76 // We need nested scopes here, because the intermediate results can keep
77 // large chunks of memory alive and we want to free them as soon as
78 // possible to keep the peak memory usage low
80 let (outputs, expanded_crate, id) = {
81 let krate = phase_1_parse_input(&sess, cfg, input);
83 controller_entry_point!(after_parse,
85 CompileState::state_after_parse(input, &sess, outdir, &krate));
87 let outputs = build_output_filenames(input, outdir, output, &krate.attrs, &sess);
88 let id = link::find_crate_name(Some(&sess), &krate.attrs, input);
89 let expanded_crate = match phase_2_configure_and_expand(&sess,
97 (outputs, expanded_crate, id)
100 controller_entry_point!(after_expand,
102 CompileState::state_after_expand(input,
108 let expanded_crate = assign_node_ids(&sess, expanded_crate);
110 let lcx = LoweringContext::new(&sess, Some(&expanded_crate));
111 let mut hir_forest = time(sess.time_passes(),
112 "lowering ast -> hir",
113 || hir_map::Forest::new(lower_crate(&lcx, &expanded_crate)));
114 let arenas = ty::CtxtArenas::new();
115 let ast_map = make_map(&sess, &mut hir_forest);
117 write_out_deps(&sess, &outputs, &id);
119 controller_entry_point!(after_write_deps,
121 CompileState::state_after_write_deps(input,
130 time(sess.time_passes(), "attribute checking", || {
131 front::check_attr::check_crate(&sess, &expanded_crate);
134 time(sess.time_passes(),
136 || lint::check_ast_crate(&sess, &expanded_crate));
138 phase_3_run_analysis_passes(&sess,
142 control.make_glob_map,
143 |tcx, mir_map, analysis| {
147 CompileState::state_after_analysis(input,
157 (control.after_analysis.callback)(state);
159 tcx.sess.abort_if_errors();
160 if control.after_analysis.stop == Compilation::Stop {
165 if log_enabled!(::log::INFO) {
166 println!("Pre-trans");
167 tcx.print_debug_stats();
169 let trans = phase_4_translate_to_llvm(tcx,
173 if log_enabled!(::log::INFO) {
174 println!("Post-trans");
175 tcx.print_debug_stats();
178 // Discard interned strings as they are no longer required.
179 token::get_ident_interner().clear();
185 let (outputs, trans) = if let Ok(out) = result {
191 phase_5_run_llvm_passes(&sess, &trans, &outputs);
193 controller_entry_point!(after_llvm,
195 CompileState::state_after_llvm(input, &sess, outdir, &trans));
197 phase_6_link_output(&sess, &trans, &outputs);
200 /// The name used for source code that doesn't originate in a file
201 /// (e.g. source from stdin or a string)
202 pub fn anon_src() -> String {
206 pub fn source_name(input: &Input) -> String {
208 // FIXME (#9639): This needs to handle non-utf8 paths
209 Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
210 Input::Str(_) => anon_src(),
214 /// CompileController is used to customise compilation, it allows compilation to
215 /// be stopped and/or to call arbitrary code at various points in compilation.
216 /// It also allows for various flags to be set to influence what information gets
217 /// collected during compilation.
219 /// This is a somewhat higher level controller than a Session - the Session
220 /// controls what happens in each phase, whereas the CompileController controls
221 /// whether a phase is run at all and whether other code (from outside the
222 /// the compiler) is run between phases.
224 /// Note that if compilation is set to stop and a callback is provided for a
225 /// given entry point, the callback is called before compilation is stopped.
227 /// Expect more entry points to be added in the future.
228 pub struct CompileController<'a> {
229 pub after_parse: PhaseController<'a>,
230 pub after_expand: PhaseController<'a>,
231 pub after_write_deps: PhaseController<'a>,
232 pub after_analysis: PhaseController<'a>,
233 pub after_llvm: PhaseController<'a>,
235 pub make_glob_map: resolve::MakeGlobMap,
238 impl<'a> CompileController<'a> {
239 pub fn basic() -> CompileController<'a> {
241 after_parse: PhaseController::basic(),
242 after_expand: PhaseController::basic(),
243 after_write_deps: PhaseController::basic(),
244 after_analysis: PhaseController::basic(),
245 after_llvm: PhaseController::basic(),
246 make_glob_map: resolve::MakeGlobMap::No,
251 pub struct PhaseController<'a> {
252 pub stop: Compilation,
253 pub callback: Box<Fn(CompileState) -> () + 'a>,
256 impl<'a> PhaseController<'a> {
257 pub fn basic() -> PhaseController<'a> {
259 stop: Compilation::Continue,
260 callback: box |_| {},
265 /// State that is passed to a callback. What state is available depends on when
266 /// during compilation the callback is made. See the various constructor methods
267 /// (`state_*`) in the impl to see which data is provided for any given entry point.
268 pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
269 pub input: &'a Input,
270 pub session: &'a Session,
271 pub cfg: Option<&'a ast::CrateConfig>,
272 pub krate: Option<&'a ast::Crate>,
273 pub crate_name: Option<&'a str>,
274 pub output_filenames: Option<&'a OutputFilenames>,
275 pub out_dir: Option<&'a Path>,
276 pub expanded_crate: Option<&'a ast::Crate>,
277 pub hir_crate: Option<&'a hir::Crate>,
278 pub ast_map: Option<&'a hir_map::Map<'ast>>,
279 pub mir_map: Option<&'a MirMap<'tcx>>,
280 pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
281 pub tcx: Option<&'a ty::ctxt<'tcx>>,
282 pub lcx: Option<&'a LoweringContext<'a>>,
283 pub trans: Option<&'a trans::CrateTranslation>,
286 impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
287 fn empty(input: &'a Input,
288 session: &'a Session,
289 out_dir: &'a Option<PathBuf>)
290 -> CompileState<'a, 'ast, 'tcx> {
294 out_dir: out_dir.as_ref().map(|s| &**s),
298 output_filenames: None,
299 expanded_crate: None,
310 fn state_after_parse(input: &'a Input,
311 session: &'a Session,
312 out_dir: &'a Option<PathBuf>,
313 krate: &'a ast::Crate)
314 -> CompileState<'a, 'ast, 'tcx> {
315 CompileState { krate: Some(krate), ..CompileState::empty(input, session, out_dir) }
318 fn state_after_expand(input: &'a Input,
319 session: &'a Session,
320 out_dir: &'a Option<PathBuf>,
321 expanded_crate: &'a ast::Crate,
323 -> CompileState<'a, 'ast, 'tcx> {
325 crate_name: Some(crate_name),
326 expanded_crate: Some(expanded_crate),
327 ..CompileState::empty(input, session, out_dir)
331 fn state_after_write_deps(input: &'a Input,
332 session: &'a Session,
333 out_dir: &'a Option<PathBuf>,
334 ast_map: &'a hir_map::Map<'ast>,
335 krate: &'a ast::Crate,
336 hir_crate: &'a hir::Crate,
338 lcx: &'a LoweringContext<'a>)
339 -> CompileState<'a, 'ast, 'tcx> {
341 crate_name: Some(crate_name),
342 ast_map: Some(ast_map),
344 hir_crate: Some(hir_crate),
346 ..CompileState::empty(input, session, out_dir)
350 fn state_after_analysis(input: &'a Input,
351 session: &'a Session,
352 out_dir: &'a Option<PathBuf>,
353 krate: &'a ast::Crate,
354 hir_crate: &'a hir::Crate,
355 analysis: &'a ty::CrateAnalysis,
356 mir_map: &'a MirMap<'tcx>,
357 tcx: &'a ty::ctxt<'tcx>,
358 lcx: &'a LoweringContext<'a>,
360 -> CompileState<'a, 'ast, 'tcx> {
362 analysis: Some(analysis),
363 mir_map: Some(mir_map),
366 hir_crate: Some(hir_crate),
368 crate_name: Some(crate_name),
369 ..CompileState::empty(input, session, out_dir)
374 fn state_after_llvm(input: &'a Input,
375 session: &'a Session,
376 out_dir: &'a Option<PathBuf>,
377 trans: &'a trans::CrateTranslation)
378 -> CompileState<'a, 'ast, 'tcx> {
379 CompileState { trans: Some(trans), ..CompileState::empty(input, session, out_dir) }
383 pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input) -> ast::Crate {
384 // These may be left in an incoherent state after a previous compile.
385 // `clear_tables` and `get_ident_interner().clear()` can be used to free
386 // memory, but they do not restore the initial state.
387 syntax::ext::mtwt::reset_tables();
388 token::reset_ident_interner();
390 let krate = time(sess.time_passes(), "parsing", || {
392 Input::File(ref file) => {
393 parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
395 Input::Str(ref src) => {
396 parse::parse_crate_from_source_str(anon_src().to_string(),
404 if sess.opts.debugging_opts.ast_json_noexpand {
405 println!("{}", json::as_json(&krate));
408 if sess.opts.debugging_opts.input_stats {
409 println!("Lines of code: {}", sess.codemap().count_lines());
410 println!("Pre-expansion node count: {}", count_nodes(&krate));
413 if let Some(ref s) = sess.opts.show_span {
414 syntax::show_span::run(sess.diagnostic(), s, &krate);
420 fn count_nodes(krate: &ast::Crate) -> usize {
421 let mut counter = NodeCounter::new();
422 visit::walk_crate(&mut counter, krate);
426 // For continuing compilation after a parsed crate has been
429 /// Run the "early phases" of the compiler: initial `cfg` processing,
430 /// loading compiler plugins (including those from `addl_plugins`),
431 /// syntax expansion, secondary `cfg` expansion, synthesis of a test
432 /// harness if one is to be provided and injection of a dependency on the
433 /// standard library and prelude.
435 /// Returns `None` if we're aborting after handling -W help.
436 pub fn phase_2_configure_and_expand(sess: &Session,
437 mut krate: ast::Crate,
439 addl_plugins: Option<Vec<String>>)
440 -> Option<ast::Crate> {
441 let time_passes = sess.time_passes();
443 // strip before anything else because crate metadata may use #[cfg_attr]
444 // and so macros can depend on configuration variables, such as
446 // #[macro_use] #[cfg(foo)]
447 // mod bar { macro_rules! baz!(() => {{}}) }
449 // baz! should not use this definition unless foo is enabled.
451 let mut feature_gated_cfgs = vec![];
452 krate = time(time_passes, "configuration 1", || {
453 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
456 *sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
457 *sess.crate_metadata.borrow_mut() = collect_crate_metadata(sess, &krate.attrs);
459 time(time_passes, "recursion limit", || {
460 middle::recursion_limit::update_recursion_limit(sess, &krate);
463 time(time_passes, "gated macro checking", || {
464 let features = syntax::feature_gate::check_crate_macros(sess.codemap(),
465 &sess.parse_sess.span_diagnostic,
468 // these need to be set "early" so that expansion sees `quote` if enabled.
469 *sess.features.borrow_mut() = features;
470 sess.abort_if_errors();
474 krate = time(time_passes, "crate injection", || {
475 syntax::std_inject::maybe_inject_crates_ref(krate, sess.opts.alt_std_name.clone())
478 let macros = time(time_passes,
480 || metadata::macro_import::read_macro_defs(sess, &krate));
482 let mut addl_plugins = Some(addl_plugins);
483 let registrars = time(time_passes, "plugin loading", || {
484 plugin::load::load_plugins(sess, &krate, addl_plugins.take().unwrap())
487 let mut registry = Registry::new(sess, &krate);
489 time(time_passes, "plugin registration", || {
490 if sess.features.borrow().rustc_diagnostic_macros {
491 registry.register_macro("__diagnostic_used",
492 diagnostics::plugin::expand_diagnostic_used);
493 registry.register_macro("__register_diagnostic",
494 diagnostics::plugin::expand_register_diagnostic);
495 registry.register_macro("__build_diagnostic_array",
496 diagnostics::plugin::expand_build_diagnostic_array);
499 for registrar in registrars {
500 registry.args_hidden = Some(registrar.args);
501 (registrar.fun)(&mut registry);
505 let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
506 llvm_passes, attributes, .. } = registry;
509 let mut ls = sess.lint_store.borrow_mut();
510 for pass in early_lint_passes {
511 ls.register_early_pass(Some(sess), true, pass);
513 for pass in late_lint_passes {
514 ls.register_late_pass(Some(sess), true, pass);
517 for (name, to) in lint_groups {
518 ls.register_group(Some(sess), true, name, to);
521 *sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
522 *sess.plugin_attributes.borrow_mut() = attributes.clone();
525 // Lint plugins are registered; now we can process command line flags.
526 if sess.opts.describe_lints {
527 super::describe_lints(&*sess.lint_store.borrow(), true);
530 sess.lint_store.borrow_mut().process_command_line(sess);
532 // Abort if there are errors from lint processing or a plugin registrar.
533 sess.abort_if_errors();
535 krate = time(time_passes, "expansion", || {
536 // Windows dlls do not have rpaths, so they don't know how to find their
537 // dependencies. It's up to us to tell the system where to find all the
538 // dependent dlls. Note that this uses cfg!(windows) as opposed to
539 // targ_cfg because syntax extensions are always loaded for the host
540 // compiler, not for the target.
541 let mut _old_path = OsString::new();
543 _old_path = env::var_os("PATH").unwrap_or(_old_path);
544 let mut new_path = sess.host_filesearch(PathKind::All)
545 .get_dylib_search_paths();
546 new_path.extend(env::split_paths(&_old_path));
547 env::set_var("PATH", &env::join_paths(new_path).unwrap());
549 let features = sess.features.borrow();
550 let cfg = syntax::ext::expand::ExpansionConfig {
551 crate_name: crate_name.to_string(),
552 features: Some(&features),
553 recursion_limit: sess.recursion_limit.get(),
554 trace_mac: sess.opts.debugging_opts.trace_macros,
556 let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
560 &mut feature_gated_cfgs,
563 env::set_var("PATH", &_old_path);
568 // Needs to go *after* expansion to be able to check the results
569 // of macro expansion. This runs before #[cfg] to try to catch as
570 // much as possible (e.g. help the programmer avoid platform
571 // specific differences)
572 time(time_passes, "complete gated feature checking 1", || {
573 let features = syntax::feature_gate::check_crate(sess.codemap(),
574 &sess.parse_sess.span_diagnostic,
577 sess.opts.unstable_features);
578 *sess.features.borrow_mut() = features;
579 sess.abort_if_errors();
582 // JBC: make CFG processing part of expansion to avoid this problem:
584 // strip again, in case expansion added anything with a #[cfg].
585 krate = time(time_passes, "configuration 2", || {
586 syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, &mut feature_gated_cfgs)
589 time(time_passes, "gated configuration checking", || {
590 let features = sess.features.borrow();
591 feature_gated_cfgs.sort();
592 feature_gated_cfgs.dedup();
593 for cfg in &feature_gated_cfgs {
594 cfg.check_and_emit(sess.diagnostic(), &features);
598 krate = time(time_passes, "maybe building test harness", || {
599 syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
602 krate = time(time_passes,
604 || syntax::std_inject::maybe_inject_prelude(&sess.parse_sess, krate));
607 "checking that all macro invocations are gone",
608 || syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
611 "checking for inline asm in case the target doesn't support it",
612 || middle::check_no_asm::check_crate(sess, &krate));
614 // One final feature gating of the true AST that gets compiled
615 // later, to make sure we've got everything (e.g. configuration
616 // can insert new attributes via `cfg_attr`)
617 time(time_passes, "complete gated feature checking 2", || {
618 let features = syntax::feature_gate::check_crate(sess.codemap(),
619 &sess.parse_sess.span_diagnostic,
622 sess.opts.unstable_features);
623 *sess.features.borrow_mut() = features;
624 sess.abort_if_errors();
627 if sess.opts.debugging_opts.input_stats {
628 println!("Post-expansion node count: {}", count_nodes(&krate));
634 pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
635 struct NodeIdAssigner<'a> {
639 impl<'a> Folder for NodeIdAssigner<'a> {
640 fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
641 assert_eq!(old_id, ast::DUMMY_NODE_ID);
642 self.sess.next_node_id()
646 let krate = time(sess.time_passes(),
647 "assigning node ids",
648 || NodeIdAssigner { sess: sess }.fold_crate(krate));
650 if sess.opts.debugging_opts.ast_json {
651 println!("{}", json::as_json(&krate));
657 pub fn make_map<'ast>(sess: &Session,
658 forest: &'ast mut front::map::Forest)
659 -> front::map::Map<'ast> {
660 // Construct the 'ast'-map
661 let map = time(sess.time_passes(),
663 move || front::map::map_crate(forest));
668 /// Run the resolution, typechecking, region checking and other
669 /// miscellaneous analysis passes on the crate. Return various
670 /// structures carrying the results of the analysis.
671 pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
672 ast_map: front::map::Map<'tcx>,
673 arenas: &'tcx ty::CtxtArenas<'tcx>,
675 make_glob_map: resolve::MakeGlobMap,
678 where F: for<'a> FnOnce(&'a ty::ctxt<'tcx>, MirMap<'tcx>, ty::CrateAnalysis) -> R
680 let time_passes = sess.time_passes();
681 let krate = ast_map.krate();
684 "external crate/lib resolution",
685 || LocalCrateReader::new(sess, &ast_map).read_crates(krate));
687 let lang_items = time(time_passes,
688 "language item collection",
689 || middle::lang_items::collect_language_items(&sess, &ast_map));
691 let resolve::CrateMap {
698 } = time(time_passes,
700 || resolve::resolve_crate(sess, &ast_map, make_glob_map));
702 // Discard MTWT tables that aren't required past resolution.
703 // FIXME: get rid of uses of MTWT tables in typeck, mir and trans and clear them
704 if !sess.opts.debugging_opts.keep_mtwt_tables {
705 // syntax::ext::mtwt::clear_tables();
708 let named_region_map = time(time_passes,
709 "lifetime resolution",
710 || middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));
713 "looking for entry point",
714 || middle::entry::find_entry_point(sess, &ast_map));
716 sess.plugin_registrar_fn.set(time(time_passes, "looking for plugin registrar", || {
717 plugin::build::find_plugin_registrar(sess.diagnostic(), krate)
720 let region_map = time(time_passes,
722 || middle::region::resolve_crate(sess, krate));
726 || middle::check_loop::check_crate(sess, krate));
729 "static item recursion checking",
730 || middle::check_static_recursion::check_crate(sess, krate, &def_map.borrow(), &ast_map));
732 ty::ctxt::create_and_enter(sess,
740 stability::Index::new(krate),
742 // passes are timed inside typeck
743 typeck::check_crate(tcx, trait_map);
747 || middle::check_const::check_crate(tcx));
749 let (exported_items, public_items) =
750 time(time_passes, "privacy checking", || {
751 rustc_privacy::check_crate(tcx,
756 // Do not move this check past lint
757 time(time_passes, "stability index", || {
758 tcx.stability.borrow_mut().build(tcx, krate, &exported_items)
762 "intrinsic checking",
763 || middle::intrinsicck::check_crate(tcx));
767 || middle::effect::check_crate(tcx));
771 || middle::check_match::check_crate(tcx));
773 let mir_map = match tcx.sess.opts.unstable_features {
774 UnstableFeatures::Disallow => {
775 // use this as a shorthand for beta/stable, and skip
776 // MIR construction there until known regressions are
780 UnstableFeatures::Allow | UnstableFeatures::Cheat => {
783 || mir::mir_map::build_mir_for_crate(tcx))
789 || middle::liveness::check_crate(tcx));
793 || borrowck::check_crate(tcx));
797 || middle::check_rvalues::check_crate(tcx, krate));
799 // Avoid overwhelming user with errors if type checking failed.
800 // I'm not sure how helpful this is, to be honest, but it avoids
802 // lot of annoying errors in the compile-fail tests (basically,
803 // lint warnings and so on -- kindck used to do this abort, but
804 // kindck is gone now). -nmatsakis
805 tcx.sess.abort_if_errors();
809 "reachability checking",
810 || reachable::find_reachable(tcx, &exported_items));
812 time(time_passes, "death checking", || {
813 middle::dead::check_crate(tcx,
818 let ref lib_features_used =
820 "stability checking",
821 || stability::check_unstable_api_usage(tcx));
823 time(time_passes, "unused lib feature checking", || {
824 stability::check_unused_or_stable_features(&tcx.sess,
830 || lint::check_crate(tcx, &exported_items));
832 // The above three passes generate errors w/o aborting
833 tcx.sess.abort_if_errors();
838 export_map: export_map,
839 exported_items: exported_items,
840 public_items: public_items,
841 reachable: reachable_map,
848 /// Run the translation phase to LLVM, after which the AST and analysis can
850 pub fn phase_4_translate_to_llvm<'tcx>(tcx: &ty::ctxt<'tcx>,
851 mut mir_map: MirMap<'tcx>,
852 analysis: ty::CrateAnalysis)
853 -> trans::CrateTranslation {
854 let time_passes = tcx.sess.time_passes();
857 "resolving dependency formats",
858 || dependency_format::calculate(&tcx.sess));
861 "erasing regions from MIR",
862 || mir::transform::erase_regions::erase_regions(tcx, &mut mir_map));
864 // Option dance to work around the lack of stack once closures.
867 move || trans::trans_crate(tcx, &mir_map, analysis))
870 /// Run LLVM itself, producing a bitcode file, assembly file or object file
871 /// as a side effect.
872 pub fn phase_5_run_llvm_passes(sess: &Session,
873 trans: &trans::CrateTranslation,
874 outputs: &OutputFilenames) {
875 if sess.opts.cg.no_integrated_as {
876 let mut map = HashMap::new();
877 map.insert(OutputType::Assembly, None);
878 time(sess.time_passes(),
880 || write::run_passes(sess, trans, &map, outputs));
882 write::run_assembler(sess, outputs);
884 // Remove assembly source, unless --save-temps was specified
885 if !sess.opts.cg.save_temps {
886 fs::remove_file(&outputs.temp_path(OutputType::Assembly)).unwrap();
889 time(sess.time_passes(),
891 || write::run_passes(sess, trans, &sess.opts.output_types, outputs));
894 sess.abort_if_errors();
897 /// Run the linker on any artifacts that resulted from the LLVM run.
898 /// This should produce either a finished executable or library.
899 pub fn phase_6_link_output(sess: &Session,
900 trans: &trans::CrateTranslation,
901 outputs: &OutputFilenames) {
902 time(sess.time_passes(),
904 || link::link_binary(sess, trans, outputs, &trans.link.crate_name));
907 fn escape_dep_filename(filename: &str) -> String {
908 // Apparently clang and gcc *only* escape spaces:
909 // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
910 filename.replace(" ", "\\ ")
913 fn write_out_deps(sess: &Session, outputs: &OutputFilenames, id: &str) {
914 let mut out_filenames = Vec::new();
915 for output_type in sess.opts.output_types.keys() {
916 let file = outputs.path(*output_type);
919 for output in sess.crate_types.borrow().iter() {
920 let p = link::filename_for_input(sess, *output, id, outputs);
921 out_filenames.push(p);
925 out_filenames.push(file);
930 // Write out dependency rules to the dep-info file if requested
931 if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
934 let deps_filename = outputs.path(OutputType::DepInfo);
937 (|| -> io::Result<()> {
938 // Build a list of files used to compile the output and
939 // write Makefile-compatible dependency rules
940 let files: Vec<String> = sess.codemap()
944 .filter(|fmap| fmap.is_real_file())
945 .filter(|fmap| !fmap.is_imported())
946 .map(|fmap| escape_dep_filename(&fmap.name))
948 let mut file = try!(fs::File::create(&deps_filename));
949 for path in &out_filenames {
950 try!(write!(file, "{}: {}\n\n", path.display(), files.join(" ")));
953 // Emit a fake target for each input file to the compilation. This
954 // prevents `make` from spitting out an error if a file is later
955 // deleted. For more info see #28735
957 try!(writeln!(file, "{}:", path));
965 sess.fatal(&format!("error writing dependencies to `{}`: {}",
966 deps_filename.display(),
972 pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
973 // Unconditionally collect crate types from attributes to make them used
974 let attr_types: Vec<config::CrateType> =
977 if a.check_name("crate_type") {
978 match a.value_str() {
979 Some(ref n) if *n == "rlib" => {
980 Some(config::CrateTypeRlib)
982 Some(ref n) if *n == "dylib" => {
983 Some(config::CrateTypeDylib)
985 Some(ref n) if *n == "lib" => {
986 Some(config::default_lib_output())
988 Some(ref n) if *n == "staticlib" => {
989 Some(config::CrateTypeStaticlib)
991 Some(ref n) if *n == "bin" => Some(config::CrateTypeExecutable),
993 session.add_lint(lint::builtin::UNKNOWN_CRATE_TYPES,
996 "invalid `crate_type` value".to_string());
1000 session.span_err(a.span, "`crate_type` requires a value");
1001 session.note("for example: `#![crate_type=\"lib\"]`");
1011 // If we're generating a test executable, then ignore all other output
1012 // styles at all other locations
1013 if session.opts.test {
1014 return vec![config::CrateTypeExecutable];
1017 // Only check command line flags if present. If no types are specified by
1018 // command line, then reuse the empty `base` Vec to hold the types that
1019 // will be found in crate attributes.
1020 let mut base = session.opts.crate_types.clone();
1021 if base.is_empty() {
1022 base.extend(attr_types);
1023 if base.is_empty() {
1024 base.push(link::default_output_for_target(session));
1031 .filter(|crate_type| {
1032 let res = !link::invalid_output_for_target(session, *crate_type);
1035 session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",
1037 session.opts.target_triple));
1045 pub fn collect_crate_metadata(session: &Session, _attrs: &[ast::Attribute]) -> Vec<String> {
1046 session.opts.cg.metadata.clone()
1049 pub fn build_output_filenames(input: &Input,
1050 odir: &Option<PathBuf>,
1051 ofile: &Option<PathBuf>,
1052 attrs: &[ast::Attribute],
1054 -> OutputFilenames {
1057 // "-" as input file will cause the parser to read from stdin so we
1058 // have to make up a name
1059 // We want to toss everything after the final '.'
1060 let dirpath = match *odir {
1061 Some(ref d) => d.clone(),
1062 None => PathBuf::new(),
1065 // If a crate name is present, we use it as the link name
1066 let stem = sess.opts
1069 .or_else(|| attr::find_crate_name(attrs).map(|n| n.to_string()))
1070 .unwrap_or(input.filestem());
1073 out_directory: dirpath,
1075 single_output_file: None,
1076 extra: sess.opts.cg.extra_filename.clone(),
1077 outputs: sess.opts.output_types.clone(),
1081 Some(ref out_file) => {
1082 let unnamed_output_types = sess.opts
1085 .filter(|a| a.is_none())
1087 let ofile = if unnamed_output_types > 1 {
1088 sess.warn("ignoring specified output filename because multiple outputs were \
1092 Some(out_file.clone())
1095 sess.warn("ignoring --out-dir flag due to -o flag.");
1098 let cur_dir = Path::new("");
1101 out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
1102 out_filestem: out_file.file_stem()
1103 .unwrap_or(OsStr::new(""))
1107 single_output_file: ofile,
1108 extra: sess.opts.cg.extra_filename.clone(),
1109 outputs: sess.opts.output_types.clone(),