Skip to content

Commit a7aa566

Browse files
committed
Add script to automate WASM build
Automate WASM build with a new Python script. The script provides several build profiles with configure flags for Emscripten flavors and WASI. The script can detect and use Emscripten SDK and WASI SDK from default locations or env vars. ``configure`` now detects Node arguments and creates HOSTRUNNER arguments for Node 16. It also sets correct arguments for ``wasm64-emscripten``.
1 parent 141f251 commit a7aa566

File tree

5 files changed

+709
-16
lines changed

5 files changed

+709
-16
lines changed

Python/sysmodule.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,14 +2789,18 @@ EM_JS(char *, _Py_emscripten_runtime, (void), {
27892789
if (typeof navigator == 'object') {
27902790
info = navigator.userAgent;
27912791
} else if (typeof process == 'object') {
2792-
info = "Node.js ".concat(process.version)
2792+
info = "Node.js ".concat(process.version);
27932793
} else {
2794-
info = "UNKNOWN"
2794+
info = "UNKNOWN";
27952795
}
27962796
var len = lengthBytesUTF8(info) + 1;
27972797
var res = _malloc(len);
2798-
stringToUTF8(info, res, len);
2798+
if (res) stringToUTF8(info, res, len);
2799+
#if __wasm64__
2800+
return BigInt(res);
2801+
#else
27992802
return res;
2803+
#endif
28002804
});
28012805

28022806
static PyObject *

Tools/wasm/README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ docker run --rm -ti -v $(pwd):/python-wasm/cpython -w /python-wasm/cpython quay.
3535

3636
### Compile a build Python interpreter
3737

38-
From within the container, run the following commands:
38+
From within the container, run the following command:
39+
40+
```shell
41+
./Tools/wasm/wasm_build.py build
42+
```
43+
44+
The command is roughly equivalent to:
3945

4046
```shell
4147
mkdir -p builddir/build
@@ -45,13 +51,13 @@ make -j$(nproc)
4551
popd
4652
```
4753

48-
### Fetch and build additional emscripten ports
54+
### Cross compile to wasm32-emscripten for browser
4955

5056
```shell
51-
embuilder build zlib bzip2
57+
./Tools/wasm/wasm_build.py emscripten-browser
5258
```
5359

54-
### Cross compile to wasm32-emscripten for browser
60+
The command is roughly equivalent to:
5561

5662
```shell
5763
mkdir -p builddir/emscripten-browser
@@ -85,22 +91,29 @@ and header files with debug builds.
8591
### Cross compile to wasm32-emscripten for node
8692

8793
```shell
88-
mkdir -p builddir/emscripten-node
89-
pushd builddir/emscripten-node
94+
./Tools/wasm/wasm_build.py emscripten-browser-dl
95+
```
96+
97+
The command is roughly equivalent to:
98+
99+
```shell
100+
mkdir -p builddir/emscripten-node-dl
101+
pushd builddir/emscripten-node-dl
90102

91103
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \
92104
emconfigure ../../configure -C \
93105
--host=wasm32-unknown-emscripten \
94106
--build=$(../../config.guess) \
95107
--with-emscripten-target=node \
108+
--enable-wasm-dynamic-linking \
96109
--with-build-python=$(pwd)/../build/python
97110

98111
emmake make -j$(nproc)
99112
popd
100113
```
101114

102115
```shell
103-
node --experimental-wasm-threads --experimental-wasm-bulk-memory --experimental-wasm-bigint builddir/emscripten-node/python.js
116+
node --experimental-wasm-threads --experimental-wasm-bulk-memory --experimental-wasm-bigint builddir/emscripten-node-dl/python.js
104117
```
105118

106119
(``--experimental-wasm-bigint`` is not needed with recent NodeJS versions)
@@ -234,6 +247,12 @@ The script ``wasi-env`` sets necessary compiler and linker flags as well as
234247
``pkg-config`` overrides. The script assumes that WASI-SDK is installed in
235248
``/opt/wasi-sdk`` or ``$WASI_SDK_PATH``.
236249

250+
```shell
251+
./Tools/wasm/wasm_build.py wasi
252+
```
253+
254+
The command is roughly equivalent to:
255+
237256
```shell
238257
mkdir -p builddir/wasi
239258
pushd builddir/wasi

0 commit comments

Comments
 (0)