cpython/Tools/wasm
2022-01-24 22:02:01 +01:00
..
config.site-wasm32-emscripten bpo-40280: Misc fixes for wasm32-emscripten (GH-30722) 2022-01-20 18:56:33 +01:00
config.site-wasm32-wasi bpo-46315: Add ifdef HAVE_ feature checks for WASI compatibility (GH-30507) 2022-01-13 09:46:04 +01:00
README.md bpo-40280: Add --with-emscripten-target to build for browser or node (GH-30552) 2022-01-12 16:08:19 +01:00
wasm_assets.py bpo-40280: Get help() working and more (GH-30858) 2022-01-24 22:02:01 +01:00

Python WebAssembly (WASM) build

This directory contains configuration and helpers to facilitate cross compilation of CPython to WebAssembly (WASM).

wasm32-emscripten build

Cross compiling to wasm32-emscripten platform needs the Emscripten tool chain and a build Python interpreter. All commands below are relative to a repository checkout.

Compile a build Python interpreter

mkdir -p builddir/build
pushd builddir/build
../../configure -C
make -j$(nproc)
popd

Fetch and build additional emscripten ports

embuilder build zlib

Cross compile to wasm32-emscripten

For browser:

mkdir -p builddir/emscripten
pushd builddir/emscripten

CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \
  emconfigure ../../configure -C \
    --host=wasm32-unknown-emscripten \
    --build=$(../../config.guess) \
    --with-emscripten-target=browser \
    --with-build-python=$(pwd)/../build/python

emmake make -j$(nproc)

For node:

CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \
  emconfigure ../../configure -C \
    --host=wasm32-unknown-emscripten \
    --build=$(../../config.guess) \
    --with-emscripten-target=node \
    --with-build-python=$(pwd)/../build/python

emmake make -j$(nproc)

Test in browser

Serve python.html with a local webserver and open the file in a browser.

emrun python.html

or

python3 -m http.server