Skip to content

Commit 0696ab4

Browse files
committed
feat(chip-wasm-guide): add first draft of compiling custom chips to wasm
1 parent 0ead636 commit 0696ab4

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Compiling custom chips to WASM
3+
sidebar_label: Custom chips as WASM
4+
description: Compile your custom chips to WASM for use with VS Code or the Wokwi CLI
5+
keywords: [Visual Studio Code, VS Code, custom chip, compiling, WASM, web assembly, API, chips, chip, docker]
6+
---
7+
8+
Custom components written with the [Chips API](/chips-api/getting-started) can be compiled into WebAssembly (WASM) for
9+
use with the Wokwi VS Code extension or the Wokwi CLI.
10+
11+
The WASM binary can be compiled by GitHub Actions, a container, or locally. This guide will cover all three
12+
approaches.
13+
14+
The simplest way to get started is by cloning/forking the [inverter-chip](https://github.com/wokwi/inverter-chip)
15+
example repository from the Wokwi GitHub. You can then compile locally, use the dev container feature from VS
16+
Code, or push to GitHub to compile the WASM binary. This repository will also be referenced multiple times throughout
17+
this guide, so it is worth having a look at.
18+
19+
## Configure your project
20+
Before compiling your chip, you should make sure your project is configured properly to ensure that they can work in
21+
Wokwi.
22+
23+
### Wokwi configuration
24+
A custom chip requires a `[[chip]]` key in `wokwi.toml`. This key specifies the name and binary used by the custom chip.
25+
Wokwi also requires a JSON file that describes the pinout of the chip. This JSON file should have the same name as the
26+
WASM binary, but its extension changed to `.json` instead of `.wasm`, e.g. `inverter.chip.wasm` is paired with
27+
`inverter.chip.json`. To configure the chip JSON file properly, see the docs on [custom chip definitions](/docs/chips-api/chip-json.md).
28+
29+
An example excerpt from `wokwi.toml`:
30+
```toml
31+
[[chip]]
32+
name = 'inverter' # To use the chip in diagram.json, add a part with "chip-inverter" type.
33+
binary = 'chips/inverter.chip.wasm'
34+
```
35+
36+
Multiple chips can be added to the same project by adding multiple `[[chip]]` keys, each with different `name` and
37+
`binary` values.
38+
39+
### Source files
40+
:::note
41+
If you are cloning/forking the inverter chip repository, then you can skip this section.
42+
:::
43+
44+
If you are setting up your project from scratch, you need to ensure that the Wokwi C API is present within your source
45+
files. It is available from [wokwi.com/api/chips/wokwi-api.h](https://wokwi.com/api/chips/wokwi-api.h).
46+
47+
You need to ensure that it is included in your project, otherwise you will not be able to make use of the Chips API.
48+
49+
## Compilation methods
50+
51+
### GitHub Actions
52+
53+
If you have cloned the inverter chip repository, then it has been configured and is ready for you to modify it to your
54+
needs. It will automatically compile the binary once a push is made to the repository, and will also automatically
55+
generate a GitHub release if you push a tag that starts with a "v", e.g. `v1.0.3`.
56+
57+
If you did not clone the inverter chip repository and are wanting to configure your own actions, then consider looking
58+
at the [build workflow file](https://github.com/wokwi/inverter-chip/blob/ff72418a11d9d56621731ab3d373c952b1b508ea/.github/workflows/build.yaml).
59+
If you do not want to use this action, then see the steps for compiling locally.
60+
61+
62+
### VS Code dev container
63+
The inverter chip repository comes with a dev container configuration file that will automatically handle setting up a
64+
Docker container for you to build your binaries in. To make use of this, simply open the custom chip folder in a
65+
container by pressing <kbd>F1</kbd> and searching for "Dev Containers: Open Folder in Container...". Alternatively, you
66+
can click the icon bottom left of the VS Code window, and click on "Reopen in Container".
67+
68+
Once done, you should be presented with a terminal window where you can now run `make`. This will run the `Makefile` in
69+
the repository, and your generated WASM files should appear in `dist/`.
70+
71+
### Docker
72+
You will have to build the [wokwi-chip-clang-action](https://github.com/wokwi/wokwi-chip-clang-action) container to make
73+
use of it, as there currently is no image available online.
74+
75+
#### Create the image
76+
1. Clone the [wokwi-chip-clang-action](https://github.com/wokwi/wokwi-chip-clang-action) repository
77+
2. Navigate to it
78+
3. Run `docker build --tag "local/wokwi-chip-clang-action"`
79+
80+
Once the `docker build` command finishes, you will be able to reference the newly created image with
81+
`local/wokwi-chip-clang-action`.
82+
83+
#### Use the image
84+
1. Navigate to your project directory which contains the C source code to compile
85+
2. Create and enter a container with `docker run --rm -it -v $(pwd):/usr/src/project --entrypoint=/bin/sh local/wokwi-chip-clang-action`
86+
3. Your prompt should now have changed - navigate to `/usr/src/project`
87+
4. Compile the project with `make`
88+
89+
### Building locally
90+
91+
You will need to configure your environment to be able to build WASM assemblies locally. This part of the documentation
92+
is written with Ubuntu/Debian distros in mind, so you may have to adjust these instructions to your machine.
93+
94+
1. Install `wasi-libc` with `sudo apt install wasi-libc`
95+
2. Install the missing `libclang_rt.builtins-wasm32.a` library
96+
1. Download `libclang_rt-27.0.tar.gz` from the [wasi-sdk GitHub releases](https://github.com/WebAssembly/wasi-sdk/releases)
97+
2. Extract `libclang_rt-27.0/wasm32-unknown-wasi/libclang_rt.builtins.a` to `/usr/lib/llvm-???/lib/clang/???/lib/wasi/`
98+
:::info
99+
Replace the `???` in the path above with the currently installed LLVM version number on your system. Check by
100+
running `clang -v` and looking at the major release number.
101+
:::
102+
3. Rename the copied file from `libclang_rt.builtins.a` to `libclang_rt.builtins-wasm32.a`
103+
104+
You can now compile WASM binaries! To test, clone the inverter repository and run
105+
`clang --target=wasm32-unknown-wasi -nostartfiles -Wl,--import-memory -Wl,--export-table -Wl,--no-entry -Werror -o dist/chip.wasm src/main.c`.
106+
107+
You may have to update the `Makefile` to remove the `--sysroot` argument, as `wasi-libc` will be installed to a different
108+
directory than what `--sysroot` is set to.
109+
110+
If successful, you should now have `chip.wasm` under `dist/`!

0 commit comments

Comments
 (0)