Skip to content

Commit 2083e1b

Browse files
authored
[Driver] Use library search group when linking statically (#36356)
1 parent 2a1a7a8 commit 2083e1b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/Driver/UnixToolChains.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
263263
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
264264
}
265265

266+
// If we are linking statically, we need to add all
267+
// dependencies to a library search group to resolve
268+
// potential circular dependencies
269+
if (staticExecutable || staticStdlib) {
270+
Arguments.push_back("-Xlinker");
271+
Arguments.push_back("--start-group");
272+
}
273+
266274
// Add any autolinking scripts to the arguments
267275
for (const Job *Cmd : context.Inputs) {
268276
auto &OutputInfo = Cmd->getOutput();
@@ -271,6 +279,11 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
271279
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
272280
}
273281

282+
if (staticExecutable || staticStdlib) {
283+
Arguments.push_back("-Xlinker");
284+
Arguments.push_back("--end-group");
285+
}
286+
274287
// Add the runtime library link paths.
275288
for (auto path : RuntimeLibPaths) {
276289
Arguments.push_back("-L");

test/Driver/linker-args-order-linux.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ print("hello world!")
66
// RUN: %target-swiftc_driver -driver-print-jobs -static-stdlib -o %t/static-stdlib %s -Xlinker --no-allow-multiple-definition 2>&1| %FileCheck %s
77
// CHECK: {{.*}}/swift-frontend -frontend -c -primary-file {{.*}}/linker-args-order-linux.swift
88
// CHECK: {{.*}}/swift-autolink-extract{{.*}}
9-
// CHECK: {{.*}}swiftrt.o /{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.o @/{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.autolink {{.*}} @{{.*}}/static-stdlib-args.lnk {{.*}} -Xlinker --no-allow-multiple-definition
9+
// CHECK: {{.*}}swiftrt.o /{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.o -Xlinker --start-group @/{{.*}}/linker-args-order-linux-{{[a-z0-9]+}}.autolink -Xlinker --end-group {{.*}} @{{.*}}/static-stdlib-args.lnk {{.*}} -Xlinker --no-allow-multiple-definition

0 commit comments

Comments
 (0)