44
55This directory contains configuration and helpers to facilitate cross
66compilation of CPython to WebAssembly (WASM). For now we support
7- * wasm32-emscripten* builds for modern browser and for * Node.js* . It's not
8- possible to build for * wasm32-wasi* out-of-the-box yet.
7+ * wasm32-emscripten* builds for modern browser and for * Node.js* . WASI
8+ ( * wasm32-wasi* ) is work-in-progress
99
1010## wasm32-emscripten build
1111
12+ For now the build system has two target flavors. The `` Emscripten/browser ``
13+ target (`` --with-emscripten-target=browser `` ) is optimized for browsers.
14+ It comes with a reduced and preloaded stdlib without tests and threading
15+ support. The `` Emscripten/node `` target has threading enabled and can
16+ access the file system directly.
17+
1218Cross compiling to the wasm32-emscripten platform needs the
1319[ Emscripten] ( https://emscripten.org/ ) SDK and a build Python interpreter.
1420Emscripten 3.1.8 or newer are recommended. All commands below are relative
@@ -76,7 +82,7 @@ and header files with debug builds.
7682
7783### Cross compile to wasm32-emscripten for node
7884
79- ```
85+ ``` shell
8086mkdir -p builddir/emscripten-node
8187pushd builddir/emscripten-node
8288
@@ -91,7 +97,7 @@ emmake make -j$(nproc)
9197popd
9298```
9399
94- ```
100+ ``` shell
95101node --experimental-wasm-threads --experimental-wasm-bulk-memory builddir/emscripten-node/python.js
96102```
97103
@@ -150,9 +156,9 @@ functions.
150156- Most stdlib modules with a dependency on external libraries are missing,
151157 e.g. `` ctypes `` , `` readline `` , `` sqlite3 `` , `` ssl `` , and more.
152158- Shared extension modules are not implemented yet. All extension modules
153- are statically linked into the main binary.
154- The experimental configure option `` --enable-wasm-dynamic-linking `` enables
155- dynamic extensions .
159+ are statically linked into the main binary. The experimental configure
160+ option `` --enable-wasm-dynamic-linking `` enables dynamic extensions
161+ supports. It's currently known to crash in combination with threading .
156162- glibc extensions for date and time formatting are not available.
157163- `` locales `` module is affected by musl libc issues,
158164 [ bpo-46390] ( https://bugs.python.org/issue46390 ) .
@@ -167,8 +173,10 @@ functions.
167173 distutils, multiprocessing, dbm, tests and similar modules
168174 are not shipped. All other modules are bundled as pre-compiled
169175 `` pyc `` files.
170- - Threading is not supported .
176+ - Threading is disabled .
171177- In-memory file system (MEMFS) is not persistent and limited.
178+ - Test modules are disabled by default. Use `` --enable-test-modules `` build
179+ test modules like `` _testcapi `` .
172180
173181## wasm32-emscripten in node
174182
@@ -205,11 +213,17 @@ AddType application/wasm wasm
205213</IfModule>
206214```
207215
216+ # WASI (wasm32-wasi)
217+
218+ WASI builds require [ WASI SDK] ( https://github.com/WebAssembly/wasi-sdk ) and
219+ currently [ wasix] ( https://github.com/singlestore-labs/wasix ) for POSIX
220+ compatibility stubs.
221+
208222# Detect WebAssembly builds
209223
210224## Python code
211225
212- ``` # python
226+ ``` python
213227import os, sys
214228
215229if sys.platform == " emscripten" :
@@ -222,7 +236,36 @@ if os.name == "posix":
222236 # Windows does not provide os.uname().
223237 machine = os.uname().machine
224238 if machine.startswith(" wasm" ):
225- # WebAssembly (wasm32 or wasm64)
239+ # WebAssembly (wasm32, wasm64 in the future)
240+ ```
241+
242+ ``` python
243+ >> > import os, sys
244+ >> > os.uname()
245+ posix.uname_result(sysname = ' Emscripten' , nodename = ' emscripten' , release = ' 1.0' , version = ' #1' , machine = ' wasm32' )
246+ >> > os.name
247+ ' posix'
248+ >> > sys.platform
249+ ' emscripten'
250+ >> > sys._emscripten_info
251+ sys._emscripten_info(
252+ emscripten_version = (3 , 1 , 8 ),
253+ runtime = ' Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0' ,
254+ pthreads = False ,
255+ shared_memory = False
256+ )
257+ >> > sys._emscripten_info
258+ sys._emscripten_info(emscripten_version = (3 , 1 , 8 ), runtime = ' Node.js v14.18.2' , pthreads = True , shared_memory = True )
259+ ```
260+
261+ ``` python
262+ >> > import os, sys
263+ >> > os.uname()
264+ posix.uname_result(sysname = ' wasi' , nodename = ' (none)' , release = ' 0.0.0' , version = ' 0.0.0' , machine = ' wasm32' )
265+ >> > os.name
266+ ' posix'
267+ >> > sys.platform
268+ ' wasi'
226269```
227270
228271## C code
@@ -231,7 +274,7 @@ Emscripten SDK and WASI SDK define several built-in macros. You can dump a
231274full list of built-ins with `` emcc -dM -E - < /dev/null `` and
232275`` /path/to/wasi-sdk/bin/clang -dM -E - < /dev/null `` .
233276
234- ``` # C
277+ ``` C
235278#ifdef __EMSCRIPTEN__
236279 // Python on Emscripten
237280#endif
0 commit comments