2013-06-15 15:40:56 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-09-04 13:54:36 -05:00
|
|
|
use driver::session;
|
2012-12-23 16:41:37 -06:00
|
|
|
use metadata::cstore;
|
2012-09-04 13:54:36 -05:00
|
|
|
use metadata::filesearch;
|
2011-10-04 17:23:32 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::HashSet;
|
2013-09-19 00:04:03 -05:00
|
|
|
use std::{os, vec};
|
2013-11-08 13:06:57 -06:00
|
|
|
use syntax::abi;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-11-08 13:06:57 -06:00
|
|
|
fn not_win32(os: abi::Os) -> bool {
|
|
|
|
os != abi::OsWin32
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn get_rpath_flags(sess: session::Session, out_filename: &Path)
|
|
|
|
-> ~[~str] {
|
2012-01-12 10:59:49 -06:00
|
|
|
let os = sess.targ_cfg.os;
|
2011-10-06 16:28:52 -05:00
|
|
|
|
2011-10-06 15:11:56 -05:00
|
|
|
// No rpath on windows
|
2013-11-08 13:06:57 -06:00
|
|
|
if os == abi::OsWin32 {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ~[];
|
2011-10-06 15:11:56 -05:00
|
|
|
}
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("preparing the RPATH!");
|
2011-10-04 17:23:32 -05:00
|
|
|
|
2012-01-12 10:59:49 -06:00
|
|
|
let sysroot = sess.filesearch.sysroot();
|
2011-10-04 17:23:32 -05:00
|
|
|
let output = out_filename;
|
2012-01-12 10:59:49 -06:00
|
|
|
let libs = cstore::get_used_crate_files(sess.cstore);
|
2012-07-03 18:11:00 -05:00
|
|
|
// We don't currently rpath extern libraries, but we know
|
2011-10-05 01:12:46 -05:00
|
|
|
// where rustrt is and we know every rust program needs it
|
2012-06-28 17:00:03 -05:00
|
|
|
let libs = vec::append_one(libs, get_sysroot_absolute_rt_lib(sess));
|
2011-10-05 01:12:46 -05:00
|
|
|
|
2013-05-03 18:47:53 -05:00
|
|
|
let rpaths = get_rpaths(os, sysroot, output, libs,
|
2013-03-20 00:17:42 -05:00
|
|
|
sess.opts.target_triple);
|
2011-10-05 01:36:06 -05:00
|
|
|
rpaths_to_flags(rpaths)
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
fn get_sysroot_absolute_rt_lib(sess: session::Session) -> Path {
|
2012-08-24 17:28:43 -05:00
|
|
|
let r = filesearch::relative_target_lib_path(sess.opts.target_triple);
|
2013-10-07 21:16:58 -05:00
|
|
|
let mut p = sess.filesearch.sysroot().join(&r);
|
2013-10-05 21:49:32 -05:00
|
|
|
p.push(os::dll_filename("rustrt"));
|
2013-09-26 19:21:59 -05:00
|
|
|
p
|
2011-10-05 01:12:46 -05:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-08 13:06:57 -06:00
|
|
|
fn get_rpaths(os: abi::Os,
|
2012-08-24 17:28:43 -05:00
|
|
|
sysroot: &Path,
|
|
|
|
output: &Path,
|
|
|
|
libs: &[Path],
|
2013-09-26 19:21:59 -05:00
|
|
|
target_triple: &str) -> ~[~str] {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("sysroot: {}", sysroot.display());
|
|
|
|
debug!("output: {}", output.display());
|
|
|
|
debug!("libs:");
|
2013-08-03 11:45:23 -05:00
|
|
|
for libpath in libs.iter() {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" {}", libpath.display());
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -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.
|
2012-08-24 17:28:43 -05:00
|
|
|
let rel_rpaths = get_rpaths_relative_to_output(os, output, libs);
|
2011-10-04 17:23:32 -05:00
|
|
|
|
2013-06-16 17:41:33 -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.
|
2012-08-24 17:28:43 -05:00
|
|
|
let fallback_rpaths = ~[get_install_prefix_rpath(target_triple)];
|
2011-10-05 00:40:38 -05:00
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
fn log_rpaths(desc: &str, rpaths: &[~str]) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{} rpaths:", desc);
|
2013-08-03 11:45:23 -05:00
|
|
|
for rpath in rpaths.iter() {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" {}", *rpath);
|
2011-10-05 00:40:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 00:07:44 -05:00
|
|
|
log_rpaths("relative", rel_rpaths);
|
2013-06-16 17:41:33 -05:00
|
|
|
log_rpaths("absolute", abs_rpaths);
|
2013-05-19 00:07:44 -05:00
|
|
|
log_rpaths("fallback", fallback_rpaths);
|
2011-10-05 00:40:38 -05:00
|
|
|
|
2012-06-28 17:00:03 -05:00
|
|
|
let mut rpaths = rel_rpaths;
|
2013-06-16 17:41:33 -05:00
|
|
|
rpaths.push_all(abs_rpaths);
|
2012-09-26 19:33:34 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-08 13:06:57 -06:00
|
|
|
fn get_rpaths_relative_to_output(os: abi::Os,
|
2012-08-24 17:28:43 -05:00
|
|
|
output: &Path,
|
2013-09-26 19:21:59 -05:00
|
|
|
libs: &[Path]) -> ~[~str] {
|
2013-08-09 22:09:47 -05:00
|
|
|
libs.iter().map(|a| get_rpath_relative_to_output(os, output, a)).collect()
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
2013-11-08 13:06:57 -06:00
|
|
|
pub fn get_rpath_relative_to_output(os: abi::Os,
|
2013-01-29 17:48:50 -06:00
|
|
|
output: &Path,
|
|
|
|
lib: &Path)
|
2013-09-26 19:21:59 -05:00
|
|
|
-> ~str {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::os;
|
2012-12-27 17:49:26 -06:00
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(not_win32(os));
|
2012-07-13 20:43:52 -05:00
|
|
|
|
2011-10-06 16:28:52 -05:00
|
|
|
// Mac doesn't appear to support $ORIGIN
|
2012-08-06 14:34:08 -05:00
|
|
|
let prefix = match os {
|
2013-11-08 13:06:57 -06:00
|
|
|
abi::OsAndroid | abi::OsLinux | abi::OsFreebsd
|
2012-11-29 18:21:49 -06:00
|
|
|
=> "$ORIGIN",
|
2013-11-08 13:06:57 -06:00
|
|
|
abi::OsMacos => "@loader_path",
|
|
|
|
abi::OsWin32 => unreachable!()
|
2011-10-06 16:28:52 -05:00
|
|
|
};
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
fn get_absolute_rpaths(libs: &[Path]) -> ~[~str] {
|
2013-08-09 22:09:47 -05:00
|
|
|
libs.iter().map(|a| get_absolute_rpath(a)).collect()
|
2013-06-16 17:41:33 -05:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
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()
|
2013-06-16 17:41:33 -05:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
pub fn get_install_prefix_rpath(target_triple: &str) -> ~str {
|
2013-08-06 23:50:23 -05:00
|
|
|
let install_prefix = env!("CFG_PREFIX");
|
|
|
|
|
|
|
|
let tlib = filesearch::relative_target_lib_path(target_triple);
|
2013-11-22 17:45:12 -06:00
|
|
|
let mut path = Path::init(install_prefix);
|
2013-10-07 21:16:58 -05:00
|
|
|
path.push(&tlib);
|
2013-09-26 19:21:59 -05:00
|
|
|
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()
|
2013-08-06 23:50:23 -05:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
pub fn minimize_rpaths(rpaths: &[~str]) -> ~[~str] {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut set = HashSet::new();
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut minimized = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for rpath in rpaths.iter() {
|
2013-09-26 19:21:59 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-02-08 20:04:28 -06:00
|
|
|
minimized
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
2013-05-27 18:04:00 -05:00
|
|
|
#[cfg(unix, test)]
|
2011-10-04 17:23:32 -05:00
|
|
|
mod test {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::os;
|
2013-05-22 21:59:22 -05:00
|
|
|
|
2013-06-16 17:41:33 -05:00
|
|
|
use back::rpath::{get_absolute_rpath, get_install_prefix_rpath};
|
2013-07-31 15:47:32 -05:00
|
|
|
use back::rpath::{minimize_rpaths, rpaths_to_flags, get_rpath_relative_to_output};
|
2013-11-08 13:06:57 -06:00
|
|
|
use syntax::abi;
|
2012-12-27 17:49:26 -06:00
|
|
|
|
2011-10-04 17:23:32 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_rpaths_to_flags() {
|
2013-09-26 19:21:59 -05:00
|
|
|
let flags = rpaths_to_flags([~"path1", ~"path2"]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(flags, ~[~"-Wl,-rpath,path1", ~"-Wl,-rpath,path2"]);
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_prefix_rpath() {
|
2012-08-24 17:28:43 -05:00
|
|
|
let res = get_install_prefix_rpath("triple");
|
2013-11-22 17:45:12 -06:00
|
|
|
let mut d = Path::init(env!("CFG_PREFIX"));
|
2013-10-05 21:49:32 -05:00
|
|
|
d.push("lib/rustc/triple/lib");
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("test_prefix_path: {} vs. {}",
|
2013-10-17 00:01:20 -05:00
|
|
|
res,
|
2013-09-26 19:21:59 -05:00
|
|
|
d.display());
|
2013-10-17 00:01:20 -05:00
|
|
|
assert!(res.as_bytes().ends_with(d.as_vec()));
|
2011-10-06 18:12:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_prefix_rpath_abs() {
|
2012-08-24 17:28:43 -05:00
|
|
|
let res = get_install_prefix_rpath("triple");
|
2013-11-22 17:45:12 -06:00
|
|
|
assert!(Path::init(res).is_absolute());
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_minimize1() {
|
2013-09-26 19:21:59 -05:00
|
|
|
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]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_minimize2() {
|
2013-09-26 19:21:59 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-04 22:07:18 -05:00
|
|
|
#[test]
|
2011-10-06 16:28:52 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
2013-07-31 15:47:32 -05:00
|
|
|
#[cfg(target_os = "android")]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_rpath_relative() {
|
2013-11-08 13:06:57 -06:00
|
|
|
let o = abi::OsLinux;
|
2012-07-13 20:43:52 -05:00
|
|
|
let res = get_rpath_relative_to_output(o,
|
2013-11-22 17:45:12 -06:00
|
|
|
&Path::init("bin/rustc"), &Path::init("lib/libstd.so"));
|
2013-09-26 19:21:59 -05:00
|
|
|
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")]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_rpath_relative() {
|
2013-11-08 13:06:57 -06:00
|
|
|
let o = abi::OsFreebsd;
|
2012-07-14 12:27:09 -05:00
|
|
|
let res = get_rpath_relative_to_output(o,
|
2013-11-22 17:45:12 -06:00
|
|
|
&Path::init("bin/rustc"), &Path::init("lib/libstd.so"));
|
2013-09-26 19:21:59 -05:00
|
|
|
assert_eq!(res.as_slice(), "$ORIGIN/../lib");
|
2011-12-30 02:18:55 -06:00
|
|
|
}
|
|
|
|
|
2011-10-06 16:28:52 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "macos")]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_rpath_relative() {
|
2013-11-08 13:06:57 -06:00
|
|
|
let o = abi::OsMacos;
|
2012-08-24 17:28:43 -05:00
|
|
|
let res = get_rpath_relative_to_output(o,
|
2013-11-22 17:45:12 -06:00
|
|
|
&Path::init("bin/rustc"),
|
|
|
|
&Path::init("lib/libstd.so"));
|
2013-11-05 16:17:30 -06:00
|
|
|
assert_eq!(res.as_slice(), "@loader_path/../lib");
|
2011-10-06 16:28:52 -05:00
|
|
|
}
|
2013-06-16 17:41:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_absolute_rpath() {
|
2013-11-22 17:45:12 -06:00
|
|
|
let res = get_absolute_rpath(&Path::init("lib/libstd.so"));
|
|
|
|
let lib = os::make_absolute(&Path::init("lib"));
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("test_get_absolute_rpath: {} vs. {}",
|
2013-09-26 19:21:59 -05:00
|
|
|
res.to_str(), lib.display());
|
2013-06-16 17:41:33 -05:00
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
|
|
|
assert_eq!(res.as_slice(), lib.as_str().expect("non-utf8 component in path"));
|
2013-06-16 17:41:33 -05:00
|
|
|
}
|
2011-10-04 17:23:32 -05:00
|
|
|
}
|