-
Notifications
You must be signed in to change notification settings - Fork 255
Add "Getting Started with Swift SDKs for WebAssembly" article #1024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae18765
Create `_wasi_sdk_dev.md`
MaxDesiatov 978a75a
Add `wasm-getting-started.md` draft
MaxDesiatov 5955493
Fix wording and typos
MaxDesiatov 2d9acd9
Update wording for installation steps, add dedicated section for embe…
MaxDesiatov c041025
Remove unused file
MaxDesiatov 7a36782
Apply suggestions from code review
MaxDesiatov a851b30
Apply suggestions from code review
MaxDesiatov 87715e2
Revert "Apply suggestions from code review"
MaxDesiatov 99494e7
Add back the Swift SDK IDs table
MaxDesiatov 585c9f8
Merge branch 'main' into maxd/wasi-swift-sdk
MaxDesiatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| layout: page | ||
| date: 2025-06-01 12:00:00 | ||
| title: Getting Started with Swift SDKs for WebAssembly | ||
| author: [maxdesiatov] | ||
| --- | ||
|
|
||
| [WebAssembly (Wasm) is a virtual instruction set](https://webassembly.org/) focused on portability, security, and | ||
| performance. Developers can build client and server applications for Wasm and then deploy them in the browser or other | ||
| Wasm runtime implementations. | ||
|
|
||
| WebAssembly support in Swift started out as a community project. Any instruction set benefits tremendously from a | ||
| standardized ABI and system interfaces, and from its inception Wasm support in Swift targeted [WebAssembly System | ||
| Interface](https://wasi.dev/), which made porting Swift core libraries to this platform much easier. | ||
|
|
||
| With Swift 6.2 and development snapshots you can easily cross-compile and run Wasm modules with Swift SDKs for WASI distributed on [swift.org](https://swift.org/download). | ||
| The distributed artifact bundles also include support for the experimental Embedded Swift mode. | ||
|
|
||
| ## Installation | ||
|
|
||
| 1. [Install `swiftly` per the instructions](https://www.swift.org/install/) for the platform that you're bulding on. | ||
|
|
||
| 2. Install latest 6.2 development snapshot with `swiftly install 6.2-snapshot`, note the exact snapshot date component in the output of this command. | ||
|
|
||
| 3. Select the installed toolchain with `swiftly use 6.2-snapshot`. | ||
|
|
||
| 4. Navigate to [the downloads page](https://www.swift.org/download/) and find the “Swift SDK for WASI” section. Find a URL of a version that exactly matches the version from step 2. | ||
| If the corresponding snapshot version is not available for the Swift SDK, you’ll have to install the matching toolchain first. | ||
|
|
||
| 5. Find the checksum value for the corresponding Swift SDK on the same page, substitute it together with the URL from step 2, and execute the following command: | ||
|
|
||
| ``` | ||
| swift sdk install <swift_sdk_url> --checksum <checksum_value> | ||
| ``` | ||
|
|
||
| 6. Run `swift sdk list` to verify the Swift SDK was installed and note its ID in the output. Two Swift SDKs will be installed, | ||
| one with support for all Swift features, and the other with a subset of features allowed in the experimental [Embedded Swift mode](#embedded-swift-support). | ||
|
|
||
| | Swift SDK ID | Description | | ||
| |:-------:|:-----------:| | ||
| | `swift-<version>_wasm` | Supports all Swift features | | ||
| | `swift-<version>_wasm-embedded` | Supports a subset of features allowed in the experimental [Embedded Swift mode](#embedded-swift-support) | | ||
|
|
||
| 7. In the future, after installing or selecting a new version of the toolchain with `swiftly` make sure to follow steps 3-6 to install a Swift SDK exactly matching the toolchain version. | ||
|
|
||
| ## Building and Running | ||
|
|
||
| Let's create a simple package to see the Swift SDK in action: | ||
|
|
||
| ``` | ||
| mkdir Hello | ||
| cd Hello | ||
| swift package init --type executable | ||
| ``` | ||
|
|
||
| Modify freshly created `Sources/Hello/Hello.swift` file to print different strings depending on the target | ||
| platform: | ||
|
|
||
| ```swift | ||
| @main | ||
| struct wasi_test { | ||
| static func main() { | ||
| #if os(WASI) | ||
| print("Hello from WASI!") | ||
| #else | ||
| print("Hello from the host system!") | ||
| #endif | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Build your package with the following command, substituting the ID from step 5 of [the "Installation" section](#installation) above. | ||
|
|
||
| ``` | ||
| swift build --swift-sdk <swift_sdk_id> | ||
| ``` | ||
|
|
||
| Recent toolchain snapshots that are compatible with Swift SDKs for WASI also include | ||
| [WasmKit](https://github.com/swiftwasm/wasmkit/), which is a Wasm runtime that `swift run` can delegate to for | ||
| execution. To run the freshly built module, use `swift run` with the same `--swift-sdk` option: | ||
|
|
||
| ``` | ||
| swift run --swift-sdk <swift_sdk_id> | ||
| ``` | ||
|
|
||
| You should see the following output: | ||
|
|
||
| ``` | ||
| [1/1] Planning build | ||
| Building for debugging... | ||
| [8/8] Linking Hello.wasm | ||
| Build of product 'Hello' complete! (1.31s) | ||
| Hello from WASI! | ||
| ``` | ||
|
|
||
| # Embedded Swift Support | ||
|
|
||
| [Embedded Swift](https://github.com/swiftlang/swift-evolution/blob/main/visions/embedded-swift.md) is an experimental subset of the language | ||
| allowing the toolchain to produce Wasm binaries that are multiple orders of magnitude smaller. One of the Swift SDKs in the artifact bundle you've installed | ||
| with the `swift sdk install` command is tailored specifically for Embedded Swift. A subset of Swift Concurrency is also supported in this mode | ||
| thanks to the functionality provided by WASI. | ||
|
|
||
| To build with Embedded Swift SDK, pass its ID as noted in `swift sdk list` output (which has an `-embedded` suffix) in the `--swift-sdk` option. You also have to pass `-c release` | ||
| to `swift build` and `swift run` to enable optimizations required for Embedded Swift. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.