Skip to content

Commit d0e6402

Browse files
authored
[Driver] Use library search group when linking statically (#36546)
1 parent 031b848 commit d0e6402

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
@@ -273,6 +273,14 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
273273
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
274274
}
275275

276+
// If we are linking statically, we need to add all
277+
// dependencies to a library search group to resolve
278+
// potential circular dependencies
279+
if (staticExecutable || staticStdlib) {
280+
Arguments.push_back("-Xlinker");
281+
Arguments.push_back("--start-group");
282+
}
283+
276284
// Add any autolinking scripts to the arguments
277285
for (const Job *Cmd : context.Inputs) {
278286
auto &OutputInfo = Cmd->getOutput();
@@ -281,6 +289,11 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
281289
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
282290
}
283291

292+
if (staticExecutable || staticStdlib) {
293+
Arguments.push_back("-Xlinker");
294+
Arguments.push_back("--end-group");
295+
}
296+
284297
// Add the runtime library link paths.
285298
for (auto path : RuntimeLibPaths) {
286299
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)