Skip to content

Commit 01b04df

Browse files
committed
Improve the WebAssembly tests
Make them work on Linux, and standardize how they're skipped if SDKs are not installed. rdar://148676808
1 parent 5701d61 commit 01b04df

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

Sources/SWBCore/SwiftSDK.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,24 @@ public struct SwiftSDK: Sendable {
6969
}
7070

7171
/// The default location storing Swift SDKs installed by SwiftPM.
72-
static var defaultSwiftSDKsDirectory: Path {
73-
get throws {
74-
try FileManager.default.url(
72+
static func defaultSwiftSDKsDirectory(hostOperatingSystem: OperatingSystem) throws -> Path {
73+
let spmURL: URL
74+
if hostOperatingSystem == .macOS {
75+
spmURL = try FileManager.default.url(
7576
for: .libraryDirectory,
7677
in: .userDomainMask,
7778
appropriateFor: nil,
7879
create: false
79-
).appendingPathComponent("org.swift.swiftpm").appendingPathComponent("swift-sdks").filePath
80+
).appendingPathComponent("org.swift.swiftpm")
81+
} else {
82+
spmURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".swiftpm")
8083
}
84+
return try spmURL.appendingPathComponent("swift-sdks").filePath
8185
}
8286

8387
/// Find Swift SDKs installed by SwiftPM.
84-
public static func findSDKs(targetTriples: [String], fs: any FSProxy) throws -> [SwiftSDK] {
85-
return try findSDKs(swiftSDKsDirectory: defaultSwiftSDKsDirectory, targetTriples: targetTriples, fs: fs)
88+
public static func findSDKs(targetTriples: [String], fs: any FSProxy, hostOperatingSystem: OperatingSystem) throws -> [SwiftSDK] {
89+
return try findSDKs(swiftSDKsDirectory: defaultSwiftSDKsDirectory(hostOperatingSystem: hostOperatingSystem), targetTriples: targetTriples, fs: fs)
8690
}
8791

8892
private static func findSDKs(swiftSDKsDirectory: Path, targetTriples: [String], fs: any FSProxy) throws -> [SwiftSDK] {

Sources/SWBTestSupport/SkippedTestSupport.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ extension Trait where Self == Testing.ConditionTrait {
115115
package static func requireSDKs(_ knownSDKs: KnownSDK..., comment: Comment? = nil) -> Self {
116116
enabled(comment != nil ? "required SDKs are not installed: \(comment?.description ?? "")" : "required SDKs are not installed.", {
117117
let sdkRegistry = try await ConditionTraitContext.shared.getCore().sdkRegistry
118-
let missingSDKs = await knownSDKs.asyncFilter { sdkRegistry.lookup($0.sdkName) == nil }.sorted()
118+
let missingSDKs = await knownSDKs.asyncFilter { knownSDK in
119+
sdkRegistry.lookup(knownSDK.sdkName) == nil && sdkRegistry.allSDKs.count(where: { $0.aliases.contains(knownSDK.sdkName) }) == 0
120+
}.sorted()
119121
return missingSDKs.isEmpty
120122
})
121123
}
@@ -136,6 +138,13 @@ extension Trait where Self == Testing.ConditionTrait {
136138
disabled("Process spawning is unavailable.", { try ProcessInfo.processInfo.hostOperatingSystem().isAppleEmbedded || ProcessInfo.processInfo.isMacCatalystApp })
137139
}
138140

141+
/// Constructs a condition trait that causes a test to be disabled if the developer directory is pointing at an Xcode developer directory.
142+
package static var skipXcodeToolchain: Self {
143+
disabled("This test is incompatible with Xcode toolchains.", {
144+
try await ConditionTraitContext.shared.getCore().developerPath.path.str.contains(".app/Contents/Developer")
145+
})
146+
}
147+
139148
/// Constructs a condition trait that causes a test to be disabled if the Foundation process spawning implementation is not using `posix_spawn_file_actions_addchdir`.
140149
package static var requireThreadSafeWorkingDirectory: Self {
141150
disabled("Thread-safe process working directory support is unavailable.", {

Sources/SWBWebAssemblyPlatform/Plugin.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ struct WebAssemblyPlatformSpecsExtension: SpecificationsExtension {
2525
func specificationFiles(resourceSearchPaths: [Path]) -> Bundle? {
2626
findResourceBundle(nameWhenInstalledInToolchain: "SwiftBuild_SWBWebAssemblyPlatform", resourceSearchPaths: resourceSearchPaths, defaultBundle: Bundle.module)
2727
}
28+
29+
func specificationDomains() -> [String: [String]] {
30+
["webassembly": ["generic-unix"]]
31+
}
2832
}
2933

3034
struct WebAssemblyPlatformExtension: PlatformInfoExtension {
@@ -76,7 +80,8 @@ struct WebAssemblySDKRegistryExtension: SDKRegistryExtension {
7680

7781
let wasmSwiftSDKs = (try? SwiftSDK.findSDKs(
7882
targetTriples: Array(supportedTriples.keys),
79-
fs: context.fs
83+
fs: context.fs,
84+
hostOperatingSystem: context.hostOperatingSystem
8085
)) ?? []
8186

8287
var wasmSDKs: [(path: Path, platform: SWBCore.Platform?, data: [String: PropertyListItem])] = []
@@ -94,6 +99,7 @@ struct WebAssemblySDKRegistryExtension: SDKRegistryExtension {
9499
"Type": .plString("SDK"),
95100
"Version": .plString("1.0.0"),
96101
"CanonicalName": .plString(wasmSDK.identifier),
102+
"Aliases": ["webassembly", "wasi"],
97103
"IsBaseSDK": .plBool(true),
98104
"DefaultProperties": .plDict([
99105
"PLATFORM_NAME": .plString("webassembly"),

Sources/SWBWebAssemblyPlatform/Specs/WasmLd.xcspec

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Domain = webassembly;
1616
Identifier = "com.apple.pbx.linkers.ld";
1717
Type = Linker;
18-
BasedOn = "default:com.apple.pbx.linkers.ld";
18+
BasedOn = "generic-unix:com.apple.pbx.linkers.ld";
1919
Name = Ld;
2020
Description = "Link executable using alternate linker";
2121
IsAbstract = Yes;
@@ -39,12 +39,6 @@
3939
},
4040
);
4141
},
42-
{
43-
Name = SDKROOT;
44-
Type = Path;
45-
CommandLineFlag = "--sysroot";
46-
IsInputDependency = Yes;
47-
},
4842
{
4943
// wasm-ld is always deterministic
5044
Name = LD_DETERMINISTIC_MODE;
@@ -138,6 +132,16 @@
138132
};
139133
Condition = "$(MACH_O_TYPE) == mh_dylib";
140134
},
135+
{
136+
Name = "CLANG__INPUT_FILE_LIST_PATH__";
137+
Type = Path;
138+
Condition = "NO"; // wasm-ld does not support -filelist
139+
},
140+
{
141+
Name = "SWIFTC__INPUT_FILE_LIST_PATH__";
142+
Type = Path;
143+
Condition = "NO"; // wasm-ld does not support -filelist
144+
},
141145
);
142146
},
143147
)

Tests/SWBWebAssemblyPlatformTests/SWBWebAssemblyPlatformTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Testing
14+
import SWBCore
1415
import SWBProtocol
1516
import SWBTestSupport
1617
import SWBTaskExecution
@@ -19,13 +20,13 @@ import SWBUtil
1920
@Suite
2021
fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
2122
@Test(
23+
.requireSDKs(.wasi),
2224
.requireThreadSafeWorkingDirectory,
23-
.enabled(if: getEnvironmentVariable("WASM_SDKROOT") != nil),
25+
.skipXcodeToolchain,
2426
arguments: ["wasm32"], [true, false]
2527
)
2628
func wasiCommandWithSwift(arch: String, enableTestability: Bool) async throws {
27-
guard let sdkroot = getEnvironmentVariable("WASM_SDKROOT") else { return }
28-
guard let toolchain = getEnvironmentVariable("WASM_TOOLCHAINS") else { return }
29+
let sdkroot = try await #require(getCore().loadSDK(llvmTargetTripleSys: "wasi")).path.str
2930

3031
try await withTemporaryDirectory { (tmpDir: Path) in
3132
let testProject = TestProject(
@@ -46,7 +47,6 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
4647
"SDKROOT": sdkroot,
4748
"SWIFT_VERSION": "6.0",
4849
"SUPPORTED_PLATFORMS": "webassembly",
49-
"TOOLCHAINS": toolchain,
5050
"ENABLE_TESTABILITY": enableTestability ? "YES" : "NO"
5151
])
5252
],
@@ -132,10 +132,9 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
132132
}
133133
}
134134

135-
@Test(.requireThreadSafeWorkingDirectory, .enabled(if: getEnvironmentVariable("WASM_SDKROOT") != nil), arguments: ["wasm32"])
135+
@Test(.requireSDKs(.wasi), .requireThreadSafeWorkingDirectory, .skipXcodeToolchain, arguments: ["wasm32"])
136136
func wasiCommandWithCAndCxx(arch: String) async throws {
137-
guard let sdkroot = getEnvironmentVariable("WASM_SDKROOT") else { return }
138-
guard let toolchain = getEnvironmentVariable("WASM_TOOLCHAINS") else { return }
137+
let sdkroot = try await #require(getCore().loadSDK(llvmTargetTripleSys: "wasi")).path.str
139138

140139
try await withTemporaryDirectory { (tmpDir: Path) in
141140
let testProject = TestProject(
@@ -157,7 +156,6 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
157156
"SDKROOT": sdkroot,
158157
"SWIFT_VERSION": "6.0",
159158
"SUPPORTED_PLATFORMS": "webassembly",
160-
"TOOLCHAINS": toolchain,
161159
])
162160
],
163161
targets: [
@@ -252,3 +250,9 @@ fileprivate struct SWBWebAssemblyPlatformTests: CoreBasedTests {
252250
}
253251
}
254252
}
253+
254+
fileprivate extension Core {
255+
func loadSDK(llvmTargetTripleSys: String) -> SDK? {
256+
sdkRegistry.allSDKs.filter { $0.defaultVariant?.llvmTargetTripleSys == llvmTargetTripleSys }.only
257+
}
258+
}

0 commit comments

Comments
 (0)