]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/back/rpath.rs
Rollup merge of #90884 - Nilstrieb:fix-span-trivial-trait-bound, r=estebank
[rust.git] / compiler / rustc_codegen_ssa / src / back / rpath.rs
1 use pathdiff::diff_paths;
2 use rustc_data_structures::fx::FxHashSet;
3 use std::env;
4 use std::fs;
5 use std::path::{Path, PathBuf};
6
7 pub struct RPathConfig<'a> {
8     pub libs: &'a [&'a Path],
9     pub out_filename: PathBuf,
10     pub is_like_osx: bool,
11     pub has_rpath: bool,
12     pub linker_is_gnu: bool,
13 }
14
15 pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
16     // No rpath on windows
17     if !config.has_rpath {
18         return Vec::new();
19     }
20
21     debug!("preparing the RPATH!");
22
23     let rpaths = get_rpaths(config);
24     let mut flags = rpaths_to_flags(&rpaths);
25
26     // Use DT_RUNPATH instead of DT_RPATH if available
27     if config.linker_is_gnu {
28         flags.push("-Wl,--enable-new-dtags".to_owned());
29     }
30
31     flags
32 }
33
34 fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
35     let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
36
37     for rpath in rpaths {
38         if rpath.contains(',') {
39             ret.push("-Wl,-rpath".into());
40             ret.push("-Xlinker".into());
41             ret.push(rpath.clone());
42         } else {
43             ret.push(format!("-Wl,-rpath,{}", &(*rpath)));
44         }
45     }
46
47     ret
48 }
49
50 fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
51     debug!("output: {:?}", config.out_filename.display());
52     debug!("libs:");
53     for libpath in config.libs {
54         debug!("    {:?}", libpath.display());
55     }
56
57     // Use relative paths to the libraries. Binaries can be moved
58     // as long as they maintain the relative relationship to the
59     // crates they depend on.
60     let rpaths = get_rpaths_relative_to_output(config);
61
62     debug!("rpaths:");
63     for rpath in &rpaths {
64         debug!("    {}", rpath);
65     }
66
67     // Remove duplicates
68     minimize_rpaths(&rpaths)
69 }
70
71 fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
72     config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
73 }
74
75 fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {
76     // Mac doesn't appear to support $ORIGIN
77     let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
78
79     let cwd = env::current_dir().unwrap();
80     let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
81     lib.pop(); // strip filename
82     let mut output = cwd.join(&config.out_filename);
83     output.pop(); // strip filename
84     let output = fs::canonicalize(&output).unwrap_or(output);
85     let relative = path_relative_from(&lib, &output)
86         .unwrap_or_else(|| panic!("couldn't create relative path from {:?} to {:?}", output, lib));
87     // FIXME (#9639): This needs to handle non-utf8 paths
88     format!("{}/{}", prefix, relative.to_str().expect("non-utf8 component in path"))
89 }
90
91 // This routine is adapted from the *old* Path's `path_relative_from`
92 // function, which works differently from the new `relative_from` function.
93 // In particular, this handles the case on unix where both paths are
94 // absolute but with only the root as the common directory.
95 fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
96     diff_paths(path, base)
97 }
98
99 fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
100     let mut set = FxHashSet::default();
101     let mut minimized = Vec::new();
102     for rpath in rpaths {
103         if set.insert(rpath) {
104             minimized.push(rpath.clone());
105         }
106     }
107     minimized
108 }
109
110 #[cfg(all(unix, test))]
111 mod tests;