Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod type_of;
use std::any::Any;
use std::ffi::CString;
use std::fmt::Debug;
use std::fs;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -180,14 +181,18 @@ pub struct GccCodegenBackend {

static LTO_SUPPORTED: AtomicBool = AtomicBool::new(false);

fn libgccjit_path(sysroot_path: &Path) -> PathBuf {
let sysroot_lib_dir = sysroot_path.join("lib");
sysroot_lib_dir.join("libgccjit.so")
}

fn load_libgccjit_if_needed(sysroot_path: &Path) {
if gccjit::is_loaded() {
// Do not load a libgccjit second time.
return;
}

let sysroot_lib_dir = sysroot_path.join("lib");
let libgccjit_target_lib_file = sysroot_lib_dir.join("libgccjit.so");
let libgccjit_target_lib_file = libgccjit_path(sysroot_path);
let path = libgccjit_target_lib_file.to_str().expect("libgccjit path");

let string = CString::new(path).expect("string to libgccjit path");
Expand All @@ -207,7 +212,16 @@ impl CodegenBackend for GccCodegenBackend {
}

fn init(&self, sess: &Session) {
load_libgccjit_if_needed(sess.opts.sysroot.path());
// We use all_paths() instead of only path() in case the path specified by --sysroot is
// invalid.
// This is the case for instance in Rust for Linux where they specify --sysroot=/dev/null.
for path in sess.opts.sysroot.all_paths() {
let libgccjit_target_lib_file = libgccjit_path(path);
if let Ok(true) = fs::exists(libgccjit_target_lib_file) {
load_libgccjit_if_needed(path);
break;
}
}

#[cfg(feature = "master")]
{
Expand Down