Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a build_wasi.sh script
  • Loading branch information
brettcannon committed Jun 2, 2023
commit 827d1ecbbd64bd31932ba89628aceca32176c07e
17 changes: 16 additions & 1 deletion Tools/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,18 @@ The script ``wasi-env`` sets necessary compiler and linker flags as well as
``pkg-config`` overrides. The script assumes that WASI-SDK is installed in
``/opt/wasi-sdk`` or ``$WASI_SDK_PATH``.

There are two scripts you can use to do a WASI build. You can either use:

```shell
./Tools/wasm/wasm_build.py wasi build
```

The command is equivalent to the following steps:
or:
```shell
./Tools/wasm/build_wasi.sh
```

The commands are equivalent to the following steps:

- Make sure `Modules/Setup.local` exists
- Make sure the necessary build tools are installed:
Expand Down Expand Up @@ -358,6 +365,14 @@ The command is equivalent to the following steps:

### Running

If you followed the instructions above, you can run the interpreter via e.g., `wasmtime` (make sure to specify/change the `$PYTHON_VERSION` to the major.minor version that you just built):

```shell
wasmtime run --mapdir /::../.. --env PYTHONPATH=/builddir/wasi/build/lib.wasi-wasm32-$PYTHON_VERSION python.wasm -- <args>
```

There are also helpers provided by `Tools/wasm/wasm_build.py` as listed below. Also, if you used `Tools/wasm/build_wasi.sh`, a `run_wasi.sh` file will be created in `builddir/wasi` which will run the above command for you.

#### REPL

```shell
Expand Down
37 changes: 37 additions & 0 deletions Tools/wasm/build_wasi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/bash

set -e -x

if [ ! -f Modules/Setup.local ]; then
touch Modules/Setup.local
fi

# TODO: check if `make` and `pkgconfig` are installed

# Create the build Python.
mkdir -p builddir/build
pushd builddir/build
../../configure -C
make -s -j 4 all
export PYTHON_VERSION=`./python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")'`
popd

# Create the host/WASI Python.
export CONFIG_SITE="../../Tools/wasm/config.site-wasm32-wasi"
export HOSTRUNNER="wasmtime run --mapdir /::../.. --env PYTHONPATH=/builddir/wasi/build/lib.wasi-wasm32-$PYTHON_VERSION python.wasm --"

mkdir -p builddir/wasi
pushd builddir/wasi
../../Tools/wasm/wasi-env \
../../configure \
-C \
--host=wasm32-unknown-wasi \
--build=$(../../config.guess) \
--with-build-python=../build/python
make -s -j 4 all
# Create a helper script for executing the host/WASI Python.
printf "#!/bin/sh\nexec $HOSTRUNNER \"\$@\"\n" > run_wasi.sh
chmod 755 run_wasi.sh
./run_wasi.sh --version
popd