|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +use std::env; |
11 | 12 | use std::ffi::OsString;
|
12 | 13 | use std::fs::{self, File};
|
13 | 14 | use std::io::{self, BufWriter};
|
@@ -56,6 +57,7 @@ pub trait Linker {
|
56 | 57 | fn no_whole_archives(&mut self);
|
57 | 58 | fn export_symbols(&mut self, sess: &Session, trans: &CrateTranslation,
|
58 | 59 | tmpdir: &Path);
|
| 60 | + fn try_gold_linker(&mut self); |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | pub struct GnuLinker<'a> {
|
@@ -199,6 +201,53 @@ impl<'a> Linker for GnuLinker<'a> {
|
199 | 201 | fn export_symbols(&mut self, _: &Session, _: &CrateTranslation, _: &Path) {
|
200 | 202 | // noop, visibility in object files takes care of this
|
201 | 203 | }
|
| 204 | + |
| 205 | + fn try_gold_linker(&mut self) { |
| 206 | + // Only use gold under specific conditions that we know work |
| 207 | + |
| 208 | + let gold_exists = match env::var_os("PATH") { |
| 209 | + Some(ref env_path) => { |
| 210 | + env::split_paths(env_path).any(|mut p| { |
| 211 | + p.push("ld.gold"); |
| 212 | + p.exists() |
| 213 | + }) |
| 214 | + } |
| 215 | + None => false |
| 216 | + }; |
| 217 | + let host_is_linux = cfg!(target_os = "linux"); |
| 218 | + // Defensively prevent trying to use gold for bogus cross-targets. |
| 219 | + let target_is_host_compatible = { |
| 220 | + let host_os_is_target_os = self.sess.target.target.target_os == env::consts::OS; |
| 221 | + let host_arch_is_target_arch = self.sess.target.target.arch == env::consts::ARCH; |
| 222 | + // Support x86_64->i686 and reverse |
| 223 | + let host_and_target_are_x86ish = |
| 224 | + (self.sess.target.target.arch == "x86" || |
| 225 | + self.sess.target.target.arch == "x86_64") && |
| 226 | + (env::consts::ARCH == "x86" || |
| 227 | + env::consts::ARCH == "x86_64"); |
| 228 | + host_os_is_target_os && (host_arch_is_target_arch || host_and_target_are_x86ish) |
| 229 | + }; |
| 230 | + // We have strong confidence that x86 works, but not much |
| 231 | + // visibility into other architectures. |
| 232 | + let target_works_with_gold = |
| 233 | + self.sess.target.target.arch == "x86" || |
| 234 | + self.sess.target.target.arch == "x86_64"; |
| 235 | + let opt_out = self.sess.opts.cg.disable_gold; |
| 236 | + |
| 237 | + let can_use_gold = |
| 238 | + gold_exists && |
| 239 | + host_is_linux && |
| 240 | + target_is_host_compatible && |
| 241 | + target_works_with_gold && |
| 242 | + !opt_out; |
| 243 | + |
| 244 | + if can_use_gold { |
| 245 | + info!("linking with ld.gold"); |
| 246 | + self.cmd.arg("-fuse-ld=gold"); |
| 247 | + } else { |
| 248 | + info!("linking with ld"); |
| 249 | + } |
| 250 | + } |
202 | 251 | }
|
203 | 252 |
|
204 | 253 | pub struct MsvcLinker<'a> {
|
@@ -358,4 +407,6 @@ impl<'a> Linker for MsvcLinker<'a> {
|
358 | 407 | arg.push(path);
|
359 | 408 | self.cmd.arg(&arg);
|
360 | 409 | }
|
| 410 | + |
| 411 | + fn try_gold_linker(&mut self) {} |
361 | 412 | }
|
0 commit comments