4 extern crate rustc_driver;
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;
14 pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
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),
23 option_env!("RUST_SYSROOT")
25 "need to specify RUST_SYSROOT env var or use rustup or multirust",
32 pub fn run(filename: &str, bencher: &mut Bencher) {
35 format!("benches/helpers/{}.rs", filename),
36 "--sysroot".to_string(),
39 let compiler_calls = &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)));
40 rustc_driver::run_compiler(args, compiler_calls, None, None);
43 impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
48 ) -> driver::CompileController<'a> {
49 let mut control: driver::CompileController<'a> = driver::CompileController::basic();
51 let bencher = self.0.clone();
53 control.after_analysis.stop = Compilation::Stop;
54 control.after_analysis.callback = Box::new(move |state| {
55 state.session.abort_if_errors();
57 let tcx = state.tcx.unwrap();
58 let (entry_node_id, _, _) = state.session.entry_fn.borrow().expect(
59 "no main or start function found",
61 let entry_def_id = tcx.hir.local_def_id(entry_node_id);
63 bencher.borrow_mut().iter(|| {
64 eval_main(tcx, entry_def_id, None);
67 state.session.abort_if_errors();