Skip to content

Commit e8f3e8f

Browse files
gh-95205: Improve WASM README.md (GH-95267)
Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 4395ff1 commit e8f3e8f

File tree

2 files changed

+112
-12
lines changed

2 files changed

+112
-12
lines changed

Tools/wasm/README.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ popd
6969
```
7070

7171
Serve `python.html` with a local webserver and open the file in a browser.
72-
73-
```shell
74-
emrun builddir/emscripten-browser/python.html
75-
```
76-
77-
or
72+
Python comes with a minimal web server script that sets necessary HTTP
73+
headers like COOP, COEP, and mimetypes. Run the script outside the container
74+
and from the root of the CPython checkout.
7875

7976
```shell
8077
./Tools/wasm/wasm_webserver.py
@@ -84,6 +81,7 @@ and open http://localhost:8000/builddir/emscripten-browser/python.html . This
8481
directory structure enables the *C/C++ DevTools Support (DWARF)* to load C
8582
and header files with debug builds.
8683

84+
8785
### Cross compile to wasm32-emscripten for node
8886

8987
```shell
@@ -232,6 +230,28 @@ WASI builds require [WASI SDK](https://github.com/WebAssembly/wasi-sdk) 15.0+
232230
and currently [wasix](https://github.com/singlestore-labs/wasix) for POSIX
233231
compatibility stubs.
234232

233+
## Cross-compile to wasm32-wasi
234+
235+
The script ``wasi-env`` sets necessary compiler and linker flags as well as
236+
``pkg-config`` overrides. The script assumes that WASI-SDK is installed in
237+
``/opt/wasi-sdk`` or ``$WASI_SDK_PATH``.
238+
239+
```shell
240+
mkdir -p builddir/wasi
241+
pushd builddir/wasi
242+
243+
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-wasi \
244+
CFLAGS="-isystem /opt/wasix/include" \
245+
LDFLAGS="-L/opt/wasix/lib -lwasix" \
246+
../../Tools/wasm/wasi-env ../../configure -C \
247+
--host=wasm32-unknown-wasi \
248+
--build=$(../../config.guess) \
249+
--with-build-python=$(pwd)/../build/python
250+
251+
make -j$(nproc)
252+
popd
253+
```
254+
235255
## WASI limitations and issues (WASI SDK 15.0)
236256

237257
A lot of Emscripten limitations also apply to WASI. Noticable restrictions
@@ -376,6 +396,16 @@ git clone https://github.com/emscripten-core/emsdk.git /opt/emsdk
376396
/opt/emsdk/emsdk activate latest
377397
```
378398

399+
### Optionally: enable ccache for EMSDK
400+
401+
The ``EM_COMPILER_WRAPPER`` must be set after the EMSDK environment is
402+
sourced. Otherwise the source script removes the environment variable.
403+
404+
```
405+
. /opt/emsdk/emsdk_env.sh
406+
EM_COMPILER_WRAPPER=ccache
407+
```
408+
379409
### Optionally: pre-build and cache static libraries
380410

381411
Emscripten SDK provides static builds of core libraries without PIC
@@ -384,12 +414,8 @@ PIC. To populate the build cache, run:
384414

385415
```shell
386416
. /opt/emsdk/emsdk_env.sh
387-
embuilder build --force zlib bzip2
388-
embuilder build --force --pic \
389-
zlib bzip2 libc-mt libdlmalloc-mt libsockets-mt \
390-
libstubs libcompiler_rt libcompiler_rt-mt crtbegin libhtml5 \
391-
libc++-mt-noexcept libc++abi-mt-noexcept \
392-
libal libGL-mt libstubs-debug libc-mt-debug
417+
embuilder build zlib bzip2 MINIMAL_PIC
418+
embuilder build --pic zlib bzip2 MINIMAL_PIC
393419
```
394420

395421
### Install [WASI-SDK](https://github.com/WebAssembly/wasi-sdk)
@@ -424,3 +450,9 @@ ln -srf -t /usr/local/bin/ ~/.wasmtime/bin/wasmtime
424450
git clone https://github.com/singlestore-labs/wasix.git ~/wasix
425451
make install -C ~/wasix
426452
```
453+
454+
### WASI debugging
455+
456+
* ``wasmtime run -g`` generates debugging symbols for gdb and lldb.
457+
* The environment variable ``RUST_LOG=wasi_common`` enables debug and
458+
trace logging.

Tools/wasm/wasi-env

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# function
5+
usage() {
6+
echo "wasi-env - Run command with WASI-SDK"
7+
echo ""
8+
echo "wasi-env is a helper to set various environment variables to"
9+
echo "run configure and make with WASI-SDK. A WASI-SDK must be either"
10+
echo "installed at /opt/wasi-sdk or the env var 'WASI_SDK_PATH' must"
11+
echo "set to the root of a WASI-SDK."
12+
echo ""
13+
echo "Usage: wasi-env command [...]"
14+
echo ""
15+
echo " -h --help display this help and exit"
16+
echo ""
17+
}
18+
19+
case $1 in
20+
-h|--help)
21+
usage
22+
exit
23+
;;
24+
esac
25+
26+
if test -z "$1"; then
27+
echo "ERROR: command required" >&2
28+
usage
29+
exit 1
30+
fi
31+
32+
WASI_SDK_PATH="${WASI_SDK_PATH:-/opt/wasi-sdk}"
33+
34+
if ! test -x "${WASI_SDK_PATH}/bin/clang"; then
35+
echo "Error: ${WASI_SDK_PATH}/bin/clang does not exist." >&2
36+
exit 2
37+
fi
38+
39+
# --sysroot is required if WASI-SDK is not installed in /opt/wasi-sdk.
40+
WASI_SYSROOT="${WASI_SDK_PATH}/share/wasi-sysroot"
41+
CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SYSROOT}"
42+
CPP="${WASI_SDK_PATH}/bin/clang-cpp --sysroot=${WASI_SYSROOT}"
43+
CXX="${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SYSROOT}"
44+
45+
# use ccache if available
46+
if command -v ccache >/dev/null 2>&1; then
47+
CC="ccache ${CC}"
48+
CPP="ccache ${CPP}"
49+
CXX="ccache ${CXX}"
50+
fi
51+
52+
LDSHARED="${WASI_SDK_PATH}/bin/wasm-ld"
53+
AR="${WASI_SDK_PATH}/bin/llvm-ar"
54+
RANLIB="${WASI_SDK_PATH}/bin/ranlib"
55+
56+
# instruct pkg-config to use sysroot
57+
PKG_CONFIG_PATH=""
58+
PKG_CONFIG_LIBDIR="${WASI_SYSROOT}/lib/pkgconfig:${WASI_SYSROOT}/share/pkgconfig"
59+
PKG_CONFIG_SYSROOT_DIR="${WASI_SYSROOT}"
60+
61+
PATH="${WASI_SDK_PATH}/bin:$PATH"
62+
63+
export WASI_SDK_PATH
64+
export CC CPP CXX LDSHARED AR RANLIB
65+
export PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR
66+
export PATH
67+
68+
exec "$@"

0 commit comments

Comments
 (0)