@@ -39,6 +39,7 @@ extern crate rustc_target;
3939extern crate rustc_driver;
4040
4141use std:: any:: Any ;
42+ use std:: cell:: OnceCell ;
4243use std:: env;
4344use std:: sync:: Arc ;
4445
@@ -124,7 +125,7 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
124125}
125126
126127pub struct CraneliftCodegenBackend {
127- pub config : Option < BackendConfig > ,
128+ pub config : OnceCell < BackendConfig > ,
128129}
129130
130131impl CodegenBackend for CraneliftCodegenBackend {
@@ -150,6 +151,15 @@ impl CodegenBackend for CraneliftCodegenBackend {
150151 sess. dcx ( )
151152 . fatal ( "`-Cinstrument-coverage` is LLVM specific and not supported by Cranelift" ) ;
152153 }
154+
155+ let config = self . config . get_or_init ( || {
156+ BackendConfig :: from_opts ( & sess. opts . cg . llvm_args )
157+ . unwrap_or_else ( |err| sess. dcx ( ) . fatal ( err) )
158+ } ) ;
159+
160+ if config. jit_mode && !sess. opts . output_types . should_codegen ( ) {
161+ sess. dcx ( ) . fatal ( "JIT mode doesn't work with `cargo check`" ) ;
162+ }
153163 }
154164
155165 fn target_config ( & self , sess : & Session ) -> TargetConfig {
@@ -211,13 +221,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
211221
212222 fn codegen_crate ( & self , tcx : TyCtxt < ' _ > ) -> Box < dyn Any > {
213223 info ! ( "codegen crate {}" , tcx. crate_name( LOCAL_CRATE ) ) ;
214- let config = self . config . clone ( ) . unwrap_or_else ( || {
215- BackendConfig :: from_opts ( & tcx. sess . opts . cg . llvm_args )
216- . unwrap_or_else ( |err| tcx. sess . dcx ( ) . fatal ( err) )
217- } ) ;
224+ let config = self . config . get ( ) . unwrap ( ) ;
218225 if config. jit_mode {
219226 #[ cfg( feature = "jit" ) ]
220- driver:: jit:: run_jit ( tcx, config. jit_args ) ;
227+ driver:: jit:: run_jit ( tcx, config. jit_args . clone ( ) ) ;
221228
222229 #[ cfg( not( feature = "jit" ) ) ]
223230 tcx. dcx ( ) . fatal ( "jit support was disabled when compiling rustc_codegen_cranelift" ) ;
@@ -363,5 +370,5 @@ fn build_isa(sess: &Session, jit: bool) -> Arc<dyn TargetIsa + 'static> {
363370/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
364371#[ unsafe( no_mangle) ]
365372pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
366- Box :: new ( CraneliftCodegenBackend { config : None } )
373+ Box :: new ( CraneliftCodegenBackend { config : OnceCell :: new ( ) } )
367374}
0 commit comments