[RFC] Building LLVM for WebAssembly

The @yowasp/clang package is finally released. (The final stretch of this involved 14 hours straight of babysitting the build. Not an exaggeration; a continuous 14 hour interval during which my primary objective was to get the CI to pass. What can I say, I really wanted to get it done today…)

Try this in Deno (install @yowasp/clang manually and drop the npm: prefix for Node):

import { runClang } from 'npm:@yowasp/clang';
import { writeFile } from 'node:fs/promises';
await runClang(['clang', '--version']);
const { 'a.out': wasm } = await runClang(['clang', 'test.c'], {
    'test.c': '#include <stdio.h>\nint main() { puts("hello, world!"); }'});
await writeFile('a.out', wasm);

This produces a wasip1 module runnable with e.g. Wasmtime:

$ deno -A demo.js 
yowasp-llvm: fetched 0% (0 / 102337718)
yowasp-llvm: fetched 29% (29603840 / 102337718)
yowasp-llvm: fetched 100% (102293847 / 102337718)
yowasp-llvm: fetched 100% (102331473 / 102337718)
yowasp-llvm: fetched 100% (102336931 / 102337718)
yowasp-llvm: fetched 100% (102337718 / 102337718)
clang version 21.1.4 (https://github.com/YoWASP/llvm-project c37f6c36d202f1454181a98a68fda9fb44b45671)
Target: wasm32-unknown-wasip1
Thread model: posix
InstalledDir: 
Build config: +assertions
$ wasmtime a.out 
hello, world!

The next step is upstreaming the patchset. (Help with this is greatly appreciated…)

8 Likes