Skip to content

Utilities for building CPython for the WASI platform

License

Notifications You must be signed in to change notification settings

antmicro/python-wasi

 
 

Repository files navigation

CPython on wasm32-wasi

This project consists of utilities and libraries for building CPython sources for the WebAssembly platform using the WASI SDK (v15+). The scripts here will configure and build various versions of CPython (3.8, 3.9, 3.10, 3.11). A Dockerfile is supplied to simplify the setup of a build environment.

Building the Docker Image

To build the Docker image, use the following command:

docker build -f docker/Dockerfile -t wasi-build:latest docker

Starting a Docker Container

To run the Docker image created above, use the following command:

docker run -it --rm -v $(pwd):$(pwd) -w $(pwd) wasi-build:latest bash

The mount for your user directory should be changed accordingly. You do not necessarily need to mount your user directory, just a writable directory that has access to the files from this project.

Build CPython

In the Docker container created in the previous step, run the following command from this project directory. It will download the CPython repository as well as the source for the wasix project, patch files in the CPython source (as needed per version), then build CPython for the WASI platform.

./run.sh

Building without Docker

It is possible to build without Docker if you have WASI SDK and the other tools required to build CPython already installed.

Cloning CPython and/or wasix Manually

The paths to the CPython source and wasix source are configurable in the run.sh script. You can clone them manually before running run.sh. This method can be used to change the source branch used prior to running the build.

Running WASI CPython

The run.sh script will execute a smoke test of the resulting CPython build using wasmtime which prints the version number of the CPython interpreter that was just built. To run an interactive session of the newly built CPython interpreter, use the following command:

wasmtime run --mapdir=$(pwd)/opt::opt \
             -- opt/wasi-python/bin/python3.wasm -i

It is possible to relocate the WASI Python installation by putting it in the desired directory and setting PYTHONHOME to that path. By default, PYTHONHOME is set to $(pwd)/opt/wasi-python.

Running Python Unit Tests

You can run the Python test suite with the following command. Many tests are currently failing due to the fact that WASI does not have support for threads, subprocesses, or sockets. As support is added for these features in the future, more tests will pass. Note that you must put the correct Python version number in the test file path.

wasmtime run --mapdir=$(pwd)/opt::opt \
             --mapdir=/tmp::/tmp \
             -- opt/wasi-python/bin/python3.wasm \
             /opt/wasi-python/lib/python3.X/test/test_runpy.py

Resetting Files in the CPython Repository

A couple files from the CPython repo get patched by the run.sh script to fix problems in the build and testing processes. The reset-python-repo.sh script can be used to undo the changes made. This allows you to easily change to another branch to build alternate versions of Python.

ToDo

This project is in early development as well as WASM and the WASI SDK. Many features expected in a POSIX-like environment are still not available. This includes threads, sockets, subprocesses, dynamically linked libraries, and file operations pertaining to file ownership and permissions. As more support is added in each of these areas, more capabalities will be unlocked in the WASI build of CPython as well.

The highest priority features for this project are 1) threads and 2) dynamically linked libraries since these are required for using many Python extension modules.

Resources

Python The Python programming language

CPython The CPython source repository

WASI SDK The WASI SDK source repository

wasix The wasix library source repository

About

Utilities for building CPython for the WASI platform

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 99.1%
  • Other 0.9%