CPython running on WebAssembly in timecraft.
Assuming in the directory of this readme, after building.
timecraft run \
-e PYTHONPATH=`pwd`/deps:`pwd`/cpython/usr/local/lib/python311.zip \
-e PYTHONHOME=`pwd`/cpython/usr/local/lib/python311.zip \
-- `pwd`/cpython/python.wasm
timecraft run \
-e PYTHONPATH=`pwd`/deps:`pwd`/cpython/usr/local/lib/python311.zip \
-e PYTHONHOME=`pwd`/cpython/usr/local/lib/python311.zip \
-- `pwd`/cpython/python.wasm \
./script.py arg1 arg2 etc
To install Python dependencies for the script you want to run, use the following command (you need pip installed in your host python):
python3 -m pip install \
--target deps/ \
--only-binary :all: --implementation py --abi none --platform any --python-version "3.11" \
whateverpackage
This will store the package (if available) in ./deps
, which is provided using
PYTHONPATH
to wasm.
make download
It retrieves the most recent build of python for the current branch. To enable debug logs, read the following "Build" section.
./build.sh
Produces:
cpython/python.wasm
- the python wasm module.cpython/usr/local/lib/python311.zip
- the python standard library.
- Clang 9+ (tested with 15.0.7 on ubuntu)
- If necessary,
libclang_rt.builtins-wasm32.a
. wasm-opt
on yourPATH
. (download).python3.11
on yourPATH
.
or
- Docker
If you don't want to install everything locally, you can build a docker image that contains those dependencies:
make build-docker
The build.sh
script creates a ./build
directory where it stores extra
dependencies in.
Clang and LLVM are expected to be on the path. Otherwise, use the following environment variables:
export CC=/usr/lib/llvm-15/bin/clang
export AR=/usr/lib/llvm-15/bin/llvm-ar
export NM=/usr/lib/llvm-15/bin/llvm-nm
export RL=/usr/lib/llvm-15/bin/llvm-ranlib
If you want to iterate faster, disable the wasm-opt
pass by providing any
value to NO_OPT
when building.
Set the NO_WASMEDGE
environment variable to build CPython without socket
support.
When making any change in configuration, it is possible you may have to run
make dist-clean
in /python
.
- Provide
NO_OPT
tobuild.sh
to disablewasm-opt
. - Provide
DEBUG
tobuild.sh
to enable--pydebug
. - Provide
PYTHONTRACEMALLOC=1
totimecraft run
to trace Python malloc calls.
Example of all those options when running tests:
NO_OPT=true DEBUG=true ./build.sh && \
timecraft run \
-e PYTHONPATH=`pwd`/deps:`pwd`/python/usr/local/lib/python311.zip \
-e PYTHONHOME=`pwd`/python/usr/local/lib/python311.zip \
-e PYTHONTRACEMALLOC=1 \
-- `pwd`/python/python.wasm ./test.py -v
/cpython
- https://github.com/stealthrocket/cpython/tree/dev/wasi-libc
- https://github.com/stealthrocket/wasi-libc/tree/dev/wasmedge_sock
- libc socket library using wasmedge extension
Static library implementation of posix sockets based on the wasmedge interface. Initially from vmware. Heavily modified. Should be contributed back to them at some point.