Skip to content

Commit df9894b

Browse files
authored
Merge pull request #811 from rust-lang/fix/use-fallback-sysroot
Use fallback sysroot directory if we cannot find libgccjit.so in the explicit directory
2 parents b87bfdc + 2da3314 commit df9894b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ mod type_of;
7171
use std::any::Any;
7272
use std::ffi::CString;
7373
use std::fmt::Debug;
74+
use std::fs;
7475
use std::ops::Deref;
7576
use std::path::{Path, PathBuf};
7677
use std::sync::atomic::{AtomicBool, Ordering};
@@ -180,14 +181,18 @@ pub struct GccCodegenBackend {
180181

181182
static 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+
183189
fn 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

Comments
 (0)