2020-07-18 00:39:53 +00:00
|
|
|
use pathdiff::diff_paths;
|
2018-08-18 10:55:43 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2015-01-27 20:20:58 +00:00
|
|
|
use std::env;
|
2015-04-16 06:21:13 +00:00
|
|
|
use std::fs;
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-08-31 11:00:29 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
pub struct RPathConfig<'a> {
|
2021-06-07 10:18:28 +00:00
|
|
|
pub libs: &'a [&'a Path],
|
2015-02-27 05:00:43 +00:00
|
|
|
pub out_filename: PathBuf,
|
2014-07-23 18:56:36 +00:00
|
|
|
pub is_like_osx: bool,
|
|
|
|
pub has_rpath: bool,
|
2015-12-15 14:53:02 +00:00
|
|
|
pub linker_is_gnu: bool,
|
2012-01-31 05:00:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:40:18 +00:00
|
|
|
pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
|
2011-10-06 20:11:56 +00:00
|
|
|
// No rpath on windows
|
2014-07-23 18:56:36 +00:00
|
|
|
if !config.has_rpath {
|
2014-03-04 18:02:49 +00:00
|
|
|
return Vec::new();
|
2011-10-06 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("preparing the RPATH!");
|
2011-10-04 22:23:32 +00:00
|
|
|
|
2021-06-07 10:18:28 +00:00
|
|
|
let rpaths = get_rpaths(config);
|
2018-10-06 09:45:11 +00:00
|
|
|
let mut flags = rpaths_to_flags(&rpaths);
|
2015-12-15 14:53:02 +00:00
|
|
|
|
|
|
|
if config.linker_is_gnu {
|
2021-12-17 11:27:14 +00:00
|
|
|
// Use DT_RUNPATH instead of DT_RPATH if available
|
2018-10-06 09:50:00 +00:00
|
|
|
flags.push("-Wl,--enable-new-dtags".to_owned());
|
2021-12-17 11:27:14 +00:00
|
|
|
|
|
|
|
// Set DF_ORIGIN for substitute $ORIGIN
|
|
|
|
flags.push("-Wl,-z,origin".to_owned());
|
2015-12-15 14:53:02 +00:00
|
|
|
}
|
|
|
|
|
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 22:03:29 +00:00
|
|
|
flags
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:08:09 +00:00
|
|
|
fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
|
2018-10-06 09:45:11 +00:00
|
|
|
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
|
|
|
|
|
2015-01-31 17:20:46 +00:00
|
|
|
for rpath in rpaths {
|
2017-01-03 17:45:02 +00:00
|
|
|
if rpath.contains(',') {
|
|
|
|
ret.push("-Wl,-rpath".into());
|
|
|
|
ret.push("-Xlinker".into());
|
|
|
|
ret.push(rpath.clone());
|
|
|
|
} else {
|
|
|
|
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 22:03:29 +00:00
|
|
|
}
|
2018-10-06 09:46:46 +00:00
|
|
|
|
|
|
|
ret
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:18:28 +00:00
|
|
|
fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
|
2014-12-20 08:09:35 +00:00
|
|
|
debug!("output: {:?}", config.out_filename.display());
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("libs:");
|
2021-06-07 10:18:28 +00:00
|
|
|
for libpath in config.libs {
|
2014-12-20 08:09:35 +00:00
|
|
|
debug!(" {:?}", libpath.display());
|
2011-10-04 22:23:32 +00: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.
|
2021-06-07 10:18:28 +00:00
|
|
|
let rpaths = get_rpaths_relative_to_output(config);
|
2011-10-04 22:23:32 +00:00
|
|
|
|
2021-06-05 14:40:53 +00:00
|
|
|
debug!("rpaths:");
|
|
|
|
for rpath in &rpaths {
|
|
|
|
debug!(" {}", rpath);
|
2011-10-05 05:40:38 +00:00
|
|
|
}
|
|
|
|
|
2011-10-04 22:23:32 +00:00
|
|
|
// Remove duplicates
|
2020-03-21 23:20:58 +00:00
|
|
|
minimize_rpaths(&rpaths)
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:18:28 +00:00
|
|
|
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
|
|
|
|
config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:40:18 +00:00
|
|
|
fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {
|
2011-10-06 21:28:52 +00:00
|
|
|
// Mac doesn't appear to support $ORIGIN
|
2014-07-23 18:56:36 +00:00
|
|
|
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
|
2011-10-06 21:28:52 +00:00
|
|
|
|
2015-02-27 05:00:43 +00:00
|
|
|
let cwd = env::current_dir().unwrap();
|
2018-10-12 14:16:00 +00:00
|
|
|
let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
|
2019-02-10 12:05:37 +00:00
|
|
|
lib.pop(); // strip filename
|
2015-04-10 01:04:11 +00:00
|
|
|
let mut output = cwd.join(&config.out_filename);
|
2019-02-10 12:05:37 +00:00
|
|
|
output.pop(); // strip filename
|
2015-04-16 06:21:13 +00:00
|
|
|
let output = fs::canonicalize(&output).unwrap_or(output);
|
2018-07-23 12:47:13 +00:00
|
|
|
let relative = path_relative_from(&lib, &output)
|
|
|
|
.unwrap_or_else(|| panic!("couldn't create relative path from {:?} to {:?}", output, lib));
|
2013-09-27 00:21:59 +00:00
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
2018-10-06 09:37:28 +00:00
|
|
|
format!("{}/{}", prefix, relative.to_str().expect("non-utf8 component in path"))
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 20:35:24 +00:00
|
|
|
// 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> {
|
2020-07-17 22:01:27 +00:00
|
|
|
diff_paths(path, base)
|
2015-02-27 05:00:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:08:09 +00:00
|
|
|
fn minimize_rpaths(rpaths: &[String]) -> Vec<String> {
|
2018-08-18 10:55:43 +00:00
|
|
|
let mut set = FxHashSet::default();
|
2014-03-04 18:02:49 +00:00
|
|
|
let mut minimized = Vec::new();
|
2015-01-31 17:20:46 +00:00
|
|
|
for rpath in rpaths {
|
2017-03-24 08:31:26 +00:00
|
|
|
if set.insert(rpath) {
|
2013-07-02 19:47:32 +00:00
|
|
|
minimized.push(rpath.clone());
|
2011-10-05 05:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-09 02:04:28 +00:00
|
|
|
minimized
|
2011-10-04 22:23:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:38:54 +00:00
|
|
|
#[cfg(all(unix, test))]
|
2019-06-05 16:11:43 +00:00
|
|
|
mod tests;
|