[PATCH] D80225: [Driver] Recognize -fuse-ld={bfd, gold, lld} but don't prepend "ld." or "ld64." for other values

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 19 11:28:44 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: martell, rnk, sbc100, theraven.
Herald added subscribers: cfe-commits, sunfish, aheejin.
Herald added a project: clang.

- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55470 added support for -fuse-ld=bfd to mean `ld.bfd` and -fuse-ld=gold to mean `ld.gold`
- rL194328 <https://reviews.llvm.org/rL194328> ported the feature and made -fuse-ld= available for other values (with the `ld.` prefix)
- D78290 <https://reviews.llvm.org/D78290> changed the prefix to `ld64.` on Darwin.

However, the prefix (`ld.` or `ld64.`) is actually cumbersome:

- For a relative path, `-fuse-ld=dir/foo` currently tries to access `x86_64-unknown-linux-gnu-ld.dir/foo` (if LLVM_DEFAULT_TARGET_TRIPLE is `x86_64-unknown-linux-gnu`).
- wasm and Windows do not seem to need the `ld.` convention.

We could teach -fuse-ld= to check whether there is a path separator, and
omit the `ld.` or `ld64.` prefix, but then a relative path will be
unnecessarily inconsistent with a single path component.

Let's hard code the currently used values which intend to get a prefix:
`bfd`, `gold`, `lld`. For all other values, don't add a prefix.

GCC currently hard codes -fuse-ld={bfd,gold,lld} but does not support
other values. I am going to make it support arbitrary values.
https://gcc.gnu.org/pipermail/gcc-patches/2020-April/543324.html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80225

Files:
  clang/lib/Driver/ToolChain.cpp
  clang/test/Driver/Inputs/Windows/ARM/8.1/usr/bin/ld.lld-link2
  clang/test/Driver/Inputs/Windows/ARM/8.1/usr/bin/lld-link2
  clang/test/Driver/Inputs/fuse_ld_windows/foo.exe
  clang/test/Driver/Inputs/fuse_ld_windows/ld.foo.exe
  clang/test/Driver/fuse-ld.c


Index: clang/test/Driver/fuse-ld.c
===================================================================
--- clang/test/Driver/fuse-ld.c
+++ clang/test/Driver/fuse-ld.c
@@ -15,6 +15,12 @@
 // RUN:     -target x86_64-unknown-freebsd \
 // RUN:     -B%S/Inputs/basic_freebsd_tree/usr/bin 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=CHECK-FREEBSD-BFD
+// RUN: %clang %s -### -fuse-ld=ld.bfd \
+// RUN:     --sysroot=%S/Inputs/basic_freebsd_tree \
+// RUN:     -target x86_64-unknown-freebsd \
+// RUN:     -B%S/Inputs/basic_freebsd_tree/usr/bin 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-FREEBSD-BFD
+
 // CHECK-FREEBSD-BFD: Inputs/basic_freebsd_tree/usr/bin{{/|\\+}}ld.bfd
 
 // RUN: %clang %s -### -fuse-ld=gold \
@@ -22,6 +28,12 @@
 // RUN:     -target x86_64-unknown-freebsd \
 // RUN:     -B%S/Inputs/basic_freebsd_tree/usr/bin 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=CHECK-FREEBSD-GOLD
+// RUN: %clang %s -### -fuse-ld=ld.gold \
+// RUN:     --sysroot=%S/Inputs/basic_freebsd_tree \
+// RUN:     -target x86_64-unknown-freebsd \
+// RUN:     -B%S/Inputs/basic_freebsd_tree/usr/bin 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-FREEBSD-GOLD
+
 // CHECK-FREEBSD-GOLD: Inputs/basic_freebsd_tree/usr/bin{{/|\\+}}ld.gold
 
 // RUN: %clang %s -### -fuse-ld=plib \
Index: clang/lib/Driver/ToolChain.cpp
===================================================================
--- clang/lib/Driver/ToolChain.cpp
+++ clang/lib/Driver/ToolChain.cpp
@@ -534,21 +534,24 @@
   const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
   StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
 
+  // If we're passed -fuse-ld= with no argument, or with the argument ld,
+  // then use whatever the default system linker is.
+  if (UseLinker.empty() || UseLinker == "ld")
+    return GetProgramPath(getDefaultLinker());
   if (llvm::sys::path::is_absolute(UseLinker)) {
     // If we're passed what looks like an absolute path, don't attempt to
     // second-guess that.
     if (llvm::sys::fs::can_execute(UseLinker))
       return std::string(UseLinker);
-  } else if (UseLinker.empty() || UseLinker == "ld") {
-    // If we're passed -fuse-ld= with no argument, or with the argument ld,
-    // then use whatever the default system linker is.
-    return GetProgramPath(getDefaultLinker());
   } else {
     llvm::SmallString<8> LinkerName;
-    if (Triple.isOSDarwin())
-      LinkerName.append("ld64.");
-    else
+    // Recognize common abbreviations by appending an OS specific prefix.
+    if (Triple.isOSDarwin()) {
+      if (UseLinker == "lld")
+        LinkerName.append("ld64.");
+    } else if (UseLinker == "bfd" || UseLinker == "gold" || UseLinker == "lld")
       LinkerName.append("ld.");
+
     LinkerName.append(UseLinker);
 
     std::string LinkerPath(GetProgramPath(LinkerName.c_str()));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80225.264974.patch
Type: text/x-patch
Size: 2830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200519/050185d8/attachment-0001.bin>


More information about the cfe-commits mailing list