From 594f81a2b4dae23827271ea3a4bbb21c1a0004e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= <mati865@gmail.com>
Date: Mon, 3 Aug 2020 15:39:09 +0200
Subject: [PATCH] Make rust.use-lld config option work with non MSVC targets

---
 config.toml.example      | 4 ++--
 src/bootstrap/builder.rs | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/config.toml.example b/config.toml.example
index 8ecd8d88d48..a9835ad12ad 100644
--- a/config.toml.example
+++ b/config.toml.example
@@ -433,7 +433,7 @@
 #
 # LLD will not be used if we're cross linking or running tests.
 #
-# Explicitly setting the linker for a target will override this option.
+# Explicitly setting the linker for a target will override this option when targeting MSVC.
 #use-lld = false
 
 # Indicates whether some LLVM tools, like llvm-objdump, will be made available in the
@@ -503,7 +503,7 @@
 # Linker to be used to link Rust code. Note that the
 # default value is platform specific, and if not specified it may also depend on
 # what platform is crossing to what platform.
-# Setting this will override the `use-lld` option for Rust code.
+# Setting this will override the `use-lld` option for Rust code when targeting MSVC.
 #linker = "cc"
 
 # Path to the `llvm-config` binary of the installation of a custom LLVM to link
diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs
index 144e146685f..e13a5f24653 100644
--- a/src/bootstrap/builder.rs
+++ b/src/bootstrap/builder.rs
@@ -1037,7 +1037,7 @@ impl<'a> Builder<'a> {
             }
         }
 
-        // FIXME: Don't use LLD if we're compiling libtest, since it fails to link it.
+        // FIXME: Don't use LLD with MSVC if we're compiling libtest, since it fails to link it.
         // See https://github.com/rust-lang/rust/issues/68647.
         let can_use_lld = mode != Mode::Std;
 
@@ -1049,6 +1049,11 @@ impl<'a> Builder<'a> {
             let target = crate::envify(&target.triple);
             cargo.env(&format!("CARGO_TARGET_{}_LINKER", target), target_linker);
         }
+
+        if self.config.use_lld && !target.contains("msvc") {
+            rustflags.arg("-Clink-args=-fuse-ld=lld");
+        }
+
         if !(["build", "check", "clippy", "fix", "rustc"].contains(&cmd)) && want_rustdoc {
             cargo.env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler));
         }