From 62dcd61eec775dfcc681fa25f317226f7ef4bb2c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Dec 2018 20:24:29 +0000 Subject: [PATCH 1/5] Import clang kickstart guide From https://gist.github.com/axic/c369b413ae782362f52c6b42bc1e3d7f --- clang.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 clang.md diff --git a/clang.md b/clang.md new file mode 100644 index 0000000..d64dc48 --- /dev/null +++ b/clang.md @@ -0,0 +1,67 @@ +# WebAssembly kickstart guide + +## Rolling your own compiler + +One of the premises of WebAssembly is to support compiling multiple languages to WebAssembly. + +### C/C++ + +#### Building + +Clang has a WebAssembly target, though it is not easy to use currently. First, a custom build must be made. + +To build `clang`: +``` +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 -G Ninja -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. +ninja +cd ../.. +``` + +Also possible to build using POSIX make: +``` +cmake -G "Unix Makefiles" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. +make -j 4 +``` + +This will take anything from 1 to 5 hours. + +To build `binaryen`: +``` +git clone https://github.com/WebAssembly/binaryen.git +cd binaryen +mkdir build +cd build +cmake -G Ninja .. +ninja +cd ../.. +``` + +#### Using + +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: +``` +clang -emit-llvm --target=wasm32 -nostdlib -S hello.c +llc -march=wasm32 -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. + From a5d6d6ae9e0977aea81c61f9b1dde3d1eca8c25c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Dec 2018 20:27:25 +0000 Subject: [PATCH 2/5] Do not use ninja/make --- clang.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/clang.md b/clang.md index d64dc48..1b83986 100644 --- a/clang.md +++ b/clang.md @@ -19,17 +19,11 @@ cd ../projects git clone http://llvm.org/git/compiler-rt.git mkdir ../build cd ../build -cmake -G Ninja -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. -ninja +cmake -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. +cmake --build . cd ../.. ``` -Also possible to build using POSIX make: -``` -cmake -G "Unix Makefiles" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD= .. -make -j 4 -``` - This will take anything from 1 to 5 hours. To build `binaryen`: @@ -38,8 +32,8 @@ git clone https://github.com/WebAssembly/binaryen.git cd binaryen mkdir build cd build -cmake -G Ninja .. -ninja +cmake .. +cmake --build . cd ../.. ``` From 615ac485a0fe2effd09ba315223ad39455a67c9c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Dec 2018 20:28:12 +0000 Subject: [PATCH 3/5] Link to clang from README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c94098..97358aa 100644 --- a/README.md +++ b/README.md @@ -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 - [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. From 05d7a39f40baea40842b6ed9b31c68ec9c23054f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Dec 2018 20:32:18 +0000 Subject: [PATCH 4/5] Use the proper clang triple Comment from @jakelang --- clang.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang.md b/clang.md index 1b83986..4f2eaf3 100644 --- a/clang.md +++ b/clang.md @@ -11,7 +11,7 @@ One of the premises of WebAssembly is to support compiling multiple languages to 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 @@ -27,7 +27,7 @@ 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 @@ -50,9 +50,9 @@ The compilation process has four steps: Note: the last step can also be accomplished with [wabt](https://github.com/webassembly/wabt) (previously called *sexpr-wasm-prototype*). Cheat sheet: -``` -clang -emit-llvm --target=wasm32 -nostdlib -S hello.c -llc -march=wasm32 -o hello.s hello.ll +```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 ``` From f4475631f48d8ce36454c27f031c134354e99abf Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Dec 2018 20:34:32 +0000 Subject: [PATCH 5/5] Clarify the layout of clang --- clang.md | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/clang.md b/clang.md index 4f2eaf3..ded7333 100644 --- a/clang.md +++ b/clang.md @@ -1,13 +1,7 @@ -# WebAssembly kickstart guide +# Compiling C/C++ to WebAssembly ## Rolling your own compiler -One of the premises of WebAssembly is to support compiling multiple languages to WebAssembly. - -### C/C++ - -#### Building - Clang has a WebAssembly target, though it is not easy to use currently. First, a custom build must be made. To build `clang`: @@ -37,7 +31,7 @@ cmake --build . cd ../.. ``` -#### Using +## Using this compiler Now everything is set to finally compile *Hello World*! @@ -58,4 +52,3 @@ wasm-as -o hello.wasm hello.wast ``` There you go, you have your very first WebAssembly binary. -