From f6d8d38e4837e063a412f99e03195478b6b8ef07 Mon Sep 17 00:00:00 2001 From: techmetx11 Date: Sat, 15 Aug 2026 19:00:56 +0100 Subject: [PATCH 1/4] Add a new environment variable for custom libgccjit path --- Readme.md | 1 + src/lib.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 9a7c624c9bc..5aae48db229 100644 --- a/Readme.md +++ b/Readme.md @@ -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_SEARCH_PATH**_: Add a custom library path to look for `libgccjit.so` in ## Extra documentation diff --git a/src/lib.rs b/src/lib.rs index 7e22d6db50c..6e7eab2b326 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -210,6 +210,10 @@ impl CodegenBackend for GccCodegenBackend { lib_path.join(sess.target.llvm_target.as_ref()).join("libgccjit.so"); paths.push(llvm_target_path); } + if let Ok(custom_path) = env::var("CG_GCCJIT_SEARCH_PATH").as_deref() { + paths.push(PathBuf::from(custom_path).join("libgccjit.so")); + } + paths } From 53bd4079f61d380a9a6e72e8908328fbb4324be2 Mon Sep 17 00:00:00 2001 From: techmetx11 Date: Fri, 21 Aug 2026 18:04:51 +0100 Subject: [PATCH 2/4] Check for the libgccjit library in the environment variable first --- Readme.md | 2 +- src/lib.rs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 5aae48db229..21f8148f6c5 100644 --- a/Readme.md +++ b/Readme.md @@ -172,7 +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_SEARCH_PATH**_: Add a custom library path to look for `libgccjit.so` in + * _**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 diff --git a/src/lib.rs b/src/lib.rs index 6e7eab2b326..3ec0e40aedb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,13 +210,18 @@ impl CodegenBackend for GccCodegenBackend { lib_path.join(sess.target.llvm_target.as_ref()).join("libgccjit.so"); paths.push(llvm_target_path); } - if let Ok(custom_path) = env::var("CG_GCCJIT_SEARCH_PATH").as_deref() { - paths.push(PathBuf::from(custom_path).join("libgccjit.so")); - } 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)); + } + // 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. @@ -231,6 +236,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); From 00182e362cb81cf3a57bc5fa3d450f1f39af10f5 Mon Sep 17 00:00:00 2001 From: techmetx11 Date: Fri, 21 Aug 2026 18:06:54 +0100 Subject: [PATCH 3/4] Add warning for if the custom libgccjit file wasn't loaded --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3ec0e40aedb..1b3148e4c08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -220,6 +220,10 @@ impl CodegenBackend for GccCodegenBackend { && 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 From 81f60b88e2bc4d67784bf4b97ff54c80df3f092d Mon Sep 17 00:00:00 2001 From: techmetx11 Date: Fri, 21 Aug 2026 18:08:04 +0100 Subject: [PATCH 4/4] Remove the punctuation at the end (could be confusing) --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1b3148e4c08..1f6f5523a98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,7 +222,7 @@ impl CodegenBackend for GccCodegenBackend { load_libgccjit_if_needed(&PathBuf::from(custom_path)); if !gccjit::is_loaded() { - println!("Could not load gccjit from CG_GCCJIT_LIBRARY_PATH: {}.", custom_path); + println!("Could not load gccjit from CG_GCCJIT_LIBRARY_PATH: {}", custom_path); } }