]>
Commit | Line | Data |
---|---|---|
b039eaaf | 1 | #![feature(rustc_private)] |
c34b1796 | 2 | |
1a4d82fc JJ |
3 | extern crate rustc; |
4 | extern crate rustc_driver; | |
c34b1796 | 5 | extern crate rustc_lint; |
92a42be0 | 6 | extern crate rustc_metadata; |
3157f602 | 7 | extern crate rustc_errors; |
94b46f34 | 8 | extern crate rustc_codegen_utils; |
1a4d82fc JJ |
9 | extern crate syntax; |
10 | ||
11 | use rustc::session::{build_session, Session}; | |
b7449926 | 12 | use rustc::session::config::{Input, Options, |
5bcae85e | 13 | OutputType, OutputTypes}; |
94b46f34 | 14 | use rustc_driver::driver::{self, compile_input, CompileController}; |
92a42be0 | 15 | use rustc_metadata::cstore::CStore; |
3157f602 | 16 | use rustc_errors::registry::Registry; |
b7449926 | 17 | use syntax::source_map::FileName; |
94b46f34 | 18 | use rustc_codegen_utils::codegen_backend::CodegenBackend; |
1a4d82fc | 19 | |
c34b1796 | 20 | use std::path::PathBuf; |
92a42be0 | 21 | use std::rc::Rc; |
c34b1796 | 22 | |
1a4d82fc JJ |
23 | fn main() { |
24 | let src = r#" | |
25 | fn main() {} | |
26 | "#; | |
27 | ||
85aaf69f | 28 | let args: Vec<String> = std::env::args().collect(); |
1a4d82fc JJ |
29 | |
30 | if args.len() < 4 { | |
31 | panic!("expected rustc path"); | |
32 | } | |
33 | ||
c34b1796 | 34 | let tmpdir = PathBuf::from(&args[1]); |
1a4d82fc | 35 | |
c34b1796 | 36 | let mut sysroot = PathBuf::from(&args[3]); |
1a4d82fc JJ |
37 | sysroot.pop(); |
38 | sysroot.pop(); | |
39 | ||
40 | compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); | |
41 | ||
42 | compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); | |
43 | } | |
44 | ||
94b46f34 | 45 | fn basic_sess(opts: Options) -> (Session, Rc<CStore>, Box<CodegenBackend>) { |
d9579d0f | 46 | let descriptions = Registry::new(&rustc::DIAGNOSTICS); |
ea8adc8c | 47 | let sess = build_session(opts, None, descriptions); |
94b46f34 XL |
48 | let codegen_backend = rustc_driver::get_codegen_backend(&sess); |
49 | let cstore = Rc::new(CStore::new(codegen_backend.metadata_loader())); | |
c34b1796 | 50 | rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); |
94b46f34 | 51 | (sess, cstore, codegen_backend) |
1a4d82fc JJ |
52 | } |
53 | ||
c34b1796 | 54 | fn compile(code: String, output: PathBuf, sysroot: PathBuf) { |
0531ce1d | 55 | syntax::with_globals(|| { |
b7449926 | 56 | let mut opts = Options::default(); |
94b46f34 XL |
57 | opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]); |
58 | opts.maybe_sysroot = Some(sysroot); | |
59 | if let Ok(linker) = std::env::var("RUSTC_LINKER") { | |
60 | opts.cg.linker = Some(linker.into()); | |
61 | } | |
62 | driver::spawn_thread_pool(opts, |opts| { | |
63 | let (sess, cstore, codegen_backend) = basic_sess(opts); | |
64 | let control = CompileController::basic(); | |
0731742a XL |
65 | let name = FileName::anon_source_code(&code); |
66 | let input = Input::Str { name, input: code }; | |
94b46f34 XL |
67 | let _ = compile_input( |
68 | codegen_backend, | |
69 | &sess, | |
70 | &cstore, | |
71 | &None, | |
72 | &input, | |
73 | &None, | |
74 | &Some(output), | |
75 | None, | |
76 | &control | |
77 | ); | |
78 | }); | |
0531ce1d | 79 | }); |
1a4d82fc | 80 | } |