rust/src/librustc/back/rpath.rs

255 lines
8.0 KiB
Rust
Raw Normal View History

2013-06-15 15:40:56 -05:00
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2012-09-04 13:54:36 -05:00
use driver::session;
use metadata::cstore;
2012-09-04 13:54:36 -05:00
use metadata::filesearch;
2011-10-04 17:23:32 -05:00
use std::hashmap::HashSet;
use std::{os, vec};
use syntax::abi;
fn not_win32(os: abi::Os) -> bool {
os != abi::OsWin32
}
pub fn get_rpath_flags(sess: session::Session, out_filename: &Path)
-> ~[~str] {
let os = sess.targ_cfg.os;
// No rpath on windows
if os == abi::OsWin32 {
2012-08-01 19:30:05 -05:00
return ~[];
}
debug!("preparing the RPATH!");
2011-10-04 17:23:32 -05:00
let sysroot = sess.filesearch.sysroot();
2011-10-04 17:23:32 -05:00
let output = out_filename;
let libs = cstore::get_used_crate_files(sess.cstore);
// We don't currently rpath extern libraries, but we know
// where rustrt is and we know every rust program needs it
let libs = vec::append_one(libs, get_sysroot_absolute_rt_lib(sess));
let rpaths = get_rpaths(os, sysroot, output, libs,
sess.opts.target_triple);
2011-10-05 01:36:06 -05:00
rpaths_to_flags(rpaths)
2011-10-04 17:23:32 -05:00
}
fn get_sysroot_absolute_rt_lib(sess: session::Session) -> Path {
let r = filesearch::relative_target_lib_path(sess.opts.target_triple);
let mut p = sess.filesearch.sysroot().join(&r);
p.push(os::dll_filename("rustrt"));
p
}
pub fn rpaths_to_flags(rpaths: &[~str]) -> ~[~str] {
// FIXME (#9639): This needs to handle non-utf8 paths
rpaths.iter().map(|rpath| format!("-Wl,-rpath,{}",*rpath)).collect()
2011-10-04 17:23:32 -05:00
}
fn get_rpaths(os: abi::Os,
sysroot: &Path,
output: &Path,
libs: &[Path],
target_triple: &str) -> ~[~str] {
debug!("sysroot: {}", sysroot.display());
debug!("output: {}", output.display());
debug!("libs:");
for libpath in libs.iter() {
debug!(" {}", libpath.display());
2011-10-04 17:23:32 -05:00
}
debug!("target_triple: {}", target_triple);
2011-10-04 17:23:32 -05:00
// 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 rel_rpaths = get_rpaths_relative_to_output(os, output, libs);
2011-10-04 17:23:32 -05:00
// Make backup absolute paths to the libraries. Binaries can
// be moved as long as the crates they link against don't move.
let abs_rpaths = get_absolute_rpaths(libs);
2011-10-04 17:23:32 -05:00
// And a final backup rpath to the global library location.
let fallback_rpaths = ~[get_install_prefix_rpath(target_triple)];
2011-10-05 00:40:38 -05:00
fn log_rpaths(desc: &str, rpaths: &[~str]) {
debug!("{} rpaths:", desc);
for rpath in rpaths.iter() {
debug!(" {}", *rpath);
2011-10-05 00:40:38 -05:00
}
}
log_rpaths("relative", rel_rpaths);
log_rpaths("absolute", abs_rpaths);
log_rpaths("fallback", fallback_rpaths);
2011-10-05 00:40:38 -05:00
let mut rpaths = rel_rpaths;
rpaths.push_all(abs_rpaths);
rpaths.push_all(fallback_rpaths);
2011-10-04 17:23:32 -05:00
// Remove duplicates
let rpaths = minimize_rpaths(rpaths);
2012-08-01 19:30:05 -05:00
return rpaths;
2011-10-04 17:23:32 -05:00
}
fn get_rpaths_relative_to_output(os: abi::Os,
output: &Path,
libs: &[Path]) -> ~[~str] {
libs.iter().map(|a| get_rpath_relative_to_output(os, output, a)).collect()
2011-10-04 17:23:32 -05:00
}
pub fn get_rpath_relative_to_output(os: abi::Os,
output: &Path,
lib: &Path)
-> ~str {
use std::os;
2013-03-28 20:39:09 -05:00
assert!(not_win32(os));
// Mac doesn't appear to support $ORIGIN
2012-08-06 14:34:08 -05:00
let prefix = match os {
abi::OsAndroid | abi::OsLinux | abi::OsFreebsd
=> "$ORIGIN",
abi::OsMacos => "@loader_path",
abi::OsWin32 => unreachable!()
};
let mut lib = os::make_absolute(lib);
lib.pop();
let mut output = os::make_absolute(output);
output.pop();
let relative = lib.path_relative_from(&output);
let relative = relative.expect("could not create rpath relative to output");
// FIXME (#9639): This needs to handle non-utf8 paths
prefix+"/"+relative.as_str().expect("non-utf8 component in path")
2011-10-04 17:23:32 -05:00
}
fn get_absolute_rpaths(libs: &[Path]) -> ~[~str] {
libs.iter().map(|a| get_absolute_rpath(a)).collect()
}
pub fn get_absolute_rpath(lib: &Path) -> ~str {
let mut p = os::make_absolute(lib);
p.pop();
// FIXME (#9639): This needs to handle non-utf8 paths
p.as_str().expect("non-utf8 component in rpath").to_owned()
}
pub fn get_install_prefix_rpath(target_triple: &str) -> ~str {
let install_prefix = env!("CFG_PREFIX");
let tlib = filesearch::relative_target_lib_path(target_triple);
let mut path = Path::init(install_prefix);
path.push(&tlib);
let path = os::make_absolute(&path);
// FIXME (#9639): This needs to handle non-utf8 paths
path.as_str().expect("non-utf8 component in rpath").to_owned()
}
pub fn minimize_rpaths(rpaths: &[~str]) -> ~[~str] {
let mut set = HashSet::new();
let mut minimized = ~[];
for rpath in rpaths.iter() {
if set.insert(rpath.as_slice()) {
2013-07-02 14:47:32 -05:00
minimized.push(rpath.clone());
2011-10-05 00:40:38 -05:00
}
}
minimized
2011-10-04 17:23:32 -05:00
}
#[cfg(unix, test)]
2011-10-04 17:23:32 -05:00
mod test {
use std::os;
use back::rpath::{get_absolute_rpath, get_install_prefix_rpath};
use back::rpath::{minimize_rpaths, rpaths_to_flags, get_rpath_relative_to_output};
use syntax::abi;
2011-10-04 17:23:32 -05:00
#[test]
fn test_rpaths_to_flags() {
let flags = rpaths_to_flags([~"path1", ~"path2"]);
assert_eq!(flags, ~[~"-Wl,-rpath,path1", ~"-Wl,-rpath,path2"]);
2011-10-04 17:23:32 -05:00
}
#[test]
fn test_prefix_rpath() {
let res = get_install_prefix_rpath("triple");
let mut d = Path::init(env!("CFG_PREFIX"));
d.push("lib/rustc/triple/lib");
debug!("test_prefix_path: {} vs. {}",
res,
d.display());
assert!(res.as_bytes().ends_with(d.as_vec()));
}
#[test]
fn test_prefix_rpath_abs() {
let res = get_install_prefix_rpath("triple");
assert!(Path::init(res).is_absolute());
2011-10-04 17:23:32 -05:00
}
#[test]
fn test_minimize1() {
let res = minimize_rpaths([~"rpath1", ~"rpath2", ~"rpath1"]);
assert_eq!(res.as_slice(), [~"rpath1", ~"rpath2"]);
2011-10-04 17:23:32 -05:00
}
2011-10-05 00:40:38 -05:00
#[test]
fn test_minimize2() {
let res = minimize_rpaths([~"1a", ~"2", ~"2",
~"1a", ~"4a", ~"1a",
~"2", ~"3", ~"4a",
~"3"]);
assert_eq!(res.as_slice(), [~"1a", ~"2", ~"4a", ~"3"]);
2011-10-05 00:40:38 -05:00
}
#[test]
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
fn test_rpath_relative() {
let o = abi::OsLinux;
let res = get_rpath_relative_to_output(o,
&Path::init("bin/rustc"), &Path::init("lib/libstd.so"));
assert_eq!(res.as_slice(), "$ORIGIN/../lib");
2011-10-04 17:23:32 -05:00
}
2011-10-05 00:40:38 -05:00
2011-12-30 02:18:55 -06:00
#[test]
#[cfg(target_os = "freebsd")]
fn test_rpath_relative() {
let o = abi::OsFreebsd;
let res = get_rpath_relative_to_output(o,
&Path::init("bin/rustc"), &Path::init("lib/libstd.so"));
assert_eq!(res.as_slice(), "$ORIGIN/../lib");
2011-12-30 02:18:55 -06:00
}
#[test]
#[cfg(target_os = "macos")]
fn test_rpath_relative() {
let o = abi::OsMacos;
let res = get_rpath_relative_to_output(o,
&Path::init("bin/rustc"),
&Path::init("lib/libstd.so"));
assert_eq!(res.as_slice(), "@loader_path/../lib");
}
#[test]
fn test_get_absolute_rpath() {
let res = get_absolute_rpath(&Path::init("lib/libstd.so"));
let lib = os::make_absolute(&Path::init("lib"));
debug!("test_get_absolute_rpath: {} vs. {}",
res.to_str(), lib.display());
// FIXME (#9639): This needs to handle non-utf8 paths
assert_eq!(res.as_slice(), lib.as_str().expect("non-utf8 component in path"));
}
2011-10-04 17:23:32 -05:00
}