Skip to content

Commit

Permalink
Plugin Wasmtime: Docs for C & C++ plugins
Browse files Browse the repository at this point in the history
- Add documentation for writing C and C++ plugins to run with rust hosts
  using wasmtime component-model
  • Loading branch information
AmmarAbouZor committed Jun 10, 2024
1 parent 7259db0 commit d5b7a5a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions developer/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Advantages](plugin/wasmtime/advantages.md)
- [Disadvantages](plugin/wasmtime/disadvantages.md)
- [Notes & Tips](plugin/wasmtime/notes-tips.md)
- [Multi-Language Plugins](plugin/wasmtime/multi-language.md)
- [Plugin Proposal](plugin/wasmtime/proposal.md)
- [Parser](plugin/wasmtime/proposal-parser.md)
- [Source](plugin/wasmtime/proposal-source.md)
Expand Down
62 changes: 62 additions & 0 deletions developer/src/plugin/wasmtime/multi-language.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Multi-Language Plugins

With WASM, it's possible to write plugins in any language that compiles to WebAssembly and supports the component model.
Many languages support WebAssembly and can be compiled to WASM modules, then with tools like [wasm-tools](https://github.com/bytecodealliance/wasm-tools), it can be possible to create components from them, producing WASM-components files that can be loaded and validated by `wasmtime` host.

## C & C++

Writing plugins in C or C++ can be done using the following steps and tools:

### 1. Code Generation & Bindgen

[wit-bindgen CLI tool](https://github.com/bytecodealliance/wit-bindgen#cli-installation) can be used to generate all functions and types form a `wit` file with the command:

```bash
wit-bindgen c {PATH_TO_WIT_FILE}
# Generating "host.c"
# Generating "host.h"
# Generating "host_component_type.o"
```

This command will generate `*.h`, `*.c`, and `*.o` files from the `wit` file, which can be included in C or C++ code.
For example, we created the file `my-component.c` and included the host header files in it to use the types and functions from the host, providing the needed functions from the plugin:

```c
// Generated header from wit-bindgen command
#include "host.h"

void host_run() {
// Calling function on the host
host_print(&my_string);
//...
}
```

### 2. Compiling as WASM Component

C and C++ code can be compiled for the wasm32-wasi target using the [WASI SDK](https://github.com/webassembly/wasi-sdk) project.
`WASI SDK` provides `clang` binaries in their releases, which are pre-configured to compile to WebAssembly. These binaries can be downloaded from their release pages and used directly to compile:

```bash
# The `clang` command here should be the binary from `WASI SDK` releases.
clang host.c host_component_type.o my-component.c -o my-core.wasm -mexec-model=reactor
```

This command will create the file `my-core.wasm`, which we need to convert to a WASM component using the [wasm-tools CLI](https://github.com/bytecodealliance/wasm-tools) to create a new WASM component based on the created WASM module:

```bash
wasm-tools component new ./my-core.wasm -o my-component.wasm
```

We can use `wasm-tools` to inspect the output binary:
```bash
wasm-tools component wit ./my-component.wasm
```

The created `my-component.wasm` file can be used and validate by a `wasmtime` Rust host.

### References & Links:

- [wasi-sdk](https://github.com/webassembly/wasi-sdk).
- [wit-bindgen](https://github.com/bytecodealliance/wit-bindgen?tab=readme-ov-file#guest-cc).
- [wasm-tools](https://github.com/bytecodealliance/wasm-tools).

0 comments on commit d5b7a5a

Please sign in to comment.