Skip to content

Commit e90a0df

Browse files
committed
Add Emscripten Build Instructions
1 parent 4959cce commit e90a0df

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Emscripten-build-instructions.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Wasm Build Instructions
2+
3+
It should be noted that the wasm build of CppInterOp is still experimental and subject to change to change.
4+
5+
## CppInterOp Wasm Build Instructions
6+
7+
This document first starts with the instructions on how to build a wasm build of CppInterOp. Before we start it should be noted that
8+
unlike the non wasm version of CppInterOp we currently only support the Clang-REPL backend using llvm>19 for osx and Linux.
9+
We will first make folder to build our wasm build of CppInterOp. This can be done by executing the following command
10+
11+
```bash
12+
mkdir CppInterOp-wasm
13+
```
14+
15+
Now move into this directory using the following command
16+
17+
```bash
18+
cd ./CppInterOp-wasm
19+
```
20+
21+
To create a wasm build of CppInterOp we make use of the emsdk toolchain. This can be installed by executing (we only currently
22+
support version 3.1.45)
23+
```bash
24+
git clone https://github.com/emscripten-core/emsdk.git
25+
cd emsdk
26+
./emsdk install 3.1.45
27+
```
28+
29+
Go back upto the top level build directory using
30+
31+
```bash
32+
cd ../..
33+
```
34+
35+
and activate the emsdk environment
36+
37+
```bash
38+
./emsdk/emsdk activate 3.1.45
39+
source ./emsdk/emsdk_env.sh
40+
```
41+
42+
Now clone the 19.x release of the LLVM project repository and CppInterOp (the building of the emscripten version of llvm can be
43+
avoided by executing micromamba install llvm -c <https://repo.mamba.pm/emscripten-forge> and setting the LLVM_BUILD_DIR appropriately)
44+
45+
46+
```bash
47+
git clone --depth=1 --branch release/19.x https://github.com/llvm/llvm-project.git
48+
git clone --depth=1 https://github.com/compiler-research/CppInterOp.git
49+
```
50+
51+
Now move into the cloned llvm-project folder and apply a required patch
52+
53+
```bash
54+
cd ./llvm-project/
55+
git apply -v ../CppInterOp/patches/llvm/emscripten-clang${{ matrix.clang-runtime }}-*.patch
56+
```
57+
58+
We are now in a position to build an emscripten build of llvm by executing the following
59+
```bash
60+
mkdir build
61+
cd build
62+
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
63+
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
64+
-DLLVM_ENABLE_ASSERTIONS=ON \
65+
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
66+
-DLLVM_ENABLE_LIBEDIT=OFF \
67+
-DLLVM_ENABLE_PROJECTS="clang;lld" \
68+
-DLLVM_ENABLE_ZSTD=OFF \
69+
-DLLVM_ENABLE_LIBXML2=OFF \
70+
-DCLANG_ENABLE_STATIC_ANALYZER=OFF \
71+
-DCLANG_ENABLE_ARCMT=OFF \
72+
-DCLANG_ENABLE_BOOTSTRAP=OFF \
73+
-DCMAKE_CXX_FLAGS="-Dwait4=__syscall_wait4" \
74+
../llvm
75+
emmake make clang -j $(nproc --all)
76+
emmake make clang-repl -j $(nproc --all)
77+
emmake make lld -j $(nproc --all)
78+
```
79+
80+
Once this finishes building we need to take note of where we built our llvm build. This can be done by executing the following
81+
82+
```bash
83+
export LLVM_BUILD_DIR=$PWD
84+
```
85+
86+
We can move onto building the wasm
87+
version of CppInterOp. To do this execute the following
88+
89+
```bash
90+
cd ../../CppInterOp/
91+
mkdir build
92+
cd ./build/
93+
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
94+
-DUSE_CLING=OFF \
95+
-DUSE_REPL=ON \
96+
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
97+
-DLLD_DIR=$LLVM_BUILD_DIR/lib/cmake/lld \
98+
-DClang_DIR=$LLVM_BUILD_DIR/lib/cmake/clang \
99+
-DBUILD_SHARED_LIBS=ON \
100+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
101+
-DCMAKE_INSTALL_PREFIX=$PREFIX \
102+
../
103+
emmake make -j $(nproc --all)
104+
```bash
105+
106+
Once this finishes building we need to take note of where we built our llvm build. This can be done by executing the following
107+
108+
```bash
109+
export CPPINTEROP_BUILD_DIR=$PWD
110+
```
111+
112+
## Xeus-cpp-lite Wasm Build Instructions
113+
114+
A project which makes use of the wasm build of CppInterOp is xeus-cpp. xeus-cpp is a C++ Jupyter kernel. To build xeus-cpp we first need to
115+
install several other dependencies. To create an Conda environment with this dependencies installed execute the following from the
116+
CppInterOp build folder (these instructions will assume you have already installed micromamba)
117+
118+
```bash
119+
cd ..
120+
micromamba create -f environment-wasm.yml --platform=emscripten-wasm32
121+
micromamba activate CppInterOp-wasm
122+
export PREFIX=$MAMBA_ROOT_PREFIX/envs/CppInterOp-wasm
123+
```
124+
Now we can build an wasm build of xeus-cpp by executing the following
125+
126+
```bash
127+
cd ..
128+
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
129+
cd ./xeus-cpp
130+
mkdir build
131+
cd build
132+
export CMAKE_PREFIX_PATH=$PREFIX
133+
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
134+
emcmake cmake \
135+
-DCMAKE_BUILD_TYPE=Release \
136+
-DCMAKE_PREFIX_PATH=$PREFIX \
137+
-DCMAKE_INSTALL_PREFIX=$PREFIX \
138+
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON \
139+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
140+
-DCppInterOp_DIR="$CPPINTEROP_BUILD_DIR/lib/cmake/CppInterOp" \
141+
..
142+
emmake make -j $(nproc --all) install
143+
```
144+
145+
To build Jupyter Lite with this kernel without creating a website you can execute the following
146+
147+
```bash
148+
cd ../..
149+
micromamba create -n xeus-lite-host jupyterlite-core
150+
micromamba activate xeus-lite-host
151+
python -m pip install jupyterlite-xeus
152+
jupyter lite build --XeusAddon.prefix=$PREFIX
153+
```
154+
155+
Once the Jupyter Lite site has built you can test the website locally by executing
156+
157+
```bash
158+
jupyter lite serve --XeusAddon.prefix=$PREFIX
159+
```

0 commit comments

Comments
 (0)