WARNING: WASM support is highly experimental! Lots of features are not working yet.
This directory contains configuration and helpers to facilitate cross compilation of CPython to WebAssembly (WASM). For now we support wasm32-emscripten builds for modern browser and for Node.js. It's not possible to build for wasm32-wasi out-of-the-box yet.
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.
mkdir -p builddir/build pushd builddir/build ../../configure -C make -j$(nproc) popd
embuilder build zlib bzip2
mkdir -p builddir/emscripten-browser pushd builddir/emscripten-browser 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) popd
Serve python.html
with a local webserver and open the file in a browser.
emrun builddir/emscripten-browser/python.html
or
python3 -m http.server
mkdir -p builddir/emscripten-node pushd builddir/emscripten-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) popd
node --experimental-wasm-threads --experimental-wasm-bulk-memory builddir/emscripten-node/python.js
ctypes
, readline
, sqlite3
, ssl
, and more.ENOSYS
or ENOSUP
.socket.accept
crashes the runtime. gethostbyname
does not resolve to a real IP address. IPv6 is not available.select
module is limited. select.select()
crashes the runtime due to lack of exectfd support.*at
variants of functions (e.g. openat
) are not available. The dir_fd
argument of os module functions can't be used.signal.alarm
, itimer
, sigaction
are not available or do not work correctly. SIGTERM
exits the runtime.pwd
module, grp
module, os.setgroups
, os.chown
, and so on.os.pread
, os.preadv
) are not available.os.mknod
and os.mkfifo
don't work and are disabled.mmap
module is unstable. flush (msync
) can crash the runtime.os.nice
and most functions of the resource
module are not available.strftime
and strptime
have known bugs, e.g. %% quoting, %U off-by-one. Extended glibc formatting features are not available.locales
module is affected by musl libc issues, bpo-46390.uuid
module is affected by memory leak and crasher in Emscripten's freeaddrinfo
,glob
leaks file descriptors.obmalloc
is disabled by default.ensurepip
is not available.pyc
files.Node builds use NODERAWFS
, USE_PTHREADS
and PROXY_TO_PTHREAD
linker options.