]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/issue-19371/foo.rs
rustc: Load the `rustc_trans` crate at runtime
[rust.git] / src / test / run-make / issue-19371 / foo.rs
1 // Copyright 2014 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(rustc_private)]
12
13 extern crate rustc;
14 extern crate rustc_driver;
15 extern crate rustc_lint;
16 extern crate rustc_metadata;
17 extern crate rustc_errors;
18 extern crate rustc_trans_utils;
19 extern crate syntax;
20
21 use rustc::session::{build_session, Session};
22 use rustc::session::config::{basic_options, Input,
23                              OutputType, OutputTypes};
24 use rustc_driver::driver::{compile_input, CompileController};
25 use rustc_metadata::cstore::CStore;
26 use rustc_errors::registry::Registry;
27 use syntax::codemap::FileName;
28 use rustc_trans_utils::trans_crate::TransCrate;
29
30 use std::path::PathBuf;
31 use std::rc::Rc;
32
33 fn main() {
34     let src = r#"
35     fn main() {}
36     "#;
37
38     let args: Vec<String> = std::env::args().collect();
39
40     if args.len() < 4 {
41         panic!("expected rustc path");
42     }
43
44     let tmpdir = PathBuf::from(&args[1]);
45
46     let mut sysroot = PathBuf::from(&args[3]);
47     sysroot.pop();
48     sysroot.pop();
49
50     compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
51
52     compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
53 }
54
55 fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>, Box<TransCrate>) {
56     let mut opts = basic_options();
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
63     let descriptions = Registry::new(&rustc::DIAGNOSTICS);
64     let sess = build_session(opts, None, descriptions);
65     let trans = rustc_driver::get_trans(&sess);
66     let cstore = Rc::new(CStore::new(trans.metadata_loader()));
67     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
68     (sess, cstore, trans)
69 }
70
71 fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
72     let (sess, cstore, trans) = basic_sess(sysroot);
73     let control = CompileController::basic();
74     let input = Input::Str { name: FileName::Anon, input: code };
75     let _ = compile_input(
76         trans,
77         &sess,
78         &cstore,
79         &None,
80         &input,
81         &None,
82         &Some(output),
83         None,
84         &control
85     );
86 }