2015-09-30 12:08:37 -05:00
|
|
|
#![feature(rustc_private)]
|
2015-03-05 20:33:58 -06:00
|
|
|
|
2019-11-15 12:41:50 -06:00
|
|
|
extern crate rustc_driver;
|
2023-01-13 09:19:25 -06:00
|
|
|
extern crate rustc_interface;
|
2020-03-11 06:49:08 -05:00
|
|
|
extern crate rustc_session;
|
2020-01-01 17:01:07 -06:00
|
|
|
extern crate rustc_span;
|
2014-11-28 22:56:09 -06:00
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
use rustc_interface::interface;
|
2023-01-13 09:19:25 -06:00
|
|
|
use rustc_session::config::{Input, Options, OutputType, OutputTypes};
|
2020-01-01 17:01:07 -06:00
|
|
|
use rustc_span::source_map::FileName;
|
2014-11-28 22:56:09 -06:00
|
|
|
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2014-11-28 22:56:09 -06:00
|
|
|
fn main() {
|
|
|
|
let src = r#"
|
|
|
|
fn main() {}
|
|
|
|
"#;
|
|
|
|
|
2015-02-16 08:04:02 -06:00
|
|
|
let args: Vec<String> = std::env::args().collect();
|
2014-11-28 22:56:09 -06:00
|
|
|
|
|
|
|
if args.len() < 4 {
|
|
|
|
panic!("expected rustc path");
|
|
|
|
}
|
|
|
|
|
2015-03-23 17:54:39 -05:00
|
|
|
let tmpdir = PathBuf::from(&args[1]);
|
2014-11-28 22:56:09 -06:00
|
|
|
|
2015-03-23 17:54:39 -05:00
|
|
|
let mut sysroot = PathBuf::from(&args[3]);
|
2014-11-28 22:56:09 -06:00
|
|
|
sysroot.pop();
|
|
|
|
sysroot.pop();
|
|
|
|
|
|
|
|
compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
|
|
|
|
|
|
|
|
compile(src.to_string(), tmpdir.join("out"), sysroot.clone());
|
|
|
|
}
|
|
|
|
|
2015-02-26 23:00:43 -06:00
|
|
|
fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
2018-12-08 13:30:23 -06:00
|
|
|
let mut opts = Options::default();
|
|
|
|
opts.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
|
|
|
|
opts.maybe_sysroot = Some(sysroot);
|
|
|
|
|
|
|
|
if let Ok(linker) = std::env::var("RUSTC_LINKER") {
|
|
|
|
opts.cg.linker = Some(linker.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = FileName::anon_source_code(&code);
|
|
|
|
let input = Input::Str { name, input: code };
|
|
|
|
|
|
|
|
let config = interface::Config {
|
|
|
|
opts,
|
|
|
|
crate_cfg: Default::default(),
|
2021-09-28 19:39:30 -05:00
|
|
|
crate_check_cfg: Default::default(),
|
2018-12-08 13:30:23 -06:00
|
|
|
input,
|
|
|
|
output_file: Some(output),
|
|
|
|
output_dir: None,
|
|
|
|
file_loader: None,
|
|
|
|
lint_caps: Default::default(),
|
2021-03-15 08:16:39 -05:00
|
|
|
parse_sess_created: None,
|
2019-10-10 18:33:00 -05:00
|
|
|
register_lints: None,
|
2019-11-11 09:09:03 -06:00
|
|
|
override_queries: None,
|
2020-09-08 06:44:41 -05:00
|
|
|
make_codegen_backend: None,
|
2019-11-15 12:41:50 -06:00
|
|
|
registry: rustc_driver::diagnostics_registry(),
|
2018-12-08 13:30:23 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
interface::run_compiler(config, |compiler| {
|
2019-08-30 02:27:35 -05:00
|
|
|
// This runs all the passes prior to linking, too.
|
2023-01-13 09:19:25 -06:00
|
|
|
let linker = compiler.enter(|queries| queries.linker());
|
2019-11-24 14:12:38 -06:00
|
|
|
if let Ok(linker) = linker {
|
|
|
|
linker.link();
|
|
|
|
}
|
2018-03-06 19:44:10 -06:00
|
|
|
});
|
2014-11-28 22:56:09 -06:00
|
|
|
}
|