Wapm Packages Clang Save

Run Clang in WebAssembly 🎉

Project README


This package ships the full clang compiler and it's Wasm linker (wasm-ld), so we can compile C programs to WASI using just WebAssembly.

Note: This repo is extending the great work from Ben Smith published in his CppCon 2019 WebAssembly talk. The WebAssembly binaries are copied from Ben's fork of llvm-project.

Run clang!

In this example, we will pass our C program to clang via stdin. Then, we will run the Wasm linker to generate the final .wasm program.

# Run the compiler
echo 'int printf(const char *, ...); int main(){printf("hello world!\n");}' | wapm run clang -cc1 -triple wasm32-unknown-wasi -isysroot /sys -internal-isystem /sys/include -emit-obj -o ./example.o -

# Run the Wasm linker
wapm run wasm-ld -L/sys/lib/wasm32-wasi /sys/lib/wasm32-wasi/crt1.o ./example.o -lc -o ./example.wasm

And last, but not least... run it with a WebAssembly runtime!

wasmer example.wasm


When you run clang normally (eg. clang example.c -o ./example), it will spawn two different process under the hood:

  1. The compiler: clang -cc1 -triple wasm32-unknown-wasi -isysroot /sys -internal-isystem /sys/include -emit-obj -o ./example.o ./example.c
  2. The linker: wasm-ld -L/sys/lib/wasm32-wasi /sys/lib/wasm32-wasi/crt1.o ./example.o -lc -o ./example.wasm

However the posix_spawn required syscall is not available in WASI.

Because of that, we need to run this two different calls ourselves.

Open Source Agenda is not affiliated with "Wapm Packages Clang" Project. README Source: wapm-packages/clang
Open Issues
Last Commit
2 years ago

Open Source Agenda Badge

Open Source Agenda Rating