Skip to content

Commit 5045b71

Browse files
Pass -static-stdlib for all compiler jobs
--static-swift-stdlib support on Linux is introduced by swiftlang#2921 but it doesn't work when linking CoreFoundation. When CoreFoundation is used on a project and a user uses --static-swift-stdlib, SwiftPM compiled object files without -static-stdlib and linked them with that option. This mismatch of compiler option broke swift autolink mechanism. The object files built without -static-stdlib references `/lib/swift/CoreFoundation/module.map`, which is intended to be used for dynamic linking, and the modulemap doesn't have `link "CoreFoundation"` statement. So the autolink sections in built object files doesn't have `-lCoreFoundation` and it causes linking failure. This patch changes to pass `-static-stdlib` for all compiler jobs if necessary. This enables to refenrece `/lib/swift_static/CoreFoundation/module.map` when using `--static-swift-stdlib` and it resolves linking failures.
1 parent fa43d0d commit 5045b71

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ public final class SwiftTargetBuildDescription {
642642
args += activeCompilationConditions
643643
args += additionalFlags
644644
args += moduleCacheArgs
645+
args += stdlibArguments
645646
args += buildParameters.sanitizers.compileSwiftFlags()
646647
args += ["-parseable-output"]
647648

@@ -763,6 +764,7 @@ public final class SwiftTargetBuildDescription {
763764
result += activeCompilationConditions
764765
result += additionalFlags
765766
result += moduleCacheArgs
767+
result += stdlibArguments
766768
result += self.buildSettingsFlags()
767769

768770
return result
@@ -811,6 +813,7 @@ public final class SwiftTargetBuildDescription {
811813
result += activeCompilationConditions
812814
result += additionalFlags
813815
result += moduleCacheArgs
816+
result += stdlibArguments
814817
result += buildParameters.sanitizers.compileSwiftFlags()
815818
result += ["-parseable-output"]
816819
result += self.buildSettingsFlags()
@@ -976,6 +979,15 @@ public final class SwiftTargetBuildDescription {
976979
private var moduleCacheArgs: [String] {
977980
return ["-module-cache-path", buildParameters.moduleCache.pathString]
978981
}
982+
983+
private var stdlibArguments: [String] {
984+
if buildParameters.shouldLinkStaticSwiftStdlib &&
985+
buildParameters.triple.isSupportingStaticStdlib {
986+
return ["-static-stdlib"]
987+
} else {
988+
return []
989+
}
990+
}
979991
}
980992

981993
/// The build description for a product.
@@ -1105,7 +1117,7 @@ public final class ProductBuildDescription {
11051117
if buildParameters.shouldLinkStaticSwiftStdlib {
11061118
if buildParameters.triple.isDarwin() {
11071119
diagnostics.emit(.swiftBackDeployError)
1108-
} else if buildParameters.triple.isLinux() || buildParameters.triple.arch == .wasm32 {
1120+
} else if buildParameters.triple.isSupportingStaticStdlib {
11091121
args += ["-static-stdlib"]
11101122
}
11111123
}
@@ -1908,3 +1920,9 @@ fileprivate extension Triple.OS {
19081920
}
19091921
}
19101922
}
1923+
1924+
fileprivate extension Triple {
1925+
var isSupportingStaticStdlib: Bool {
1926+
isLinux() || arch == .wasm32
1927+
}
1928+
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,43 @@ final class BuildPlanTests: XCTestCase {
24212421
])
24222422
}
24232423

2424+
func testShouldLinkStaticSwiftStdlib() throws {
2425+
let fs = InMemoryFileSystem(emptyFiles:
2426+
"/Pkg/Sources/exe/main.swift",
2427+
"/Pkg/Sources/lib/lib.swift"
2428+
)
2429+
2430+
let diagnostics = DiagnosticsEngine()
2431+
let graph = loadPackageGraph(fs: fs, diagnostics: diagnostics,
2432+
manifests: [
2433+
Manifest.createV4Manifest(
2434+
name: "Pkg",
2435+
path: "/Pkg",
2436+
url: "/Pkg",
2437+
packageKind: .root,
2438+
targets: [
2439+
TargetDescription(name: "exe", dependencies: ["lib"]),
2440+
TargetDescription(name: "lib", dependencies: []),
2441+
]),
2442+
]
2443+
)
2444+
2445+
let supportingTriples: [TSCUtility.Triple] = [.x86_64Linux, .arm64Linux, .wasi]
2446+
for triple in supportingTriples {
2447+
let result = BuildPlanResult(plan: try BuildPlan(
2448+
buildParameters: mockBuildParameters(shouldLinkStaticSwiftStdlib: true, destinationTriple: triple),
2449+
graph: graph, diagnostics: diagnostics, fileSystem: fs)
2450+
)
2451+
2452+
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
2453+
XCTAssertTrue(exe.contains("-static-stdlib"))
2454+
let lib = try result.target(for: "lib").swiftTarget().compileArguments()
2455+
XCTAssertTrue(lib.contains("-static-stdlib"))
2456+
let link = try result.buildProduct(for: "exe").linkArguments()
2457+
XCTAssertTrue(link.contains("-static-stdlib"))
2458+
}
2459+
}
2460+
24242461
func testBinaryTargets(platform: String, arch: String, destinationTriple: TSCUtility.Triple)
24252462
throws {
24262463
let fs = InMemoryFileSystem(emptyFiles:

0 commit comments

Comments
 (0)