]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/back/rpath.rs
Auto merge of #103591 - lqd:win-lto, r=Mark-Simulacrum
[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     if config.linker_is_gnu {
27         // Use DT_RUNPATH instead of DT_RPATH if available
28         flags.push("-Wl,--enable-new-dtags".to_owned());
29
30         // Set DF_ORIGIN for substitute $ORIGIN
31         flags.push("-Wl,-z,origin".to_owned());
32     }
33
34     flags
35 }
36
37 fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
38     let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
39
40     for rpath in rpaths {
41         if rpath.contains(',') {
42             ret.push("-Wl,-rpath".into());
43             ret.push("-Xlinker".into());
44             ret.push(rpath.clone());
45         } else {
46             ret.push(format!("-Wl,-rpath,{}", &(*rpath)));
47         }
48     }
49
50     ret
51 }
52
53 fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
54     debug!("output: {:?}", config.out_filename.display());
55     debug!("libs:");
56     for libpath in config.libs {
57         debug!("    {:?}", libpath.display());
58     }
59
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);
64
65     debug!("rpaths:");
66     for rpath in &rpaths {
67         debug!("    {}", rpath);
68     }
69
70     // Remove duplicates
71     minimize_rpaths(&rpaths)
72 }
73
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()
76 }
77
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" };
81
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"))
92 }
93
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)
100 }
101
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());
108         }
109     }
110     minimized
111 }
112
113 #[cfg(all(unix, test))]
114 mod tests;