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
;
19 use session
::config
::DebugInfoLevel
::{NoDebugInfo, LimitedDebugInfo, FullDebugInfo}
;
21 /// Linker abstraction used by back::link to build up the command to invoke a
24 /// This trait is the total list of requirements needed by `back::link` and
25 /// represents the meaning of each option being passed down. This trait is then
26 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
27 /// MSVC linker (e.g. `link.exe`) is being used.
29 fn link_dylib(&mut self, lib
: &str);
30 fn link_rust_dylib(&mut self, lib
: &str, path
: &Path
);
31 fn link_framework(&mut self, framework
: &str);
32 fn link_staticlib(&mut self, lib
: &str);
33 fn link_rlib(&mut self, lib
: &Path
);
34 fn link_whole_rlib(&mut self, lib
: &Path
);
35 fn link_whole_staticlib(&mut self, lib
: &str, search_path
: &[PathBuf
]);
36 fn include_path(&mut self, path
: &Path
);
37 fn framework_path(&mut self, path
: &Path
);
38 fn output_filename(&mut self, path
: &Path
);
39 fn add_object(&mut self, path
: &Path
);
40 fn gc_sections(&mut self, is_dylib
: bool
);
41 fn position_independent_executable(&mut self);
42 fn optimize(&mut self);
43 fn debuginfo(&mut self);
44 fn no_default_libraries(&mut self);
45 fn build_dylib(&mut self, out_filename
: &Path
);
46 fn args(&mut self, args
: &[String
]);
47 fn hint_static(&mut self);
48 fn hint_dynamic(&mut self);
49 fn whole_archives(&mut self);
50 fn no_whole_archives(&mut self);
53 pub struct GnuLinker
<'a
> {
54 pub cmd
: &'a
mut Command
,
55 pub sess
: &'a Session
,
58 impl<'a
> GnuLinker
<'a
> {
59 fn takes_hints(&self) -> bool
{
60 !self.sess
.target
.target
.options
.is_like_osx
64 impl<'a
> Linker
for GnuLinker
<'a
> {
65 fn link_dylib(&mut self, lib
: &str) { self.cmd.arg("-l").arg(lib); }
66 fn link_staticlib(&mut self, lib
: &str) { self.cmd.arg("-l").arg(lib); }
67 fn link_rlib(&mut self, lib
: &Path
) { self.cmd.arg(lib); }
68 fn include_path(&mut self, path
: &Path
) { self.cmd.arg("-L").arg(path); }
69 fn framework_path(&mut self, path
: &Path
) { self.cmd.arg("-F").arg(path); }
70 fn output_filename(&mut self, path
: &Path
) { self.cmd.arg("-o").arg(path); }
71 fn add_object(&mut self, path
: &Path
) { self.cmd.arg(path); }
72 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
73 fn args(&mut self, args
: &[String
]) { self.cmd.args(args); }
75 fn link_rust_dylib(&mut self, lib
: &str, _path
: &Path
) {
76 self.cmd
.arg("-l").arg(lib
);
79 fn link_framework(&mut self, framework
: &str) {
80 self.cmd
.arg("-framework").arg(framework
);
83 fn link_whole_staticlib(&mut self, lib
: &str, search_path
: &[PathBuf
]) {
84 let target
= &self.sess
.target
.target
;
85 if !target
.options
.is_like_osx
{
86 self.cmd
.arg("-Wl,--whole-archive")
88 .arg("-Wl,--no-whole-archive");
90 // -force_load is the OSX equivalent of --whole-archive, but it
91 // involves passing the full path to the library to link.
92 let mut v
= OsString
::from("-Wl,-force_load,");
93 v
.push(&archive
::find_library(lib
, search_path
, &self.sess
));
98 fn link_whole_rlib(&mut self, lib
: &Path
) {
99 if self.sess
.target
.target
.options
.is_like_osx
{
100 let mut v
= OsString
::from("-Wl,-force_load,");
104 self.cmd
.arg("-Wl,--whole-archive").arg(lib
)
105 .arg("-Wl,--no-whole-archive");
109 fn gc_sections(&mut self, is_dylib
: bool
) {
110 // The dead_strip option to the linker specifies that functions and data
111 // unreachable by the entry point will be removed. This is quite useful
112 // with Rust's compilation model of compiling libraries at a time into
113 // one object file. For example, this brings hello world from 1.7MB to
116 // Note that this is done for both executables and dynamic libraries. We
117 // won't get much benefit from dylibs because LLVM will have already
118 // stripped away as much as it could. This has not been seen to impact
119 // link times negatively.
121 // -dead_strip can't be part of the pre_link_args because it's also used
122 // for partial linking when using multiple codegen units (-r). So we
124 if self.sess
.target
.target
.options
.is_like_osx
{
125 self.cmd
.arg("-Wl,-dead_strip");
127 // If we're building a dylib, we don't use --gc-sections because LLVM
128 // has already done the best it can do, and we also don't want to
129 // eliminate the metadata. If we're building an executable, however,
130 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
132 } else if !is_dylib
{
133 self.cmd
.arg("-Wl,--gc-sections");
137 fn optimize(&mut self) {
138 if !self.sess
.target
.target
.options
.linker_is_gnu { return }
140 // GNU-style linkers support optimization with -O. GNU ld doesn't
141 // need a numeric argument, but other linkers do.
142 if self.sess
.opts
.optimize
== config
::Default
||
143 self.sess
.opts
.optimize
== config
::Aggressive
{
144 self.cmd
.arg("-Wl,-O1");
148 fn debuginfo(&mut self) {
149 // Don't do anything special here for GNU-style linkers.
152 fn no_default_libraries(&mut self) {
153 // Unfortunately right now passing -nodefaultlibs to gcc on windows
154 // doesn't work so hot (in terms of native dependencies). This if
155 // statement should hopefully be removed one day though!
156 if !self.sess
.target
.target
.options
.is_like_windows
{
157 self.cmd
.arg("-nodefaultlibs");
161 fn build_dylib(&mut self, out_filename
: &Path
) {
162 // On mac we need to tell the linker to let this library be rpathed
163 if self.sess
.target
.target
.options
.is_like_osx
{
164 self.cmd
.args(&["-dynamiclib", "-Wl,-dylib"]);
166 if self.sess
.opts
.cg
.rpath
{
167 let mut v
= OsString
::from("-Wl,-install_name,@rpath/");
168 v
.push(out_filename
.file_name().unwrap());
172 self.cmd
.arg("-shared");
176 fn whole_archives(&mut self) {
177 if !self.takes_hints() { return }
178 self.cmd
.arg("-Wl,--whole-archive");
181 fn no_whole_archives(&mut self) {
182 if !self.takes_hints() { return }
183 self.cmd
.arg("-Wl,--no-whole-archive");
186 fn hint_static(&mut self) {
187 if !self.takes_hints() { return }
188 self.cmd
.arg("-Wl,-Bstatic");
191 fn hint_dynamic(&mut self) {
192 if !self.takes_hints() { return }
193 self.cmd
.arg("-Wl,-Bdynamic");
197 pub struct MsvcLinker
<'a
> {
198 pub cmd
: &'a
mut Command
,
199 pub sess
: &'a Session
,
202 impl<'a
> Linker
for MsvcLinker
<'a
> {
203 fn link_rlib(&mut self, lib
: &Path
) { self.cmd.arg(lib); }
204 fn add_object(&mut self, path
: &Path
) { self.cmd.arg(path); }
205 fn args(&mut self, args
: &[String
]) { self.cmd.args(args); }
206 fn build_dylib(&mut self, _out_filename
: &Path
) { self.cmd.arg("/DLL"); }
207 fn gc_sections(&mut self, _is_dylib
: bool
) { self.cmd.arg("/OPT:REF,ICF"); }
209 fn link_dylib(&mut self, lib
: &str) {
210 self.cmd
.arg(&format
!("{}.lib", lib
));
213 fn link_rust_dylib(&mut self, lib
: &str, path
: &Path
) {
214 // When producing a dll, the MSVC linker may not actually emit a
215 // `foo.lib` file if the dll doesn't actually export any symbols, so we
216 // check to see if the file is there and just omit linking to it if it's
218 let name
= format
!("{}.lib", lib
);
219 if fs
::metadata(&path
.join(&name
)).is_ok() {
224 fn link_staticlib(&mut self, lib
: &str) {
225 self.cmd
.arg(&format
!("{}.lib", lib
));
228 fn position_independent_executable(&mut self) {
232 fn no_default_libraries(&mut self) {
233 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
234 // as there's been trouble in the past of linking the C++ standard
235 // library required by LLVM. This likely needs to happen one day, but
236 // in general Windows is also a more controlled environment than
237 // Unix, so it's not necessarily as critical that this be implemented.
239 // Note that there are also some licensing worries about statically
240 // linking some libraries which require a specific agreement, so it may
241 // not ever be possible for us to pass this flag.
244 fn include_path(&mut self, path
: &Path
) {
245 let mut arg
= OsString
::from("/LIBPATH:");
250 fn output_filename(&mut self, path
: &Path
) {
251 let mut arg
= OsString
::from("/OUT:");
256 fn framework_path(&mut self, _path
: &Path
) {
257 panic
!("frameworks are not supported on windows")
259 fn link_framework(&mut self, _framework
: &str) {
260 panic
!("frameworks are not supported on windows")
263 fn link_whole_staticlib(&mut self, lib
: &str, _search_path
: &[PathBuf
]) {
265 self.link_staticlib(lib
);
267 fn link_whole_rlib(&mut self, path
: &Path
) {
269 self.link_rlib(path
);
271 fn optimize(&mut self) {
272 // Needs more investigation of `/OPT` arguments
275 fn debuginfo(&mut self) {
276 match self.sess
.opts
.debuginfo
{
278 // Do nothing if debuginfo is disabled
282 // This will cause the Microsoft linker to generate a PDB file
283 // from the CodeView line tables in the object files.
284 self.cmd
.arg("/DEBUG");
289 fn whole_archives(&mut self) {
290 // hints not supported?
292 fn no_whole_archives(&mut self) {
293 // hints not supported?
296 // On windows static libraries are of the form `foo.lib` and dynamic
297 // libraries are not linked against directly, but rather through their
298 // import libraries also called `foo.lib`. As a result there's no
299 // possibility for a native library to appear both dynamically and
300 // statically in the same folder so we don't have to worry about hints like
301 // we do on Unix platforms.
302 fn hint_static(&mut self) {}
303 fn hint_dynamic(&mut self) {}