]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs
Rollup merge of #93755 - ChayimFriedman2:allow-comparing-vecs-with-different-allocato...
[rust.git] / compiler / rustc_codegen_cranelift / src / bin / cg_clif.rs
1 #![feature(rustc_private)]
2 #![warn(rust_2018_idioms)]
3 #![warn(unused_lifetimes)]
4 #![warn(unreachable_pub)]
5
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;
11
12 use std::panic;
13
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;
19
20 // FIXME use std::lazy::SyncLazy once it stabilizes
21 use once_cell::sync::Lazy;
22
23 const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new";
24
25 static DEFAULT_HOOK: Lazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
26     Lazy::new(|| {
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);
31
32             // Separate the output with an empty line
33             eprintln!();
34
35             // Print the ICE message
36             rustc_driver::report_ice(info, BUG_REPORT_URL);
37         }));
38         hook
39     });
40
41 #[derive(Default)]
42 pub struct CraneliftPassesCallbacks {
43     time_passes: bool,
44 }
45
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);
52
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()
57         }));
58
59         config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath;
60     }
61 }
62
63 fn main() {
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()
71             .enumerate()
72             .map(|(i, arg)| {
73                 arg.into_string().unwrap_or_else(|arg| {
74                     early_error(
75                         ErrorOutputType::default(),
76                         &format!("Argument {} is not valid Unicode: {:?}", i, arg),
77                     )
78                 })
79             })
80             .collect::<Vec<_>>();
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 })
84         })));
85         run_compiler.run()
86     });
87
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);
91     }
92
93     std::process::exit(exit_code)
94 }