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