Vanya Agnesandra

Compile Veilid for WASM


Veilid | Veilid WASM

Testing Versions

Have you read The Dev Guide?

I can’t complain about learning the hard way - it’s a great way to learn - but before you follow my fallible foolish footsteps, consider skipping all this stuff with a simple and snappy setup.sh.

That being said, imo there is value in this, even if it’s academic.

Make sure you’ve Cloned the Repo with git submodules.

Install Dependencies

capnproto

The veilid-core build script checks capnp --version prior to compilation.

The capnp executable is written in c++, and is distributed via distro package managers. Install on:

CapnProto’s Install Docs are Awesome

wasm-bindgen

Generates javascript / typescript bindings to a wasm library’s public functions.

The wasm-bindgen executable is provided by wasm-bindgen-cli, which can be installed with cargo:

cargo install wasm-bindgen-cli

wasm-strip

Removes unnecessary sections to optimize wasm blobs for size.

The wasm-strip executable comes from the excellent WebAssembly Binary Toolkit “WABT” project, which is distributed via distro package managers as wabt. This comes with a number of other wasm tools. Install on:

Python 3 (Debug Only)

Used to generate the debug sourcemap using llvm-dwarfdump.

LLVM-DwarfDump (Debug Only)

Extracts embedded debug information. Used to generate sourcemaps for debug builds.

Part of the LLVM compiler toolset. Install on:

Build with the release profile

Note that this will embed your working directory and username into the compiled wasm.

/home/username/penname/projects/veilid/unreleasedprojectname/$environment/$user's_fork/veilid/

Use exactly “release“, case-sensitive.

/home/USER/.../veilid/veilid-wasm/wasm_build.sh release

Build for Release manually

Here are the steps I used to replicate what wasm_build.sh does - I have an article on Dissecting wasm-build.sh that goes into way too much detail:

cd /home/USER/.../veilid/veilid-wasm/
cargo build --target wasm32-unknown-unknown --release
WASMDIR=../target/wasm32-unknown-unknown/release
mkdir $WASMDIR/pkg
wasm-bindgen --out-dir $WASMDIR/pkg --target web $WASMDIR/veilid_wasm.wasm
wasm-strip $WASMDIR/pkg/veilid_wasm_bg.wasm

Build with the debug profile

Run wasm_build.sh with no parameters (or any - as long as it’s not release).

/home/USER/.../veilid/veilid-wasm/wasm_build.sh

Build for Debug manually

cd /home/USER/.../veilid/veilid-wasm/
RUSTFLAGS="-O -g" cargo build --target wasm32-unknown-unknown
WASMDIR=../target/wasm32-unknown-unknown/debug
mkdir $WASMDIR/pkg
wasm-bindgen --out-dir $WASMDIR/pkg --target web --keep-debug --debug $WASMDIR/veilid_wasm.wasm
python3 wasm-sourcemap.py $WASMDIR/pkg/veilid_wasm_bg.wasm -o $WASMDIR/pkg/veilid_wasm_bg.wasm.map --dwarfdump /usr/bin/llvm-dwarfdump

Sizes

Building with the release profile (and running wasm-strip) makes a huge difference:

Profile After Step Size
release cargo build 8.5MB
release wasm-bindgen 5.6MB
release wasm-strip 4.4MB
debug cargo build 197MB
debug wasm-bindgen 195MB