use pathdiff::diff_paths;
use rustc_data_structures::fx::FxHashSet;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
pub struct RPathConfig<'a> {
pub libs: &'a [&'a Path],
pub out_filename: PathBuf,
pub is_like_osx: bool,
pub has_rpath: bool,
pub linker_is_gnu: bool,
}
pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<OsString> {
if !config.has_rpath {
return Vec::new();
}
debug!("preparing the RPATH!");
let rpaths = get_rpaths(config);
let mut flags = rpaths_to_flags(rpaths);
if config.linker_is_gnu {
flags.push("-Wl,--enable-new-dtags".into());
flags.push("-Wl,-z,origin".into());
}
flags
}
fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
let mut ret = Vec::with_capacity(rpaths.len()); for rpath in rpaths {
if rpath.to_string_lossy().contains(',') {
ret.push("-Wl,-rpath".into());
ret.push("-Xlinker".into());
ret.push(rpath);
} else {
let mut single_arg = OsString::from("-Wl,-rpath,");
single_arg.push(rpath);
ret.push(single_arg);
}
}
ret
}
fn get_rpaths(config: &mut 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: &mut RPathConfig<'_>) -> Vec<OsString> {
config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
}
fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> OsString {
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
let cwd = env::current_dir().unwrap();
let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
lib.pop(); let mut output = cwd.join(&config.out_filename);
output.pop(); let output = fs::canonicalize(&output).unwrap_or(output);
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;