Skip to content

Commit 219feaf

Browse files
committed
Add -Xclang-linker option to the compiler.
In the Darwin toolchain the linker is invoked directly, and compiler_rt is used if it is found, but in Unix platforms, clang++ is invoked instead, and the clang driver will invoke the linker. Howerver there was no way of modifying this clang++ invocation, so there's no way of providing `--rtlib=` and change the platform default (which is normally libgcc). The only workaround is doing the work that the Swift driver is doing "manually". The change adds a new option (with help hidden, but we can change that) to allow providing extra arguments to the clang++ invocation. The change is done in the two places in the Unix and Windows toolchains that I found the clang driver was being used. Includes some simple tests.
1 parent 4d041a3 commit 219feaf

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ def Xcc : Separate<["-"], "Xcc">, Flags<[FrontendOption]>,
748748
MetaVarName<"<arg>">,
749749
HelpText<"Pass <arg> to the C/C++/Objective-C compiler">;
750750

751+
def Xclang_linker : Separate<["-"], "Xclang-linker">, Flags<[HelpHidden]>,
752+
MetaVarName<"<arg>">, HelpText<"Pass <arg> to Clang when it is use for linking.">;
753+
751754
def Xllvm : Separate<["-"], "Xllvm">,
752755
Flags<[FrontendOption, HelpHidden]>,
753756
MetaVarName<"<arg>">, HelpText<"Pass <arg> to LLVM.">;

lib/Driver/UnixToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
328328
// These custom arguments should be right before the object file at the end.
329329
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
330330
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
331+
context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker);
331332

332333
// This should be the last option, for convenience in checking output.
333334
Arguments.push_back("-o");

lib/Driver/WindowsToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ toolchains::Windows::constructInvocation(const LinkJobAction &job,
164164

165165
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
166166
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
167+
context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker);
167168

168169
// Run clang++ in verbose mode if "-v" is set
169170
if (context.Args.hasArg(options::OPT_v)) {

test/Driver/linker.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
// RUN: %swiftc_driver -driver-print-jobs -target armv7-unknown-linux-gnueabihf -Xlinker -rpath -Xlinker customrpath -L foo %s 2>&1 > %t.linux.txt
5050
// RUN: %FileCheck -check-prefix LINUX-linker-order %s < %t.linux.txt
5151

52+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-unknown-linux-gnu -Xclang-linker -foo -Xclang-linker foopath %s 2>&1 > %t.linux.txt
53+
// RUN: %FileCheck -check-prefix LINUX-clang-linker-order %s < %t.linux.txt
54+
55+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-unknown-windows-msvc -Xclang-linker -foo -Xclang-linker foopath %s 2>&1 > %t.windows.txt
56+
// RUN: %FileCheck -check-prefix WINDOWS-clang-linker-order %s < %t.windows.txt
57+
5258
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 -g %s | %FileCheck -check-prefix DEBUG %s
5359

5460
// RUN: %empty-directory(%t)
@@ -310,6 +316,20 @@
310316
// LINUX-linker-order: -Xlinker -rpath -Xlinker customrpath
311317
// LINUX-linker-order: -o {{.*}}
312318

319+
// LINUX-clang-linker-order: swift
320+
// LINUX-clang-linker-order: -o [[OBJECTFILE:.*]]
321+
322+
// LINUX-clang-linker-order: clang++{{"? }}
323+
// LINUX-clang-linker-order: -foo foopath
324+
// LINUX-clang-linker-order: -o {{.*}}
325+
326+
// WINDOWS-clang-linker-order: swift
327+
// WINDOWS-clang-linker-order: -o [[OBJECTFILE:.*]]
328+
329+
// WINDOWS-clang-linker-order: clang++{{"? }}
330+
// WINDOWS-clang-linker-order: -foo foopath
331+
// WINDOWS-clang-linker-order: -o {{.*}}
332+
313333
// DEBUG: bin/swift
314334
// DEBUG-NEXT: bin/swift
315335
// DEBUG-NEXT: bin/ld{{"? }}
@@ -390,4 +410,3 @@
390410

391411
// Clean up the test executable because hard links are expensive.
392412
// RUN: rm -rf %t/DISTINCTIVE-PATH/usr/bin/swiftc
393-

0 commit comments

Comments
 (0)