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.
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.
11 #![feature(plugin, rustc_private, box_syntax)]
14 extern crate rustc_driver;
15 extern crate rustc_llvm;
16 extern crate rustc_trans;
17 #[macro_use] extern crate syntax;
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;
26 use std::path::{PathBuf, Path};
30 impl FileLoader for JitLoader {
31 fn file_exists(&self, _: &Path) -> bool { true }
32 fn abs_path(&self, _: &Path) -> Option<PathBuf> { None }
33 fn read_file(&self, _: &Path) -> io::Result<String> {
36 pub fn test_add(a: i32, b: i32) -> i32 { a + b }
41 #[derive(Copy, Clone)]
44 impl<'a> CompilerCalls<'a> for JitCalls {
45 fn build_controller(&mut self,
48 -> CompileController<'a> {
49 let mut cc = CompileController::basic();
50 cc.after_llvm.stop = Compilation::Stop;
51 cc.after_llvm.run_callback_on_error = true;
52 cc.after_llvm.callback = Box::new(|state| {
53 state.session.abort_if_errors();
54 let trans = state.trans.unwrap();
55 assert_eq!(trans.modules.len(), 1);
56 let rs_llmod = match trans.modules[0].source {
57 ModuleSource::Preexisting(_) => unimplemented!(),
58 ModuleSource::Translated(llvm) => llvm.llmod,
60 unsafe { rustc_llvm::LLVMDumpModule(rs_llmod) };
69 let mut path = match std::env::args().nth(2) {
70 Some(path) => PathBuf::from(&path),
71 None => panic!("missing rustc path")
74 // Remove two segments from rustc path to get sysroot.
78 let args: Vec<String> =
79 format!("_ _ --sysroot {} --crate-type dylib", path.to_str().unwrap())
80 .split(' ').map(|s| s.to_string()).collect();
82 let (result, _) = rustc_driver::run_compiler(
83 &args, &mut JitCalls, Some(box JitLoader), None);
84 if let Err(n) = result {
85 panic!("Error {}", n);