]> git.lizzy.rs Git - rust.git/blob - src/bin/cg_clif.rs
Use with_no_trimmed_paths
[rust.git] / src / bin / cg_clif.rs
1 #![feature(rustc_private)]
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
9 use rustc_data_structures::profiling::print_time_passes_entry;
10 use rustc_interface::interface;
11 use rustc_session::config::ErrorOutputType;
12 use rustc_session::early_error;
13 use rustc_target::spec::PanicStrategy;
14
15 #[derive(Default)]
16 pub struct CraneliftPassesCallbacks {
17     time_passes: bool,
18 }
19
20 impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
21     fn config(&mut self, config: &mut interface::Config) {
22         // If a --prints=... option has been given, we don't print the "total"
23         // time because it will mess up the --prints output. See #64339.
24         self.time_passes = config.opts.prints.is_empty()
25             && (config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time);
26
27         config.opts.cg.panic = Some(PanicStrategy::Abort);
28         config.opts.debugging_opts.panic_abort_tests = true;
29         config.opts.maybe_sysroot = Some(
30             std::env::current_exe()
31                 .unwrap()
32                 .parent()
33                 .unwrap()
34                 .parent()
35                 .unwrap()
36                 .parent()
37                 .unwrap()
38                 .join("build_sysroot")
39                 .join("sysroot"),
40         );
41     }
42 }
43
44 fn main() {
45     let start = std::time::Instant::now();
46     rustc_driver::init_rustc_env_logger();
47     let mut callbacks = CraneliftPassesCallbacks::default();
48     rustc_driver::install_ice_hook();
49     let exit_code = rustc_driver::catch_with_exit_code(|| {
50         let mut use_jit = false;
51
52         let mut args = std::env::args_os()
53             .enumerate()
54             .map(|(i, arg)| {
55                 arg.into_string().unwrap_or_else(|arg| {
56                     early_error(
57                         ErrorOutputType::default(),
58                         &format!("Argument {} is not valid Unicode: {:?}", i, arg),
59                     )
60                 })
61             })
62             .filter(|arg| {
63                 if arg == "--jit" {
64                     use_jit = true;
65                     false
66                 } else {
67                     true
68                 }
69             })
70             .collect::<Vec<_>>();
71         if use_jit {
72             args.push("-Cprefer-dynamic".to_string());
73         }
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 {
77                 config: rustc_codegen_cranelift::BackendConfig { use_jit },
78             })
79         })));
80         run_compiler.run()
81     });
82     // The extra `\t` is necessary to align this label with the others.
83     print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
84     std::process::exit(exit_code)
85 }