@@ -10,20 +10,52 @@ available yet, specifically:
10
10
2 . While a preview of multi-threading and atomics is available in some browsers and stand-alone
11
11
WebAssembly hosts, [ the corresponding proposal] ( https://github.com/WebAssembly/threads/ ) haven't
12
12
formally reached the implementation phase yet.
13
+ The multi-threading feature is available in ` wasm32-unknown-wasip1-threads ` target, but it's not
14
+ in ` wasm32-unknown-wasi ` target.
13
15
3 . Dynamic linking is not formally specified and tooling for it is not available yet.
14
16
* Binary size is a high priority requirement. Since WebAssembly payloads are usually served in browsers,
15
17
one wouldn't want end users to download multi-megabyte binaries.
16
18
17
- Nevertheless, an early implementation of the WebAssembly target is available [ in a separate
18
- fork] ( https://github.com/SwiftWasm ) . Here we're describing some decisions that were made while developing
19
+ ## Building Swift SDK for WebAssembly
20
+
21
+ The [ Swift SDK] ( https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md )
22
+ for WebAssembly is built using the following command:
23
+
24
+ ``` bash
25
+ ./utils/build-script --build-wasm-stdlib
26
+ ```
27
+
28
+ This command will build the Swift compiler for the host platform and then build the Swift standard library
29
+ for WebAssembly targets. The resulting Swift SDK ` .artifactbundle ` will be placed in the ` ../swift-sdk-generator/Bundles `
30
+ directory.
31
+
32
+ ## Building Swift SDK for WebAssembly without building the compiler
33
+
34
+ Building the Swift compiler is a time-consuming process. If you only want to build the Swift standard library
35
+ with pre-built Swift compiler, you can use the following command:
36
+
37
+ ``` console
38
+ $ SWIFT_TOOLS_PATH=path/to/swift-development-snapshot/usr/bin
39
+ $ ./utils/build-script \
40
+ --skip-build-llvm \
41
+ --skip-build-swift \
42
+ --skip-build-cmark \
43
+ --build-wasm-stdlib \
44
+ --native-swift-tools-path="$SWIFT_TOOLS_PATH" \
45
+ --native-clang-tools-path="$SWIFT_TOOLS_PATH" \
46
+ --native-llvm-tools-path="$SWIFT_TOOLS_PATH"
47
+ ```
48
+
49
+ ## Notes on the implementation
50
+
51
+ Here we're describing some decisions that were made while developing
19
52
the implementation.
20
53
21
- ## Relative Pointers
54
+ ### Relative Pointers
22
55
23
- Relative pointers are used in Swift runtime, but currently it's not feasible to use them for the WebAssembly
24
- target due to the design of WebAssembly and lack of LLVM support. If LLVM supported subtraction relocation
25
- type on WebAssembly like ` R_X86_64_PC32 ` or ` X86_64_RELOC_SUBTRACTOR ` , this issue can be solved easily.
56
+ Relative pointers are used in Swift runtime, but currently it's not feasible to use them for some cases
57
+ where the pointee is a function pointer. The reason is that WebAssembly has a separate address space for
58
+ functions and data, and the offset bwtween a function and a data pointer cannot be defined. Therefore,
59
+ we have to use absolute pointers for function pointers in WebAssembly (see ` include/swift/ABI/CompactFunctionPointer.h `
60
+ for more details).
26
61
27
- Since ` R_X86_64_PC32 ` and ` X86_64_RELOC_SUBTRACTOR ` are mainly used to generate PIC but WebAssembly doesn't
28
- require PIC because it doesn't support dynamic linking. In addition, the memory space also begins at 0, so
29
- it's unnecessary to relocate at load time. All absolute addresses can be embedded in wasm binary file directly.
0 commit comments