]>
Commit | Line | Data |
---|---|---|
b039eaaf | 1 | #![feature(rustc_private)] |
c34b1796 | 2 | |
1a4d82fc | 3 | extern crate rustc; |
532ac7d7 | 4 | extern crate rustc_interface; |
60c5eb7d | 5 | extern crate rustc_driver; |
1a4d82fc JJ |
6 | extern crate syntax; |
7 | ||
532ac7d7 | 8 | use rustc::session::DiagnosticOutput; |
b7449926 | 9 | use rustc::session::config::{Input, Options, |
5bcae85e | 10 | OutputType, OutputTypes}; |
532ac7d7 | 11 | use rustc_interface::interface; |
b7449926 | 12 | use syntax::source_map::FileName; |
1a4d82fc | 13 | |
c34b1796 AL |
14 | use std::path::PathBuf; |
15 | ||
1a4d82fc JJ |
16 | fn main() { |
17 | let src = r#" | |
18 | fn main() {} | |
19 | "#; | |
20 | ||
85aaf69f | 21 | let args: Vec<String> = std::env::args().collect(); |
1a4d82fc JJ |
22 | |
23 | if args.len() < 4 { | |
24 | panic!("expected rustc path"); | |
25 | } | |
26 | ||
c34b1796 | 27 | let tmpdir = PathBuf::from(&args[1]); |
1a4d82fc | 28 | |
c34b1796 | 29 | let mut sysroot = PathBuf::from(&args[3]); |
1a4d82fc JJ |
30 | sysroot.pop(); |
31 | sysroot.pop(); | |
32 | ||
33 | compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); | |
34 | ||
35 | compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); | |
36 | } | |
37 | ||
c34b1796 | 38 | fn compile(code: String, output: PathBuf, sysroot: PathBuf) { |
532ac7d7 XL |
39 | let mut opts = Options::default(); |
40 | opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]); | |
41 | opts.maybe_sysroot = Some(sysroot); | |
42 | ||
43 | if let Ok(linker) = std::env::var("RUSTC_LINKER") { | |
44 | opts.cg.linker = Some(linker.into()); | |
45 | } | |
46 | ||
47 | let name = FileName::anon_source_code(&code); | |
48 | let input = Input::Str { name, input: code }; | |
49 | ||
50 | let config = interface::Config { | |
51 | opts, | |
52 | crate_cfg: Default::default(), | |
53 | input, | |
54 | input_path: None, | |
55 | output_file: Some(output), | |
56 | output_dir: None, | |
57 | file_loader: None, | |
58 | diagnostic_output: DiagnosticOutput::Default, | |
59 | stderr: None, | |
60 | crate_name: None, | |
61 | lint_caps: Default::default(), | |
e74abb32 | 62 | register_lints: None, |
60c5eb7d XL |
63 | override_queries: None, |
64 | registry: rustc_driver::diagnostics_registry(), | |
532ac7d7 XL |
65 | }; |
66 | ||
67 | interface::run_compiler(config, |compiler| { | |
e74abb32 | 68 | // This runs all the passes prior to linking, too. |
60c5eb7d XL |
69 | let linker = compiler.enter(|queries| { |
70 | queries.linker() | |
71 | }); | |
72 | if let Ok(linker) = linker { | |
73 | linker.link(); | |
74 | } | |
0531ce1d | 75 | }); |
1a4d82fc | 76 | } |