Skip to content

Draft: Compile LFortran itself to WASM

Ondřej Čertík requested to merge certik/lfortran:wasm into master

Current status: everything compiles (without LLVM) and few simple things disabled.

Steps to compile:

  1. Install emscripten using: https://emscripten.org/docs/getting_started/downloads.html, it should work on all platforms. I tested on Apple M1. No root required.

  2. ./build0.sh (as usual)

  3. Load emscripten using source ./emsdk_env.sh (per the downloads manual) and:

emcmake cmake \
    -DCMAKE_BUILD_TYPE=Debug \
    -DWITH_LLVM=no \
    -DLFORTRAN_BUILD_ALL=yes \
    -DWITH_STACKTRACE=no \
    -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH_LFORTRAN;$CONDA_PREFIX" \
    -DCMAKE_INSTALL_PREFIX=`pwd`/inst \
    .
cmake --build . -j16 --target install

Everything will compile. It fails while compiling the Fortran part of the runtime library because the lfortran executable does not exist. It creates lfortran.js and lfortran.wasm.

To run, this currently fails:

$ node src/bin/lfortran.js -- -h                 

/Users/certik/repos/lfortran/lfortran/src/bin/lfortran.js:141
      throw ex;
      ^
5555320 - Exception catching is disabled, this exception cannot be caught. Compile with -s NO_DISABLE_EXCEPTION_CATCHING or -s EXCEPTION_CATCHING_ALLOWED=[..] to catch.
(Use `node --trace-uncaught ...` to show where the exception was thrown)

Not sure if it is supposed to work.

The main step now is to figure out how to actually run it, first locally and then in the browser.

Given that we skipped LLVM, we might need to write our own WASM backend in LFortran.

Exceptions I think don't work, but we don't need exceptions at this point. They are only used to report compiler errors and only locally and we can rework it not to need them at all.

Merge request reports