Skip to content

Commit

Permalink
[Driver] Allow setting the default linker during build
Browse files Browse the repository at this point in the history
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
  • Loading branch information
petrhosek committed Dec 14, 2016
1 parent f1038cc commit 5b22d87
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions include/clang/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
6 changes: 5 additions & 1 deletion include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class ToolChain {

protected:
MultilibSet Multilibs;
const char *DefaultLinker = "ld";

ToolChain(const Driver &D, const llvm::Triple &T,
const llvm::opt::ArgList &Args);
Expand Down Expand Up @@ -272,6 +271,11 @@ class ToolChain {
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;
Expand Down
46 changes: 22 additions & 24 deletions lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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; }
Expand Down
12 changes: 12 additions & 0 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,10 @@ class LLVM_LIBRARY_VISIBILITY MipsLLVMToolChain : public Linux {
: RuntimeLibType::RLT_CompilerRT;
}

const char *getDefaultLinker() const override {
return "lld";
}

private:
Multilib SelectedMultilib;
std::string LibSuffix;
Expand Down Expand Up @@ -1090,6 +1094,10 @@ class LLVM_LIBRARY_VISIBILITY Fuchsia : public Generic_ELF {
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;
Expand Down Expand Up @@ -1289,6 +1297,10 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly final : public ToolChain {
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

const char *getDefaultLinker() const override {
return "lld";
}

Tool *buildLinker() const override;
};

Expand Down

0 comments on commit 5b22d87

Please sign in to comment.