Skip to content

WASM builds #35

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

Merged
merged 11 commits into from
Oct 14, 2024
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
44 changes: 44 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,47 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}
file-name: libpowersync_x64.dylib
tag: ${{ needs.draft_release.outputs.tag }}

publish_wasm:
name: Publish WASM builds
needs: [draft_release]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Install Rust Nightly
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly-2024-05-18
components: rust-src

- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v14
with:
version: 3.1.68

- name: Build WASM
run: ./tool/build_wasm.sh

- name: Upload libpowersync.wasm
uses: ./.github/actions/upload
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
file-name: libpowersync.wasm
tag: ${{ needs.draft_release.outputs.tag }}

- name: Upload libpowersync-async.wasm
uses: ./.github/actions/upload
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
file-name: libpowersync-async.wasm
tag: ${{ needs.draft_release.outputs.tag }}

- name: Upload libpowersync-wasm.a
uses: ./.github/actions/upload
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
file-name: libpowersync-wasm.a
tag: ${{ needs.draft_release.outputs.tag }}
11 changes: 8 additions & 3 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
on:
push:
name: "linux"
name: "wasm"
jobs:
build_wasm:
name: Basic WASM build
Expand All @@ -16,5 +16,10 @@ jobs:
toolchain: nightly-2024-05-18
components: rust-src

- name: Build WASM bytecode
run: RUSTFLAGS="--emit=llvm-bc -C linker=/bin/true" cargo build -p powersync_loadable --profile wasm --no-default-features --features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/static sqlite_nostd/omit_load_extension" -Z build-std=panic_abort,core,alloc --target wasm32-unknown-emscripten
- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v14
with:
version: 3.1.68

- name: Build WASM
run: ./tool/build_wasm.sh
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ lto = true

[profile.wasm]
inherits = "release"
# Enabling LTO in WASM build gives:
# warning: Linking globals named '__rust_alloc_error_handler': symbol multiply defined!
# error: failed to load bitcode of module "sgnxivc9sns8d6t":
lto = false

[profile.wasm_asyncify]
inherits = "wasm"

[workspace.package]
version = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn powersync_client_id_impl(
create_sqlite_text_fn!(
powersync_client_id,
powersync_client_id_impl,
"powersync_last_synced_at"
"powersync_client_id"
);

fn powersync_last_synced_at_impl(
Expand Down
2 changes: 1 addition & 1 deletion crates/loadable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords.workspace = true

[lib]
name = "powersync"
crate-type = ["cdylib"]
crate-type = ["cdylib", "staticlib"]

[dependencies]
sqlite_nostd = { workspace=true }
Expand Down
25 changes: 0 additions & 25 deletions crates/loadable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,3 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
#[cfg(not(test))]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[cfg(target_family = "wasm")]
#[no_mangle]
pub fn __rust_alloc_error_handler(_: core::alloc::Layout) -> ! {
core::intrinsics::abort()
}

#[cfg(target_family = "wasm")]
#[no_mangle]
static __rust_alloc_error_handler_should_panic: u8 = 0;

#[cfg(target_family = "wasm")]
#[no_mangle]
static _CLOCK_PROCESS_CPUTIME_ID: i32 = 1;

#[cfg(target_family = "wasm")]
#[no_mangle]
static _CLOCK_THREAD_CPUTIME_ID: i32 = 1;

// Not used, but must be defined in some cases. Most notably when using native sqlite3 and loading
// the extension.
// #[allow(non_upper_case_globals)]
// #[no_mangle]
// pub static mut _Unwind_Resume: *mut core::ffi::c_void = core::ptr::null_mut();

43 changes: 43 additions & 0 deletions tool/build_wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
set -e

# Normal build
# target/wasm32-unknown-emscripten/wasm/powersync.wasm
RUSTFLAGS="-C link-arg=-sSIDE_MODULE=2" \
cargo build \
-p powersync_loadable \
--profile wasm \
--no-default-features \
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
-Z build-std=panic_abort,core,alloc \
--target wasm32-unknown-emscripten

cp "target/wasm32-unknown-emscripten/wasm/powersync.wasm" "libpowersync.wasm"

# Asyncify
# target/wasm32-unknown-emscripten/wasm_asyncify/powersync.wasm
RUSTFLAGS="-C link-arg=-sSIDE_MODULE=2 -C link-arg=-sASYNCIFY=1 -C link-arg=-sJSPI_IMPORTS=@wasm/asyncify_imports.json" \
cargo build \
-p powersync_loadable \
--profile wasm_asyncify \
--no-default-features \
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
-Z build-std=panic_abort,core,alloc \
--target wasm32-unknown-emscripten

cp "target/wasm32-unknown-emscripten/wasm_asyncify/powersync.wasm" "libpowersync-async.wasm"


# Static lib.
# Works for both sync and asyncify builds.
# Works for both emscripten and wasi.
# target/wasm32-wasi/wasm/libpowersync.a
cargo build \
-p powersync_loadable \
--profile wasm \
--no-default-features \
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
-Z build-std=panic_abort,core,alloc \
--target wasm32-wasi

cp "target/wasm32-wasi/wasm/libpowersync.a" "libpowersync-wasm.a"
14 changes: 14 additions & 0 deletions wasm/asyncify_imports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
"sqlite3_close",
"sqlite3_finalize",
"sqlite3_open_v2",
"sqlite3_prepare",
"sqlite3_prepare16",
"sqlite3_prepare_v2",
"sqlite3_prepare16_v2",
"sqlite3_prepare_v3",
"sqlite3_prepare16_v3",
"sqlite3_reset",
"sqlite3_step",
"sqlite3_exec"
]
Loading