1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::ffi::OsString;
12 use std::path::{Path, PathBuf};
13 use std::process::Command;
16 use rustc_back::archive;
20 /// Linker abstraction used by back::link to build up the command to invoke a
23 /// This trait is the total list of requirements needed by `back::link` and
24 /// represents the meaning of each option being passed down. This trait is then
25 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
26 /// MSVC linker (e.g. `link.exe`) is being used.
28 fn link_dylib(&mut self, lib: &str);
29 fn link_rust_dylib(&mut self, lib: &str, path: &Path);
30 fn link_framework(&mut self, framework: &str);
31 fn link_staticlib(&mut self, lib: &str);
32 fn link_rlib(&mut self, lib: &Path);
33 fn link_whole_rlib(&mut self, lib: &Path);
34 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
35 fn include_path(&mut self, path: &Path);
36 fn framework_path(&mut self, path: &Path);
37 fn output_filename(&mut self, path: &Path);
38 fn add_object(&mut self, path: &Path);
39 fn gc_sections(&mut self, is_dylib: bool);
40 fn position_independent_executable(&mut self);
41 fn optimize(&mut self);
42 fn no_default_libraries(&mut self);
43 fn build_dylib(&mut self, out_filename: &Path);
44 fn args(&mut self, args: &[String]);
45 fn hint_static(&mut self);
46 fn hint_dynamic(&mut self);
47 fn whole_archives(&mut self);
48 fn no_whole_archives(&mut self);
51 pub struct GnuLinker<'a> {
52 pub cmd: &'a mut Command,
53 pub sess: &'a Session,
56 impl<'a> GnuLinker<'a> {
57 fn takes_hints(&self) -> bool {
58 !self.sess.target.target.options.is_like_osx
62 impl<'a> Linker for GnuLinker<'a> {
63 fn link_dylib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
64 fn link_staticlib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
65 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
66 fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
67 fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
68 fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
69 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
70 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
71 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
73 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
74 self.cmd.arg("-l").arg(lib);
77 fn link_framework(&mut self, framework: &str) {
78 self.cmd.arg("-framework").arg(framework);
81 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
82 let target = &self.sess.target.target;
83 if !target.options.is_like_osx {
84 self.cmd.arg("-Wl,--whole-archive")
86 .arg("-Wl,--no-whole-archive");
88 // -force_load is the OSX equivalent of --whole-archive, but it
89 // involves passing the full path to the library to link.
90 let mut v = OsString::from("-Wl,-force_load,");
91 v.push(&archive::find_library(lib,
92 &target.options.staticlib_prefix,
93 &target.options.staticlib_suffix,
95 &self.sess.diagnostic().handler));
100 fn link_whole_rlib(&mut self, lib: &Path) {
101 if self.sess.target.target.options.is_like_osx {
102 let mut v = OsString::from("-Wl,-force_load,");
106 self.cmd.arg("-Wl,--whole-archive").arg(lib)
107 .arg("-Wl,--no-whole-archive");
111 fn gc_sections(&mut self, is_dylib: bool) {
112 // The dead_strip option to the linker specifies that functions and data
113 // unreachable by the entry point will be removed. This is quite useful
114 // with Rust's compilation model of compiling libraries at a time into
115 // one object file. For example, this brings hello world from 1.7MB to
118 // Note that this is done for both executables and dynamic libraries. We
119 // won't get much benefit from dylibs because LLVM will have already
120 // stripped away as much as it could. This has not been seen to impact
121 // link times negatively.
123 // -dead_strip can't be part of the pre_link_args because it's also used
124 // for partial linking when using multiple codegen units (-r). So we
126 if self.sess.target.target.options.is_like_osx {
127 self.cmd.arg("-Wl,-dead_strip");
129 // If we're building a dylib, we don't use --gc-sections because LLVM
130 // has already done the best it can do, and we also don't want to
131 // eliminate the metadata. If we're building an executable, however,
132 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
134 } else if !is_dylib {
135 self.cmd.arg("-Wl,--gc-sections");
139 fn optimize(&mut self) {
140 if !self.sess.target.target.options.linker_is_gnu { return }
142 // GNU-style linkers support optimization with -O. GNU ld doesn't
143 // need a numeric argument, but other linkers do.
144 if self.sess.opts.optimize == config::Default ||
145 self.sess.opts.optimize == config::Aggressive {
146 self.cmd.arg("-Wl,-O1");
150 fn no_default_libraries(&mut self) {
151 // Unfortunately right now passing -nodefaultlibs to gcc on windows
152 // doesn't work so hot (in terms of native dependencies). This if
153 // statement should hopefully be removed one day though!
154 if !self.sess.target.target.options.is_like_windows {
155 self.cmd.arg("-nodefaultlibs");
159 fn build_dylib(&mut self, out_filename: &Path) {
160 // On mac we need to tell the linker to let this library be rpathed
161 if self.sess.target.target.options.is_like_osx {
162 self.cmd.args(&["-dynamiclib", "-Wl,-dylib"]);
164 if self.sess.opts.cg.rpath {
165 let mut v = OsString::from("-Wl,-install_name,@rpath/");
166 v.push(out_filename.file_name().unwrap());
170 self.cmd.arg("-shared");
174 fn whole_archives(&mut self) {
175 if !self.takes_hints() { return }
176 self.cmd.arg("-Wl,--whole-archive");
179 fn no_whole_archives(&mut self) {
180 if !self.takes_hints() { return }
181 self.cmd.arg("-Wl,--no-whole-archive");
184 fn hint_static(&mut self) {
185 if !self.takes_hints() { return }
186 self.cmd.arg("-Wl,-Bstatic");
189 fn hint_dynamic(&mut self) {
190 if !self.takes_hints() { return }
191 self.cmd.arg("-Wl,-Bdynamic");
195 pub struct MsvcLinker<'a> {
196 pub cmd: &'a mut Command,
197 pub sess: &'a Session,
200 impl<'a> Linker for MsvcLinker<'a> {
201 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
202 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
203 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
204 fn build_dylib(&mut self, _out_filename: &Path) { self.cmd.arg("/DLL"); }
205 fn gc_sections(&mut self, _is_dylib: bool) { self.cmd.arg("/OPT:REF,ICF"); }
207 fn link_dylib(&mut self, lib: &str) {
208 self.cmd.arg(&format!("{}.lib", lib));
211 fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
212 // When producing a dll, the MSVC linker may not actually emit a
213 // `foo.lib` file if the dll doesn't actually export any symbols, so we
214 // check to see if the file is there and just omit linking to it if it's
216 let name = format!("{}.lib", lib);
217 if fs::metadata(&path.join(&name)).is_ok() {
222 fn link_staticlib(&mut self, lib: &str) {
223 self.cmd.arg(&format!("{}.lib", lib));
226 fn position_independent_executable(&mut self) {
230 fn no_default_libraries(&mut self) {
231 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
232 // as there's been trouble in the past of linking the C++ standard
233 // library required by LLVM. This likely needs to happen one day, but
234 // in general Windows is also a more controlled environment than
235 // Unix, so it's not necessarily as critical that this be implemented.
237 // Note that there are also some licensing worries about statically
238 // linking some libraries which require a specific agreement, so it may
239 // not ever be possible for us to pass this flag.
242 fn include_path(&mut self, path: &Path) {
243 let mut arg = OsString::from("/LIBPATH:");
248 fn output_filename(&mut self, path: &Path) {
249 let mut arg = OsString::from("/OUT:");
254 fn framework_path(&mut self, _path: &Path) {
255 panic!("frameworks are not supported on windows")
257 fn link_framework(&mut self, _framework: &str) {
258 panic!("frameworks are not supported on windows")
261 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
263 self.link_staticlib(lib);
265 fn link_whole_rlib(&mut self, path: &Path) {
267 self.link_rlib(path);
269 fn optimize(&mut self) {
270 // Needs more investigation of `/OPT` arguments
272 fn whole_archives(&mut self) {
273 // hints not supported?
275 fn no_whole_archives(&mut self) {
276 // hints not supported?
279 // On windows static libraries are of the form `foo.lib` and dynamic
280 // libraries are not linked against directly, but rather through their
281 // import libraries also called `foo.lib`. As a result there's no
282 // possibility for a native library to appear both dynamically and
283 // statically in the same folder so we don't have to worry about hints like
284 // we do on Unix platforms.
285 fn hint_static(&mut self) {}
286 fn hint_dynamic(&mut self) {}