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