Skip to content

Commit e3dad14

Browse files
authored
Merge pull request #35 from powersync-ja/wasm-side-module
WASM builds
2 parents aa09d5d + 5d20c09 commit e3dad14

File tree

8 files changed

+114
-34
lines changed

8 files changed

+114
-34
lines changed

.github/workflows/release.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,47 @@ jobs:
237237
repo-token: ${{ secrets.GITHUB_TOKEN }}
238238
file-name: libpowersync_x64.dylib
239239
tag: ${{ needs.draft_release.outputs.tag }}
240+
241+
publish_wasm:
242+
name: Publish WASM builds
243+
needs: [draft_release]
244+
runs-on: ubuntu-latest
245+
steps:
246+
- uses: actions/checkout@v3
247+
with:
248+
submodules: true
249+
250+
- name: Install Rust Nightly
251+
uses: dtolnay/rust-toolchain@stable
252+
with:
253+
toolchain: nightly-2024-05-18
254+
components: rust-src
255+
256+
- name: Setup emsdk
257+
uses: mymindstorm/setup-emsdk@v14
258+
with:
259+
version: 3.1.68
260+
261+
- name: Build WASM
262+
run: ./tool/build_wasm.sh
263+
264+
- name: Upload libpowersync.wasm
265+
uses: ./.github/actions/upload
266+
with:
267+
repo-token: ${{ secrets.GITHUB_TOKEN }}
268+
file-name: libpowersync.wasm
269+
tag: ${{ needs.draft_release.outputs.tag }}
270+
271+
- name: Upload libpowersync-async.wasm
272+
uses: ./.github/actions/upload
273+
with:
274+
repo-token: ${{ secrets.GITHUB_TOKEN }}
275+
file-name: libpowersync-async.wasm
276+
tag: ${{ needs.draft_release.outputs.tag }}
277+
278+
- name: Upload libpowersync-wasm.a
279+
uses: ./.github/actions/upload
280+
with:
281+
repo-token: ${{ secrets.GITHUB_TOKEN }}
282+
file-name: libpowersync-wasm.a
283+
tag: ${{ needs.draft_release.outputs.tag }}

.github/workflows/wasm.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on:
22
push:
3-
name: "linux"
3+
name: "wasm"
44
jobs:
55
build_wasm:
66
name: Basic WASM build
@@ -16,5 +16,10 @@ jobs:
1616
toolchain: nightly-2024-05-18
1717
components: rust-src
1818

19-
- name: Build WASM bytecode
20-
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
19+
- name: Setup emsdk
20+
uses: mymindstorm/setup-emsdk@v14
21+
with:
22+
version: 3.1.68
23+
24+
- name: Build WASM
25+
run: ./tool/build_wasm.sh

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ lto = true
1919

2020
[profile.wasm]
2121
inherits = "release"
22-
# Enabling LTO in WASM build gives:
23-
# warning: Linking globals named '__rust_alloc_error_handler': symbol multiply defined!
24-
# error: failed to load bitcode of module "sgnxivc9sns8d6t":
25-
lto = false
22+
23+
[profile.wasm_asyncify]
24+
inherits = "wasm"
2625

2726
[workspace.package]
2827
version = "0.3.0"

crates/core/src/kv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn powersync_client_id_impl(
3636
create_sqlite_text_fn!(
3737
powersync_client_id,
3838
powersync_client_id_impl,
39-
"powersync_last_synced_at"
39+
"powersync_client_id"
4040
);
4141

4242
fn powersync_last_synced_at_impl(

crates/loadable/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ keywords.workspace = true
1010

1111
[lib]
1212
name = "powersync"
13-
crate-type = ["cdylib"]
13+
crate-type = ["cdylib", "staticlib"]
1414

1515
[dependencies]
1616
sqlite_nostd = { workspace=true }

crates/loadable/src/lib.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,3 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
3030
#[cfg(not(test))]
3131
#[lang = "eh_personality"]
3232
extern "C" fn eh_personality() {}
33-
34-
#[cfg(target_family = "wasm")]
35-
#[no_mangle]
36-
pub fn __rust_alloc_error_handler(_: core::alloc::Layout) -> ! {
37-
core::intrinsics::abort()
38-
}
39-
40-
#[cfg(target_family = "wasm")]
41-
#[no_mangle]
42-
static __rust_alloc_error_handler_should_panic: u8 = 0;
43-
44-
#[cfg(target_family = "wasm")]
45-
#[no_mangle]
46-
static _CLOCK_PROCESS_CPUTIME_ID: i32 = 1;
47-
48-
#[cfg(target_family = "wasm")]
49-
#[no_mangle]
50-
static _CLOCK_THREAD_CPUTIME_ID: i32 = 1;
51-
52-
// Not used, but must be defined in some cases. Most notably when using native sqlite3 and loading
53-
// the extension.
54-
// #[allow(non_upper_case_globals)]
55-
// #[no_mangle]
56-
// pub static mut _Unwind_Resume: *mut core::ffi::c_void = core::ptr::null_mut();
57-

tool/build_wasm.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Normal build
5+
# target/wasm32-unknown-emscripten/wasm/powersync.wasm
6+
RUSTFLAGS="-C link-arg=-sSIDE_MODULE=2" \
7+
cargo build \
8+
-p powersync_loadable \
9+
--profile wasm \
10+
--no-default-features \
11+
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
12+
-Z build-std=panic_abort,core,alloc \
13+
--target wasm32-unknown-emscripten
14+
15+
cp "target/wasm32-unknown-emscripten/wasm/powersync.wasm" "libpowersync.wasm"
16+
17+
# Asyncify
18+
# target/wasm32-unknown-emscripten/wasm_asyncify/powersync.wasm
19+
RUSTFLAGS="-C link-arg=-sSIDE_MODULE=2 -C link-arg=-sASYNCIFY=1 -C link-arg=-sJSPI_IMPORTS=@wasm/asyncify_imports.json" \
20+
cargo build \
21+
-p powersync_loadable \
22+
--profile wasm_asyncify \
23+
--no-default-features \
24+
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
25+
-Z build-std=panic_abort,core,alloc \
26+
--target wasm32-unknown-emscripten
27+
28+
cp "target/wasm32-unknown-emscripten/wasm_asyncify/powersync.wasm" "libpowersync-async.wasm"
29+
30+
31+
# Static lib.
32+
# Works for both sync and asyncify builds.
33+
# Works for both emscripten and wasi.
34+
# target/wasm32-wasi/wasm/libpowersync.a
35+
cargo build \
36+
-p powersync_loadable \
37+
--profile wasm \
38+
--no-default-features \
39+
--features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/omit_load_extension" \
40+
-Z build-std=panic_abort,core,alloc \
41+
--target wasm32-wasi
42+
43+
cp "target/wasm32-wasi/wasm/libpowersync.a" "libpowersync-wasm.a"

wasm/asyncify_imports.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
"sqlite3_close",
3+
"sqlite3_finalize",
4+
"sqlite3_open_v2",
5+
"sqlite3_prepare",
6+
"sqlite3_prepare16",
7+
"sqlite3_prepare_v2",
8+
"sqlite3_prepare16_v2",
9+
"sqlite3_prepare_v3",
10+
"sqlite3_prepare16_v3",
11+
"sqlite3_reset",
12+
"sqlite3_step",
13+
"sqlite3_exec"
14+
]

0 commit comments

Comments
 (0)