]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/issue-19371/foo.rs
0336fe277c51f8eb6f064d6e248d1f0d00e26653
[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 syntax;
19
20 use rustc::dep_graph::DepGraph;
21 use rustc::session::{build_session, Session};
22 use rustc::session::config::{basic_options, build_configuration, Input,
23                              OutputType, OutputTypes};
24 use rustc_driver::driver::{compile_input, CompileController, anon_src};
25 use rustc_metadata::cstore::CStore;
26 use rustc_errors::registry::Registry;
27
28 use std::collections::HashSet;
29 use std::path::PathBuf;
30 use std::rc::Rc;
31
32 fn main() {
33     let src = r#"
34     fn main() {}
35     "#;
36
37     let args: Vec<String> = std::env::args().collect();
38
39     if args.len() < 4 {
40         panic!("expected rustc path");
41     }
42
43     let tmpdir = PathBuf::from(&args[1]);
44
45     let mut sysroot = PathBuf::from(&args[3]);
46     sysroot.pop();
47     sysroot.pop();
48
49     compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
50
51     compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
52 }
53
54 fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
55     let mut opts = basic_options();
56     opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
57     opts.maybe_sysroot = Some(sysroot);
58
59     let descriptions = Registry::new(&rustc::DIAGNOSTICS);
60     let dep_graph = DepGraph::new(opts.build_dep_graph());
61     let cstore = Rc::new(CStore::new(&dep_graph));
62     let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone());
63     rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
64     (sess, cstore)
65 }
66
67 fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
68     let (sess, cstore) = basic_sess(sysroot);
69     let cfg = build_configuration(&sess, HashSet::new());
70     let control = CompileController::basic();
71     let input = Input::Str { name: anon_src(), input: code };
72     compile_input(&sess, &cstore, &input, &None, &Some(output), None, &control);
73 }