|
| 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 | +extern crate rustc; |
| 12 | +extern crate rustc_trans; |
| 13 | +extern crate syntax; |
| 14 | + |
| 15 | +use rustc::session::{build_session, Session}; |
| 16 | +use rustc::session::config::{basic_options, build_configuration, OutputTypeExe}; |
| 17 | +use rustc_trans::driver::driver::{Input, StrInput, compile_input}; |
| 18 | +use syntax::diagnostics::registry::Registry; |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let src = r#" |
| 22 | + fn main() {} |
| 23 | + "#; |
| 24 | + |
| 25 | + let args = std::os::args(); |
| 26 | + |
| 27 | + if args.len() < 4 { |
| 28 | + panic!("expected rustc path"); |
| 29 | + } |
| 30 | + |
| 31 | + let tmpdir = Path::new(args[1].as_slice()); |
| 32 | + |
| 33 | + let mut sysroot = Path::new(args[3].as_slice()); |
| 34 | + sysroot.pop(); |
| 35 | + sysroot.pop(); |
| 36 | + |
| 37 | + compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); |
| 38 | + |
| 39 | + compile(src.to_string(), tmpdir.join("out"), sysroot.clone()); |
| 40 | +} |
| 41 | + |
| 42 | +fn basic_sess(sysroot: Path) -> Session { |
| 43 | + let mut opts = basic_options(); |
| 44 | + opts.output_types = vec![OutputTypeExe]; |
| 45 | + opts.maybe_sysroot = Some(sysroot); |
| 46 | + |
| 47 | + let descriptions = Registry::new(&rustc::DIAGNOSTICS); |
| 48 | + let sess = build_session(opts, None, descriptions); |
| 49 | + sess |
| 50 | +} |
| 51 | + |
| 52 | +fn compile(code: String, output: Path, sysroot: Path) { |
| 53 | + let sess = basic_sess(sysroot); |
| 54 | + let cfg = build_configuration(&sess); |
| 55 | + |
| 56 | + compile_input(sess, |
| 57 | + cfg, |
| 58 | + &StrInput(code), |
| 59 | + &None, |
| 60 | + &Some(output), |
| 61 | + None); |
| 62 | +} |
0 commit comments