Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ $ LIBRARY_PATH="[gcc-path value]" LD_LIBRARY_PATH="[gcc-path value]" rustc +$(ca
* _**CG_GCCJIT_DUMP_EVERYTHING**_: Enables dumping of all intermediate representations and passes.
* _**CG_GCCJIT_KEEP_INTERMEDIATES**_: Keeps intermediate files generated during the compilation process.
* _**CG_GCCJIT_VERBOSE**_: Enables verbose output from the GCC driver.
* _**CG_GCCJIT_LIBRARY_PATH**_: Add a custom path to the `libgccjit` library file to load first. If the specified file wasn't found (or couldn't be loaded), then it'll automatically look through sysroot paths to find the library instead.

## Extra documentation

Expand Down
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ 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};
use std::sync::{Arc, Mutex};
use std::{env, fs};

#[cfg(feature = "master")]
use gccjit::TargetInfo;
Expand Down Expand Up @@ -210,9 +210,22 @@ impl CodegenBackend for GccCodegenBackend {
lib_path.join(sess.target.llvm_target.as_ref()).join("libgccjit.so");
paths.push(llvm_target_path);
}

paths
}

// If the user set the CG_GCCJIT_LIBRARY_PATH environment variable, then we'll load the
// library first from there.
if let Ok(custom_path) = env::var("CG_GCCJIT_LIBRARY_PATH").as_deref()
&& let Ok(true) = fs::exists(custom_path)
{
load_libgccjit_if_needed(&PathBuf::from(custom_path));

if !gccjit::is_loaded() {
println!("Could not load gccjit from CG_GCCJIT_LIBRARY_PATH: {}", custom_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.
Expand All @@ -227,6 +240,13 @@ impl CodegenBackend for GccCodegenBackend {

if !gccjit::is_loaded() {
let mut paths = vec![];

// Add the path from the environment variable to tell the user that we tried to load
// the library from there.
if let Ok(custom_path) = env::var("CG_GCCJIT_LIBRARY_PATH").as_deref() {
paths.push(PathBuf::from(custom_path));
}

for path in sess.opts.sysroot.all_paths() {
for libgccjit_target_lib_file in file_paths(path, sess) {
paths.push(libgccjit_target_lib_file);
Expand Down
Loading