rust/src/librustc_back/rpath.rs

246 lines
7.2 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.
use std::collections::HashSet;
use std::os;
2015-01-22 18:31:00 -06:00
use std::old_io::IoError;
2014-07-06 16:08:09 -05:00
use syntax::ast;
2014-12-08 17:35:22 -06:00
pub struct RPathConfig<F, G> where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
2014-07-06 16:08:09 -05:00
pub used_crates: Vec<(ast::CrateNum, Option<Path>)>,
pub out_filename: Path,
pub is_like_osx: bool,
pub has_rpath: bool,
2014-12-08 17:35:22 -06:00
pub get_install_prefix_lib_path: F,
pub realpath: G,
}
2014-12-08 17:35:22 -06:00
pub fn get_rpath_flags<F, G>(config: RPathConfig<F, G>) -> Vec<String> where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
// No rpath on windows
if !config.has_rpath {
return Vec::new();
}
let mut flags = Vec::new();
Add generation of static libraries to rustc This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes #552
2013-11-15 16:03:29 -06:00
debug!("preparing the RPATH!");
2011-10-04 17:23:32 -05:00
2014-07-06 16:08:09 -05:00
let libs = config.used_crates.clone();
2014-09-14 22:27:36 -05:00
let libs = libs.into_iter().filter_map(|(_, l)| {
l.map(|p| p.clone())
2014-05-04 01:43:38 -05:00
}).collect::<Vec<_>>();
2015-01-07 10:58:31 -06:00
let rpaths = get_rpaths(config, &libs[]);
flags.push_all(&rpaths_to_flags(&rpaths[])[]);
Add generation of static libraries to rustc This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes #552
2013-11-15 16:03:29 -06:00
flags
2011-10-04 17:23:32 -05:00
}
2014-07-06 16:08:09 -05:00
fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
let mut ret = Vec::new();
Add generation of static libraries to rustc This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes #552
2013-11-15 16:03:29 -06:00
for rpath in rpaths.iter() {
2015-01-07 10:58:31 -06:00
ret.push(format!("-Wl,-rpath,{}", &(*rpath)[]));
Add generation of static libraries to rustc This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes #552
2013-11-15 16:03:29 -06:00
}
return ret;
2011-10-04 17:23:32 -05:00
}
2014-12-08 17:35:22 -06:00
fn get_rpaths<F, G>(mut config: RPathConfig<F, G>, libs: &[Path]) -> Vec<String> where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
debug!("output: {:?}", config.out_filename.display());
debug!("libs:");
for libpath in libs.iter() {
debug!(" {:?}", libpath.display());
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.
2014-07-06 16:08:09 -05:00
let rel_rpaths = get_rpaths_relative_to_output(&mut config, libs);
2011-10-04 17:23:32 -05:00
// And a final backup rpath to the global library location.
2014-07-06 16:08:09 -05:00
let fallback_rpaths = vec!(get_install_prefix_rpath(config));
2011-10-05 00:40:38 -05:00
fn log_rpaths(desc: &str, rpaths: &[String]) {
debug!("{} rpaths:", desc);
for rpath in rpaths.iter() {
debug!(" {}", *rpath);
2011-10-05 00:40:38 -05:00
}
}
2015-01-07 10:58:31 -06:00
log_rpaths("relative", &rel_rpaths[]);
log_rpaths("fallback", &fallback_rpaths[]);
2011-10-05 00:40:38 -05:00
let mut rpaths = rel_rpaths;
2015-01-07 10:58:31 -06:00
rpaths.push_all(&fallback_rpaths[]);
2011-10-04 17:23:32 -05:00
// Remove duplicates
2015-01-07 10:58:31 -06:00
let rpaths = minimize_rpaths(&rpaths[]);
2012-08-01 19:30:05 -05:00
return rpaths;
2011-10-04 17:23:32 -05:00
}
2014-12-08 17:35:22 -06:00
fn get_rpaths_relative_to_output<F, G>(config: &mut RPathConfig<F, G>,
libs: &[Path]) -> Vec<String> where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
2014-07-06 16:08:09 -05:00
libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
2011-10-04 17:23:32 -05:00
}
2014-12-08 17:35:22 -06:00
fn get_rpath_relative_to_output<F, G>(config: &mut RPathConfig<F, G>, lib: &Path) -> String where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
use std::os;
// Mac doesn't appear to support $ORIGIN
let prefix = if config.is_like_osx {
"@loader_path"
} else {
"$ORIGIN"
};
let mut lib = (config.realpath)(&os::make_absolute(lib).unwrap()).unwrap();
lib.pop();
let mut output = (config.realpath)(&os::make_absolute(&config.out_filename).unwrap()).unwrap();
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
format!("{}/{}",
prefix,
relative.as_str().expect("non-utf8 component in path"))
2011-10-04 17:23:32 -05:00
}
2014-12-08 17:35:22 -06:00
fn get_install_prefix_rpath<F, G>(config: RPathConfig<F, G>) -> String where
F: FnOnce() -> Path,
G: FnMut(&Path) -> Result<Path, IoError>,
{
2014-07-06 16:08:09 -05:00
let path = (config.get_install_prefix_lib_path)();
let path = os::make_absolute(&path).unwrap();
// FIXME (#9639): This needs to handle non-utf8 paths
path.as_str().expect("non-utf8 component in rpath").to_string()
}
2014-07-06 16:08:09 -05:00
fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
let mut set = HashSet::new();
let mut minimized = Vec::new();
for rpath in rpaths.iter() {
2015-01-07 10:58:31 -06:00
if set.insert(&rpath[]) {
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
}
2014-09-29 01:38:54 -05:00
#[cfg(all(unix, test))]
2011-10-04 17:23:32 -05:00
mod test {
2014-07-06 16:08:09 -05:00
use super::{RPathConfig};
use super::{minimize_rpaths, rpaths_to_flags, get_rpath_relative_to_output};
2011-10-04 17:23:32 -05:00
#[test]
fn test_rpaths_to_flags() {
2014-11-17 02:39:01 -06:00
let flags = rpaths_to_flags(&[
"path1".to_string(),
"path2".to_string()
]);
assert_eq!(flags,
["-Wl,-rpath,path1",
"-Wl,-rpath,path2"]);
2011-10-04 17:23:32 -05:00
}
#[test]
fn test_minimize1() {
2014-11-17 02:39:01 -06:00
let res = minimize_rpaths(&[
"rpath1".to_string(),
"rpath2".to_string(),
"rpath1".to_string()
]);
assert!(res == [
"rpath1",
"rpath2",
]);
2011-10-04 17:23:32 -05:00
}
2011-10-05 00:40:38 -05:00
#[test]
fn test_minimize2() {
2014-11-17 02:39:01 -06:00
let res = minimize_rpaths(&[
"1a".to_string(),
"2".to_string(),
"2".to_string(),
"1a".to_string(),
"4a".to_string(),
"1a".to_string(),
"2".to_string(),
"3".to_string(),
"4a".to_string(),
"3".to_string()
]);
assert!(res == [
"1a",
"2",
"4a",
"3",
]);
2011-10-05 00:40:38 -05:00
}
#[test]
2014-10-11 11:32:57 -05:00
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_rpath_relative() {
2014-07-06 16:08:09 -05:00
let config = &mut RPathConfig {
used_crates: Vec::new(),
out_filename: Path::new("bin/rustc"),
get_install_prefix_lib_path: || panic!(),
has_rpath: true,
is_like_osx: false,
2014-07-06 16:08:09 -05:00
realpath: |p| Ok(p.clone())
};
let res = get_rpath_relative_to_output(config, &Path::new("lib/libstd.so"));
assert_eq!(res, "$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]
2014-12-19 06:05:06 -06:00
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
fn test_rpath_relative() {
let config = &mut RPathConfig {
used_crates: Vec::new(),
has_rpath: true,
is_like_osx: false,
out_filename: Path::new("bin/rustc"),
get_install_prefix_lib_path: || panic!(),
realpath: |p| Ok(p.clone())
};
let res = get_rpath_relative_to_output(config, &Path::new("lib/libstd.so"));
assert_eq!(res, "$ORIGIN/../lib");
}
#[test]
#[cfg(target_os = "macos")]
fn test_rpath_relative() {
2014-07-06 16:08:09 -05:00
let config = &mut RPathConfig {
used_crates: Vec::new(),
has_rpath: true,
is_like_osx: true,
2014-07-06 16:08:09 -05:00
out_filename: Path::new("bin/rustc"),
get_install_prefix_lib_path: || panic!(),
2014-07-11 19:57:45 -05:00
realpath: |p| Ok(p.clone())
2014-07-06 16:08:09 -05:00
};
let res = get_rpath_relative_to_output(config, &Path::new("lib/libstd.so"));
assert_eq!(res, "@loader_path/../lib");
}
2011-10-04 17:23:32 -05:00
}