From 71bc62b9f696ae83ef1713bd96054c92cda9f27f Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 8 Sep 2020 13:44:41 +0200 Subject: [PATCH] Add option to pass a custom codegen backend from a driver --- compiler/rustc_driver/src/lib.rs | 12 +++++++++++- compiler/rustc_interface/src/interface.rs | 5 +++++ compiler/rustc_interface/src/util.rs | 10 +++++++++- src/librustdoc/core.rs | 1 + src/librustdoc/doctest.rs | 1 + src/test/run-make-fulldeps/issue-19371/foo.rs | 1 + src/test/ui-fulldeps/compiler-calls.rs | 8 +++++++- src/tools/clippy/src/driver.rs | 4 ++-- 8 files changed, 37 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 972e04fd101f0..fa5be646de24a 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -141,6 +141,9 @@ pub fn run_compiler( callbacks: &mut (dyn Callbacks + Send), file_loader: Option>, emitter: Option>, + make_codegen_backend: Option< + Box Box + Send>, + >, ) -> interface::Result<()> { let mut args = Vec::new(); for arg in at_args { @@ -162,6 +165,11 @@ pub fn run_compiler( let sopts = config::build_session_options(&matches); let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg")); + // We wrap `make_codegen_backend` in another `Option` such that `dummy_config` can take + // ownership of it when necessary, while also allowing the non-dummy config to take ownership + // when `dummy_config` is not used. + let mut make_codegen_backend = Some(make_codegen_backend); + let mut dummy_config = |sopts, cfg, diagnostic_output| { let mut config = interface::Config { opts: sopts, @@ -177,6 +185,7 @@ pub fn run_compiler( lint_caps: Default::default(), register_lints: None, override_queries: None, + make_codegen_backend: make_codegen_backend.take().unwrap(), registry: diagnostics_registry(), }; callbacks.config(&mut config); @@ -253,6 +262,7 @@ pub fn run_compiler( lint_caps: Default::default(), register_lints: None, override_queries: None, + make_codegen_backend: make_codegen_backend.unwrap(), registry: diagnostics_registry(), }; @@ -1265,7 +1275,7 @@ pub fn main() -> ! { }) }) .collect::>(); - run_compiler(&args, &mut callbacks, None, None) + run_compiler(&args, &mut callbacks, None, None, None) }); // The extra `\t` is necessary to align this label with the others. print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed()); diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 4d84462c42b0b..73a51ad477b22 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -154,6 +154,10 @@ pub struct Config { pub override_queries: Option, + /// This is a callback from the driver that is called to create a codegen backend. + pub make_codegen_backend: + Option Box + Send>>, + /// Registry of diagnostics codes. pub registry: Registry, } @@ -167,6 +171,7 @@ pub fn create_compiler_and_run(config: Config, f: impl FnOnce(&Compiler) -> R config.file_loader, config.input_path.clone(), config.lint_caps, + config.make_codegen_backend, registry.clone(), ); diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 0eed6938c3169..7ace707cc88e9 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -63,9 +63,17 @@ pub fn create_session( file_loader: Option>, input_path: Option, lint_caps: FxHashMap, + make_codegen_backend: Option< + Box Box + Send>, + >, descriptions: Registry, ) -> (Lrc, Lrc>) { - let codegen_backend = get_codegen_backend(&sopts); + let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend { + make_codegen_backend(&sopts) + } else { + get_codegen_backend(&sopts) + }; + // target_override is documented to be called before init(), so this is okay let target_override = codegen_backend.target_override(&sopts); diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 074a43f2a7099..391859050e8a6 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -419,6 +419,7 @@ pub fn run_core( (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id) }; }), + make_codegen_backend: None, registry: rustc_driver::diagnostics_registry(), }; diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 7b7c152d8abbf..7a6c9eabb5f40 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -95,6 +95,7 @@ pub fn run(options: Options) -> Result<(), ErrorReported> { lint_caps, register_lints: None, override_queries: None, + make_codegen_backend: None, registry: rustc_driver::diagnostics_registry(), }; diff --git a/src/test/run-make-fulldeps/issue-19371/foo.rs b/src/test/run-make-fulldeps/issue-19371/foo.rs index af84faa7511c6..2636423c1a480 100644 --- a/src/test/run-make-fulldeps/issue-19371/foo.rs +++ b/src/test/run-make-fulldeps/issue-19371/foo.rs @@ -60,6 +60,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) { lint_caps: Default::default(), register_lints: None, override_queries: None, + make_codegen_backend: None, registry: rustc_driver::diagnostics_registry(), }; diff --git a/src/test/ui-fulldeps/compiler-calls.rs b/src/test/ui-fulldeps/compiler-calls.rs index e97dcab6ae560..0025b47403d19 100644 --- a/src/test/ui-fulldeps/compiler-calls.rs +++ b/src/test/ui-fulldeps/compiler-calls.rs @@ -26,7 +26,13 @@ fn main() { let mut count = 1; let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()]; rustc_driver::catch_fatal_errors(|| { - rustc_driver::run_compiler(&args, &mut TestCalls { count: &mut count }, None, None).ok(); + rustc_driver::run_compiler( + &args, + &mut TestCalls { count: &mut count }, + None, + None, + None, + ).ok(); }).ok(); assert_eq!(count, 2); } diff --git a/src/tools/clippy/src/driver.rs b/src/tools/clippy/src/driver.rs index 47315fa64cd80..f4f2259cefd51 100644 --- a/src/tools/clippy/src/driver.rs +++ b/src/tools/clippy/src/driver.rs @@ -357,7 +357,7 @@ pub fn main() { args.extend(vec!["--sysroot".into(), sys_root]); }; - return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None); + return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None, None); } if orig_args.iter().any(|a| a == "--version" || a == "-V") { @@ -420,6 +420,6 @@ pub fn main() { let mut default = DefaultCallbacks; let callbacks: &mut (dyn rustc_driver::Callbacks + Send) = if clippy_enabled { &mut clippy } else { &mut default }; - rustc_driver::run_compiler(&args, callbacks, None, None) + rustc_driver::run_compiler(&args, callbacks, None, None, None) })) }