[RFC] Building LLVM for WebAssembly

OK, for everyone who wants to try this out, I’ve added a horrifically cursed “compiler driver driver” that makes the Wasm Clang look like any other Clang by intercepting the output of -### and redirecting the (impossible in the Wasm sandbox) linker subprocess call. To use it, open a shell on a Linux machine with CMake, cURL, git, Python 3, and Wasmtime installed, and run:

$ git clone https://github.com/YoWASP/clang yowasp-clang
$ cd yowasp-clang
$ ./build.sh
... wait about one hour...

Once this is all done, you should be able to have the following test succeed:

$ cat >test.cc <<END
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
}
END
$ ./wasi-prefix/bin/clang++ test.cc -o test.wasm
$ wasmtime test.wasm
Hello, world!

Note that the “compiler driver driver” (what’s installed as wasi-prefix/bin/clang and wasi-prefix/bin/clang++) is a truly vile Python script I wrote in half a hour and while it should work reasonably well, it may also break in some funny way I’m not responsible for. It should not be in any way dangerous (you can read the entire thing), just kind of fragile, don’t expect it to survive being fed to some random ./configure (although you may try, I’m curious).

2 Likes