This document describes how to build the basisu command-line tool as a WASI (WebAssembly System Interface) executable, and how to run it using Wasmtime.
WASI builds run the encoder inside a secure, portable WebAssembly sandbox with no native dependencies.
Install Wasmtime using the official installer:
curl https://wasmtime.dev/install.sh | bash
Verify:
wasmtime --version
Download the latest WASI SDK from:
https://github.com/WebAssembly/wasi-sdk/releases/latest https://github.com/WebAssembly/wasi-sdk/releases
Example (adjust version if needed):
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-29/wasi-sdk-29.0-x86_64-linux.tar.gz tar xf wasi-sdk-29.0-x86_64-linux.tar.gz
You must set the path so CMake can find the WASI compiler:
export WASI_SDK_PATH=/path/to/wasi-sdk-29.0-x86_64-linux
Example:
export WASI_SDK_PATH=$HOME/wasi-sdk-29.0-x86_64-linux
Verify:
$WASI_SDK_PATH/bin/clang --version
WASI builds come in two modes:
Create a fresh build directory and configure using the WASI toolchain file:
mkdir build cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake -DCMAKE_BUILD_TYPE=Release -DBASISU_WASM_THREADING=ON
Or for a single threaded build (will run much slower):
cmake .. -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk.cmake -DCMAKE_BUILD_TYPE=Release -DBASISU_WASM_THREADING=OFF
.wasm executableBuild using:
make
This produces:
bin/basisu.wasm bin/examples.wasm (if EXAMPLES=ON)
basisu.wasm with WasmtimeWASI programs are sandboxed and cannot access your filesystem unless you explicitly grant permission.
Use one or more --dir= arguments to allow input/output files.
bin$ wasmtime run --wasm threads=yes --wasi threads=yes --dir=. --dir=.. --dir=../test_files basisu.wasm -test
Use backslashes under Windows: “wasmtime run --wasm threads=yes --wasi threads=yes --dir=. --dir=.. --dir=..\test_files basisu.wasm -test”
For the single threaded wasm executables, “--wasm threads=yes --wasi threads=yes” isn't needed.
A Windows .cmd batch script example:
wasmtime --dir=. --dir=.. --dir=..\test_files --dir=d:/dev/test_images::/test_images --dir=d:/dev/test_images/bik::/bik basisu.wasm %*
A shell script example:
#!/usr/bin/env bash wasmtime run --dir=. --dir=../test_files --dir=/mnt/d/dev/test_images::/test_images --dir=/mnt/d/dev/test_images/bik::/test_images/bik --wasm threads=yes --wasi threads=yes ./basisu.wasm "$@"
wasmtime run --wasm threads=yes --wasi threads=yes --dir=. basisu.wasm xmen.png -stats
wasmtime run --wasm threads=yes --wasi threads=yes --dir=. basisu.wasm xmen.ktx2
--dir=.WASI_SDK_PATH.To build and run BasisU under WASI:
--dir= permissions on .wasm executables in “bin” directoryThis produces a safe, portable, sandboxed version of the Basis Universal encoder that runs anywhere.