2024-06-04 21:56:38 -05:00
|
|
|
//@ edition: 2021
|
|
|
|
//@ run-pass
|
|
|
|
//@ run-flags: {{sysroot-base}} {{target-linker}}
|
|
|
|
//@ ignore-stage1 (requires matching sysroot built with in-tree compiler)
|
|
|
|
|
|
|
|
// Regression test for <https://github.com/rust-lang/rust/issues/19371>.
|
|
|
|
//
|
|
|
|
// This test ensures that `compile_input` can be called twice in one task
|
|
|
|
// without causing a panic.
|
|
|
|
|
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
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2024-06-30 13:44:11 -05:00
|
|
|
use rustc_interface::Linker;
|
2018-12-08 13:30:23 -06:00
|
|
|
use rustc_interface::interface;
|
2023-02-26 14:27:27 -06:00
|
|
|
use rustc_session::config::{Input, Options, OutFileName, OutputType, OutputTypes};
|
2023-11-01 22:10:12 -05:00
|
|
|
use rustc_span::FileName;
|
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
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
if args.len() < 2 {
|
|
|
|
panic!("expected sysroot (and optional linker)");
|
2014-11-28 22:56:09 -06:00
|
|
|
}
|
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
let sysroot = PathBuf::from(&args[1]);
|
|
|
|
let linker = args.get(2).map(PathBuf::from);
|
2014-11-28 22:56:09 -06:00
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
// compiletest sets the current dir to `output_base_dir` when running.
|
|
|
|
let tmpdir = std::env::current_dir().unwrap().join("tmp");
|
|
|
|
std::fs::create_dir_all(&tmpdir).unwrap();
|
2014-11-28 22:56:09 -06:00
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
compile(src.to_string(), tmpdir.join("out"), sysroot.clone(), linker.as_deref());
|
|
|
|
compile(src.to_string(), tmpdir.join("out"), sysroot.clone(), linker.as_deref());
|
2014-11-28 22:56:09 -06:00
|
|
|
}
|
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
fn compile(code: String, output: PathBuf, sysroot: PathBuf, linker: Option<&Path>) {
|
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);
|
|
|
|
|
2024-06-04 21:56:38 -05:00
|
|
|
if let Some(linker) = linker {
|
|
|
|
opts.cg.linker = Some(linker.to_owned());
|
2018-12-08 13:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-02-26 14:27:27 -06:00
|
|
|
output_file: Some(OutFileName::Real(output)),
|
2018-12-08 13:30:23 -06:00
|
|
|
output_dir: None,
|
2023-03-03 16:25:18 -06:00
|
|
|
ice_file: None,
|
2018-12-08 13:30:23 -06:00
|
|
|
file_loader: None,
|
2024-09-05 09:00:48 -05:00
|
|
|
locale_resources: Vec::new(),
|
2018-12-08 13:30:23 -06:00
|
|
|
lint_caps: Default::default(),
|
2024-03-03 23:31:49 -06:00
|
|
|
psess_created: None,
|
2023-10-13 12:28:34 -05:00
|
|
|
hash_untracked_state: 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(),
|
2023-10-16 15:11:57 -05:00
|
|
|
using_internal_features: std::sync::Arc::default(),
|
2023-07-03 06:11:27 -05:00
|
|
|
expanded_args: Default::default(),
|
2018-12-08 13:30:23 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
interface::run_compiler(config, |compiler| {
|
2023-06-20 20:26:49 -05:00
|
|
|
let linker = compiler.enter(|queries| {
|
2024-06-30 13:44:11 -05:00
|
|
|
queries.global_ctxt()?.enter(|tcx| {
|
|
|
|
tcx.analysis(())?;
|
|
|
|
Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)
|
|
|
|
})
|
2023-06-20 20:26:49 -05:00
|
|
|
});
|
2023-11-19 20:26:09 -06:00
|
|
|
linker.unwrap().link(&compiler.sess, &*compiler.codegen_backend).unwrap();
|
2018-03-06 19:44:10 -06:00
|
|
|
});
|
2014-11-28 22:56:09 -06:00
|
|
|
}
|