rustc_codegen_ssa/back/
rpath.rsuse std::ffi::OsString;
use std::path::{Path, PathBuf};
use pathdiff::diff_paths;
use rustc_data_structures::fx::FxHashSet;
use rustc_fs_util::try_canonicalize;
use tracing::debug;
pub(super) struct RPathConfig<'a> {
pub libs: &'a [&'a Path],
pub out_filename: PathBuf,
pub is_like_osx: bool,
pub linker_is_gnu: bool,
}
pub(super) fn get_rpath_linker_args(config: &RPathConfig<'_>) -> Vec<OsString> {
debug!("preparing the RPATH!");
let rpaths = get_rpaths(config);
let mut args = Vec::with_capacity(rpaths.len() * 2); for rpath in rpaths {
args.push("-rpath".into());
args.push(rpath);
}
if config.linker_is_gnu {
args.push("--enable-new-dtags".into());
args.push("-z".into());
args.push("origin".into());
}
args
}
fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {
debug!("output: {:?}", config.out_filename.display());
debug!("libs:");
for libpath in config.libs {
debug!(" {:?}", libpath.display());
}
let rpaths = get_rpaths_relative_to_output(config);
debug!("rpaths:");
for rpath in &rpaths {
debug!(" {:?}", rpath);
}
minimize_rpaths(&rpaths)
}
fn get_rpaths_relative_to_output(config: &RPathConfig<'_>) -> Vec<OsString> {
config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
}
fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsString {
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
let lib = lib.parent().unwrap();
let output = config.out_filename.parent().unwrap();
let lib = if lib == Path::new("") { Path::new(".") } else { lib };
let output = if output == Path::new("") { Path::new(".") } else { output };
let lib = try_canonicalize(lib).unwrap();
let output = try_canonicalize(output).unwrap();
let relative = path_relative_from(&lib, &output)
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
let mut rpath = OsString::from(prefix);
rpath.push("/");
rpath.push(relative);
rpath
}
fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
diff_paths(path, base)
}
fn minimize_rpaths(rpaths: &[OsString]) -> Vec<OsString> {
let mut set = FxHashSet::default();
let mut minimized = Vec::new();
for rpath in rpaths {
if set.insert(rpath) {
minimized.push(rpath.clone());
}
}
minimized
}
#[cfg(all(unix, test))]
mod tests;