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 // Use DT_RUNPATH instead of DT_RPATH if available
27 if config
.linker_is_gnu
{
28 flags
.push("-Wl,--enable-new-dtags".to_owned());
34 fn rpaths_to_flags(rpaths
: &[String
]) -> Vec
<String
> {
35 let mut ret
= Vec
::with_capacity(rpaths
.len()); // the minimum needed capacity
38 if rpath
.contains('
,'
) {
39 ret
.push("-Wl,-rpath".into());
40 ret
.push("-Xlinker".into());
41 ret
.push(rpath
.clone());
43 ret
.push(format
!("-Wl,-rpath,{}", &(*rpath
)));
50 fn get_rpaths(config
: &mut RPathConfig
<'_
>) -> Vec
<String
> {
51 debug
!("output: {:?}", config
.out_filename
.display());
53 for libpath
in config
.libs
{
54 debug
!(" {:?}", libpath
.display());
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
);
63 for rpath
in &rpaths
{
68 minimize_rpaths(&rpaths
)
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()
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" }
;
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"))
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
)
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());
110 #[cfg(all(unix, test))]