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.
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(rustc_private)]
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;
21 use rustc::dep_graph::DepGraph;
22 use rustc::session::{build_session, Session};
23 use rustc::session::config::{basic_options, build_configuration, Input,
24 OutputType, OutputTypes};
25 use rustc_driver::driver::{compile_input, CompileController, anon_src};
26 use rustc_metadata::cstore::CStore;
27 use rustc_errors::registry::Registry;
29 use std::collections::HashSet;
30 use std::path::PathBuf;
38 let args: Vec<String> = std::env::args().collect();
41 panic!("expected rustc path");
44 let tmpdir = PathBuf::from(&args[1]);
46 let mut sysroot = PathBuf::from(&args[3]);
50 compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
52 compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
55 fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
56 let mut opts = basic_options();
57 opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
58 opts.maybe_sysroot = Some(sysroot);
60 let descriptions = Registry::new(&rustc::DIAGNOSTICS);
61 let cstore = Rc::new(CStore::new(Box::new(rustc_trans::LlvmMetadataLoader)));
62 let sess = build_session(opts, None, descriptions);
63 rustc_trans::init(&sess);
64 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
68 fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
69 let (sess, cstore) = basic_sess(sysroot);
70 let cfg = build_configuration(&sess, HashSet::new());
71 let control = CompileController::basic();
72 let input = Input::Str { name: anon_src(), input: code };
73 compile_input(&sess, &cstore, &input, &None, &Some(output), None, &control);