]> git.lizzy.rs Git - rust.git/blob - benches/helpers/miri_helper.rs
Partial rustup
[rust.git] / benches / helpers / miri_helper.rs
1 extern crate getopts;
2 extern crate miri;
3 extern crate rustc;
4 extern crate rustc_driver;
5 extern crate test;
6
7 use self::miri::eval_main;
8 use self::rustc::session::Session;
9 use self::rustc_driver::{driver, CompilerCalls, Compilation};
10 use std::cell::RefCell;
11 use std::rc::Rc;
12 use test::Bencher;
13
14 pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
15
16 fn find_sysroot() -> String {
17     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
18     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
19     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
20     match (home, toolchain) {
21         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
22         _ => {
23             option_env!("RUST_SYSROOT")
24                 .expect(
25                     "need to specify RUST_SYSROOT env var or use rustup or multirust",
26                 )
27                 .to_owned()
28         }
29     }
30 }
31
32 pub fn run(filename: &str, bencher: &mut Bencher) {
33     let args = &[
34         "miri".to_string(),
35         format!("benches/helpers/{}.rs", filename),
36         "--sysroot".to_string(),
37         find_sysroot(),
38     ];
39     let compiler_calls = &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)));
40     rustc_driver::run_compiler(args, compiler_calls, None, None);
41 }
42
43 impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
44     fn build_controller(
45         &mut self,
46         _: &Session,
47         _: &getopts::Matches,
48     ) -> driver::CompileController<'a> {
49         let mut control: driver::CompileController<'a> = driver::CompileController::basic();
50
51         let bencher = self.0.clone();
52
53         control.after_analysis.stop = Compilation::Stop;
54         control.after_analysis.callback = Box::new(move |state| {
55             state.session.abort_if_errors();
56
57             let tcx = state.tcx.unwrap();
58             let (entry_node_id, _, _) = state.session.entry_fn.borrow().expect(
59                 "no main or start function found",
60             );
61             let entry_def_id = tcx.hir.local_def_id(entry_node_id);
62
63             bencher.borrow_mut().iter(|| {
64                 eval_main(tcx, entry_def_id, None);
65             });
66
67             state.session.abort_if_errors();
68         });
69
70         control
71     }
72 }