Skip to content

Commit 9132802

Browse files
feat(lang/c): use wasm32-wasip2-clang binary directly (#289)
This commit updates the C code to use the included `wasm32-wasip2-clang` binary directly for building P2 components.
1 parent ff67f45 commit 9132802

File tree

1 file changed

+28
-5
lines changed
  • component-model/src/language-support

1 file changed

+28
-5
lines changed

component-model/src/language-support/c.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,30 @@ uint32_t exports_docs_adder_add_add(uint32_t x, uint32_t y)
5353
}
5454
```
5555
56-
## 4. Compile a WebAssembly module (P1) with `clang`
56+
## 4. Compile a WebAssembly P2 component with `wasi-sdk`'s `wasm32-wasip2-clang`
5757
58-
Now, you can compile the function into a Wasm module via clang:
58+
Given that `wasi-sdk` has ben installed, we can build a Preview 2 component quickly by using the provided `wasm32-wasip2-clang` binary:
59+
60+
```console
61+
/opt/wasi-sdk/bin/wasm32-wasip2-clang \
62+
-o adder.wasm \
63+
-mexec-model=reactor \
64+
component.c \
65+
adder.c \
66+
adder_component_type.o
67+
```
68+
69+
After this command completes, you will have a new file named `adder.wasm` available in the source folder.
70+
71+
For use cases that require building a P1 module and/or adapting an existing P1 module into a P2 module,
72+
such as buliding for a platform that does not support P2, details on a more manual approach can be found below:
73+
74+
<details>
75+
<summary>Manual P1 & P2 build using `wasi-sdk`'s `clang` and `wasm-tools`</summary>
76+
77+
## Build a WebAssembly module (P1) with `clang`
78+
79+
Compile the component code into a WebAssembly P1 module via clang:
5980

6081
```console
6182
clang component.c adder.c adder_component_type.o -o adder.wasm -mexec-model=reactor
@@ -77,7 +98,7 @@ clang component.c adder.c adder_component_type.o -o adder.wasm -mexec-model=reac
7798
[wasi-sdk-images]: https://github.com/WebAssembly/wasi-sdk/pkgs/container/wasi-sdk
7899
[wasi-sdk-dockerfile]: https://github.com/WebAssembly/wasi-sdk/blob/main/docker/Dockerfile
79100
80-
## 5. Convert the P1 component to a P2 component with `wasm-tools`
101+
### Convert the P1 component to a P2 component with `wasm-tools`
81102
82103
Next, we need to transform the P1 component to a P2 component. To do this, we can use `wasm-tools component new`:
83104
@@ -88,7 +109,7 @@ wasm-tools component new ./adder.wasm -o adder.component.wasm
88109
> [!NOTE]
89110
> The `.component.` extension has no special meaning -- `.wasm` files can be either modules or components.
90111
91-
## 6. (optional) Build a WASI-enabled WebAssembly (P2) component with `wasm-tools`
112+
### (optional) Build a WASI-enabled WebAssembly (P2) component with `wasm-tools`
92113

93114
Do note `wasm-tools component new` may fail if your code references any [WASI][wasi] APIs that must be imported, for
94115
example via standard library imports like `stdio.h`.
@@ -135,7 +156,9 @@ wasm-tools component new adder.wasm --adapt wasi_snapshot_preview1.wasm -o adder
135156

136157
[wasmtime-releases]: https://github.com/bytecodealliance/wasmtime/releases
137158

138-
## 7. Inspect the built component
159+
</details>
160+
161+
## 5. Inspect the built component
139162

140163
Finally, you can inspect the embedded wit to see your component (including any WASI imports if necessary):
141164

0 commit comments

Comments
 (0)