Skip to content

Commit 3aae004

Browse files
authored
Rollup merge of #103072 - cuviper:compiletest-path, r=Mark-Simulacrum
compiletest: set the dylib path when gathering target cfg If the compiler is built with `rpath = false`, then it won't find its own libraries unless the library search path is set. We already do that while running the actual compiletests, but #100260 added another rustc command for getting the target cfg. Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu) thread 'main' panicked at 'error: failed to get cfg info from "[...]/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" --- stdout --- stderr [...]/build/x86_64-unknown-linux-gnu/stage1/bin/rustc: error while loading shared libraries: librustc_driver-a2a76dc626cd02d2.so: cannot open shared object file: No such file or directory ', src/tools/compiletest/src/common.rs:476:13 Now the library path is set here as well, so it works without rpath.
2 parents 11ebe65 + f8a0cc2 commit 3aae004

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/tools/compiletest/src/common.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ pub use self::Mode::*;
22

33
use std::ffi::OsString;
44
use std::fmt;
5+
use std::iter;
56
use std::path::{Path, PathBuf};
67
use std::process::Command;
78
use std::str::FromStr;
89

9-
use crate::util::PathBufExt;
10+
use crate::util::{add_dylib_path, PathBufExt};
1011
use lazycell::LazyCell;
1112
use test::ColorConfig;
1213

@@ -385,8 +386,7 @@ impl Config {
385386
}
386387

387388
fn target_cfg(&self) -> &TargetCfg {
388-
self.target_cfg
389-
.borrow_with(|| TargetCfg::new(&self.rustc_path, &self.target, &self.target_rustcflags))
389+
self.target_cfg.borrow_with(|| TargetCfg::new(self))
390390
}
391391

392392
pub fn matches_arch(&self, arch: &str) -> bool {
@@ -457,21 +457,23 @@ pub enum Endian {
457457
}
458458

459459
impl TargetCfg {
460-
fn new(rustc_path: &Path, target: &str, target_rustcflags: &Vec<String>) -> TargetCfg {
461-
let output = match Command::new(rustc_path)
460+
fn new(config: &Config) -> TargetCfg {
461+
let mut command = Command::new(&config.rustc_path);
462+
add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
463+
let output = match command
462464
.arg("--print=cfg")
463465
.arg("--target")
464-
.arg(target)
465-
.args(target_rustcflags)
466+
.arg(&config.target)
467+
.args(&config.target_rustcflags)
466468
.output()
467469
{
468470
Ok(output) => output,
469-
Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", rustc_path),
471+
Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", config.rustc_path),
470472
};
471473
if !output.status.success() {
472474
panic!(
473475
"error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
474-
rustc_path,
476+
config.rustc_path,
475477
String::from_utf8(output.stdout).unwrap(),
476478
String::from_utf8(output.stderr).unwrap(),
477479
);

src/tools/compiletest/src/runtest.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::errors::{self, Error, ErrorKind};
1313
use crate::header::TestProps;
1414
use crate::json;
1515
use crate::read2::read2_abbreviated;
16-
use crate::util::{logv, PathBufExt};
16+
use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
1717
use crate::ColorConfig;
1818
use regex::{Captures, Regex};
1919
use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
@@ -26,6 +26,7 @@ use std::fs::{self, create_dir_all, File, OpenOptions};
2626
use std::hash::{Hash, Hasher};
2727
use std::io::prelude::*;
2828
use std::io::{self, BufReader};
29+
use std::iter;
2930
use std::path::{Path, PathBuf};
3031
use std::process::{Child, Command, ExitStatus, Output, Stdio};
3132
use std::str;
@@ -72,19 +73,6 @@ fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
7273
f()
7374
}
7475

75-
/// The name of the environment variable that holds dynamic library locations.
76-
pub fn dylib_env_var() -> &'static str {
77-
if cfg!(windows) {
78-
"PATH"
79-
} else if cfg!(target_os = "macos") {
80-
"DYLD_LIBRARY_PATH"
81-
} else if cfg!(target_os = "haiku") {
82-
"LIBRARY_PATH"
83-
} else {
84-
"LD_LIBRARY_PATH"
85-
}
86-
}
87-
8876
/// The platform-specific library name
8977
pub fn get_lib_name(lib: &str, dylib: bool) -> String {
9078
// In some casess (e.g. MUSL), we build a static
@@ -1811,16 +1799,7 @@ impl<'test> TestCx<'test> {
18111799

18121800
// Need to be sure to put both the lib_path and the aux path in the dylib
18131801
// search path for the child.
1814-
let mut path =
1815-
env::split_paths(&env::var_os(dylib_env_var()).unwrap_or_default()).collect::<Vec<_>>();
1816-
if let Some(p) = aux_path {
1817-
path.insert(0, PathBuf::from(p))
1818-
}
1819-
path.insert(0, PathBuf::from(lib_path));
1820-
1821-
// Add the new dylib search path var
1822-
let newpath = env::join_paths(&path).unwrap();
1823-
command.env(dylib_env_var(), newpath);
1802+
add_dylib_path(&mut command, iter::once(lib_path).chain(aux_path));
18241803

18251804
let mut child = disable_error_reporting(|| command.spawn())
18261805
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", &command));

src/tools/compiletest/src/util.rs

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::common::Config;
22
use std::env;
33
use std::ffi::OsStr;
44
use std::path::PathBuf;
5+
use std::process::Command;
56

67
use tracing::*;
78

@@ -111,3 +112,25 @@ impl PathBufExt for PathBuf {
111112
}
112113
}
113114
}
115+
116+
/// The name of the environment variable that holds dynamic library locations.
117+
pub fn dylib_env_var() -> &'static str {
118+
if cfg!(windows) {
119+
"PATH"
120+
} else if cfg!(target_os = "macos") {
121+
"DYLD_LIBRARY_PATH"
122+
} else if cfg!(target_os = "haiku") {
123+
"LIBRARY_PATH"
124+
} else {
125+
"LD_LIBRARY_PATH"
126+
}
127+
}
128+
129+
/// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
130+
/// If the dylib_path_var is already set for this cmd, the old value will be overwritten!
131+
pub fn add_dylib_path(cmd: &mut Command, paths: impl Iterator<Item = impl Into<PathBuf>>) {
132+
let path_env = env::var_os(dylib_env_var());
133+
let old_paths = path_env.as_ref().map(env::split_paths);
134+
let new_paths = paths.map(Into::into).chain(old_paths.into_iter().flatten());
135+
cmd.env(dylib_env_var(), env::join_paths(new_paths).unwrap());
136+
}

0 commit comments

Comments
 (0)