@@ -9,9 +9,237 @@ use WASM runtimes such as [wasmtime](https://wasmtime.dev/).
99** NOTE** : If you are looking for general information about WebAssembly that is
1010not directly related to CPython, please see https://github.com/psf/webassembly .
1111
12- ## Build
1312
14- See [ the devguide on how to build and run for WASI] ( https://devguide.python.org/getting-started/setup-building/#wasi ) .
13+ ## Working on the WASI build
14+
15+ This directory provides a CLI for building and packaging WASI builds for
16+ distribution.
17+
18+ Run all commands below from the root of the CPython source checkout.
19+
20+ To see all the available commands, run:
21+
22+ ``` shell
23+ python3 Platforms/WASI --help
24+ ```
25+
26+ The ` python3 ` interpreter used to run this CLI must be a Python version that
27+ is receiving bugfixes. This requirement does not apply to the build Python,
28+ which the CLI builds from the source checkout.
29+
30+
31+ ### Prerequisites
32+
33+ There are some tools that must be available to successfully build.
34+
35+ 1 . C compiler
36+ 2 . ` make `
37+ 3 . WASI SDK
38+ 4 . Wasmtime (or some other WASI runtime configured via ` --host-runner ` )
39+
40+ The default runner requires Wasmtime be on ` PATH ` .
41+
42+ The WASI SDK must be the same version as specified in config.toml. The search
43+ for the WASI SDK is done via:
44+
45+ 1 . ` --wasi-sdk ` CLI option
46+ 2 . ` WASI_SDK_PATH ` environment variable
47+ 3 . ` /opt ` where the WASI SDK has been unpacked from its tarball
48+
49+ Note that all prerequisites are included and configured appropriately in the
50+ [ WASI dev container image] ( https://github.com/python/cpython-devcontainers/pkgs/container/wasicontainer ) .
51+ You can download it via:
52+
53+ ``` shell
54+ podman pull ghcr.io/python/wasicontainer:latest
55+ ```
56+
57+ The ` latest ` image contains the WASI SDK versions required by all supported
58+ CPython branches.
59+
60+ ### Development loop
61+
62+ The common way to get started is to first do a full build:
63+
64+ ``` shell
65+ python3 Platforms/WASI build --quiet --logdir cross-build/logs -- --with-pydebug --config-cache
66+ ```
67+
68+ In the end, you will end up with a "build Python" which is a local build of
69+ Python used for cross-builds. You will also have the WASI build.
70+
71+ Once you have the build you can run the test you want.
72+
73+ Bash:
74+ ``` bash
75+ " $( python3 Platforms/WASI path) /python.sh" -m test test_os
76+ ```
77+
78+ Fish:
79+ ``` fish
80+ set -l wasi_dir (python3 Platforms/WASI path); "$wasi_dir/python.sh" -m test test_os
81+ ```
82+
83+ If you are working on C code and need a rebuild:
84+
85+ ``` shell
86+ python3 Platforms/WASI make-host
87+ ```
88+
89+
90+ ### Building
91+
92+ In general,
93+ [ the devguide covers how to build and run for WASI] ( https://devguide.python.org/getting-started/setup-building/#wasi ) ,
94+ but we will cover some of the details here.
95+
96+ The simplest way to get a pydebug WASI build is:
97+
98+ ``` shell
99+ python3 Platforms/WASI build -- --with-pydebug
100+ ```
101+
102+ This builds the build Python and the WASI build with ` --with-pydebug ` passed to
103+ ` configure ` (as is anything that comes after ` -- ` ). The builds are placed in
104+ the ` cross-build/ ` directory of the source checkout, each in a subdirectory
105+ matching the compiler triple for the build.
106+
107+ You can do the two builds separately if you want:
108+
109+ ``` shell
110+ python3 Platforms/WASI build-python -- --with-pydebug
111+ python3 Platforms/WASI build-host
112+ ```
113+
114+ This can be broken down even more to the separate ` configure ` and ` make ` steps:
115+
116+ ``` shell
117+ python3 Platforms/WASI configure-build-python -- --with-pydebug
118+ python3 Platforms/WASI make-build-python
119+ python3 Platforms/WASI configure-host
120+ python3 Platforms/WASI make-host
121+ ```
122+
123+ Note that ` configure-host ` figures out to do a pydebug build by looking at the
124+ build Python.
125+
126+ There is a ` --quiet ` flag to redirect output from the underlying commands to a
127+ directory. The ` --logdir ` flag controls where the log files go (which defaults
128+ to ` /tmp ` ).
129+
130+ ``` shell
131+ python3 Platforms/WASI build --quiet --logdir cross-build/logs
132+ ```
133+
134+
135+ ### Packaging
136+
137+ The ` package ` command is used to gather all the files necessary to make a
138+ release and place them in an archive:
139+
140+ ``` shell
141+ python3 Platforms/WASI package
142+ ```
143+
144+ The files are gathered into a versioned directory inside ` dist/ ` in the source
145+ checkout. An archive containing that directory is placed alongside it in
146+ ` dist/ ` . The gathered directory includes a ` bin/python3.wasmtime ` file to ease
147+ launching the interpreter.
148+
149+ If you just need to gather the files for a release, you can use the ` gather `
150+ command:
151+
152+ ``` shell
153+ python3 Platforms/WASI gather
154+ ```
155+
156+ Both ` package ` and ` gather ` require a completed build and delete the entire
157+ existing ` dist/ ` directory before gathering files.
158+
159+ There is no command just to archive the gathered files.
160+
161+
162+ ### Paths
163+
164+ The ` path ` command prints the location of the build Python, WASI build, or
165+ gathered distribution files:
166+
167+ ``` shell
168+ python3 Platforms/WASI path build-python
169+ python3 Platforms/WASI path wasi
170+ python3 Platforms/WASI path dist
171+ ```
172+
173+ With no location argument, ` path ` defaults to ` wasi ` . The ` dist ` location
174+ returns the versioned distribution directory inside ` dist/ ` , not the top-level
175+ ` dist/ ` directory. The ` build-python ` and ` wasi ` locations can be queried before
176+ building; the ` dist ` location requires WASI build metadata to determine the
177+ versioned directory name.
178+
179+
180+ ### Cleanup
181+
182+ The ` clean ` command deletes the entire ` cross-build/ ` and ` dist/ ` directories,
183+ including all build files, gathered distribution files, and archives:
184+
185+ ``` shell
186+ python3 Platforms/WASI clean
187+ ```
188+
189+
190+ ### Testing
191+
192+ To find out where the WASI build directory is, you can run:
193+
194+ ``` shell
195+ python3 Platforms/WASI path
196+ ```
197+
198+ From there, you can run the test suite.
199+
200+ For Bash-like shells:
201+
202+ ``` bash
203+ make buildbottest -C " $( python3 Platforms/WASI path) "
204+ ```
205+
206+ For fish:
207+
208+ ``` fish
209+ make buildbottest -C (python3 Platforms/WASI path)
210+ ```
211+
212+ There is a ` python.sh ` file in the WASI build directory, so you can also run
213+ tests that way.
214+
215+ Bash:
216+
217+ ``` bash
218+ " $( python3 Platforms/WASI path) /python.sh" -m test
219+ ```
220+
221+ Fish:
222+
223+ ``` fish
224+ set -l wasi_dir (python3 Platforms/WASI path); "$wasi_dir/python.sh" -m test
225+ ```
226+
227+ If you want to test the files meant for distribution, the directory containing
228+ the files can be found via ` python3 Platforms/WASI path dist ` and there is a
229+ ` bin/python3.wasmtime ` shell script.
230+
231+ Bash:
232+
233+ ``` bash
234+ " $( python3 Platforms/WASI path dist) /bin/python3.wasmtime" -m test
235+ ```
236+
237+ Fish:
238+
239+ ``` fish
240+ set -l dist_dir (python3 Platforms/WASI path dist); "$dist_dir/bin/python3.wasmtime" -m test
241+ ```
242+
15243
16244## Detecting WASI builds
17245
@@ -48,6 +276,7 @@ posix.uname_result(
48276' wasi'
49277```
50278
279+
51280### C code
52281
53282WASI SDK defines several built-in macros. You can dump a full list of built-ins
0 commit comments