2015-09-30 10:08:37 -07:00
|
|
|
#![feature(rustc_private)]
|
2015-03-05 18:33:58 -08:00
|
|
|
|
2019-11-15 19:41:50 +01:00
|
|
|
extern crate rustc_driver;
|
2023-01-13 15:19:25 +00:00
|
|
|
extern crate rustc_interface;
|
2020-03-11 12:49:08 +01:00
|
|
|
extern crate rustc_session;
|
2020-01-02 00:01:07 +01:00
|
|
|
extern crate rustc_span;
|
2014-11-28 21:56:09 -07:00
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
use rustc_interface::interface;
|
2023-02-26 15:27:27 -05:00
|
|
|
use rustc_session::config::{Input, Options, OutFileName, OutputType, OutputTypes};
|
2020-01-02 00:01:07 +01:00
|
|
|
use rustc_span::source_map::FileName;
|
2014-11-28 21:56:09 -07:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2014-11-28 21:56:09 -07:00
|
|
|
fn main() {
|
|
|
|
let src = r#"
|
|
|
|
fn main() {}
|
|
|
|
"#;
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
let args: Vec<String> = std::env::args().collect();
|
2014-11-28 21:56:09 -07:00
|
|
|
|
|
|
|
if args.len() < 4 {
|
|
|
|
panic!("expected rustc path");
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:54:39 -07:00
|
|
|
let tmpdir = PathBuf::from(&args[1]);
|
2014-11-28 21:56:09 -07:00
|
|
|
|
2015-03-23 15:54:39 -07:00
|
|
|
let mut sysroot = PathBuf::from(&args[3]);
|
2014-11-28 21:56:09 -07: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 21:00:43 -08:00
|
|
|
fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
2018-12-08 20:30:23 +01: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-29 02:39:30 +02:00
|
|
|
crate_check_cfg: Default::default(),
|
2018-12-08 20:30:23 +01:00
|
|
|
input,
|
2023-02-26 15:27:27 -05:00
|
|
|
output_file: Some(OutFileName::Real(output)),
|
2018-12-08 20:30:23 +01:00
|
|
|
output_dir: None,
|
2023-03-03 22:25:18 +00:00
|
|
|
ice_file: None,
|
2018-12-08 20:30:23 +01:00
|
|
|
file_loader: None,
|
2022-10-13 10:13:02 +01:00
|
|
|
locale_resources: &[],
|
2018-12-08 20:30:23 +01:00
|
|
|
lint_caps: Default::default(),
|
2021-03-15 21:16:39 +08:00
|
|
|
parse_sess_created: None,
|
2019-10-10 19:33:00 -04:00
|
|
|
register_lints: None,
|
2019-11-11 16:09:03 +01:00
|
|
|
override_queries: None,
|
2020-09-08 13:44:41 +02:00
|
|
|
make_codegen_backend: None,
|
2019-11-15 19:41:50 +01:00
|
|
|
registry: rustc_driver::diagnostics_registry(),
|
2018-12-08 20:30:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
interface::run_compiler(config, |compiler| {
|
2023-06-21 11:26:49 +10:00
|
|
|
let linker = compiler.enter(|queries| {
|
|
|
|
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
|
|
|
|
let ongoing_codegen = queries.ongoing_codegen()?;
|
|
|
|
queries.linker(ongoing_codegen)
|
|
|
|
});
|
2023-07-11 22:53:23 -05:00
|
|
|
linker.unwrap().link().unwrap();
|
2018-03-07 02:44:10 +01:00
|
|
|
});
|
2014-11-28 21:56:09 -07:00
|
|
|
}
|