@@ -69,6 +69,7 @@ mod type_;
6969mod type_of;
7070
7171use std:: any:: Any ;
72+ use std:: ffi:: CString ;
7273use std:: fmt:: Debug ;
7374use std:: ops:: Deref ;
7475use std:: path:: PathBuf ;
@@ -142,7 +143,7 @@ impl TargetInfo {
142143
143144#[ derive( Clone ) ]
144145pub struct LockedTargetInfo {
145- info : Arc < Mutex < IntoDynSyncSend < TargetInfo > > > ,
146+ info : Arc < Mutex < IntoDynSyncSend < Option < TargetInfo > > > > ,
146147}
147148
148149impl Debug for LockedTargetInfo {
@@ -153,11 +154,21 @@ impl Debug for LockedTargetInfo {
153154
154155impl LockedTargetInfo {
155156 fn cpu_supports ( & self , feature : & str ) -> bool {
156- self . info . lock ( ) . expect ( "lock" ) . cpu_supports ( feature)
157+ self . info
158+ . lock ( )
159+ . expect ( "lock" )
160+ . as_ref ( )
161+ . expect ( "target info not initialized" )
162+ . cpu_supports ( feature)
157163 }
158164
159165 fn supports_target_dependent_type ( & self , typ : CType ) -> bool {
160- self . info . lock ( ) . expect ( "lock" ) . supports_target_dependent_type ( typ)
166+ self . info
167+ . lock ( )
168+ . expect ( "lock" )
169+ . as_ref ( )
170+ . expect ( "target info not initialized" )
171+ . supports_target_dependent_type ( typ)
161172 }
162173}
163174
@@ -169,6 +180,19 @@ pub struct GccCodegenBackend {
169180
170181static LTO_SUPPORTED : AtomicBool = AtomicBool :: new ( false ) ;
171182
183+ fn load_libgccjit_if_needed ( ) {
184+ if gccjit:: is_loaded ( ) {
185+ // Do not load a libgccjit second time.
186+ return ;
187+ }
188+
189+ let string = CString :: new ( "libgccjit.so" ) . expect ( "string to libgccjit path" ) ;
190+
191+ if let Err ( error) = gccjit:: load ( & string) {
192+ panic ! ( "Cannot load libgccjit.so: {}" , error) ;
193+ }
194+ }
195+
172196impl CodegenBackend for GccCodegenBackend {
173197 fn locale_resource ( & self ) -> & ' static str {
174198 crate :: DEFAULT_LOCALE_RESOURCE
@@ -179,6 +203,8 @@ impl CodegenBackend for GccCodegenBackend {
179203 }
180204
181205 fn init ( & self , _sess : & Session ) {
206+ load_libgccjit_if_needed ( ) ;
207+
182208 #[ cfg( feature = "master" ) ]
183209 {
184210 let target_cpu = target_cpu ( _sess) ;
@@ -189,7 +215,8 @@ impl CodegenBackend for GccCodegenBackend {
189215 context. add_command_line_option ( format ! ( "-march={}" , target_cpu) ) ;
190216 }
191217
192- * * self . target_info . info . lock ( ) . expect ( "lock" ) = context. get_target_info ( ) ;
218+ * self . target_info . info . lock ( ) . expect ( "lock" ) =
219+ IntoDynSyncSend ( Some ( context. get_target_info ( ) ) ) ;
193220 }
194221
195222 #[ cfg( feature = "master" ) ]
@@ -217,6 +244,9 @@ impl CodegenBackend for GccCodegenBackend {
217244 . info
218245 . lock ( )
219246 . expect ( "lock" )
247+ . 0
248+ . as_ref ( )
249+ . expect ( "target info not initialized" )
220250 . supports_128bit_integers
221251 . store ( check_context. get_last_error ( ) == Ok ( None ) , Ordering :: SeqCst ) ;
222252 }
@@ -438,13 +468,12 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
438468 let info = {
439469 // Check whether the target supports 128-bit integers, and sized floating point types (like
440470 // Float16).
441- let context = Context :: default ( ) ;
442- Arc :: new ( Mutex :: new ( IntoDynSyncSend ( context. get_target_info ( ) ) ) )
471+ Arc :: new ( Mutex :: new ( IntoDynSyncSend ( None ) ) )
443472 } ;
444473 #[ cfg( not( feature = "master" ) ) ]
445- let info = Arc :: new ( Mutex :: new ( IntoDynSyncSend ( TargetInfo {
474+ let info = Arc :: new ( Mutex :: new ( IntoDynSyncSend ( Some ( TargetInfo {
446475 supports_128bit_integers : AtomicBool :: new ( false ) ,
447- } ) ) ) ;
476+ } ) ) ) ) ;
448477
449478 Box :: new ( GccCodegenBackend {
450479 lto_supported : Arc :: new ( AtomicBool :: new ( false ) ) ,
0 commit comments