|
1 | | -# Installation - Latest Release (Swift 6.1) |
| 1 | +# Installation |
2 | 2 |
|
3 | | -SwiftWasm provides [Swift SDK](https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md)s for WebAssembly. |
4 | | - |
5 | | -Before installing the Swift SDK, you need to ensure the following: |
6 | | - |
7 | | -- You need to [install an Open Source toolchain from swift.org](https://www.swift.org/install/). (Not the Xcode toolchain) |
8 | | -- You cannot use toolchains bundled with Xcode to use the Swift SDK. |
9 | | -- If you are using macOS, please follow the [official guide](https://www.swift.org/install/macos/package_installer/) to install the toolchain. |
10 | | - |
11 | | -Please ensure you have installed the Swift 6.1 Open Source toolchain. |
12 | | - |
13 | | -```sh |
14 | | -swift --version |
15 | | -``` |
16 | | - |
17 | | -| Toolchain | Output | |
18 | | -|-----------|--------| |
19 | | -| ❌ Xcode | `Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)` | |
20 | | -| ✅ Open Source (macOS) | `Apple Swift version 6.1 (swift-6.1-RELEASE)` | |
21 | | -| ✅ Open Source (Others) | `Swift version 6.1 (swift-6.1-RELEASE)` | |
22 | | - |
23 | | -Once you have installed the Open Source toolchain, you can install the Swift SDK for WebAssembly: |
24 | | - |
25 | | -```bash |
26 | | -swift sdk install "https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.1-RELEASE/swift-wasm-6.1-RELEASE-wasm32-unknown-wasi.artifactbundle.zip" --checksum "7550b4c77a55f4b637c376f5d192f297fe185607003a6212ad608276928db992" |
27 | | -``` |
28 | | - |
29 | | -After installing the Swift SDK, you can see `6.1-RELEASE-wasm32-unknown-wasi` in the Swift SDK list: |
30 | | - |
31 | | -```bash |
32 | | -swift sdk list |
33 | | -``` |
34 | | - |
35 | | -You can also find other SDKs from [the GitHub Releases page](https://github.com/swiftwasm/swift/releases). |
36 | | - |
37 | | -## Hello, World |
38 | | - |
39 | | -First, create a new directory for your project and navigate into it: |
40 | | - |
41 | | -```bash |
42 | | -$ mkdir hello && cd hello |
43 | | -``` |
44 | | - |
45 | | -Create a new Swift package: |
46 | | - |
47 | | -```bash |
48 | | -$ swift package init --type executable |
49 | | -``` |
50 | | - |
51 | | -You can use the installed SDKs to cross-compile Swift packages for WebAssembly: |
52 | | - |
53 | | -```bash |
54 | | -$ swift build --swift-sdk wasm32-unknown-wasi |
55 | | -... |
56 | | -$ file .build/wasm32-unknown-wasi/debug/hello.wasm |
57 | | -.build/wasm32-unknown-wasi/debug/hello.wasm: WebAssembly (wasm) binary module version 0x1 (MVP) |
58 | | -``` |
59 | | - |
60 | | -You can run the built WebAssembly module using [`wasmtime`](https://wasmtime.dev/): |
61 | | - |
62 | | -```bash |
63 | | -$ wasmtime .build/wasm32-unknown-wasi/debug/hello.wasm |
64 | | -Hello, world! |
65 | | -``` |
66 | | - |
67 | | -## FAQ |
68 | | - |
69 | | -### How to check if I am using Open Source toolchain or Xcode toolchain? |
70 | | - |
71 | | -```bash |
72 | | -$ swift --version | head -n1 |
73 | | -``` |
74 | | - |
75 | | -| Toolchain | Output | |
76 | | -|-----------|--------| |
77 | | -| Xcode | `Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)` | |
78 | | -| Open Source (macOS) | `Apple Swift version 6.1 (swift-6.1-RELEASE)` | |
79 | | -| Open Source (Others) | `Swift version 6.1 (swift-6.1-RELEASE)` | |
80 | | - |
81 | | - |
82 | | -### `<unknown>:0: error: module compiled with Swift 6.1 cannot be imported by the Swift x.y.z` |
83 | | - |
84 | | -This error occurs when the Swift toolchain version you are using is different from the version of the Swift SDK you have installed. |
85 | | - |
86 | | -To resolve this issue, you can either: |
87 | | - |
88 | | -1. Switch the Swift toolchain to the same version as the Swift SDK you have installed. Check the [official guide](https://www.swift.org/install/macos/package_installer/) for how to switch the toolchain. |
89 | | -2. Install the Swift SDK for the same version as the Swift toolchain you are using. |
90 | | - Use the following shell snippet to query compatible Swift SDK for your current toolchain version: |
91 | | - |
92 | | -```console |
93 | | -( |
94 | | - V="$(swiftc --version | head -n1)"; \ |
95 | | - TAG="$(curl -sL "https://raw.githubusercontent.com/swiftwasm/swift-sdk-index/refs/heads/main/v1/tag-by-version.json" | jq -r --arg v "$V" '.[$v] | .[-1]')"; \ |
96 | | - curl -sL "https://raw.githubusercontent.com/swiftwasm/swift-sdk-index/refs/heads/main/v1/builds/$TAG.json" | \ |
97 | | - jq -r '.["swift-sdks"]["wasm32-unknown-wasi"] | "swift sdk install \"\(.url)\" --checksum \"\(.checksum)\""' |
98 | | -) |
99 | | -``` |
100 | | - |
101 | | - |
102 | | -### What is the difference between the Swift Toolchain and the Swift SDK? |
103 | | - |
104 | | -The Swift toolchain is a complete package that includes the Swift compiler, standard library, and other tools. |
105 | | - |
106 | | -The Swift SDK includes a subset of the Swift toolchain that includes only the necessary components for cross-compilation and some supplementary resources. |
107 | | - |
108 | | -### What is included in the Swift SDK for WebAssembly? |
109 | | - |
110 | | -The Swift SDK for WebAssembly includes only the pre-built Swift standard libraries for WebAssembly. It does not include the Swift compiler or other tools that are part of the Swift toolchain. |
| 3 | +Now Swift SDK for WebAssembly is available from the official Swift project. |
| 4 | +See the [Swift.org article](https://www.swift.org/documentation/articles/wasm-getting-started.html) for instructions on how to install it. |
0 commit comments