Skip to content

Commit 521dfb1

Browse files
committed
Strengthen wasmSwiftSDKDependencySpecialization test
1 parent d7605b2 commit 521dfb1

1 file changed

Lines changed: 76 additions & 9 deletions

File tree

Tests/SWBWebAssemblyPlatformTests/SWBWebAssemblyPlatformTests.swift

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,21 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
130130
/// Regression test: a wasm app target depending on a library with platform specialization
131131
/// must not fail with `unable to find sdk 'webassembly'`.
132132
///
133-
/// The failure is triggered by `SpecializationParameters.imposed(on:workspaceContext:)`
134-
/// in `swift-build/Sources/SWBCore/DependencyResolution.swift`, which sets
135-
/// `SDKROOT = platform.sdkCanonicalName` (= `"webassembly"`) on dependencies — and the
136-
/// SDK registry has no SDK or alias by that canonical name when only a Swift SDK is
137-
/// providing the wasm sysroot.
133+
/// Two code paths in swift-build push `SDKROOT = platform.sdkCanonicalName` (= `"webassembly"`)
134+
/// for Swift-SDK-backed builds, neither of which has a matching SDK in the registry when the
135+
/// only wasm sysroot comes from a synthesized Swift SDK (whose canonical name is the manifest
136+
/// path, not the platform name):
137+
///
138+
/// 1. `SpecializationParameters.imposed(on:workspaceContext:)`
139+
/// in `swift-build/Sources/SWBCore/DependencyResolution.swift` — exercised by `MyLibrary`,
140+
/// which is reachable via specialization from `MyApp`.
141+
/// 2. `addRunDestinationSettingsPlatformSDK` in
142+
/// `swift-build/Sources/SWBCore/Settings/Settings.swift` else branch — exercised by
143+
/// `MyMacOSLib`, whose configured SDK (`macosx`) does not match the destination platform
144+
/// (`webassembly`) but whose `SUPPORTED_PLATFORMS` still includes wasm.
138145
@Test(.requireSDKs(.host))
139146
func wasmSwiftSDKDependencySpecialization() async throws {
140-
try await withTemporaryDirectory { tmpDir in
147+
try await withTemporaryDirectory { (tmpDir: Path) in
141148
let clangCompilerPath = try await self.clangCompilerPath
142149
let swiftCompilerPath = try await self.swiftCompilerPath
143150
let swiftVersion = try await self.swiftVersion
@@ -148,6 +155,7 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
148155
children: [
149156
TestFile("App.swift"),
150157
TestFile("Lib.swift"),
158+
TestFile("MacLib.swift"),
151159
]),
152160
targets: [
153161
TestStandardTarget(
@@ -170,8 +178,10 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
170178
buildPhases: [
171179
TestSourcesBuildPhase([TestBuildFile("App.swift")]),
172180
],
173-
dependencies: ["MyLibrary"]
181+
dependencies: ["MyLibrary", "MyMacOSLib"]
174182
),
183+
// Exercises DependencyResolution.swift `SpecializationParameters.imposed(on:)`:
184+
// gets specialized to wasm via the dependency from MyApp.
175185
TestStandardTarget(
176186
"MyLibrary",
177187
type: .staticLibrary,
@@ -193,6 +203,31 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
193203
buildPhases: [
194204
TestSourcesBuildPhase([TestBuildFile("Lib.swift")]),
195205
]),
206+
// Exercises Settings.swift `addRunDestinationSettingsPlatformSDK` else branch:
207+
// configured SDK ("macosx") differs from destination platform ("webassembly")
208+
// but SUPPORTED_PLATFORMS includes wasm, so the SDK gets re-targeted.
209+
// No ALLOW_TARGET_PLATFORM_SPECIALIZATION here — that flag would short-circuit
210+
// the early-return guard at Settings.swift:3600 and skip the else branch.
211+
TestStandardTarget(
212+
"MyMacOSLib",
213+
type: .staticLibrary,
214+
buildConfigurations: [
215+
TestBuildConfiguration("Debug",
216+
buildSettings: [
217+
"PRODUCT_NAME": "$(TARGET_NAME)",
218+
"SDKROOT": "macosx",
219+
"SUPPORTED_PLATFORMS": "$(AVAILABLE_PLATFORMS)",
220+
"CLANG_ENABLE_MODULES": "YES",
221+
"SWIFT_EXEC": swiftCompilerPath.str,
222+
"SWIFT_VERSION": swiftVersion,
223+
"CC": clangCompilerPath.str,
224+
"CLANG_EXPLICIT_MODULES_LIBCLANG_PATH": libClangPath.str,
225+
"CLANG_USE_RESPONSE_FILE": "NO",
226+
]),
227+
],
228+
buildPhases: [
229+
TestSourcesBuildPhase([TestBuildFile("MacLib.swift")]),
230+
]),
196231
])
197232
// Use a dedicated core for this test so the SDKs it registers do not impact other tests
198233
let core = try await Self.makeCore()
@@ -225,10 +260,42 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
225260
""")
226261
})
227262

263+
let sysroot = sdkManifestDir.join("WASI.sdk")
264+
let sdkroot = sdkManifestDir.join("WASI.sdk")
265+
228266
let destination = try RunDestinationInfo(sdkManifestPath: sdkManifestPath, triple: "wasm32-unknown-wasip1", targetArchitecture: "wasm32", supportedArchitectures: ["wasm32"], disableOnlyActiveArch: false, core: core)
229267
let parameters = BuildParameters(configuration: "Debug", activeRunDestination: destination)
230-
await tester.checkBuild(parameters, runDestination: nil, fs: localFS) { results in
231-
// The bug surfaces as an error literally containing "unable to find sdk 'webassembly'".
268+
269+
// Path 1: build MyApp — exercises DependencyResolution.swift `SpecializationParameters.imposed(on:)`
270+
// because MyLibrary is reached via dependency from MyApp, which pre-imposes SDKROOT.
271+
// Without the DependencyResolution fix, MyLibrary's specialized configuration would
272+
// fail with `unable to find sdk 'webassembly'`.
273+
await tester.checkBuild(parameters, runDestination: nil, targetName: "MyApp", fs: localFS) { results in
274+
results.checkTask(.matchTargetName("MyLibrary"), .matchRuleType("SwiftDriver Compilation")) { task in
275+
task.checkCommandLineContains([
276+
["-static-stdlib"],
277+
["-sdk", sdkroot.str],
278+
["-sysroot", sysroot.str],
279+
["-target", "wasm32-unknown-wasip1"],
280+
].reduce([], +))
281+
}
282+
283+
results.checkNoErrors()
284+
}
285+
286+
// Path 2: build MyMacOSLib standalone — exercises Settings.swift `addRunDestinationSettingsPlatformSDK`
287+
// else branch because no dependency imposes SDKROOT, and the target's own SDK ("macosx")
288+
// doesn't match the destination platform ("webassembly"). Without the Settings.swift fix,
289+
// this branch would push SDKROOT="webassembly" and the lookup would fail.
290+
await tester.checkBuild(parameters, runDestination: nil, targetName: "MyMacOSLib", fs: localFS) { results in
291+
results.checkTask(.matchTargetName("MyMacOSLib"), .matchRuleType("SwiftDriver Compilation")) { task in
292+
task.checkCommandLineContains([
293+
["-sdk", sdkroot.str],
294+
["-sysroot", sysroot.str],
295+
["-target", "wasm32-unknown-wasip1"],
296+
].reduce([], +))
297+
}
298+
232299
results.checkNoErrors()
233300
}
234301
}

0 commit comments

Comments
 (0)