]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/llvm-phase/test.rs
report the total number of errors on compilation failure
[rust.git] / src / test / run-make / llvm-phase / test.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(plugin, rustc_private, box_syntax)]
12
13 extern crate rustc;
14 extern crate rustc_driver;
15 extern crate rustc_llvm;
16 extern crate rustc_trans;
17 #[macro_use] extern crate syntax;
18 extern crate getopts;
19
20 use rustc_driver::{CompilerCalls, Compilation};
21 use rustc_driver::driver::CompileController;
22 use rustc_trans::ModuleSource;
23 use rustc::session::Session;
24 use syntax::codemap::FileLoader;
25 use std::env;
26 use std::io;
27 use std::path::{PathBuf, Path};
28
29 struct JitLoader;
30
31 impl FileLoader for JitLoader {
32     fn file_exists(&self, _: &Path) -> bool { true }
33     fn abs_path(&self, _: &Path) -> Option<PathBuf> { None }
34     fn read_file(&self, _: &Path) -> io::Result<String> {
35         Ok(r#"
36 #[no_mangle]
37 pub fn test_add(a: i32, b: i32) -> i32 { a + b }
38 "#.to_string())
39     }
40 }
41
42 #[derive(Copy, Clone)]
43 struct JitCalls;
44
45 impl<'a> CompilerCalls<'a> for JitCalls {
46     fn build_controller(&mut self,
47                         _: &Session,
48                         _: &getopts::Matches)
49                         -> CompileController<'a> {
50         let mut cc = CompileController::basic();
51         cc.after_llvm.stop = Compilation::Stop;
52         cc.after_llvm.run_callback_on_error = true;
53         cc.after_llvm.callback = Box::new(|state| {
54             state.session.abort_if_errors();
55             let trans = state.trans.unwrap();
56             assert_eq!(trans.modules.len(), 1);
57             let rs_llmod = match trans.modules[0].source {
58                 ModuleSource::Preexisting(_) => unimplemented!(),
59                 ModuleSource::Translated(llvm) => llvm.llmod,
60             };
61             unsafe { rustc_llvm::LLVMDumpModule(rs_llmod) };
62         });
63         cc
64     }
65 }
66
67 fn main() {
68     use rustc_driver;
69
70     let mut path = match std::env::args().nth(2) {
71         Some(path) => PathBuf::from(&path),
72         None => panic!("missing rustc path")
73     };
74
75     // Remove two segments from rustc path to get sysroot.
76     path.pop();
77     path.pop();
78
79     let mut args: Vec<String> =
80         format!("_ _ --sysroot {} --crate-type dylib", path.to_str().unwrap())
81         .split(' ').map(|s| s.to_string()).collect();
82     args.push("--out-dir".to_string());
83     args.push(env::var("TMPDIR").unwrap());
84
85     let (result, _) = rustc_driver::run_compiler(
86         &args, &mut JitCalls, Some(box JitLoader), None);
87     if let Err(n) = result {
88         panic!("Error {:?}", n);
89     }
90 }