]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs
Merge commit '3a31c6d8272c14388a34622193baf553636fe470' into sync_cg_clif-2021-07-07
[rust.git] / compiler / rustc_codegen_cranelift / src / bin / cg_clif.rs
1 #![feature(rustc_private, once_cell)]
2
3 extern crate rustc_data_structures;
4 extern crate rustc_driver;
5 extern crate rustc_interface;
6 extern crate rustc_session;
7 extern crate rustc_target;
8
9 use std::lazy::SyncLazy;
10 use std::panic;
11
12 use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
13 use rustc_interface::interface;
14 use rustc_session::config::ErrorOutputType;
15 use rustc_session::early_error;
16 use rustc_target::spec::PanicStrategy;
17
18 const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new";
19
20 static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
21     SyncLazy::new(|| {
22         let hook = panic::take_hook();
23         panic::set_hook(Box::new(|info| {
24             // Invoke the default handler, which prints the actual panic message and optionally a backtrace
25             (*DEFAULT_HOOK)(info);
26
27             // Separate the output with an empty line
28             eprintln!();
29
30             // Print the ICE message
31             rustc_driver::report_ice(info, BUG_REPORT_URL);
32         }));
33         hook
34     });
35
36 #[derive(Default)]
37 pub struct CraneliftPassesCallbacks {
38     time_passes: bool,
39 }
40
41 impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
42     fn config(&mut self, config: &mut interface::Config) {
43         // If a --prints=... option has been given, we don't print the "total"
44         // time because it will mess up the --prints output. See #64339.
45         self.time_passes = config.opts.prints.is_empty()
46             && (config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time);
47
48         config.opts.cg.panic = Some(PanicStrategy::Abort);
49         config.opts.debugging_opts.panic_abort_tests = true;
50         config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| {
51             std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned()
52         }));
53     }
54 }
55
56 fn main() {
57     let start_time = std::time::Instant::now();
58     let start_rss = get_resident_set_size();
59     rustc_driver::init_rustc_env_logger();
60     let mut callbacks = CraneliftPassesCallbacks::default();
61     SyncLazy::force(&DEFAULT_HOOK); // Install ice hook
62     let exit_code = rustc_driver::catch_with_exit_code(|| {
63         let args = std::env::args_os()
64             .enumerate()
65             .map(|(i, arg)| {
66                 arg.into_string().unwrap_or_else(|arg| {
67                     early_error(
68                         ErrorOutputType::default(),
69                         &format!("Argument {} is not valid Unicode: {:?}", i, arg),
70                     )
71                 })
72             })
73             .collect::<Vec<_>>();
74         let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
75         run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
76             Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
77         })));
78         run_compiler.run()
79     });
80
81     if callbacks.time_passes {
82         let end_rss = get_resident_set_size();
83         print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss);
84     }
85
86     std::process::exit(exit_code)
87 }