Skip to content

Commit 6928e63

Browse files
Suggested updates to proof of concept C host documentation
- Fills in documentation on how to use the C host - Updates Dockerfile to be cross architecture - Adds a new Dockerfile that is just the host Signed-off-by: Kate Goldenring <kgoldenr@akamai.com>
1 parent aa3e99d commit 6928e63

File tree

4 files changed

+148
-38
lines changed

4 files changed

+148
-38
lines changed

component-model/examples/example-c-host/Dockerfile renamed to component-model/examples/example-c-host/Dockerfile.guest_and_host

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@ FROM ubuntu:24.04 AS build
22

33
ARG WASMTIME_VERSION=42.0.1
44
ARG WASI_SDK_VERSION=27
5+
ARG TARGETARCH
56

67
RUN apt-get update && apt-get install -y --no-install-recommends \
78
gcc libc6-dev curl xz-utils ca-certificates \
89
&& rm -rf /var/lib/apt/lists/*
910

1011
# Install wasmtime C API
11-
RUN curl -sL https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasmtime-v${WASMTIME_VERSION}-x86_64-linux-c-api.tar.xz \
12-
| tar xJ --strip-components=1 -C /usr/local \
13-
&& ldconfig
12+
RUN set -eux; \
13+
case "${TARGETARCH}" in \
14+
amd64) WASMTIME_ARCH=x86_64 ;; \
15+
arm64) WASMTIME_ARCH=aarch64 ;; \
16+
*) echo "Unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
17+
esac; \
18+
curl -sL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasmtime-v${WASMTIME_VERSION}-${WASMTIME_ARCH}-linux-c-api.tar.xz" \
19+
| tar xJ --strip-components=1 -C /usr/local; \
20+
ldconfig
1421

1522
# Install wasi-sdk (for building the guest component)
16-
RUN curl -sLO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION}.0-x86_64-linux.deb \
17-
&& dpkg -i wasi-sdk-${WASI_SDK_VERSION}.0-x86_64-linux.deb \
18-
&& rm wasi-sdk-${WASI_SDK_VERSION}.0-x86_64-linux.deb
23+
RUN set -eux; \
24+
case "${TARGETARCH}" in \
25+
amd64) WASI_SDK_ARCH=amd64 ;; \
26+
arm64) WASI_SDK_ARCH=arm64 ;; \
27+
*) echo "Unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
28+
esac; \
29+
curl -sLO "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION}.0-${WASI_SDK_ARCH}-linux.deb"; \
30+
dpkg -i "wasi-sdk-${WASI_SDK_VERSION}.0-${WASI_SDK_ARCH}-linux.deb"; \
31+
rm "wasi-sdk-${WASI_SDK_VERSION}.0-${WASI_SDK_ARCH}-linux.deb"
1932

2033
# Install wit-bindgen and wasm-tools (pin versions)
2134
ARG WIT_BINDGEN_VERSION=0.53.1
2235
ARG WASM_TOOLS_VERSION=1.245.1
23-
RUN curl -sL https://github.com/bytecodealliance/wit-bindgen/releases/download/v${WIT_BINDGEN_VERSION}/wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux.tar.gz \
24-
| tar xz --strip-components=1 -C /usr/local/bin --wildcards '*/wit-bindgen' \
25-
&& curl -sL https://github.com/bytecodealliance/wasm-tools/releases/download/v${WASM_TOOLS_VERSION}/wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux.tar.gz \
36+
RUN set -eux; \
37+
case "${TARGETARCH}" in \
38+
amd64) TOOL_ARCH=x86_64 ;; \
39+
arm64) TOOL_ARCH=aarch64 ;; \
40+
*) echo "Unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
41+
esac; \
42+
curl -sL "https://github.com/bytecodealliance/wit-bindgen/releases/download/v${WIT_BINDGEN_VERSION}/wit-bindgen-${WIT_BINDGEN_VERSION}-${TOOL_ARCH}-linux.tar.gz" \
43+
| tar xz --strip-components=1 -C /usr/local/bin --wildcards '*/wit-bindgen'; \
44+
curl -sL "https://github.com/bytecodealliance/wasm-tools/releases/download/v${WASM_TOOLS_VERSION}/wasm-tools-${WASM_TOOLS_VERSION}-${TOOL_ARCH}-linux.tar.gz" \
2645
| tar xz --strip-components=1 -C /usr/local/bin --wildcards '*/wasm-tools'
2746

2847
WORKDIR /src
@@ -55,4 +74,4 @@ COPY --from=build /usr/local/bin/adder-host /usr/local/bin/adder-host
5574
COPY --from=build /src/adder.wasm /opt/adder.wasm
5675

5776
ENTRYPOINT ["adder-host"]
58-
CMD ["1", "2", "/opt/adder.wasm"]
77+
CMD ["1", "2", "/opt/adder.wasm"]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM ubuntu:24.04 AS build
2+
3+
ARG WASMTIME_VERSION=42.0.1
4+
ARG TARGETARCH
5+
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
gcc libc6-dev curl xz-utils ca-certificates \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
# Install wasmtime C API
11+
RUN set -eux; \
12+
case "${TARGETARCH}" in \
13+
amd64) WASMTIME_ARCH=x86_64 ;; \
14+
arm64) WASMTIME_ARCH=aarch64 ;; \
15+
*) echo "Unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
16+
esac; \
17+
curl -sL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasmtime-v${WASMTIME_VERSION}-${WASMTIME_ARCH}-linux-c-api.tar.xz" \
18+
| tar xJ --strip-components=1 -C /usr/local; \
19+
ldconfig
20+
21+
WORKDIR /src
22+
23+
# Build the host
24+
COPY host.c .
25+
RUN gcc -o /usr/local/bin/adder-host host.c -lwasmtime
26+
27+
# Runtime image
28+
FROM ubuntu:24.04
29+
30+
COPY --from=build /usr/local/lib/libwasmtime.so /usr/local/lib/
31+
RUN ldconfig
32+
COPY --from=build /usr/local/bin/adder-host /usr/local/bin/adder-host
33+
34+
ENTRYPOINT ["adder-host"]
35+
CMD ["1", "2", "/component/add.wasm"]

component-model/examples/example-c-host/justfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

component-model/src/language-support/building-a-simple-component/c.md

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ with the ability to add more language generators in the future.
2929
[wasi]: https://wasi.dev/
3030
[rust]: https://www.rust-lang.org/learn/get-started
3131
[sample-wit]: https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/tutorial/wit/adder/world.wit
32-
[cargo-config]: https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/example-host/Cargo.toml
3332

3433
## 1. Download dependencies
3534

@@ -303,39 +302,98 @@ world root {
303302
...
304303
```
305304

306-
### 6. Run the component from the example host
305+
## 6. Run the component from the example C host
307306

308-
The following section requires you to have [a Rust toolchain][rust] installed.
307+
This repository includes a C application that can execute components that implement the add interface. This application embeds Wasmtime using the Wasmtime C API:
308+
`component-model/examples/example-c-host/host.c`.
309309

310-
> [!WARNING]
311-
> You must be careful to use a version of the adapter (`wasi_snapshot_preview1.wasm`)
312-
> that is compatible with the version of `wasmtime` that will be used,
313-
> to ensure that WASI interface versions (and relevant implementation) match.
314-
> (The `wasmtime` version is specified in [the Cargo configuration file][cargo-config]
315-
> for the example host.)
310+
The application expects three arguments: the two numbers to add and the Wasm component that executed the addition. For example:
316311

317-
{{#include ../example-host-part1.md}}
312+
```sh
313+
./adder-host <x> <y> <path-to-component.wasm>
314+
```
315+
316+
You can either use a Dockerfile to execute your add component with the C application or directly run the application.
317+
318+
### Option A: Compile and run the host directly
319+
320+
If the Wasmtime C API headers and library are installed on your system,
321+
you can compile and run the host directly:
318322

319-
A successful run should show the following output
320-
(of course, the paths to your example host and adder component will vary,
321-
and you should substitute `adder.wasm` with `adder.component.wasm`
322-
if you followed the manual instructions above):
323+
On Linux, the following commands install the C API artifacts in `/usr/local`
324+
using the same approach as `Dockerfile.host`:
323325

324-
{{#include ../example-host-part2.md}}
326+
```console
327+
sudo apt-get update
328+
sudo apt-get install -y --no-install-recommends \
329+
gcc libc6-dev curl xz-utils ca-certificates
325330

326-
## 7. Run the component from the example C host
331+
WASMTIME_VERSION=42.0.1
332+
case "$(uname -m)" in
333+
x86_64) WASMTIME_ARCH=x86_64 ;;
334+
aarch64|arm64) WASMTIME_ARCH=aarch64 ;;
335+
*) echo "unsupported architecture: $(uname -m)" >&2; exit 1 ;;
336+
esac
327337

328-
After setting up the wasmtime toolchain
329-
*TODO: somehow,*
330-
the `example-c-host` project can be built, and run our `adder` much like the rust one.
338+
curl -sL "https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasmtime-v${WASMTIME_VERSION}-${WASMTIME_ARCH}-linux-c-api.tar.xz" \
339+
| sudo tar xJ --strip-components=1 -C /usr/local
331340

332-
*TODO: the C++ API is nicer and may be more appropriate for example code*
341+
sudo ldconfig
342+
```
333343

334-
C/C++ language guest components can also be composed with components written in any other language
335-
and run by their toolchains, or even composed with a C language command component and run via the
336-
`wasmtime` CLI or any other host.
344+
```console
345+
cd component-model/examples/example-c-host
346+
gcc -o adder-host host.c -lwasmtime
347+
./adder-host 1 2 /absolute/path/to/adder.wasm
348+
```
349+
350+
If `libwasmtime.so` is not in a default library path on Linux,
351+
set `LD_LIBRARY_PATH` before running:
352+
353+
```console
354+
LD_LIBRARY_PATH=/path/to/wasmtime/lib ./adder-host 1 2 /absolute/path/to/adder.wasm
355+
```
356+
357+
Expected output:
358+
359+
```sh
360+
1 + 2 = 3
361+
```
362+
363+
### Option B: Run with Docker (`Dockerfile.host`)
364+
365+
Instead of installing the Wasmtime C API, you can use the provided Dockerfile which builds the C application.
366+
367+
From `component-model/examples/example-c-host`:
368+
369+
```console
370+
cp ../../examples/example-host/add.wasm ./add.wasm
371+
372+
docker build \
373+
-f Dockerfile.host \
374+
-t example-c-host:latest \
375+
.
376+
```
377+
378+
Then run the container, passing in the component as a volume.
379+
380+
```console
381+
docker run --rm \
382+
-v /absolute/path/to/component-docs/component-model/examples/example-c-host/adder.wasm:/component/add.wasm:ro \
383+
example-c-host:latest 1 2 /component/add.wasm
384+
```
385+
386+
Expected output:
387+
388+
```sh
389+
1 + 2 = 3
390+
```
337391

338-
*TODO: work in `wasmtime --invoke 'add(2,2)' adder.wasm`*
392+
`Dockerfile.guest_and_host` is also provided in the same directory if you want
393+
an all-in-one image that builds both the guest component and the C host.
339394

340-
[!NOTE]: #
341-
[!WARNING]: #
395+
Its guest build path follows the same sequence described in steps 2-4:
396+
`wit-bindgen c ...`, then `/opt/wasi-sdk/bin/wasm32-wasip2-clang ...`.
397+
The Dockerfile additionally automates fetching `world.wit` and `component.c`
398+
from this repository and pins tool versions for reproducibility.
399+
At the time of writing, `Dockerfile.guest_and_host` is Linux `x86_64`-specific.

0 commit comments

Comments
 (0)