@@ -71,6 +71,7 @@ mod type_of;
7171use std:: any:: Any ;
7272use std:: ffi:: CString ;
7373use std:: fmt:: Debug ;
74+ use std:: fs;
7475use std:: ops:: Deref ;
7576use std:: path:: { Path , PathBuf } ;
7677use std:: sync:: atomic:: { AtomicBool , Ordering } ;
@@ -180,14 +181,18 @@ pub struct GccCodegenBackend {
180181
181182static LTO_SUPPORTED : AtomicBool = AtomicBool :: new ( false ) ;
182183
184+ fn libgccjit_path ( sysroot_path : & Path ) -> PathBuf {
185+ let sysroot_lib_dir = sysroot_path. join ( "lib" ) ;
186+ sysroot_lib_dir. join ( "libgccjit.so" )
187+ }
188+
183189fn load_libgccjit_if_needed ( sysroot_path : & Path ) {
184190 if gccjit:: is_loaded ( ) {
185191 // Do not load a libgccjit second time.
186192 return ;
187193 }
188194
189- let sysroot_lib_dir = sysroot_path. join ( "lib" ) ;
190- let libgccjit_target_lib_file = sysroot_lib_dir. join ( "libgccjit.so" ) ;
195+ let libgccjit_target_lib_file = libgccjit_path ( sysroot_path) ;
191196 let path = libgccjit_target_lib_file. to_str ( ) . expect ( "libgccjit path" ) ;
192197
193198 let string = CString :: new ( path) . expect ( "string to libgccjit path" ) ;
@@ -207,7 +212,16 @@ impl CodegenBackend for GccCodegenBackend {
207212 }
208213
209214 fn init ( & self , sess : & Session ) {
210- load_libgccjit_if_needed ( sess. opts . sysroot . path ( ) ) ;
215+ // We use all_paths() instead of only path() in case the path specified by --sysroot is
216+ // invalid.
217+ // This is the case for instance in Rust for Linux where they specify --sysroot=/dev/null.
218+ for path in sess. opts . sysroot . all_paths ( ) {
219+ let libgccjit_target_lib_file = libgccjit_path ( path) ;
220+ if let Ok ( true ) = fs:: exists ( libgccjit_target_lib_file) {
221+ load_libgccjit_if_needed ( path) ;
222+ break ;
223+ }
224+ }
211225
212226 #[ cfg( feature = "master" ) ]
213227 {
0 commit comments