Skip to content

Commit 71205ac

Browse files
committed
Move libgccjit.so in the sysroot
This will be useful for the unification work of the handling of the library between all Rust setups.
1 parent 9d4c385 commit 71205ac

File tree

9 files changed

+30
-82
lines changed

9 files changed

+30
-82
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
- name: Setup path to libgccjit
6262
run: |
6363
sudo dpkg --force-overwrite -i ${{ matrix.libgccjit_version.gcc }}
64+
ls /usr/lib/libgccjit*
6465
echo 'gcc-path = "/usr/lib/"' > config.toml
6566
6667
# Some run-make tests fail if we use our forked GCC because it doesn't

.github/workflows/failures.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ jobs:
5454
if: matrix.libgccjit_version.gcc == 'libgccjit12.so'
5555
run: |
5656
echo 'gcc-path = "/usr/lib/gcc/x86_64-linux-gnu/12"' > config.toml
57-
echo "LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV
58-
echo "LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV
5957
6058
- name: Download artifact
6159
if: matrix.libgccjit_version.gcc != 'libgccjit12.so'

.github/workflows/gcc12.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ jobs:
5151
- name: Setup path to libgccjit
5252
run: echo 'gcc-path = "/usr/lib/gcc/x86_64-linux-gnu/12"' > config.toml
5353

54-
- name: Set env
55-
run: |
56-
echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV
57-
echo "LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV
58-
echo "LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV
59-
6054
#- name: Cache rust repository
6155
## We only clone the rust repository for rustc tests
6256
#if: ${{ contains(matrix.commands, 'rustc') }}

.github/workflows/stdarch.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,15 @@ jobs:
5959
sudo ln -s /usr/share/intel-sde/sde /usr/bin/sde
6060
sudo ln -s /usr/share/intel-sde/sde64 /usr/bin/sde64
6161
62-
- name: Set env
62+
- name: Set config
6363
run: |
64-
echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV
6564
echo 'download-gccjit = true' > config.toml
6665
6766
- name: Build
6867
run: |
6968
./y.sh prepare --only-libcore
7069
./y.sh build --sysroot --release --release-sysroot
7170
72-
- name: Set env (part 2)
73-
run: |
74-
# Set the `LD_LIBRARY_PATH` and `LIBRARY_PATH` env variables...
75-
echo "LD_LIBRARY_PATH="$(./y.sh info | grep -v Using) >> $GITHUB_ENV
76-
echo "LIBRARY_PATH="$(./y.sh info | grep -v Using) >> $GITHUB_ENV
77-
7871
- name: Clean
7972
if: ${{ !matrix.cargo_runner }}
8073
run: |

build_system/src/build.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::collections::HashMap;
22
use std::ffi::OsStr;
33
use std::fs;
4-
use std::path::Path;
4+
use std::os::unix::fs::symlink;
5+
use std::path::{Path, PathBuf};
56

67
use crate::config::{Channel, ConfigInfo};
78
use crate::utils::{
@@ -100,6 +101,20 @@ fn cleanup_sysroot_previous_build(library_dir: &Path) {
100101
pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
101102
let start_dir = get_sysroot_dir();
102103

104+
// Copy libgccjit.so to sysroot.
105+
let lib_path = start_dir.join("sysroot").join("lib");
106+
let libgccjit_path =
107+
PathBuf::from(config.gcc_path.as_ref().expect("libgccjit should be set by this point"))
108+
.join("libgccjit.so");
109+
let libgccjit_in_sysroot_path = lib_path.join("libgccjit.so");
110+
// First remove the file to be able to create the symlink even when the file exists.
111+
let _ = fs::remove_file(&libgccjit_in_sysroot_path);
112+
println!("Directory {:?} exists: {:?}", libgccjit_path, std::fs::exists(&libgccjit_path));
113+
create_dir(&lib_path)?;
114+
println!("Directory {:?} exists: {:?}", lib_path, std::fs::exists(&lib_path));
115+
symlink(libgccjit_path, libgccjit_in_sysroot_path)
116+
.map_err(|error| format!("Cannot create symlink for libgccjit.so: {}", error))?;
117+
103118
let library_dir = start_dir.join("sysroot_src").join("library");
104119
cleanup_sysroot_previous_build(&library_dir);
105120

@@ -148,7 +163,7 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
148163
run_command_with_output_and_env(&args, Some(&sysroot_dir), Some(&env))?;
149164

150165
// Copy files to sysroot
151-
let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple));
166+
let sysroot_path = lib_path.join(format!("rustlib/{}/lib/", config.target_triple));
152167
// To avoid errors like "multiple candidates for `rmeta` dependency `core` found", we clean the
153168
// sysroot directory before copying the sysroot build artifacts.
154169
let _ = fs::remove_dir_all(&sysroot_path);
@@ -175,13 +190,6 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
175190
fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
176191
let mut env = HashMap::new();
177192

178-
let gcc_path =
179-
args.config_info.gcc_path.clone().expect(
180-
"The config module should have emitted an error if the GCC path wasn't provided",
181-
);
182-
env.insert("LD_LIBRARY_PATH".to_string(), gcc_path.clone());
183-
env.insert("LIBRARY_PATH".to_string(), gcc_path);
184-
185193
if args.config_info.no_default_features {
186194
env.insert("RUSTFLAGS".to_string(), "-Csymbol-mangling-version=v0".to_string());
187195
}

build_system/src/config.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -429,19 +429,6 @@ impl ConfigInfo {
429429
// display metadata load errors
430430
env.insert("RUSTC_LOG".to_string(), "warn".to_string());
431431

432-
let sysroot = current_dir
433-
.join(get_sysroot_dir())
434-
.join(format!("sysroot/lib/rustlib/{}/lib", self.target_triple));
435-
let ld_library_path = format!(
436-
"{target}:{sysroot}:{gcc_path}",
437-
target = self.cargo_target_dir,
438-
sysroot = sysroot.display(),
439-
gcc_path = gcc_path,
440-
);
441-
env.insert("LIBRARY_PATH".to_string(), ld_library_path.clone());
442-
env.insert("LD_LIBRARY_PATH".to_string(), ld_library_path.clone());
443-
env.insert("DYLD_LIBRARY_PATH".to_string(), ld_library_path);
444-
445432
// NOTE: To avoid the -fno-inline errors, use /opt/gcc/bin/gcc instead of cc.
446433
// To do so, add a symlink for cc to /opt/gcc/bin/gcc in our PATH.
447434
// Another option would be to add the following Rust flag: -Clinker=/opt/gcc/bin/gcc

build_system/src/test.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,6 @@ fn cargo_tests(test_env: &Env, test_args: &TestArg) -> Result<(), String> {
214214
// We don't want to pass things like `RUSTFLAGS`, since they contain the -Zcodegen-backend flag.
215215
// That would force `cg_gcc` to *rebuild itself* and only then run tests, which is undesirable.
216216
let mut env = HashMap::new();
217-
env.insert(
218-
"LD_LIBRARY_PATH".into(),
219-
test_env.get("LD_LIBRARY_PATH").expect("LD_LIBRARY_PATH missing!").to_string(),
220-
);
221-
env.insert(
222-
"LIBRARY_PATH".into(),
223-
test_env.get("LIBRARY_PATH").expect("LIBRARY_PATH missing!").to_string(),
224-
);
225217
env.insert(
226218
"CG_RUSTFLAGS".into(),
227219
test_env.get("CG_RUSTFLAGS").map(|s| s.as_str()).unwrap_or("").to_string(),
@@ -1276,11 +1268,6 @@ pub fn run() -> Result<(), String> {
12761268

12771269
if !args.use_system_gcc {
12781270
args.config_info.setup_gcc_path()?;
1279-
let gcc_path = args.config_info.gcc_path.clone().expect(
1280-
"The config module should have emitted an error if the GCC path wasn't provided",
1281-
);
1282-
env.insert("LIBRARY_PATH".to_string(), gcc_path.clone());
1283-
env.insert("LD_LIBRARY_PATH".to_string(), gcc_path);
12841271
}
12851272

12861273
build_if_no_backend(&env, &args)?;

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use std::any::Any;
7272
use std::ffi::CString;
7373
use std::fmt::Debug;
7474
use std::ops::Deref;
75-
use std::path::PathBuf;
75+
use std::path::{Path, PathBuf};
7676
use std::sync::atomic::{AtomicBool, Ordering};
7777
use std::sync::{Arc, Mutex};
7878

@@ -180,13 +180,17 @@ pub struct GccCodegenBackend {
180180

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

183-
fn load_libgccjit_if_needed() {
183+
fn load_libgccjit_if_needed(sysroot_path: &Path) {
184184
if gccjit::is_loaded() {
185185
// Do not load a libgccjit second time.
186186
return;
187187
}
188188

189-
let string = CString::new("libgccjit.so").expect("string to libgccjit path");
189+
let sysroot_lib_dir = sysroot_path.join("lib");
190+
let libgccjit_target_lib_file = sysroot_lib_dir.join("libgccjit.so");
191+
let path = libgccjit_target_lib_file.to_str().expect("libgccjit path");
192+
193+
let string = CString::new(path).expect("string to libgccjit path");
190194

191195
if let Err(error) = gccjit::load(&string) {
192196
panic!("Cannot load libgccjit.so: {}", error);
@@ -202,12 +206,12 @@ impl CodegenBackend for GccCodegenBackend {
202206
"gcc"
203207
}
204208

205-
fn init(&self, _sess: &Session) {
206-
load_libgccjit_if_needed();
209+
fn init(&self, sess: &Session) {
210+
load_libgccjit_if_needed(sess.opts.sysroot.path());
207211

208212
#[cfg(feature = "master")]
209213
{
210-
let target_cpu = target_cpu(_sess);
214+
let target_cpu = target_cpu(sess);
211215

212216
// Get the second TargetInfo with the correct CPU features by setting the arch.
213217
let context = Context::default();

tests/lang_tests_common.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
33
#![allow(clippy::uninlined_format_args)]
44

5-
use std::env::{self, current_dir};
5+
use std::env::current_dir;
66
use std::path::{Path, PathBuf};
77
use std::process::Command;
88

9-
use boml::Toml;
109
use lang_tester::LangTester;
1110
use tempfile::TempDir;
1211

@@ -23,29 +22,6 @@ pub fn main_inner(profile: Profile) {
2322
let current_dir = current_dir().expect("current dir");
2423
let current_dir = current_dir.to_str().expect("current dir").to_string();
2524

26-
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
27-
28-
let gcc_path = std::fs::read_to_string(manifest_dir.join("config.toml"))
29-
.ok()
30-
.and_then(|v| {
31-
let toml = Toml::parse(&v).expect("Failed to parse `config.toml`");
32-
toml.get_string("gcc-path").map(PathBuf::from).ok()
33-
})
34-
.unwrap_or_else(|| {
35-
// then we try to retrieve it from the `target` folder.
36-
let commit = include_str!("../libgccjit.version").trim();
37-
Path::new("build/libgccjit").join(commit)
38-
});
39-
40-
let gcc_path = Path::new(&gcc_path)
41-
.canonicalize()
42-
.expect("failed to get absolute path of `gcc-path`")
43-
.display()
44-
.to_string();
45-
unsafe {
46-
env::set_var("LD_LIBRARY_PATH", gcc_path);
47-
}
48-
4925
fn rust_filter(path: &Path) -> bool {
5026
path.is_file() && path.extension().expect("extension").to_str().expect("to_str") == "rs"
5127
}

0 commit comments

Comments
 (0)