Cross compiling for WebAssembly with Emscripten#

Prerequisites#

You need CMake and compilers etc. installed as per the normal build instructions. Before building with Emscripten, you also need to install Emscripten andactivate it using the commands below (seehttps://emscripten.org/docs/getting_started/downloads.html for details).

gitclonehttps://github.com/emscripten-core/emsdk.gitcdemsdk# replace <version> with the desired EMSDK version.# e.g. for Pyodide 0.26, you need EMSDK version 3.1.58# the versions can be found in the Makefile.envs file in the Pyodide repo:# https://github.com/pyodide/pyodide/blob/10b484cfe427e076c929a55dc35cfff01ea8d3bc/Makefile.envs./emsdkinstall<version>./emsdkactivate<version>source./emsdk_env.sh

If you want to build PyArrow forPyodide, youneedpyodide-build installed viapip, and to be running with thesame version of Python that Pyodide is built for, along with the sameversions of emsdk tools.

# install Pyodide build tools.# e.g., for version 0.26 of Pyodide, pyodide-build 0.26 and later workpipinstall"pyodide-build>=0.26"

Then build with theninja-release-emscripten CMake preset,like below:

emcmakecmake--preset"ninja-release-emscripten"ninjainstall

This will install a built static library version oflibarrow it into theEmscripten sysroot cache, meaning you can build things that depend on itand they will findlibarrow.

e.g. if you want to build for Pyodide, run the commands above, and thengo toarrow/python and run

pyodidebuild

It should make a wheel targeting the currently enabled version ofPyodide in thedist subdirectory.

Manual Build#

If you want to manually build for Emscripten, take a look at theCMakePresets.json file in thearrow/cpp directory for a list of thingsyou will need to override. In particular you will need:

  1. Build dependencies set toBUNDLED, so it uses properly crosscompiled build dependencies.

  2. CMAKE_TOOLCHAIN_FILE set by usingemcmakecmake instead of justcmake.

  3. You will need to setARROW_ENABLE_THREADING toOFF for buildstargeting single-threaded Emscripten environments such as Pyodide.

  4. ARROW_FLIGHT and anything else that uses network probably won’twork.

  5. ARROW_JEMALLOC andARROW_MIMALLOC again probably need to beOFF

  6. ARROW_BUILD_STATIC set toON andARROW_BUILD_SHARED set toOFF is most likely to work.