]>
Commit | Line | Data |
---|---|---|
136023e0 | 1 | #![feature(rustc_private, once_cell)] |
29967ef6 XL |
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 | ||
136023e0 XL |
9 | use std::lazy::SyncLazy; |
10 | use std::panic; | |
11 | ||
5869c6ff | 12 | use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry}; |
29967ef6 XL |
13 | use rustc_interface::interface; |
14 | use rustc_session::config::ErrorOutputType; | |
15 | use rustc_session::early_error; | |
16 | use rustc_target::spec::PanicStrategy; | |
17 | ||
136023e0 XL |
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 | ||
29967ef6 XL |
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; | |
fc512014 | 50 | config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| { |
6a06907d | 51 | std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned() |
fc512014 | 52 | })); |
29967ef6 XL |
53 | } |
54 | } | |
55 | ||
56 | fn main() { | |
5869c6ff XL |
57 | let start_time = std::time::Instant::now(); |
58 | let start_rss = get_resident_set_size(); | |
29967ef6 XL |
59 | rustc_driver::init_rustc_env_logger(); |
60 | let mut callbacks = CraneliftPassesCallbacks::default(); | |
136023e0 | 61 | SyncLazy::force(&DEFAULT_HOOK); // Install ice hook |
29967ef6 | 62 | let exit_code = rustc_driver::catch_with_exit_code(|| { |
5869c6ff | 63 | let args = std::env::args_os() |
29967ef6 XL |
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 | }) | |
29967ef6 | 73 | .collect::<Vec<_>>(); |
29967ef6 XL |
74 | let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks); |
75 | run_compiler.set_make_codegen_backend(Some(Box::new(move |_| { | |
5869c6ff | 76 | Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None }) |
29967ef6 XL |
77 | }))); |
78 | run_compiler.run() | |
79 | }); | |
5869c6ff XL |
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 | ||
29967ef6 XL |
86 | std::process::exit(exit_code) |
87 | } |