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