1 #![feature(rustc_private)]
2 #![warn(rust_2018_idioms)]
3 #![warn(unused_lifetimes)]
4 #![warn(unreachable_pub)]
6 extern crate rustc_data_structures;
7 extern crate rustc_driver;
8 extern crate rustc_interface;
9 extern crate rustc_session;
10 extern crate rustc_target;
14 use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
15 use rustc_interface::interface;
16 use rustc_session::config::{ErrorOutputType, TrimmedDefPaths};
17 use rustc_session::early_error;
18 use rustc_target::spec::PanicStrategy;
20 // FIXME use std::lazy::SyncLazy once it stabilizes
21 use once_cell::sync::Lazy;
23 const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new";
25 static DEFAULT_HOOK: Lazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
27 let hook = panic::take_hook();
28 panic::set_hook(Box::new(|info| {
29 // Invoke the default handler, which prints the actual panic message and optionally a backtrace
30 (*DEFAULT_HOOK)(info);
32 // Separate the output with an empty line
35 // Print the ICE message
36 rustc_driver::report_ice(info, BUG_REPORT_URL);
42 pub struct CraneliftPassesCallbacks {
46 impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
47 fn config(&mut self, config: &mut interface::Config) {
48 // If a --prints=... option has been given, we don't print the "total"
49 // time because it will mess up the --prints output. See #64339.
50 self.time_passes = config.opts.prints.is_empty()
51 && (config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time);
53 config.opts.cg.panic = Some(PanicStrategy::Abort);
54 config.opts.debugging_opts.panic_abort_tests = true;
55 config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| {
56 std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned()
59 config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath;
64 let start_time = std::time::Instant::now();
65 let start_rss = get_resident_set_size();
66 rustc_driver::init_rustc_env_logger();
67 let mut callbacks = CraneliftPassesCallbacks::default();
68 Lazy::force(&DEFAULT_HOOK); // Install ice hook
69 let exit_code = rustc_driver::catch_with_exit_code(|| {
70 let args = std::env::args_os()
73 arg.into_string().unwrap_or_else(|arg| {
75 ErrorOutputType::default(),
76 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
81 let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
82 run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
83 Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
88 if callbacks.time_passes {
89 let end_rss = get_resident_set_size();
90 print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss);
93 std::process::exit(exit_code)