|
1 | 1 | # Compiling C/C++ to WebAssembly
|
2 | 2 |
|
3 |
| -## Rolling your own compiler |
| 3 | +Many high level languages already support compilation to WebAssembly |
| 4 | +through the experimental LLVM backend. Unfortunately, it is a tedious |
| 5 | +and poorly documented process. This document aims to alleviate some |
| 6 | +of this tedium with a step-by-step tutorial to compile some basic C |
| 7 | +code to WAST format using LLVM, as well as provide instructions for building |
| 8 | +the toolchain. |
4 | 9 |
|
5 |
| -Clang has a WebAssembly target, though it is not easy to use currently. First, a custom build must be made. |
| 10 | +## Dependencies |
| 11 | + |
| 12 | +- LLVM + Clang: Must be built with the experimental WebAssebmly backend enabled |
| 13 | +- Binaryen: Needed to convert the `.s` output of LLVM's backend to WAST |
| 14 | + |
| 15 | +## Install LLVM and Clang with the WebAssembly backend |
| 16 | + |
| 17 | +### From the repo |
| 18 | + |
| 19 | +First, clone the needed repositories: |
6 | 20 |
|
7 |
| -To build `clang`: |
8 | 21 | ```sh
|
9 | 22 | git clone http://llvm.org/git/llvm.git
|
10 | 23 | cd llvm/tools
|
11 | 24 | git clone http://llvm.org/git/clang.git
|
12 | 25 | cd ../projects
|
13 | 26 | git clone http://llvm.org/git/compiler-rt.git
|
| 27 | + |
| 28 | +Then initialize CMake: |
| 29 | + |
| 30 | +```sh |
14 | 31 | mkdir ../build
|
15 | 32 | cd ../build
|
16 | 33 | cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..
|
| 34 | +``` |
| 35 | + |
| 36 | +Lastly, call `make` as usual: |
| 37 | + |
| 38 | +```sh |
17 | 39 | cmake --build .
|
18 |
| -cd ../.. |
19 | 40 | ```
|
20 | 41 |
|
21 |
| -This will take anything from 1 to 5 hours. |
| 42 | +At the end of the (long) build the binaries will be in `bin`. Feel free to add that to your `PATH` for ease of use. |
| 43 | + |
| 44 | +## Install Binaryen |
| 45 | + |
| 46 | +This one is much easier. Simply: |
22 | 47 |
|
23 |
| -To build `binaryen`: |
24 | 48 | ```sh
|
25 | 49 | git clone https://github.com/WebAssembly/binaryen.git
|
26 | 50 | cd binaryen
|
27 | 51 | mkdir build
|
28 | 52 | cd build
|
29 | 53 | cmake ..
|
30 | 54 | cmake --build .
|
31 |
| -cd ../.. |
32 | 55 | ```
|
33 | 56 |
|
34 |
| -## Using this compiler |
| 57 | +CMake will also generate an `install` target if you want to actually install Binaryen on your system. |
35 | 58 |
|
36 |
| -Now everything is set to finally compile *Hello World*! |
| 59 | +## Compile C/C++ to WebAssembly |
37 | 60 |
|
38 |
| -The compilation process has four steps: |
39 |
| -- compiling (`clang`) |
40 |
| -- linking to LLVM IR (`llc`) |
41 |
| -- compiling to WebAssembly S-expressions (`s2wasm`) |
42 |
| -- compiling to WebAssembly binary (`wasm-as`) |
| 61 | +First we must compile C to LLVM bitcode through the Clang frontend: |
43 | 62 |
|
44 |
| -Note: the last step can also be accomplished with [wabt](https://github.com/webassembly/wabt) (previously called *sexpr-wasm-prototype*). |
| 63 | +`clang -emit-llvm --target=wasm32-unknown-unknown-elf -c -o source.bc source.c` |
45 | 64 |
|
46 |
| -Cheat sheet: |
47 |
| -```sh |
48 |
| -clang -emit-llvm --target=wasm32-unknown-unknown-elf -nostdlib -S hello.c |
49 |
| -llc -o hello.s hello.ll |
50 |
| -s2wasm -o hello.wast hello.s |
51 |
| -wasm-as -o hello.wasm hello.wast |
52 |
| -``` |
| 65 | +Next we can generate linear WASM output from the bitcode: |
| 66 | + |
| 67 | +`llc -asm-verbose=false -o main.s main.bc` |
| 68 | + |
| 69 | +The backend output is in linear WASM format so we must convert this to WAST with binaryen's `s2wasm` tool: |
| 70 | +
|
| 71 | +`s2wasm -o main.wast main.s` |
53 | 72 |
|
54 |
| -There you go, you have your very first WebAssembly binary. |
| 73 | +The code will now be in WAST format but must be cleaned up with `ewasm-cleanup` to be deployed as a contract. |
0 commit comments