Skip to content

Commit

Permalink
Compile sqlite wasm files
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 27, 2025
1 parent 51a6061 commit 8e87ca9
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 2 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: macos-latest
permissions:
contents: write

Expand All @@ -21,13 +21,23 @@ jobs:
with:
flutter-version: '3.x'
channel: 'stable'
- name: Setup Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Setup macOS build dependencies
run: brew install cmake llvm binaryen wasi-libc wasi-runtimes

- name: Install Melos
run: flutter pub global activate melos

- name: Install Dependencies and Compile Assets
run: melos prepare

- name: Compile sqlite3.wasm on macOS
if: runner.os == 'macOS'
working-directory: packages/sqlite3_wasm_build
run: ./build.sh

- name: Create Draft Release
env:
GH_TOKEN: ${{ github.token }}
Expand All @@ -39,4 +49,4 @@ jobs:
body="Release $tag
$changes"
gh release create "$tag" --title "$tag" --notes "$body"
gh release upload "${{ github.ref_name }}" packages/powersync/assets/powersync_db.worker.js packages/powersync/assets/powersync_sync.worker.js
gh release upload "${{ github.ref_name }}" packages/powersync/assets/powersync_db.worker.js packages/powersync/assets/powersync_sync.worker.js packages/sqlite3_wasm_build/dist/*.wasm
24 changes: 24 additions & 0 deletions .github/workflows/test_wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build SQLite3 WASM

on:
push:
branches: ["**"]

jobs:
compile_sqlite3_wasm:
name: Compile sqlite3 wasm
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- name: Setup Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Install Dart SDK
uses: dart-lang/setup-dart@v1
- name: Setup macOS build dependencies
run: brew install cmake llvm binaryen wasi-libc wasi-runtimes
- name: Compile sqlite3.wasm on macOS
if: runner.os == 'macOS'
working-directory: packages/sqlite3_wasm_build
run: ./build.sh
4 changes: 4 additions & 0 deletions packages/sqlite3_wasm_build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
pnpm-lock.yaml
sqlite3.dart
dist
17 changes: 17 additions & 0 deletions packages/sqlite3_wasm_build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This directory includes tools to build `sqlite3.wasm` files compatible with the
PowerSync Dart SDK.

This build process is adapted from [`package:sqlite3`][upstream], with patches
applied to link the [PowerSync SQLite extension][core] statically.

### Working on patches

To adapt the patches:

1. Clone `https://github.com/simolus3/sqlite3.dart.git` somewhere.
2. Apply the existing patches to your clone: `git apply patches/*`.
3. Rebase onto a new upstream release if necessary.
4. Obtain a new patchset with `git format-patch upstreamref..HEAD`.

[upstream]: https://github.com/simolus3/sqlite3.dart/tree/main/sqlite3/assets/wasm
[core]: https://github.com/powersync-ja/powersync-sqlite-core
29 changes: 29 additions & 0 deletions packages/sqlite3_wasm_build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
set -e

SQLITE_VERSION="2.7.2"
POWERSYNC_CORE_VERSION="0.3.9"
SQLITE_PATH="sqlite3.dart"

if [ -d "$SQLITE_PATH" ]; then
echo "Deleting existing clone"
rm -rf $SQLITE_PATH
fi

git clone --branch "sqlite3-$SQLITE_VERSION" --depth 1 https://github.com/simolus3/sqlite3.dart.git $SQLITE_PATH

cd $SQLITE_PATH
git apply ../patches/*

cd "sqlite3/"
dart pub get # We need the analyzer dependency resolved to extract required symbols

cmake -Dwasi_sysroot=/opt/homebrew/share/wasi-sysroot \
-Dclang=/opt/homebrew/opt/llvm/bin/clang\
-DPOWERSYNC_VERSION="$POWERSYNC_CORE_VERSION" \
-S assets/wasm -B .dart_tool/sqlite3_build
cmake --build .dart_tool/sqlite3_build/ -t output -j

cd ../../
mkdir -p dist
cp $SQLITE_PATH/sqlite3/example/web/*.wasm dist
10 changes: 10 additions & 0 deletions packages/sqlite3_wasm_build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "dart_powersync_wasm",
"version": "0.0.1",
"description": "WebAssembly modules containing SQLite + PowerSync for Dart apps.",
"repository": {
"type": "git",
"url": "git+https://github.com/powersync-ja/powersync.dart.git",
"directory": "packages/sqlite3_wasm_build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
From 04fd5a3a01be7b42ec97c2d9865e4c7501b5b6a8 Mon Sep 17 00:00:00 2001
From: Simon Binder <[email protected]>
Date: Mon, 27 Jan 2025 15:12:53 +0100
Subject: [PATCH] Link PowerSync core extension

---
sqlite3/assets/wasm/CMakeLists.txt | 7 ++++++-
sqlite3/assets/wasm/os_web.c | 11 ++++++++++-
sqlite3/assets/wasm/sqlite_cfg.h | 2 +-
3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/sqlite3/assets/wasm/CMakeLists.txt b/sqlite3/assets/wasm/CMakeLists.txt
index e4283b3..5c52593 100644
--- a/sqlite3/assets/wasm/CMakeLists.txt
+++ b/sqlite3/assets/wasm/CMakeLists.txt
@@ -6,6 +6,7 @@ project(${PROJECT_NAME} LANGUAGES C)
set(triple wasm32-unknown-wasi)
set(wasi_sysroot "/usr/share/wasi-sysroot" CACHE PATH "Path to wasi sysroot")
set(clang "clang" CACHE FILEPATH "Path to wasm-capable clang executable")
+set(POWERSYNC_VERSION "0.3.9" CACHE STRING "PowerSync core version to use")

include(FetchContent)

@@ -27,6 +28,9 @@ FetchContent_MakeAvailable(sqlite3mc)

file(DOWNLOAD https://raw.githubusercontent.com/sqlite/sqlite/master/src/test_vfstrace.c "${CMAKE_BINARY_DIR}/vfstrace.c")

+set(POWERSYNC_A "${CMAKE_BINARY_DIR}/libpowersync-wasm.a")
+file(DOWNLOAD "https://github.com/powersync-ja/powersync-sqlite-core/releases/download/v${POWERSYNC_VERSION}/libpowersync-wasm.a" "${POWERSYNC_A}")
+
# Generate symbols we need to export from the sqlite3.wasm build
add_custom_command(
OUTPUT required_symbols.txt
@@ -88,8 +92,9 @@ macro(base_sqlite3_target name debug crypto)
-Wl,--import-memory
--sysroot ${wasi_sysroot}
${sources}
+ ${POWERSYNC_A}
@${CMAKE_CURRENT_BINARY_DIR}/required_symbols.txt
- DEPENDS ${sources} required_symbols
+ DEPENDS ${sources} ${POWERSYNC_A} required_symbols
VERBATIM
)

diff --git a/sqlite3/assets/wasm/os_web.c b/sqlite3/assets/wasm/os_web.c
index 04f2e97..666902d 100644
--- a/sqlite3/assets/wasm/os_web.c
+++ b/sqlite3/assets/wasm/os_web.c
@@ -4,6 +4,15 @@

#include "sqlite3.h"

-int sqlite3_os_init(void) { return SQLITE_OK; }
+extern int sqlite3_powersync_init(sqlite3 *db, char **pzErrMsg,
+ const sqlite3_api_routines *pApi);
+
+int sqlite3_os_init(void) {
+ int rc = sqlite3_auto_extension((void (*)(void)) & sqlite3_powersync_init);
+ if (rc != SQLITE_OK) {
+ return rc;
+ }
+ return SQLITE_OK;
+}

int sqlite3_os_end(void) { return SQLITE_OK; }
diff --git a/sqlite3/assets/wasm/sqlite_cfg.h b/sqlite3/assets/wasm/sqlite_cfg.h
index 6f3e3c2..45847f8 100644
--- a/sqlite3/assets/wasm/sqlite_cfg.h
+++ b/sqlite3/assets/wasm/sqlite_cfg.h
@@ -43,7 +43,7 @@
// Disable things we don't need
#define SQLITE_OMIT_DEPRECATED
#define SQLITE_OMIT_PROGRESS_CALLBACK
-#define SQLITE_OMIT_AUTHORIZATION
+// #define SQLITE_OMIT_AUTHORIZATION // This breaks the powersync-core build
#define SQLITE_UNTESTABLE
#define SQLITE_OMIT_COMPILEOPTION_DIAGS
#define SQLITE_OMIT_LOAD_EXTENSION
--
2.39.5 (Apple Git-154)

0 comments on commit 8e87ca9

Please sign in to comment.