1 use pathdiff::diff_paths;
2 use rustc_data_structures::fx::FxHashSet;
5 use std::path::{Path, PathBuf};
7 pub struct RPathConfig<'a> {
8 pub libs: &'a [&'a Path],
9 pub out_filename: PathBuf,
10 pub is_like_osx: bool,
12 pub linker_is_gnu: bool,
15 pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
16 // No rpath on windows
17 if !config.has_rpath {
21 debug!("preparing the RPATH!");
23 let rpaths = get_rpaths(config);
24 let mut flags = rpaths_to_flags(&rpaths);
26 if config.linker_is_gnu {
27 // Use DT_RUNPATH instead of DT_RPATH if available
28 flags.push("-Wl,--enable-new-dtags".to_owned());
30 // Set DF_ORIGIN for substitute $ORIGIN
31 flags.push("-Wl,-z,origin".to_owned());
37 fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
38 let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
41 if rpath.contains(',') {
42 ret.push("-Wl,-rpath".into());
43 ret.push("-Xlinker".into());
44 ret.push(rpath.clone());
46 ret.push(format!("-Wl,-rpath,{}", &(*rpath)));
53 fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
54 debug!("output: {:?}", config.out_filename.display());
56 for libpath in config.libs {
57 debug!(" {:?}", libpath.display());
60 // Use relative paths to the libraries. Binaries can be moved
61 // as long as they maintain the relative relationship to the
62 // crates they depend on.
63 let rpaths = get_rpaths_relative_to_output(config);
66 for rpath in &rpaths {
71 minimize_rpaths(&rpaths)
74 fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
75 config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
78 fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {
79 // Mac doesn't appear to support $ORIGIN
80 let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
82 let cwd = env::current_dir().unwrap();
83 let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
84 lib.pop(); // strip filename
85 let mut output = cwd.join(&config.out_filename);
86 output.pop(); // strip filename
87 let output = fs::canonicalize(&output).unwrap_or(output);
88 let relative = path_relative_from(&lib, &output)
89 .unwrap_or_else(|| panic!("couldn't create relative path from {:?} to {:?}", output, lib));
90 // FIXME (#9639): This needs to handle non-utf8 paths
91 format!("{}/{}", prefix, relative.to_str().expect("non-utf8 component in path"))
94 // This routine is adapted from the *old* Path's `path_relative_from`
95 // function, which works differently from the new `relative_from` function.
96 // In particular, this handles the case on unix where both paths are
97 // absolute but with only the root as the common directory.
98 fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
99 diff_paths(path, base)
102 fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
103 let mut set = FxHashSet::default();
104 let mut minimized = Vec::new();
105 for rpath in rpaths {
106 if set.insert(rpath) {
107 minimized.push(rpath.clone());
113 #[cfg(all(unix, test))]