| 1 |
= RunningOnWASI |
| 2 |
|
| 3 |
https://wasi.dev[WASI] is a somewhat POSIX-compatible system interface |
| 4 |
for writing https://webassembly.org[WebAssembly] (Wasm) programs. |
| 5 |
|
| 6 |
MLton does not run on WASI directly (since it requires unimplemented |
| 7 |
functionality), but it can cross-compile to this target, resuling in |
| 8 |
binaries that can run in the browser (using a shim) and from the |
| 9 |
command line (using something like https://wasmtime.dev[Wasmtime]). |
| 10 |
|
| 11 |
== Notes |
| 12 |
|
| 13 |
* To compile MLton for WASI: |
| 14 |
|
| 15 |
** You will need a WASI compiler and sysroot. These are both included |
| 16 |
in https://github.com/WebAssembly/wasi-sdk[wasi-sdk]. Follow the |
| 17 |
directions to download and unpack a copy. |
| 18 |
|
| 19 |
** The <<GMP#>> library is required. It must be compiled for |
| 20 |
WASI. Download it from https://gmplib.org, build it and install it |
| 21 |
to a local prefix. An example configuration command is below |
| 22 |
(substitute your own wasi-sdk paths): |
| 23 |
+ |
| 24 |
[,shell] |
| 25 |
---- |
| 26 |
./configure --host=wasm32-wasi \ |
| 27 |
--prefix=$HOME/gmp-wasi-INSTALL \ |
| 28 |
CC=$HOME/wasi-sdk-22.0/bin/clang \ |
| 29 |
RANLIB=$HOME/wasi-sdk-22.0/bin/ranlib \ |
| 30 |
CFLAGS=-D_WASI_EMULATED_SIGNAL LDFLAGS=-lwasi-emulated-signal |
| 31 |
---- |
| 32 |
|
| 33 |
** During compilation of MLton, some files are generated, compiled, and |
| 34 |
ran. When compiling the MLton runtime for WASI, you will need a way |
| 35 |
of executing these binaries. Install https://wasmtime.dev[Wasmtime] |
| 36 |
to do this. |
| 37 |
|
| 38 |
** MLton must be compiled and installed, then the MLton WASI runtime |
| 39 |
target must be compiled and installed. Build and install MLton normally, |
| 40 |
then clean the source directory and build the WASI runtime. An example |
| 41 |
to build and install the WASI runtime is: |
| 42 |
+ |
| 43 |
[,shell] |
| 44 |
---- |
| 45 |
gmake CC=$HOME/wasi-sdk-22.0/bin/clang \ |
| 46 |
AR=$HOME/wasi-sdk-22.0/bin/ar \ |
| 47 |
RANLIB=$HOME/wasi-sdk-22.0/bin/ranlib \ |
| 48 |
TARGET_OS=wasi \ |
| 49 |
TARGET_ARCH=wasm32 \ |
| 50 |
TARGET=wasm32-wasi \ |
| 51 |
WITH_GMP_DIR=$HOME/gmp-wasi-INSTALL \ |
| 52 |
PREFIX=$HOME/mlton-INSTALL \ |
| 53 |
dirs runtime install-runtime |
| 54 |
---- |
| 55 |
+ |
| 56 |
Once this is complete, you should have a functional MLton that can |
| 57 |
cross-compile to WASI. |
| 58 |
|
| 59 |
* Compiling WASI binaries with MLton: |
| 60 |
|
| 61 |
** Once installed, you can run MLton with the |
| 62 |
``-target wasm32-wasi`` flag, to build WASI binaries: |
| 63 |
+ |
| 64 |
[,shell] |
| 65 |
---- |
| 66 |
mlton -target wasm32-wasi hello-world.sml |
| 67 |
---- |
| 68 |
|
| 69 |
** Using Wasmtime, you can run your WASI binaries locally: |
| 70 |
+ |
| 71 |
[,shell] |
| 72 |
---- |
| 73 |
wasmtime hello-world |
| 74 |
---- |
| 75 |
+ |
| 76 |
---- |
| 77 |
Hello, World! |
| 78 |
---- |
| 79 |
|
| 80 |
== Known issues |
| 81 |
|
| 82 |
* Many functions are unimplemented and will cause link errors during |
| 83 |
compilation, allowing for early detection of incompatibilities. See |
| 84 |
https://github.com/WebAssembly/wasi-libc[wasi-libc] for what is |
| 85 |
available. In general, anything listed in ``platform/wasi.h`` is |
| 86 |
unavailable for use. |
| 87 |
|
| 88 |
* Disk-backed heap is not supported. Do not set ``may-page-heap`` to |
| 89 |
``true``. |
| 90 |
|
| 91 |
* The maximum amount of available memory is fixed. See |
| 92 |
``platform/wasi.c`` for the current value. |
| 93 |
|
| 94 |
* The WASI runtime is restrictive regarding file IO and other |
| 95 |
operations. You will need to map directories for use by compiled |
| 96 |
binaries. See the Wasmtime help for details on how to do this. |
| 97 |
|
| 98 |
* MLton currently only supports WASI, which requires a shim to run in |
| 99 |
browsers. A few options are available: |
| 100 |
|
| 101 |
** https://github.com/bjorn3/browser_wasi_shim |
| 102 |
** https://runno.dev/wasi |