Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import clang kickstart guide #132

Merged
merged 5 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ One of the [design goals](https://github.com/ewasm/design/blob/master/rationale.

At present, we've developed support for the following languages and toolchains:

- LLVM: C, C++, and Rust: documentation pending
- [C/C++ (LLVM) WebAssembly tutorial](./clang.md)
- Rust: documentation pending
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a PR open for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. Want to merge that too.

- [AssemblyScript](https://github.com/AssemblyScript/assemblyscript), a subset of TypeScript, which uses the JavaScript toolchain: see the [etherts org](https://github.com/etherts/docs) for more information on writing contracts in AssemblyScript.

If you're interested in adding support for another language, framework, or toolset, see the Contributing section above and reach out.
Expand Down
54 changes: 54 additions & 0 deletions clang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Compiling C/C++ to WebAssembly

## Rolling your own compiler

Clang has a WebAssembly target, though it is not easy to use currently. First, a custom build must be made.

To build `clang`:
```sh
git clone http://llvm.org/git/llvm.git
cd llvm/tools
git clone http://llvm.org/git/clang.git
cd ../projects
git clone http://llvm.org/git/compiler-rt.git
mkdir ../build
cd ../build
cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= ..
cmake --build .
cd ../..
```

This will take anything from 1 to 5 hours.

To build `binaryen`:
```sh
git clone https://github.com/WebAssembly/binaryen.git
cd binaryen
mkdir build
cd build
cmake ..
cmake --build .
cd ../..
```

## Using this compiler

Now everything is set to finally compile *Hello World*!

The compilation process has four steps:
- compiling (`clang`)
- linking to LLVM IR (`llc`)
- compiling to WebAssembly S-expressions (`s2wasm`)
- compiling to WebAssembly binary (`wasm-as`)

Note: the last step can also be accomplished with [wabt](https://github.com/webassembly/wabt) (previously called *sexpr-wasm-prototype*).

Cheat sheet:
```sh
clang -emit-llvm --target=wasm32-unknown-unknown-elf -nostdlib -S hello.c
llc -o hello.s hello.ll
s2wasm -o hello.wast hello.s
wasm-as -o hello.wasm hello.wast
```

There you go, you have your very first WebAssembly binary.