Forewarning: this can be a bit painful and may not work as expected. I've already had issues with even including stdlib through clang.
Make sure you have git installed and properly configured before continuing. This is trivial on Windows these days (https://git-scm.com/download/win) but is required to pull down Binaryen and Wabt.
Make sure to add the binary to your PATH variable in Windows.
You will first need to install a Subversion client for Windows. This will be used to clone the LLVM and Clang repositories that contain experimental WebAssembly support. I recommend Win32SVN. However, you can install a variety of clients as long as they support the usual command line syntax.
- Win32SVN: https://sourceforge.net/projects/win32svn/
- TortiseSVN: https://sourceforge.net/projects/tortoisesvn/
- SilkSVN: https://sliksvn.com/download/
Make sure to add the binary to your PATH variable in Windows.
To configure LLVM, Binaryen, and Wabt to build you will need CMake. Note: do not use the Cygwin version of this tool as it will lack platform support for Visual Studio.
Make sure to add the binary to your PATH variable in Windows.
To build LLVM, Binaryen, and Wabt you will need to install Visual Studio with support for building C\C++ applications and be sure to include the optional packages which include standard libraries, etc.
rem locations, e.g.
mkdir c:\llvm
rem checkout LLVM
cd c:\llvm
svn co http://llvm.org/svn/llvm-project/llvm/trunk source
rem checkout clang
cd c:\llvm\tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
rem build folder
cd c:\llvm
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=c:\llvm -DLLVM_TARGETS_TO_BUILD= -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly c:\llvm\source
rem compile (or open the LLVM.sln file in Visual Studio and build using the ALL_BUILD target)
cmake --build --target ALL_BUILD c:\llvm\source
Add "c:\llvm\build\Debug\bin" to your PATH to gain access to "clang.exe" and "llc.exe" from the command line.
cd c:\
git clone [email protected]:WebAssembly/binaryen.git
cd binaryen
cmake --build .
Add "c:\binaryen\bin" to your PATH to gain access to the "s2wasm.exe" from the command line.
cd c:\
git clone [email protected]:WebAssembly/wabt.git
cd wabt
cmake --build .
Add "c:\wabt\Debug" to your PATH to gain access to the "wast2wasm.exe" from the command line.
Let's create "fib.c" and compile it, first to LLVM bitcode, then to WebAssembly-like file. (Notice -O2 or above near clang to avoid failure during llc
run.)
clang -emit-llvm --target=wasm32 -Oz fib.c -c -o fib.bc
llc -asm-verbose=false -o fib.s fib.bc
See output at "fib.s".
Convert to WebAssembly s-expressions:
s2wasm fib.s > fib.wast
See output at "fib.wast".
Convert to WebAssembly:
wast2wasm fib.wast -o fib.wasm
See output at "fib.wasm".
Note: a "compile.bat" script has been included to repeat these steps automatically. See below.
To run this example, see: https://developer.mozilla.org/en-US/docs/WebAssembly/Loading_and_running
Built from the original guide made by yurydelendik at (https://gist.github.com/yurydelendik/4eeff8248aeb14ce763e).
wast2wasm has been renamed to wat2wasm as of this wabt commit.