You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WASI builds require the [WASI SDK](https://github.com/WebAssembly/wasi-sdk) 16.0+.
302
-
See `.devcontainer/Dockerfile` for an example of how to download and
303
-
install the WASI SDK.
301
+
**NOTE**: The instructions below assume a Unix-based OS due to cross-compilation for CPython being set up for `./configure`.
304
302
305
-
### Build
303
+
### Prerequisites
304
+
305
+
Developing for WASI requires two things to be installed:
306
+
307
+
1. The [WASI SDK](https://github.com/WebAssembly/wasi-sdk) 16.0+
308
+
(see `.devcontainer/Dockerfile` for an example of how to download and install the WASI SDK)
309
+
2. A WASI host/runtime ([wasmtime](https://wasmtime.dev) 14+ is recommended and what the instructions below assume)
306
310
307
-
The script ``wasi-env`` sets necessary compiler and linker flags as well as
308
-
``pkg-config`` overrides. The script assumes that WASI-SDK is installed in
309
-
``/opt/wasi-sdk`` or ``$WASI_SDK_PATH``.
310
311
311
-
There are two scripts you can use to do a WASI build from a source checkout. You can either use:
312
+
### Building
312
313
314
+
Building for WASI requires doing a cross-build where you have a "build" Python to help produce a WASI build of CPython (technically it's a "host x host" cross-build because the build Python is also the target Python while the host build is the WASI build; yes, it's confusing terminology). In the end you should have a build Python in `cross-build/build` and a WASI build in `cross-build/wasm32-wasi`.
315
+
316
+
The easiest way to do a build is to use the `wasi.py` script. You can either have it perform the entire build process from start to finish in one step, or you can do it in discrete steps that mirror running `configure` and `make` for each of the two builds of Python you end up producing (which are beneficial when you only need to do a specific step after getting a complete build, e.g. editing some code and you just need to run `make` for the WASI build).
317
+
318
+
The discrete steps are:
313
319
```shell
314
-
./Tools/wasm/wasm_build.py wasi build
320
+
python Tools/wasm/wasi.py configure-build-python
321
+
python Tools/wasm/wasi.py make-build-python
322
+
python Tools/wasm/wasi.py configure-host
323
+
python Tools/wasm/wasi.py make-host
315
324
```
316
325
317
-
or:
326
+
To do it in a single command, run:
318
327
```shell
319
-
./Tools/wasm/build_wasi.sh
328
+
python Tools/wasm/wasi.py build
320
329
```
321
330
322
-
The commands are equivalent to the following steps:
323
-
324
-
- Make sure `Modules/Setup.local` exists
325
-
- Make sure the necessary build tools are installed:
326
-
-[WASI SDK](https://github.com/WebAssembly/wasi-sdk) (which ships with `clang`)
-`HOSTRUNNER="wasmtime run --mapdir /::$(dirname $(dirname $(pwd))) --env PYTHONPATH=/builddir/wasi/build/lib.wasi-wasm32-$PYTHON_VERSION $(pwd)/python.wasm --"`
345
-
- Maps the source checkout to `/` in the WASI runtime
346
-
- Stdlib gets loaded from `/Lib`
347
-
- Gets `_sysconfigdata__wasi_wasm32-wasi.py` on to `sys.path` via `PYTHONPATH`
348
-
- Set by `wasi-env`
349
-
-`WASI_SDK_PATH`
350
-
-`WASI_SYSROOT`
351
-
-`CC`
352
-
-`CPP`
353
-
-`CXX`
354
-
-`LDSHARED`
355
-
-`AR`
356
-
-`RANLIB`
357
-
-`CFLAGS`
358
-
-`LDFLAGS`
359
-
-`PKG_CONFIG_PATH`
360
-
-`PKG_CONFIG_LIBDIR`
361
-
-`PKG_CONFIG_SYSROOT_DIR`
362
-
-`PATH`
363
-
-`make all`
364
-
331
+
That will:
365
332
366
-
### Running
367
-
368
-
If you followed the instructions above, you can run the interpreter via e.g., `wasmtime` from within the `Tools/wasi` directory (make sure to set/change `$PYTHON_VERSION` and do note the paths are relative to running in`builddir/wasi` for simplicity only):
333
+
1. Run `configure` for the build Python (same as `wasi.py configure-build-python`)
334
+
2. Run `make` for the build Python (`wasi.py make-build-python`)
335
+
3. Run `configure` for the WASI build (`wasi.py configure-host`)
336
+
4. Run `make` for the WASI build (`wasi.py make-host`)
369
337
338
+
See the `--help` for the various options available for each of the subcommands which controls things like the location of the WASI SDK, the command to use with the WASI host/runtime, etc. Also note that you can use `--` as a separtor for any of the `configure`-related commands -- including `build` -- to pass arguments to `configure` itself. For example, if you want a pydebug build that also caches the results from `configure`, you can do:
370
339
```shell
371
-
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 (it also uses absolute paths, so it can be executed from anywhere).
375
-
376
-
#### REPL
377
-
343
+
The `wasi.py` script is able to infer details from the build Python, and so you only technically need to specify `--with-pydebug` once for `configure-build-python` and `configure-host` will detect its use if you use the discrete steps:
If you used `wasi.py` to do your build then there will be a `cross-build/wasm32-wasi/python.sh` file which you can use to run the `python.wasm` file (see the output from the `configure-host` subcommand):
384
355
```shell
385
-
./Tools/wasm/wasm_build.py wasi test
356
+
cross-build/wasm32-wasi/python.sh --version
386
357
```
387
358
388
-
### Debugging
359
+
While you _can_ run `python.wasm` directly, Python will fail to start up without certain things being set (e.g. `PYTHONPATH` for `sysconfig` data). As such, the `python.sh` file records these details for you.
389
360
390
-
*``wasmtime run -g`` generates debugging symbols for gdb and lldb. The
0 commit comments