Skip to content

Commit 97ec750

Browse files
committed
Factor out logic for finding the linker driver into a common function.
1 parent 332506f commit 97ec750

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

include/swift/Driver/ToolChain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ class ToolChain {
294294
void getClangLibraryPath(const llvm::opt::ArgList &Args,
295295
SmallString<128> &LibPath) const;
296296

297+
// Returns the Clang driver executable to use for linking.
298+
const char *getClangLinkerDriver(const llvm::opt::ArgList &Args) const;
299+
297300
/// Returns the name the clang library for a given sanitizer would have on
298301
/// the current toolchain.
299302
///

lib/Driver/ToolChains.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,26 @@ void ToolChain::getRuntimeLibraryPaths(SmallVectorImpl<std::string> &runtimeLibP
13321332
}
13331333
}
13341334

1335+
const char *ToolChain::getClangLinkerDriver(
1336+
const llvm::opt::ArgList &Args) const {
1337+
// We don't use `clang++` unconditionally because we want to avoid pulling in
1338+
// a C++ standard library if it's not needed, in particular because the
1339+
// standard library that `clang++` selects by default may not be the one that
1340+
// is desired.
1341+
const char *LinkerDriver =
1342+
Args.hasArg(options::OPT_enable_experimental_cxx_interop) ?
1343+
"clang++" : "clang";
1344+
if (const Arg *A = Args.getLastArg(options::OPT_tools_directory)) {
1345+
StringRef toolchainPath(A->getValue());
1346+
1347+
// If there is a linker driver in the toolchain folder, use that instead.
1348+
if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {toolchainPath}))
1349+
LinkerDriver = Args.MakeArgString(tool.get());
1350+
}
1351+
1352+
return LinkerDriver;
1353+
}
1354+
13351355
bool ToolChain::sanitizerRuntimeLibExists(const ArgList &args,
13361356
StringRef sanitizerName,
13371357
bool shared) const {

lib/Driver/UnixToolChains.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,9 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
181181
}
182182

183183
// Configure the toolchain.
184-
//
185-
// We don't use `clang++` unconditionally because we want to avoid pulling in
186-
// a C++ standard library if it's not needed, in particular because the
187-
// standard library that `clang++` selects by default may not be the one that
188-
// is desired.
189-
const char *LinkerDriver =
190-
context.Args.hasArg(options::OPT_enable_experimental_cxx_interop) ?
191-
"clang++" : "clang";
192184
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
193185
StringRef toolchainPath(A->getValue());
194186

195-
// If there is a linker driver in the toolchain folder, use that instead.
196-
if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {toolchainPath})) {
197-
LinkerDriver = context.Args.MakeArgString(tool.get());
198-
}
199-
200187
// Look for binutils in the toolchain folder.
201188
Arguments.push_back("-B");
202189
Arguments.push_back(context.Args.MakeArgString(A->getValue()));
@@ -350,7 +337,7 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
350337
Arguments.push_back(
351338
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
352339

353-
InvocationInfo II{LinkerDriver, Arguments};
340+
InvocationInfo II{getClangLinkerDriver(context.Args), Arguments};
354341
II.allowsResponseFiles = true;
355342

356343
return II;

lib/Driver/WindowsToolChains.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
8484
Arguments.push_back("/DEBUG");
8585
}
8686

87-
// Configure the toolchain.
88-
const char *LinkerDriver =
89-
context.Args.hasArg(options::OPT_enable_experimental_cxx_interop) ?
90-
"clang++" : "clang";
91-
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
92-
StringRef toolchainPath(A->getValue());
93-
94-
// If there is a linker driver in the toolchain folder, use that instead.
95-
if (auto tool = llvm::sys::findProgramByName(LinkerDriver, {toolchainPath}))
96-
LinkerDriver = context.Args.MakeArgString(tool.get());
97-
}
98-
9987
// Rely on `-libc` to correctly identify the MSVC Runtime Library. We use
10088
// `-nostartfiles` as that limits the difference to just the
10189
// `-defaultlib:libcmt` which is passed unconditionally with the `clang++`
@@ -189,7 +177,7 @@ toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job,
189177
Arguments.push_back(
190178
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
191179

192-
InvocationInfo II{LinkerDriver, Arguments};
180+
InvocationInfo II{getClangLinkerDriver(context.Args), Arguments};
193181
II.allowsResponseFiles = true;
194182

195183
return II;

0 commit comments

Comments
 (0)