From: Petr Hosek Date: Wed, 14 Dec 2016 16:46:50 +0000 (+0000) Subject: [Driver] Allow setting the default linker during build X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b22d87412550a73573eca16c906ea247ce405d1;p=clang [Driver] Allow setting the default linker during build This change allows setting the default linker used by the Clang driver when configuring the build. Differential Revision: https://reviews.llvm.org/D25263 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289668 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 422bedaa98..509c7a428f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,6 +198,9 @@ set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld") set(ENABLE_X86_RELAX_RELOCATIONS OFF CACHE BOOL "enable x86 relax relocations by default") +set(CLANG_DEFAULT_LINKER "" CACHE STRING + "Default linker to use (linker name or absolute path, empty for platform default)") + set(CLANG_DEFAULT_CXX_STDLIB "" CACHE STRING "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default") if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR diff --git a/include/clang/Config/config.h.cmake b/include/clang/Config/config.h.cmake index 9bf9ea5d8a..55f0ca94de 100644 --- a/include/clang/Config/config.h.cmake +++ b/include/clang/Config/config.h.cmake @@ -8,6 +8,9 @@ /* Bug report URL. */ #define BUG_REPORT_URL "${BUG_REPORT_URL}" +/* Default linker to use. */ +#define CLANG_DEFAULT_LINKER "${CLANG_DEFAULT_LINKER}" + /* Default C++ stdlib to use. */ #define CLANG_DEFAULT_CXX_STDLIB "${CLANG_DEFAULT_CXX_STDLIB}" diff --git a/include/clang/Driver/ToolChain.h b/include/clang/Driver/ToolChain.h index 53b8fbcdaf..cca239c4be 100644 --- a/include/clang/Driver/ToolChain.h +++ b/include/clang/Driver/ToolChain.h @@ -105,7 +105,6 @@ private: protected: MultilibSet Multilibs; - const char *DefaultLinker = "ld"; ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args); @@ -272,6 +271,11 @@ public: return 0; } + /// GetDefaultLinker - Get the default linker to use. + virtual const char *getDefaultLinker() const { + return "ld"; + } + /// GetDefaultRuntimeLibType - Get the default runtime library variant to use. virtual RuntimeLibType GetDefaultRuntimeLibType() const { return ToolChain::RLT_Libgcc; diff --git a/lib/Driver/ToolChain.cpp b/lib/Driver/ToolChain.cpp index 90eaff12a5..6adc0386ee 100644 --- a/lib/Driver/ToolChain.cpp +++ b/lib/Driver/ToolChain.cpp @@ -351,33 +351,31 @@ std::string ToolChain::GetProgramPath(const char *Name) const { } std::string ToolChain::GetLinkerPath() const { - if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { - StringRef UseLinker = A->getValue(); - - if (llvm::sys::path::is_absolute(UseLinker)) { - // If we're passed -fuse-ld= with what looks like an absolute path, - // don't attempt to second-guess that. - if (llvm::sys::fs::exists(UseLinker)) - return UseLinker; - } else { - // 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("ld"); - - llvm::SmallString<8> LinkerName("ld."); - LinkerName.append(UseLinker); - - std::string LinkerPath(GetProgramPath(LinkerName.c_str())); - if (llvm::sys::fs::exists(LinkerPath)) - return LinkerPath; - } + const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ); + StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER; + + 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::exists(UseLinker)) + return 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("ld."); + LinkerName.append(UseLinker); + + std::string LinkerPath(GetProgramPath(LinkerName.c_str())); + if (llvm::sys::fs::exists(LinkerPath)) + return LinkerPath; + } + if (A) getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); - return ""; - } - return GetProgramPath(DefaultLinker); + return GetProgramPath(getDefaultLinker()); } types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 1b1e04099c..46707aa06b 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -3059,9 +3059,6 @@ MipsLLVMToolChain::MipsLLVMToolChain(const Driver &D, LibSuffix = tools::mips::getMipsABILibSuffix(Args, Triple); getFilePaths().clear(); getFilePaths().push_back(computeSysRoot() + "/usr/lib" + LibSuffix); - - // Use LLD by default. - DefaultLinker = "lld"; } void MipsLLVMToolChain::AddClangSystemIncludeArgs( @@ -4749,9 +4746,6 @@ Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, getFilePaths().push_back(D.SysRoot + "/lib"); getFilePaths().push_back(D.ResourceDir + "/lib/fuchsia"); - - // Use LLD by default. - DefaultLinker = "lld"; } Tool *Fuchsia::buildAssembler() const { @@ -5173,9 +5167,6 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple, assert(Triple.isArch32Bit() != Triple.isArch64Bit()); getFilePaths().push_back( getDriver().SysRoot + "/lib" + (Triple.isArch32Bit() ? "32" : "64")); - - // Use LLD by default. - DefaultLinker = "lld"; } bool WebAssembly::IsMathErrnoDefault() const { return false; } diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index 0515aee808..7dab08915d 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -961,6 +961,10 @@ public: : RuntimeLibType::RLT_CompilerRT; } + const char *getDefaultLinker() const override { + return "lld"; + } + private: Multilib SelectedMultilib; std::string LibSuffix; @@ -1090,6 +1094,10 @@ public: void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override; + const char *getDefaultLinker() const override { + return "lld"; + } + protected: Tool *buildAssembler() const override; Tool *buildLinker() const override; @@ -1289,6 +1297,10 @@ private: const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; + const char *getDefaultLinker() const override { + return "lld"; + } + Tool *buildLinker() const override; };