rustc_codegen_ssa/back/
rpath.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::ffi::OsString;
use std::path::{Path, PathBuf};

use pathdiff::diff_paths;
use rustc_data_structures::fx::FxHashSet;
use rustc_fs_util::try_canonicalize;
use tracing::debug;

pub(super) struct RPathConfig<'a> {
    pub libs: &'a [&'a Path],
    pub out_filename: PathBuf,
    pub is_like_osx: bool,
    pub linker_is_gnu: bool,
}

pub(super) fn get_rpath_linker_args(config: &RPathConfig<'_>) -> Vec<OsString> {
    debug!("preparing the RPATH!");

    let rpaths = get_rpaths(config);
    let mut args = Vec::with_capacity(rpaths.len() * 2); // the minimum needed capacity

    for rpath in rpaths {
        args.push("-rpath".into());
        args.push(rpath);
    }

    if config.linker_is_gnu {
        // Use DT_RUNPATH instead of DT_RPATH if available
        args.push("--enable-new-dtags".into());

        // Set DF_ORIGIN for substitute $ORIGIN
        args.push("-z".into());
        args.push("origin".into());
    }

    args
}

fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {
    debug!("output: {:?}", config.out_filename.display());
    debug!("libs:");
    for libpath in config.libs {
        debug!("    {:?}", libpath.display());
    }

    // Use relative paths to the libraries. Binaries can be moved
    // as long as they maintain the relative relationship to the
    // crates they depend on.
    let rpaths = get_rpaths_relative_to_output(config);

    debug!("rpaths:");
    for rpath in &rpaths {
        debug!("    {:?}", rpath);
    }

    // Remove duplicates
    minimize_rpaths(&rpaths)
}

fn get_rpaths_relative_to_output(config: &RPathConfig<'_>) -> Vec<OsString> {
    config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
}

fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsString {
    // Mac doesn't appear to support $ORIGIN
    let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };

    // Strip filenames
    let lib = lib.parent().unwrap();
    let output = config.out_filename.parent().unwrap();

    // If output or lib is empty, just assume it locates in current path
    let lib = if lib == Path::new("") { Path::new(".") } else { lib };
    let output = if output == Path::new("") { Path::new(".") } else { output };

    let lib = try_canonicalize(lib).unwrap();
    let output = try_canonicalize(output).unwrap();
    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
}

// This routine is adapted from the *old* Path's `path_relative_from`
// function, which works differently from the new `relative_from` function.
// In particular, this handles the case on unix where both paths are
// absolute but with only the root as the common directory.
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;