r211785 - Implement the -fuse-ld= option.

Logan Chien tzuhsiang.chien at gmail.com
Thu Jun 26 07:23:45 PDT 2014


Author: logan
Date: Thu Jun 26 09:23:45 2014
New Revision: 211785

URL: http://llvm.org/viewvc/llvm-project?rev=211785&view=rev
Log:
Implement the -fuse-ld= option.

This commit implements the -fuse-ld= option, so that the user
can specify -fuse-ld=bfd to use ld.bfd.

This commit re-applies r194328 with some test case changes.
It seems that r194328 was breaking macosx or mingw build
because clang can't find ld.bfd or ld.gold in the given sysroot.
We should use -B to specify the executable search path instead.

Patch originally by David Chisnall.

Added:
    cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld
    cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.bfd
    cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.gold
    cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/
    cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld
    cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.bfd
    cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.gold
    cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/
    cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.bfd
    cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.gold
    cfe/trunk/test/Driver/fuse-ld.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
    cfe/trunk/include/clang/Driver/Options.td
    cfe/trunk/include/clang/Driver/ToolChain.h
    cfe/trunk/lib/Driver/ToolChain.cpp
    cfe/trunk/lib/Driver/ToolChains.cpp
    cfe/trunk/lib/Driver/Tools.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td Thu Jun 26 09:23:45 2014
@@ -22,6 +22,8 @@ def err_drv_unknown_stdin_type_clang_cl
 def err_drv_unknown_language : Error<"language not recognized: '%0'">;
 def err_drv_invalid_arch_name : Error<
   "invalid arch name '%0'">;
+def err_drv_invalid_linker_name : Error<
+  "invalid linker name in argument '%0'">;
 def err_drv_invalid_rtlib_name : Error<
   "invalid runtime library name in argument '%0'">;
 def err_drv_unsupported_rtlib_for_platform : Error<

Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Thu Jun 26 09:23:45 2014
@@ -1542,7 +1542,7 @@ def fprofile_dir : Joined<["-"], "fprofi
 
 defm profile_use : BooleanFFlag<"profile-use">, Group<clang_ignored_f_Group>;
 def fprofile_use_EQ : Joined<["-"], "fprofile-use=">, Group<clang_ignored_f_Group>;
-def fuse_ld_EQ : Joined<["-"], "fuse-ld=">, Group<clang_ignored_f_Group>;
+def fuse_ld_EQ : Joined<["-"], "fuse-ld=">, Group<f_Group>;
 
 defm align_functions : BooleanFFlag<"align-functions">, Group<clang_ignored_f_Group>;
 def falign_functions_EQ : Joined<["-"], "falign-functions=">, Group<clang_ignored_f_Group>;

Modified: cfe/trunk/include/clang/Driver/ToolChain.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/ToolChain.h?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/ToolChain.h (original)
+++ cfe/trunk/include/clang/Driver/ToolChain.h Thu Jun 26 09:23:45 2014
@@ -158,6 +158,10 @@ public:
   std::string GetFilePath(const char *Name) const;
   std::string GetProgramPath(const char *Name) const;
 
+  /// Returns the linker path, respecting the -fuse-ld= argument to determine
+  /// the linker suffix or name.
+  std::string GetLinkerPath() const;
+
   /// \brief Dispatch to the specific toolchain for verbose printing.
   ///
   /// This is used when handling the verbose option to print detailed,

Modified: cfe/trunk/lib/Driver/ToolChain.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChain.cpp?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChain.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChain.cpp Thu Jun 26 09:23:45 2014
@@ -15,6 +15,7 @@
 #include "clang/Driver/Options.h"
 #include "clang/Driver/SanitizerArgs.h"
 #include "clang/Driver/ToolChain.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
@@ -147,6 +148,30 @@ std::string ToolChain::GetProgramPath(co
   return D.GetProgramPath(Name, *this);
 }
 
+std::string ToolChain::GetLinkerPath() const {
+  if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
+    StringRef Suffix = A->getValue();
+
+    // If we're passed -fuse-ld= with no argument, or with the argument ld,
+    // then use whatever the default system linker is.
+    if (Suffix.empty() || Suffix == "ld")
+      return GetProgramPath("ld");
+
+    llvm::SmallString<8> LinkerName("ld.");
+    LinkerName.append(Suffix);
+
+    std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
+    if (llvm::sys::fs::exists(LinkerPath))
+      return LinkerPath;
+
+    getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
+    return "";
+  }
+
+  return GetProgramPath("ld");
+}
+
+
 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
   return types::lookupTypeForExtension(Ext);
 }

Modified: cfe/trunk/lib/Driver/ToolChains.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains.cpp?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains.cpp Thu Jun 26 09:23:45 2014
@@ -3001,7 +3001,7 @@ Linux::Linux(const Driver &D, const llvm
   PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
                          GCCInstallation.getTriple().str() + "/bin").str());
 
-  Linker = GetProgramPath("ld");
+  Linker = GetLinkerPath();
 
   Distro Distro = DetectDistro(Arch);
 

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=211785&r1=211784&r2=211785&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Thu Jun 26 09:23:45 2014
@@ -5614,7 +5614,7 @@ void darwin::Link::ConstructJob(Compilat
   Args.AddAllArgs(CmdArgs, options::OPT_F);
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -5804,7 +5804,7 @@ void solaris::Link::ConstructJob(Compila
   addProfileRT(getToolChain(), Args, CmdArgs);
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -5912,7 +5912,7 @@ void auroraux::Link::ConstructJob(Compil
   addProfileRT(getToolChain(), Args, CmdArgs);
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -6114,7 +6114,7 @@ void openbsd::Link::ConstructJob(Compila
   }
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -6250,7 +6250,7 @@ void bitrig::Link::ConstructJob(Compilat
   }
 
   const char *Exec =
-    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -6514,7 +6514,7 @@ void freebsd::Link::ConstructJob(Compila
   addProfileRT(ToolChain, Args, CmdArgs);
 
   const char *Exec =
-    Args.MakeArgString(ToolChain.GetProgramPath("ld"));
+    Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -6767,7 +6767,7 @@ void netbsd::Link::ConstructJob(Compilat
 
   addProfileRT(getToolChain(), Args, CmdArgs);
 
-  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -7325,7 +7325,7 @@ void minix::Link::ConstructJob(Compilati
          Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
   }
 
-  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 
@@ -7503,7 +7503,7 @@ void dragonfly::Link::ConstructJob(Compi
 
   addProfileRT(getToolChain(), Args, CmdArgs);
 
-  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
+  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
 }
 

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.bfd
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.bfd?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.gold
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/arm-linux-androideabi/bin/ld.gold?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.bfd
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.bfd?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.gold
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.gold?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.bfd
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.bfd?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.gold
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/Inputs/basic_freebsd_tree/usr/bin/ld.gold?rev=211785&view=auto
==============================================================================
    (empty)

Added: cfe/trunk/test/Driver/fuse-ld.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/fuse-ld.c?rev=211785&view=auto
==============================================================================
--- cfe/trunk/test/Driver/fuse-ld.c (added)
+++ cfe/trunk/test/Driver/fuse-ld.c Thu Jun 26 09:23:45 2014
@@ -0,0 +1,63 @@
+// RUN: %clang %s -### \
+// RUN:     -target x86_64-unknown-freebsd 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-FREEBSD-LD
+// CHECK-FREEBSD-LD: ld
+
+// RUN: %clang %s -### -fuse-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 \
+// 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 \
+// 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-PLIB
+// CHECK-FREEBSD-PLIB: error: invalid linker name
+
+
+
+// RUN: %clang %s -### \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -B%S/Inputs/basic_android_tree/bin 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-ANDROID-ARM-LD
+// CHECK-ANDROID-ARM-LD: Inputs/basic_android_tree/bin/arm-linux-androideabi-ld
+
+// RUN: %clang %s -### -fuse-ld=bfd \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -B%S/Inputs/basic_android_tree/bin 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-ANDROID-ARM-BFD
+// CHECK-ANDROID-ARM-BFD: Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.bfd
+
+// RUN: %clang %s -### -fuse-ld=gold \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -B%S/Inputs/basic_android_tree/bin 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-ANDROID-ARM-GOLD
+// CHECK-ANDROID-ARM-GOLD: Inputs/basic_android_tree/bin/arm-linux-androideabi-ld.gold
+
+// RUN: %clang %s -### \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -gcc-toolchain %S/Inputs/basic_android_tree 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-ANDROID-ARM-LD-TC
+// CHECK-ANDROID-ARM-LD-TC: Inputs/basic_android_tree/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld
+
+// RUN: %clang %s -### -fuse-ld=bfd \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -gcc-toolchain %S/Inputs/basic_android_tree 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-ANDROID-ARM-BFD-TC
+// CHECK-ANDROID-ARM-BFD-TC: Inputs/basic_android_tree/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld.bfd
+
+// RUN: %clang %s -### -fuse-ld=gold \
+// RUN:     -target arm-linux-androideabi \
+// RUN:     -gcc-toolchain %S/Inputs/basic_android_tree 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-ANDROID-ARM-GOLD-TC
+// CHECK-ANDROID-ARM-GOLD-TC: Inputs/basic_android_tree/lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld.gold





More information about the cfe-commits mailing list