Skip to content

Commit 4d72864

Browse files
jakelangaxic
authored andcommitted
Improve clang documentation
1 parent 0474802 commit 4d72864

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

clang.md

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,73 @@
11
# Compiling C/C++ to WebAssembly
22

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.
49

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:
620

7-
To build `clang`:
821
```sh
922
git clone http://llvm.org/git/llvm.git
1023
cd llvm/tools
1124
git clone http://llvm.org/git/clang.git
1225
cd ../projects
1326
git clone http://llvm.org/git/compiler-rt.git
27+
28+
Then initialize CMake:
29+
30+
```sh
1431
mkdir ../build
1532
cd ../build
1633
cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..
34+
```
35+
36+
Lastly, call `make` as usual:
37+
38+
```sh
1739
cmake --build .
18-
cd ../..
1940
```
2041

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:
2247

23-
To build `binaryen`:
2448
```sh
2549
git clone https://github.com/WebAssembly/binaryen.git
2650
cd binaryen
2751
mkdir build
2852
cd build
2953
cmake ..
3054
cmake --build .
31-
cd ../..
3255
```
3356

34-
## Using this compiler
57+
CMake will also generate an `install` target if you want to actually install Binaryen on your system.
3558

36-
Now everything is set to finally compile *Hello World*!
59+
## Compile C/C++ to WebAssembly
3760

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:
4362

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`
4564

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`
5372
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

Comments
 (0)