2015-05-08 15:31:23 -07:00
|
|
|
// Copyright 2015 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::ffi::OsString;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::process::Command;
|
2015-06-25 00:33:52 -07:00
|
|
|
use std::fs;
|
2015-05-08 15:31:23 -07:00
|
|
|
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 00:14:20 -07:00
|
|
|
use back::archive;
|
2015-05-08 15:31:23 -07:00
|
|
|
use session::Session;
|
|
|
|
use session::config;
|
2015-07-04 23:25:38 +02:00
|
|
|
use session::config::DebugInfoLevel::{NoDebugInfo, LimitedDebugInfo, FullDebugInfo};
|
2015-05-08 15:31:23 -07:00
|
|
|
|
|
|
|
/// Linker abstraction used by back::link to build up the command to invoke a
|
|
|
|
/// linker.
|
|
|
|
///
|
|
|
|
/// This trait is the total list of requirements needed by `back::link` and
|
|
|
|
/// represents the meaning of each option being passed down. This trait is then
|
|
|
|
/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
|
|
|
|
/// MSVC linker (e.g. `link.exe`) is being used.
|
|
|
|
pub trait Linker {
|
|
|
|
fn link_dylib(&mut self, lib: &str);
|
2015-06-25 00:33:52 -07:00
|
|
|
fn link_rust_dylib(&mut self, lib: &str, path: &Path);
|
2015-05-08 15:31:23 -07:00
|
|
|
fn link_framework(&mut self, framework: &str);
|
|
|
|
fn link_staticlib(&mut self, lib: &str);
|
|
|
|
fn link_rlib(&mut self, lib: &Path);
|
2015-07-07 21:33:44 -07:00
|
|
|
fn link_whole_rlib(&mut self, lib: &Path);
|
2015-05-08 15:31:23 -07:00
|
|
|
fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
|
|
|
|
fn include_path(&mut self, path: &Path);
|
|
|
|
fn framework_path(&mut self, path: &Path);
|
|
|
|
fn output_filename(&mut self, path: &Path);
|
|
|
|
fn add_object(&mut self, path: &Path);
|
|
|
|
fn gc_sections(&mut self, is_dylib: bool);
|
|
|
|
fn position_independent_executable(&mut self);
|
|
|
|
fn optimize(&mut self);
|
2015-07-04 23:25:38 +02:00
|
|
|
fn debuginfo(&mut self);
|
2015-05-08 15:31:23 -07:00
|
|
|
fn no_default_libraries(&mut self);
|
|
|
|
fn build_dylib(&mut self, out_filename: &Path);
|
|
|
|
fn args(&mut self, args: &[String]);
|
|
|
|
fn hint_static(&mut self);
|
|
|
|
fn hint_dynamic(&mut self);
|
|
|
|
fn whole_archives(&mut self);
|
|
|
|
fn no_whole_archives(&mut self);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GnuLinker<'a> {
|
|
|
|
pub cmd: &'a mut Command,
|
|
|
|
pub sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> GnuLinker<'a> {
|
|
|
|
fn takes_hints(&self) -> bool {
|
|
|
|
!self.sess.target.target.options.is_like_osx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for GnuLinker<'a> {
|
|
|
|
fn link_dylib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
|
|
|
|
fn link_staticlib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
|
|
|
|
fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
|
|
|
|
fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
|
|
|
|
fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
|
|
|
|
fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
|
|
|
|
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
|
|
|
|
fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
|
|
|
|
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
|
|
|
|
|
2015-06-25 00:33:52 -07:00
|
|
|
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
|
|
|
|
self.cmd.arg("-l").arg(lib);
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:31:23 -07:00
|
|
|
fn link_framework(&mut self, framework: &str) {
|
|
|
|
self.cmd.arg("-framework").arg(framework);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
|
|
|
|
let target = &self.sess.target.target;
|
|
|
|
if !target.options.is_like_osx {
|
|
|
|
self.cmd.arg("-Wl,--whole-archive")
|
|
|
|
.arg("-l").arg(lib)
|
|
|
|
.arg("-Wl,--no-whole-archive");
|
|
|
|
} else {
|
|
|
|
// -force_load is the OSX equivalent of --whole-archive, but it
|
|
|
|
// involves passing the full path to the library to link.
|
|
|
|
let mut v = OsString::from("-Wl,-force_load,");
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 00:14:20 -07:00
|
|
|
v.push(&archive::find_library(lib, search_path, &self.sess));
|
2015-05-08 15:31:23 -07:00
|
|
|
self.cmd.arg(&v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:33:44 -07:00
|
|
|
fn link_whole_rlib(&mut self, lib: &Path) {
|
|
|
|
if self.sess.target.target.options.is_like_osx {
|
|
|
|
let mut v = OsString::from("-Wl,-force_load,");
|
|
|
|
v.push(lib);
|
|
|
|
self.cmd.arg(&v);
|
|
|
|
} else {
|
|
|
|
self.cmd.arg("-Wl,--whole-archive").arg(lib)
|
|
|
|
.arg("-Wl,--no-whole-archive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:31:23 -07:00
|
|
|
fn gc_sections(&mut self, is_dylib: bool) {
|
|
|
|
// The dead_strip option to the linker specifies that functions and data
|
|
|
|
// unreachable by the entry point will be removed. This is quite useful
|
|
|
|
// with Rust's compilation model of compiling libraries at a time into
|
|
|
|
// one object file. For example, this brings hello world from 1.7MB to
|
|
|
|
// 458K.
|
|
|
|
//
|
|
|
|
// Note that this is done for both executables and dynamic libraries. We
|
|
|
|
// won't get much benefit from dylibs because LLVM will have already
|
|
|
|
// stripped away as much as it could. This has not been seen to impact
|
|
|
|
// link times negatively.
|
|
|
|
//
|
|
|
|
// -dead_strip can't be part of the pre_link_args because it's also used
|
|
|
|
// for partial linking when using multiple codegen units (-r). So we
|
|
|
|
// insert it here.
|
|
|
|
if self.sess.target.target.options.is_like_osx {
|
|
|
|
self.cmd.arg("-Wl,-dead_strip");
|
|
|
|
|
|
|
|
// If we're building a dylib, we don't use --gc-sections because LLVM
|
|
|
|
// has already done the best it can do, and we also don't want to
|
|
|
|
// eliminate the metadata. If we're building an executable, however,
|
|
|
|
// --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
|
|
|
|
// reduction.
|
|
|
|
} else if !is_dylib {
|
|
|
|
self.cmd.arg("-Wl,--gc-sections");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {
|
|
|
|
if !self.sess.target.target.options.linker_is_gnu { return }
|
|
|
|
|
|
|
|
// GNU-style linkers support optimization with -O. GNU ld doesn't
|
|
|
|
// need a numeric argument, but other linkers do.
|
|
|
|
if self.sess.opts.optimize == config::Default ||
|
|
|
|
self.sess.opts.optimize == config::Aggressive {
|
|
|
|
self.cmd.arg("-Wl,-O1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 23:25:38 +02:00
|
|
|
fn debuginfo(&mut self) {
|
|
|
|
// Don't do anything special here for GNU-style linkers.
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:31:23 -07:00
|
|
|
fn no_default_libraries(&mut self) {
|
|
|
|
// Unfortunately right now passing -nodefaultlibs to gcc on windows
|
|
|
|
// doesn't work so hot (in terms of native dependencies). This if
|
|
|
|
// statement should hopefully be removed one day though!
|
|
|
|
if !self.sess.target.target.options.is_like_windows {
|
|
|
|
self.cmd.arg("-nodefaultlibs");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_dylib(&mut self, out_filename: &Path) {
|
|
|
|
// On mac we need to tell the linker to let this library be rpathed
|
|
|
|
if self.sess.target.target.options.is_like_osx {
|
|
|
|
self.cmd.args(&["-dynamiclib", "-Wl,-dylib"]);
|
|
|
|
|
|
|
|
if self.sess.opts.cg.rpath {
|
|
|
|
let mut v = OsString::from("-Wl,-install_name,@rpath/");
|
|
|
|
v.push(out_filename.file_name().unwrap());
|
|
|
|
self.cmd.arg(&v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.cmd.arg("-shared");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn whole_archives(&mut self) {
|
|
|
|
if !self.takes_hints() { return }
|
|
|
|
self.cmd.arg("-Wl,--whole-archive");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn no_whole_archives(&mut self) {
|
|
|
|
if !self.takes_hints() { return }
|
|
|
|
self.cmd.arg("-Wl,--no-whole-archive");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_static(&mut self) {
|
|
|
|
if !self.takes_hints() { return }
|
|
|
|
self.cmd.arg("-Wl,-Bstatic");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_dynamic(&mut self) {
|
|
|
|
if !self.takes_hints() { return }
|
|
|
|
self.cmd.arg("-Wl,-Bdynamic");
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 14:57:21 -07:00
|
|
|
|
|
|
|
pub struct MsvcLinker<'a> {
|
|
|
|
pub cmd: &'a mut Command,
|
|
|
|
pub sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for MsvcLinker<'a> {
|
|
|
|
fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
|
|
|
|
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
|
|
|
|
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
|
|
|
|
fn build_dylib(&mut self, _out_filename: &Path) { self.cmd.arg("/DLL"); }
|
|
|
|
fn gc_sections(&mut self, _is_dylib: bool) { self.cmd.arg("/OPT:REF,ICF"); }
|
|
|
|
|
|
|
|
fn link_dylib(&mut self, lib: &str) {
|
|
|
|
self.cmd.arg(&format!("{}.lib", lib));
|
|
|
|
}
|
2015-06-25 00:33:52 -07:00
|
|
|
|
|
|
|
fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
|
|
|
|
// When producing a dll, the MSVC linker may not actually emit a
|
|
|
|
// `foo.lib` file if the dll doesn't actually export any symbols, so we
|
|
|
|
// check to see if the file is there and just omit linking to it if it's
|
|
|
|
// not present.
|
|
|
|
let name = format!("{}.lib", lib);
|
|
|
|
if fs::metadata(&path.join(&name)).is_ok() {
|
|
|
|
self.cmd.arg(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 14:57:21 -07:00
|
|
|
fn link_staticlib(&mut self, lib: &str) {
|
|
|
|
self.cmd.arg(&format!("{}.lib", lib));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn position_independent_executable(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
|
|
|
fn no_default_libraries(&mut self) {
|
|
|
|
// Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
|
|
|
|
// as there's been trouble in the past of linking the C++ standard
|
|
|
|
// library required by LLVM. This likely needs to happen one day, but
|
|
|
|
// in general Windows is also a more controlled environment than
|
|
|
|
// Unix, so it's not necessarily as critical that this be implemented.
|
|
|
|
//
|
|
|
|
// Note that there are also some licensing worries about statically
|
|
|
|
// linking some libraries which require a specific agreement, so it may
|
|
|
|
// not ever be possible for us to pass this flag.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn include_path(&mut self, path: &Path) {
|
|
|
|
let mut arg = OsString::from("/LIBPATH:");
|
|
|
|
arg.push(path);
|
|
|
|
self.cmd.arg(&arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_filename(&mut self, path: &Path) {
|
|
|
|
let mut arg = OsString::from("/OUT:");
|
|
|
|
arg.push(path);
|
|
|
|
self.cmd.arg(&arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn framework_path(&mut self, _path: &Path) {
|
|
|
|
panic!("frameworks are not supported on windows")
|
|
|
|
}
|
|
|
|
fn link_framework(&mut self, _framework: &str) {
|
|
|
|
panic!("frameworks are not supported on windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
|
|
|
|
// not supported?
|
|
|
|
self.link_staticlib(lib);
|
|
|
|
}
|
2015-07-07 21:33:44 -07:00
|
|
|
fn link_whole_rlib(&mut self, path: &Path) {
|
|
|
|
// not supported?
|
|
|
|
self.link_rlib(path);
|
|
|
|
}
|
2015-05-11 14:57:21 -07:00
|
|
|
fn optimize(&mut self) {
|
|
|
|
// Needs more investigation of `/OPT` arguments
|
|
|
|
}
|
2015-07-04 23:25:38 +02:00
|
|
|
|
|
|
|
fn debuginfo(&mut self) {
|
|
|
|
match self.sess.opts.debuginfo {
|
|
|
|
NoDebugInfo => {
|
|
|
|
// Do nothing if debuginfo is disabled
|
|
|
|
},
|
|
|
|
LimitedDebugInfo |
|
|
|
|
FullDebugInfo => {
|
|
|
|
// This will cause the Microsoft linker to generate a PDB file
|
|
|
|
// from the CodeView line tables in the object files.
|
|
|
|
self.cmd.arg("/DEBUG");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 14:57:21 -07:00
|
|
|
fn whole_archives(&mut self) {
|
|
|
|
// hints not supported?
|
|
|
|
}
|
|
|
|
fn no_whole_archives(&mut self) {
|
|
|
|
// hints not supported?
|
|
|
|
}
|
|
|
|
|
|
|
|
// On windows static libraries are of the form `foo.lib` and dynamic
|
|
|
|
// libraries are not linked against directly, but rather through their
|
|
|
|
// import libraries also called `foo.lib`. As a result there's no
|
|
|
|
// possibility for a native library to appear both dynamically and
|
|
|
|
// statically in the same folder so we don't have to worry about hints like
|
|
|
|
// we do on Unix platforms.
|
|
|
|
fn hint_static(&mut self) {}
|
|
|
|
fn hint_dynamic(&mut self) {}
|
|
|
|
}
|