Skip to content

Commit 0e4bb40

Browse files
kesmit13claude
andcommitted
Add WASM build script for wasm32-wasip2 target
Add resources/build_wasm.sh that cross-compiles the package as a WASM wheel targeting wasm32-wasip2. The script sets up a host venv, configures the WASI SDK toolchain (clang, ar, linker flags), and uses `python -m build` to produce the wheel, then unpacks it into build/. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5eef5fd commit 0e4bb40

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

resources/build_wasm.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -eou pipefail
4+
5+
# CPYTHON_ROOT must contain a build of cpython for wasm32-wasip2
6+
7+
TARGET="wasm32-wasip2"
8+
CROSS_BUILD="${CPYTHON_ROOT}/cross-build/${TARGET}"
9+
WASI_SDK_PATH=${WASI_SDK_PATH:-/opt/wasi-sdk}
10+
PYTHON_VERSION=$(grep '^VERSION=' "${CROSS_BUILD}/Makefile" | sed 's/VERSION=[[:space:]]*//')
11+
12+
if [ ! -e wasm_venv ]; then
13+
uv venv --python ${PYTHON_VERSION} wasm_venv
14+
fi
15+
16+
. wasm_venv/bin/activate
17+
18+
HOST_PYTHON=$(which python3)
19+
20+
uv pip install build wheel cython setuptools
21+
22+
ARCH_TRIPLET=_wasi_wasm32-wasi
23+
24+
export CC="${WASI_SDK_PATH}/bin/clang"
25+
export CXX="${WASI_SDK_PATH}/bin/clang++"
26+
27+
export PYTHONPATH="${CROSS_BUILD}/build/lib.wasi-wasm32-${PYTHON_VERSION}"
28+
29+
export CFLAGS="--target=${TARGET} -fPIC -I${CROSS_BUILD}/install/include/python${PYTHON_VERSION} -D__EMSCRIPTEN__=1"
30+
export CXXFLAGS="--target=${TARGET} -fPIC -I${CROSS_BUILD}/install/include/python${PYTHON_VERSION}"
31+
export LDSHARED=${CC}
32+
export AR="${WASI_SDK_PATH}/bin/ar"
33+
export RANLIB=true
34+
export LDFLAGS="--target=${TARGET} -shared -Wl,--allow-undefined"
35+
export _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata__wasi_wasm32-wasi
36+
export _PYTHON_HOST_PLATFORM=wasm32-wasi
37+
38+
python3 -m build -n -w
39+
wheel unpack --dest build dist/*.whl
40+
41+
rm -rf ./wasm_venv

0 commit comments

Comments
 (0)