From 7fb1f30d10e3d25c84456e60671bbbc5a81f3b26 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 16 Sep 2025 16:35:12 +0100 Subject: [PATCH 1/7] Add Wasm Swift SDK checks for `swift run` and `swift test` --- lit.cfg | 8 ++- wasm/Hello/.gitignore | 8 +++ wasm/Hello/Package.swift | 15 ++++++ wasm/Hello/Sources/Hello/Hello.swift | 9 ++++ wasm/Hello/Tests/HelloTests/HelloTests.swift | 13 +++++ wasm/swiftpm.md | 56 ++++++++++++++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 wasm/Hello/.gitignore create mode 100644 wasm/Hello/Package.swift create mode 100644 wasm/Hello/Sources/Hello/Hello.swift create mode 100644 wasm/Hello/Tests/HelloTests/HelloTests.swift create mode 100644 wasm/swiftpm.md diff --git a/lit.cfg b/lit.cfg index afb28f2..040c41a 100644 --- a/lit.cfg +++ b/lit.cfg @@ -123,7 +123,7 @@ else: # that's possible if lit_config.params.get("have-network"): config.available_features.add("have-network") - + ### # Get the package path. @@ -162,6 +162,10 @@ if swiftpm_srcdir is None: if os.path.exists(swiftpm_srcdir): config.available_features.add("have-swiftpm") config.substitutions.append( ('%{swiftpm_srcdir}', swiftpm_srcdir) ) + config.substitutions.append( + ('%{swift-sdk-generator_srcdir}', os.path.join(swiftpm_srcdir, "..", "swift-sdk-generator")) + ) + config.substitutions.append( ('%{swiftpm_homedir}', os.path.join(os.environ['HOME'], ".swiftpm", "swift-sdks")) ) # Use the default Swift src layout if Swift the benchmark suite path is not # provided as a param. @@ -263,11 +267,13 @@ if swiftpm_build is not None: config.substitutions.append( ('%{swift-build}', os.path.join(swiftpm_build, "swift-build")) ) config.substitutions.append( ('%{swift-test}', os.path.join(swiftpm_build, "swift-test")) ) config.substitutions.append( ('%{swift-run}', os.path.join(swiftpm_build, "swift-run")) ) + config.substitutions.append( ('%{swift-sdk}', os.path.join(swiftpm_build, "swift-sdk")) ) else: config.substitutions.append( ('%{swift-package}', swift_path + ' package') ) config.substitutions.append( ('%{swift-build}', swift_path + ' build') ) config.substitutions.append( ('%{swift-test}', swift_path + ' test') ) config.substitutions.append( ('%{swift-run}', swift_path + ' run') ) + config.substitutions.append( ('%{swift-sdk}', swift_path + ' sdk') ) ### diff --git a/wasm/Hello/.gitignore b/wasm/Hello/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/wasm/Hello/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/wasm/Hello/Package.swift b/wasm/Hello/Package.swift new file mode 100644 index 0000000..0c92bd8 --- /dev/null +++ b/wasm/Hello/Package.swift @@ -0,0 +1,15 @@ +// swift-tools-version: 6.2 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Hello", + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "Hello" + ), + ] +) diff --git a/wasm/Hello/Sources/Hello/Hello.swift b/wasm/Hello/Sources/Hello/Hello.swift new file mode 100644 index 0000000..805404e --- /dev/null +++ b/wasm/Hello/Sources/Hello/Hello.swift @@ -0,0 +1,9 @@ +import WASILibc + +@main +struct Hello { + static func main() { + print("Hello, world!") + puts("Hello from WASILibc!") + } +} diff --git a/wasm/Hello/Tests/HelloTests/HelloTests.swift b/wasm/Hello/Tests/HelloTests/HelloTests.swift new file mode 100644 index 0000000..02c32f5 --- /dev/null +++ b/wasm/Hello/Tests/HelloTests/HelloTests.swift @@ -0,0 +1,13 @@ +import Testing +import XCTest +@testable import Hello + +@Test func example() async throws { + #expect(Int("42") == 42) +} + +final class HelloTests: XCTestCase { + func testExample() throws { + XCTAssertEqual(Int("42") == 42) + } +} diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md new file mode 100644 index 0000000..4692c0f --- /dev/null +++ b/wasm/swiftpm.md @@ -0,0 +1,56 @@ +# SwiftPM checks for Wasm Swift SDKs + +1. Let's install Swift SDK for Wasm first: + +``` +RUN: %{swift-sdk} list | %{FileCheck} --check-prefix CHECK-SDK-LIST-BEFORE %s +CHECK-SDK-LIST-BEFORE: No Swift SDKs are currently installed. +RUN: find "%{swift-sdk-generator_srcdir}/Bundles" -d 1 | xargs %{swift-sdk} install | %{FileCheck} --check-prefix CHECK-SDK-INSTALL %s +CHECK-SDK-INSTALL: Swift SDK bundle at +CHECK-SDK-INSTALL: successfully installed as +RUN: %{swift-sdk} list | grep wasm | wc -l | %{FileCheck} --check-prefix CHECK-SDK-LIST-AFTER %s +CHECK-SDK-LIST-AFTER: 2 +``` + +2. Creating a package from `init` template: + +``` +RUN: rm -rf %t.dir +RUN: mkdir -p %t.dir +RUN: cp -r %S/Hello %t.dir +``` + +3. Building and running prepared `"Hello, world!"` executable from the newly created package: + + a) Non-embedded Swift SDK + + ``` + RUN: %{swift-sdk} list | grep -v embedded | xargs %{swift-run} --package-path %t.dir/Hello --swift-sdk | %{FileCheck} --check-prefix CHECK-RUN-OUTPUT %s + CHECK-RUN-OUTPUT: Hello, world! + CHECK-RUN-OUTPUT-NEXT: Hello from WASILibc! + ``` + + b) Embedded Swift SDK + + ``` + RUN: %{swift-sdk} list | grep embedded | xargs %{swift-run} --package-path %t.dir/Hello --swift-sdk | %{FileCheck} --check-prefix CHECK-EMBEDDED-RUN-OUTPUT %s + CHECK-EMBEDDED-RUN-OUTPUT: Hello, world! + CHECK-EMBEDDED-RUN-OUTPUT-NEXT: Hello from WASILibc! + ``` + +4. Running tests from the newly created package: + + a) Non-embedded Swift SDK + + ``` + RUN: %{swift-sdk} list | grep -v embedded | xargs %{swift-test} --package-path %t.dir/Hello --swift-sdk | %{FileCheck} --check-prefix CHECK-RUN-OUTPUT %s + ``` + + +5. Clean up installed Swift SDK: + +``` +RUN: find %{swiftpm_homedir}/ -d 1 | xargs rm -rf +RUN: %{swift-sdk} list | %{FileCheck} --check-prefix CHECK-SDK-LIST-FINAL %s +CHECK-SDK-LIST-FINAL: No Swift SDKs are currently installed. +``` From 501149e37cd53835528b13d01c16cd393021b04f Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 16 Sep 2025 16:37:02 +0100 Subject: [PATCH 2/7] Remove unused comments from `Package.swift` --- wasm/Hello/Package.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/wasm/Hello/Package.swift b/wasm/Hello/Package.swift index 0c92bd8..028c6ff 100644 --- a/wasm/Hello/Package.swift +++ b/wasm/Hello/Package.swift @@ -1,13 +1,9 @@ // swift-tools-version: 6.2 -// The swift-tools-version declares the minimum version of Swift required to build this package. - import PackageDescription let package = Package( name: "Hello", targets: [ - // Targets are the basic building blocks of a package, defining a module or a test suite. - // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Hello" ), From 0c66dad24b83c5f2515cd2bdf762d9cccf87ae17 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 16 Sep 2025 16:38:50 +0100 Subject: [PATCH 3/7] Add `REQUIRES: platform=Linux` to the new test --- wasm/swiftpm.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md index 4692c0f..950e22d 100644 --- a/wasm/swiftpm.md +++ b/wasm/swiftpm.md @@ -1,5 +1,11 @@ # SwiftPM checks for Wasm Swift SDKs +This test can only run on Linux CI as we only build Swift SDK for Wasm on Linux CI nodes. + +``` +REQUIRES: platform=Linux +``` + 1. Let's install Swift SDK for Wasm first: ``` From 2ca691192191441e35d041b54e08b0988e4b845f Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 17 Sep 2025 09:31:57 +0100 Subject: [PATCH 4/7] Use `-mindepth 1 -maxdepth 1` in `wasm/swiftpm.md` --- wasm/swiftpm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md index 950e22d..9692df1 100644 --- a/wasm/swiftpm.md +++ b/wasm/swiftpm.md @@ -11,7 +11,7 @@ REQUIRES: platform=Linux ``` RUN: %{swift-sdk} list | %{FileCheck} --check-prefix CHECK-SDK-LIST-BEFORE %s CHECK-SDK-LIST-BEFORE: No Swift SDKs are currently installed. -RUN: find "%{swift-sdk-generator_srcdir}/Bundles" -d 1 | xargs %{swift-sdk} install | %{FileCheck} --check-prefix CHECK-SDK-INSTALL %s +RUN: find "%{swift-sdk-generator_srcdir}/Bundles" -mindepth 1 -maxdepth 1 | xargs %{swift-sdk} install | %{FileCheck} --check-prefix CHECK-SDK-INSTALL %s CHECK-SDK-INSTALL: Swift SDK bundle at CHECK-SDK-INSTALL: successfully installed as RUN: %{swift-sdk} list | grep wasm | wc -l | %{FileCheck} --check-prefix CHECK-SDK-LIST-AFTER %s From f1ac9b2fba85cedc5fd4fbb51091999202133319 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 19 Sep 2025 12:35:43 +0100 Subject: [PATCH 5/7] Exclude checks for `swift test` from `swiftpm.md` for now Enabling this check requires https://github.com/swiftlang/swift-package-manager/pull/9114 to be merged first, which is currently failing due to an unfixed regression on `main`. --- wasm/swiftpm.md | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md index 9692df1..2acaf15 100644 --- a/wasm/swiftpm.md +++ b/wasm/swiftpm.md @@ -44,16 +44,7 @@ RUN: cp -r %S/Hello %t.dir CHECK-EMBEDDED-RUN-OUTPUT-NEXT: Hello from WASILibc! ``` -4. Running tests from the newly created package: - - a) Non-embedded Swift SDK - - ``` - RUN: %{swift-sdk} list | grep -v embedded | xargs %{swift-test} --package-path %t.dir/Hello --swift-sdk | %{FileCheck} --check-prefix CHECK-RUN-OUTPUT %s - ``` - - -5. Clean up installed Swift SDK: +4. Clean up installed Swift SDK: ``` RUN: find %{swiftpm_homedir}/ -d 1 | xargs rm -rf From d0f5b0974b6bc0bf3dc22b6a573c2cdc4eadfdb5 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 19 Sep 2025 16:14:05 +0100 Subject: [PATCH 6/7] Use `-mindepth 1 -maxdepth 1` in step 4 of `swiftpm.md` --- wasm/swiftpm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md index 2acaf15..4853b78 100644 --- a/wasm/swiftpm.md +++ b/wasm/swiftpm.md @@ -47,7 +47,7 @@ RUN: cp -r %S/Hello %t.dir 4. Clean up installed Swift SDK: ``` -RUN: find %{swiftpm_homedir}/ -d 1 | xargs rm -rf +RUN: find %{swiftpm_homedir}/ -mindepth 1 -maxdepth 1 | xargs rm -rf RUN: %{swift-sdk} list | %{FileCheck} --check-prefix CHECK-SDK-LIST-FINAL %s CHECK-SDK-LIST-FINAL: No Swift SDKs are currently installed. ``` From a24279bfcb75d77fa91e51ef5f11595098ccdbbf Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 19 Sep 2025 20:14:03 +0100 Subject: [PATCH 7/7] wasm/swiftpm.md: clean up wording for every step --- wasm/swiftpm.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wasm/swiftpm.md b/wasm/swiftpm.md index 4853b78..b66de24 100644 --- a/wasm/swiftpm.md +++ b/wasm/swiftpm.md @@ -18,7 +18,7 @@ RUN: %{swift-sdk} list | grep wasm | wc -l | %{FileCheck} --check-prefix CHECK-S CHECK-SDK-LIST-AFTER: 2 ``` -2. Creating a package from `init` template: +2. Using a prepared basic package that exercises Swift stdlib and `import WASILibc`: ``` RUN: rm -rf %t.dir @@ -26,7 +26,7 @@ RUN: mkdir -p %t.dir RUN: cp -r %S/Hello %t.dir ``` -3. Building and running prepared `"Hello, world!"` executable from the newly created package: +3. Building and running the prepared package: a) Non-embedded Swift SDK @@ -44,7 +44,7 @@ RUN: cp -r %S/Hello %t.dir CHECK-EMBEDDED-RUN-OUTPUT-NEXT: Hello from WASILibc! ``` -4. Clean up installed Swift SDK: +4. Clean up installed Swift SDKs: ``` RUN: find %{swiftpm_homedir}/ -mindepth 1 -maxdepth 1 | xargs rm -rf