Wasm support is available in preview in latest development snapshots off the main
branch distributed on swift.org/download. Note that upstream Swift 5.9 and 5.10 did not get all of the necessary patches before branching off, so those stable upstream versions don't support it. You'll have to use SwiftWasm fork of the toolchain for 5.9 and 5.10.
Cross-compilation for Wasm works the same way it does for other platforms. When building with swiftc
(Swift Driver) directly, -target
is the name of the option taking the necessary triple. When building with swift build
(SwiftPM), the option is called --triple
. When using Swift SDKs, you should pass --experimental-swift-sdk
option to swift build
instead of --triple
.
The triple that the upstream compiler currently supports in Embedded Swift mode without any additional Swift SDK installation is wasm32-unknown-none-wasm
. I've described the complete list of required options for that triple in this post, in addition to steps for installing wasm-ld
linker that needs to be installed separately on macOS. Docker images of Swift for Linux already have wasm-ld
included.
To get WASI support outside of Embedded Swift, you still need to install Swift SDKs from SwiftWasm. Swift SDK for WASI greatly benefits from upstream compiler support, which allowed distribution size to be reduced from 1.25 GB to 79 MB on macOS, and from ~900 MB to ~110 MB on Linux. The installation steps are also simplified, as you can pass the URL of artifactbundle.zip
archive to swift experimental-sdk install
command available in the upstream toolchain.
That is correct for wasm32-unknown-wasi
triple, but not the case for wasm32-unknown-none-wasm
triple. For the latter same restrictions apply as for other Embedded Swift platforms.
IIRC Swift stdlib doesn't provide any file IO functions. I reckon you're referring to Swift System or Swift Foundation libraries instead?
Correct, as long as a given binary doesn't have any additional imports on top of the standard ones for wasi_snapshot_preview1
. It usually should not, unless you've passed certain linker arguments or specified function attributes that declare additional imports.