Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to build with emscripten #306

Merged
merged 4 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The [EVA compiler for CKKS](https://arxiv.org/abs/1912.11951) is available at [G
- [Installing Microsoft SEAL](#installing-microsoft-seal)
- [Building and Installing on Windows](#building-and-installing-on-windows)
- [Building for Android and iOS](#building-for-android-and-ios)
- [Building for WebAssembly](#building-for-webassembly)
- [Basic CMake Options](#basic-cmake-options)
- [Advanced CMake Options](#advanced-cmake-options)
- [Linking with Microsoft SEAL through CMake](#linking-with-microsoft-seal-through-cmake)
Expand Down Expand Up @@ -312,6 +313,63 @@ lipo -create -output build/lib/libsealc.a build/lib/x86_64/libsealc-*.a build/li
The native libraries generated through these methods are meant to be called only through the .NET library described in the following sections.
Specifically, they do not contain any wrappers that can be used from Java (for Android) or Objective C (for iOS).

#### Building for WebAssembly

Microsoft SEAL can be compiled for JavaScript and WebAssembly using [emscripten](https://emscripten.org). Building for the Web means SEAL can be run in any client/server environment such as all the major browsers (e.g. Edge, Chrome, Firefox, Safari) and NodeJS.

Building for WebAssembly requires the emscripten toolchain to be installed. The easiest way to configure the toolchain is to clone [emsdk](https://github.com/emscripten-core/emsdk) somewhere on your system and follow the instructions in the README.

Inside the `emsdk` repo, run the following:
```PowerShell
# Install the latest toolchain
./emsdk install latest
./emsdk activate latest

# Source the environment
source ./emsdk_env.sh
```

With the same shell, navigate to the root directory of Microsoft SEAL, run the following commands to build for WebAssembly:

```PowerShell
# Configure CMake. Example flags for a release build
emcmake cmake \
-DSEAL_USE_CXX17=ON \
-DCMAKE_CXX_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
-DCMAKE_C_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
-DSEAL_USE_INTRIN=OFF \
-DSEAL_USE_ZLIB=ON \
-DSEAL_USE_MSGSL=OFF \
-DSEAL_BUILD_EXAMPLES=OFF \
-DSEAL_BUILD_TESTS=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DSEAL_THROW_ON_TRANSPARENT_CIPHERTEXT=ON \
-DCMAKE_BUILD_TYPE=Release \
.

# Make the static library (shared libs are not supported with emscripten)
emmake make -j

# Build the WebAssembly module
emcc \
-Wall \
-flto \
-O3 \
lib/libseal-3.6.a \
--bind \
-o "bin/seal_wasm.js" \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1

```
**Note**: There are many flags to consider when building a WebAssembly module. Please refer to the [settings.js](https://github.com/emscripten-core/emscripten/blob/main/src/settings.js) file for advanced build flags.

Building will generate two output files in the top-level `bin/` directory:
- seal_wasm.js
- seal_wasm.wasm

Notice the file sizes for the artifacts are very small. This is because the optimization flags performed DCE as there are no JavaScript <-> WebAssembly "bindings". Defining these bindings is **necessary** in order to call into WebAssembly from the JavaScript domain; however, Microsoft SEAL does not include any definitions at this time. The build flag `--bind` expects the bindings to be specified using the [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html) syntax.

#### Basic CMake Options

The following options can be used with CMake to configure the build. The default value for each option is denoted with boldface in the **Values** column.
Expand Down
4 changes: 3 additions & 1 deletion cmake/CheckCXXIntrinsicsHeader.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ if(SEAL_USE_INTRIN)
SEAL_ARM64
)
if(SEAL_ARM64)
set(SEAL_INTRIN_HEADER "arm_neon.h")
set(SEAL_INTRIN_HEADER "arm_neon.h")
elseif(EMSCRIPTEN)
set(SEAL_INTRIN_HEADER "wasm_simd128.h")
else()
set(SEAL_INTRIN_HEADER "x86intrin.h")
endif()
Expand Down
3 changes: 3 additions & 0 deletions cmake/SEALMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ macro(seal_combine_archives target dependency)
set(DEL_CMD "rm")
set(DEL_CMD_OPTS "-rf")
endif()
if(EMSCRIPTEN)
set(AR_CMD_PATH "emar")
endif()
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${AR_CMD_PATH}" x $<TARGET_FILE:${target}>
COMMAND "${AR_CMD_PATH}" x $<TARGET_FILE:${dependency}>
Expand Down
3 changes: 2 additions & 1 deletion native/src/seal/util/clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
#ifdef SEAL_USE_INTRIN
#if defined(__aarch64__)
#include <arm_neon.h>
#elif defined(EMSCRIPTEN)
#include <wasm_simd128.h>
#else
#include <x86intrin.h>
#endif


#ifdef SEAL_USE___BUILTIN_CLZLL
#define SEAL_MSB_INDEX_UINT64(result, value) \
{ \
Expand Down
2 changes: 1 addition & 1 deletion native/src/seal/util/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static_assert(sizeof(unsigned long long) == 8, "Require sizeof(unsigned long lon

#if defined(_WIN32)
#define SEAL_SYSTEM SEAL_SYSTEM_WINDOWS
#elif defined(__linux__) || defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
#elif defined(__linux__) || defined(__FreeBSD__) || defined(EMSCRIPTEN) || (defined(__APPLE__) && defined(__MACH__))
#define SEAL_SYSTEM SEAL_SYSTEM_UNIX_LIKE
#else
#define SEAL_SYSTEM SEAL_SYSTEM_OTHER
Expand Down
2 changes: 2 additions & 0 deletions native/src/seal/util/gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifdef SEAL_USE_INTRIN
#if defined(__aarch64__)
#include <arm_neon.h>
#elif defined(EMSCRIPTEN)
#include <wasm_simd128.h>
#else
#include <x86intrin.h>
#endif
Expand Down