Skip to content

Commit 2b0505e

Browse files
authored
Use mergeable symbols in embedded (#8654)
1 parent 7a927cd commit 2b0505e

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

+19-11
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,19 @@ public final class SwiftModuleBuildDescription {
490490
args += ["-v"]
491491
}
492492

493-
// Enable batch mode whenever WMO is off.
494-
if !self.useWholeModuleOptimization {
495-
args += ["-enable-batch-mode"]
493+
if self.useWholeModuleOptimization {
494+
args.append("-whole-module-optimization")
495+
args.append("-num-threads")
496+
args.append(String(ProcessInfo.processInfo.activeProcessorCount))
497+
} else {
498+
args.append("-incremental")
499+
args.append("-enable-batch-mode")
500+
}
501+
502+
// Workaround for https://github.com/swiftlang/swift-package-manager/issues/8648
503+
if self.useMergeableSymbols {
504+
args.append("-Xfrontend")
505+
args.append("-mergeable-symbols")
496506
}
497507

498508
args += ["-serialize-diagnostics"]
@@ -779,14 +789,6 @@ public final class SwiftModuleBuildDescription {
779789
result.append(outputFileMapPath.pathString)
780790
}
781791

782-
if self.useWholeModuleOptimization {
783-
result.append("-whole-module-optimization")
784-
result.append("-num-threads")
785-
result.append(String(ProcessInfo.processInfo.activeProcessorCount))
786-
} else {
787-
result.append("-incremental")
788-
}
789-
790792
result.append("-c")
791793
result.append(contentsOf: self.sources.map(\.pathString))
792794

@@ -1044,6 +1046,12 @@ public final class SwiftModuleBuildDescription {
10441046
return true
10451047
}
10461048
}
1049+
1050+
// Workaround for https://github.com/swiftlang/swift-package-manager/issues/8648
1051+
/// Whether to build Swift code with -Xfrontend -mergeable-symbols.
1052+
package var useMergeableSymbols: Bool {
1053+
return self.target.underlying.isEmbeddedSwiftTarget
1054+
}
10471055
}
10481056

10491057
extension SwiftModuleBuildDescription {

Tests/BuildTests/BuildPlanTests.swift

+50-6
Original file line numberDiff line numberDiff line change
@@ -2240,17 +2240,61 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
22402240
)
22412241
XCTAssertNoDiagnostics(observability.diagnostics)
22422242

2243-
// WMO should always be on with Embedded
2244-
let plan = try await mockBuildPlan(
2243+
// -Xfrontend -mergeable symbols should be passed with Embedded
2244+
let result = try await BuildPlanResult(plan: mockBuildPlan(
22452245
graph: graph,
22462246
fileSystem: fs,
22472247
observabilityScope: observability.topScope
2248+
))
2249+
result.checkTargetsCount(1)
2250+
2251+
// Compile Swift Target
2252+
let aCompileArguments = try result.moduleBuildDescription(for: "A").swift().compileArguments()
2253+
let aCompileArgumentsPattern: [StringPattern] = ["-whole-module-optimization"]
2254+
let aCompileArgumentsNegativePattern: [StringPattern] = ["-wmo"]
2255+
XCTAssertMatch(aCompileArguments, aCompileArgumentsPattern)
2256+
XCTAssertNoMatch(aCompileArguments, aCompileArgumentsNegativePattern)
2257+
}
2258+
2259+
// Workaround for: https://github.com/swiftlang/swift-package-manager/issues/8648
2260+
func test_mergeableSymbols_enabledInEmbedded() async throws {
2261+
let Pkg: AbsolutePath = "/Pkg"
2262+
let fs: FileSystem = InMemoryFileSystem(
2263+
emptyFiles:
2264+
Pkg.appending(components: "Sources", "A", "A.swift").pathString
22482265
)
22492266

2250-
let a = try BuildPlanResult(plan: plan)
2251-
.moduleBuildDescription(for: "A").swift().emitCommandLine()
2252-
XCTAssertMatch(a, ["-whole-module-optimization"])
2253-
XCTAssertNoMatch(a, ["-wmo"])
2267+
let observability = ObservabilitySystem.makeForTesting()
2268+
let graph = try loadModulesGraph(
2269+
fileSystem: fs,
2270+
manifests: [
2271+
Manifest.createRootManifest(
2272+
displayName: "Pkg",
2273+
path: .init(validating: Pkg.pathString),
2274+
targets: [
2275+
TargetDescription(
2276+
name: "A",
2277+
settings: [.init(tool: .swift, kind: .enableExperimentalFeature("Embedded"))]
2278+
),
2279+
]
2280+
),
2281+
],
2282+
observabilityScope: observability.topScope
2283+
)
2284+
XCTAssertNoDiagnostics(observability.diagnostics)
2285+
2286+
// -Xfrontend -mergeable symbols should be passed with Embedded
2287+
let result = try await BuildPlanResult(plan: mockBuildPlan(
2288+
graph: graph,
2289+
fileSystem: fs,
2290+
observabilityScope: observability.topScope
2291+
))
2292+
result.checkTargetsCount(1)
2293+
2294+
// Compile Swift Target
2295+
let aCompileArguments = try result.moduleBuildDescription(for: "A").swift().compileArguments()
2296+
let aCompileArgumentsPattern: [StringPattern] = ["-Xfrontend", "-mergeable-symbols"]
2297+
XCTAssertMatch(aCompileArguments, aCompileArgumentsPattern)
22542298
}
22552299

22562300
func testREPLArguments() async throws {

0 commit comments

Comments
 (0)