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 #[macro_use] extern crate syntax;
19 use rustc_driver
::{CompilerCalls, Compilation}
;
20 use rustc_driver
::driver
::CompileController
;
21 use rustc
::session
::Session
;
22 use syntax
::codemap
::FileLoader
;
24 use std
::path
::{PathBuf, Path}
;
28 impl FileLoader
for JitLoader
{
29 fn file_exists(&self, _
: &Path
) -> bool { true }
30 fn abs_path(&self, _
: &Path
) -> Option
<PathBuf
> { None }
31 fn read_file(&self, _
: &Path
) -> io
::Result
<String
> {
34 pub fn test_add(a: i32, b: i32) -> i32 { a + b }
39 #[derive(Copy, Clone)]
42 impl<'a
> CompilerCalls
<'a
> for JitCalls
{
43 fn build_controller(&mut self,
46 -> CompileController
<'a
> {
47 let mut cc
= CompileController
::basic();
48 cc
.after_llvm
.stop
= Compilation
::Stop
;
49 cc
.after_llvm
.run_callback_on_error
= true;
50 cc
.after_llvm
.callback
= Box
::new(|state
| {
51 state
.session
.abort_if_errors();
52 let trans
= state
.trans
.unwrap();
53 assert_eq
!(trans
.modules
.len(), 1);
54 let rs_llmod
= trans
.modules
[0].llmod
;
55 unsafe { rustc_llvm::LLVMDumpModule(rs_llmod) }
;
64 let mut path
= match std
::env
::args().nth(2) {
65 Some(path
) => PathBuf
::from(&path
),
66 None
=> panic
!("missing rustc path")
69 // Remove two segments from rustc path to get sysroot.
73 let args
: Vec
<String
> =
74 format
!("_ _ --sysroot {} --crate-type dylib", path
.to_str().unwrap())
75 .split(' '
).map(|s
| s
.to_string()).collect();
77 let (result
, _
) = rustc_driver
::run_compiler_with_file_loader(
78 &args
, &mut JitCalls
, box JitLoader
);
79 if let Err(n
) = result
{
80 panic
!("Error {}", n
);