1 #![feature(rustc_private, once_cell)]
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;
9 use std::lazy::SyncLazy;
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;
18 const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new";
20 static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
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);
27 // Separate the output with an empty line
30 // Print the ICE message
31 rustc_driver::report_ice(info, BUG_REPORT_URL);
37 pub struct CraneliftPassesCallbacks {
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);
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()
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()
66 arg.into_string().unwrap_or_else(|arg| {
68 ErrorOutputType::default(),
69 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
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 })
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);
86 std::process::exit(exit_code)