Skip to content

Commit d042fe2

Browse files
committed
feat: add WASM support with sqlite3_wasm_extra_init function and related build configurations
1 parent 4066dea commit d042fe2

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ jobs:
4343
- os: macos-latest
4444
name: isim
4545
make: PLATFORM=isim
46+
- os: ubuntu-latest
47+
name: wasm
4648

4749
defaults:
4850
run:
@@ -70,6 +72,10 @@ jobs:
7072
run: make curl/windows/libcurl.a
7173
shell: msys2 {0}
7274

75+
- name: wasm install wabt
76+
if: matrix.name == 'wasm'
77+
run: apt install wabt
78+
7379
- name: build sqlite-sync
7480
run: make extension ${{ matrix.make && matrix.make || ''}}
7581

@@ -141,7 +147,9 @@ jobs:
141147
if: always()
142148
with:
143149
name: cloudsync-${{ matrix.name }}${{ matrix.arch && format('-{0}', matrix.arch) || '' }}
144-
path: dist/cloudsync.*
150+
path: |
151+
dist/cloudsync.*
152+
dist/sqlite-wasm.zip
145153
if-no-files-found: error
146154

147155
release:

Makefile

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ SQLITE3 ?= sqlite3
88
# set curl version to download and build
99
CURL_VERSION ?= 8.12.1
1010

11+
# set sqlite version for WASM static build
12+
SQLITE_VERSION ?= 3.50.1
13+
1114
# Set default platform if not specified
1215
ifeq ($(OS),Windows_NT)
1316
PLATFORM := windows
@@ -47,6 +50,7 @@ CURL_DIR = curl
4750
CURL_SRC = $(CURL_DIR)/src/curl-$(CURL_VERSION)
4851
COV_DIR = coverage
4952
CUSTOM_CSS = $(TEST_DIR)/sqliteai.css
53+
BUILD_WASM = build/wasm
5054

5155
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
5256
TEST_SRC = $(wildcard $(TEST_DIR)/*.c)
@@ -110,6 +114,8 @@ else ifeq ($(PLATFORM),isim)
110114
T_LDFLAGS = -framework Security
111115
CFLAGS += -arch x86_64 -arch arm64 $(SDK)
112116
CURL_CONFIG = --host=arm64-apple-darwin --with-secure-transport CFLAGS="-arch x86_64 -arch arm64 -isysroot $$(xcrun --sdk iphonesimulator --show-sdk-path) -miphonesimulator-version-min=11.0"
117+
else ifeq ($(PLATFORM),wasm)
118+
TARGET := $(DIST_DIR)/sqlite-wasm.zip
113119
else # linux
114120
TARGET := $(DIST_DIR)/cloudsync.so
115121
LDFLAGS += -shared -lssl -lcrypto
@@ -127,7 +133,7 @@ endif
127133
# Windows .def file generation
128134
$(DEF_FILE):
129135
ifeq ($(PLATFORM),windows)
130-
@echo "LIBRARY js.dll" > $@
136+
@echo "LIBRARY cloudsync.dll" > $@
131137
@echo "EXPORTS" >> $@
132138
@echo " sqlite3_cloudsync_init" >> $@
133139
endif
@@ -139,12 +145,29 @@ $(shell mkdir -p $(BUILD_DIRS) $(DIST_DIR))
139145
extension: $(TARGET)
140146
all: $(TARGET)
141147

148+
ifneq ($(PLATFORM),wasm)
142149
# Loadable library
143150
$(TARGET): $(RELEASE_OBJ) $(DEF_FILE) $(CURL_LIB)
144151
$(CC) $(RELEASE_OBJ) $(DEF_FILE) -o $@ $(LDFLAGS)
145152
ifeq ($(PLATFORM),windows)
146153
# Generate import library for Windows
147-
dlltool -D $@ -d $(DEF_FILE) -l $(DIST_DIR)/js.lib
154+
dlltool -D $@ -d $(DEF_FILE) -l $(DIST_DIR)/cloudsync.lib
155+
endif
156+
else
157+
#WASM build
158+
EMSDK := $(BUILD_WASM)/emsdk
159+
$(EMSDK):
160+
git clone https://github.com/emscripten-core/emsdk.git $(EMSDK)
161+
cd $(EMSDK) && ./emsdk install latest && ./emsdk activate latest && source ./emsdk_env.sh
162+
163+
SQLITE_SRC := $(BUILD_WASM)/sqlite
164+
$(SQLITE_SRC): $(EMSDK)
165+
git clone --branch version-$(SQLITE_VERSION) --depth 1 https://github.com/sqlite/sqlite.git $(SQLITE_SRC)
166+
source ./$(EMSDK)/emsdk_env.sh && cd $(SQLITE_SRC) && ./configure --enable-all
167+
168+
$(TARGET): $(SQLITE_SRC) $(SRC_FILES)
169+
cd $(SQLITE_SRC)/ext/wasm && $(MAKE) dist sqlite3_wasm_extra_init.c=../../../../../src/wasm.c
170+
mv $(SQLITE_SRC)/ext/wasm/sqlite-wasm-*.zip $(TARGET)
148171
endif
149172

150173
# Test executable
@@ -268,7 +291,7 @@ endif
268291

269292
# Clean up generated files
270293
clean:
271-
rm -rf $(BUILD_DIRS) $(DIST_DIR)/* $(COV_DIR) *.gcda *.gcno *.gcov $(CURL_DIR)/src *.sqlite
294+
rm -rf $(BUILD_DIRS) $(DIST_DIR)/* $(COV_DIR) *.gcda *.gcno *.gcov $(CURL_DIR)/src *.sqlite $(BUILD_WASM)
272295

273296
# Help message
274297
help:
@@ -283,6 +306,7 @@ help:
283306
@echo " android (needs ARCH to be set to x86_64 or arm64-v8a and ANDROID_NDK to be set)"
284307
@echo " ios (only on macOS)"
285308
@echo " isim (only on macOS)"
309+
@echo " wasm (needs wabt[brew install wabt/apt install wabt])"
286310
@echo ""
287311
@echo "Targets:"
288312
@echo " all - Build the extension (default)"

src/wasm.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifdef SQLITE_WASM_EXTRA_INIT
2+
3+
#include "sqlite3.h"
4+
#include <stdio.h>
5+
6+
#define CLOUDSYNC_OMIT_NETWORK
7+
#include "utils.c"
8+
#include "network.c"
9+
#include "dbutils.c"
10+
#include "cloudsync.c"
11+
#include "vtab.c"
12+
#include "pk.c"
13+
#include "lz4.c"
14+
//ciao
15+
int sqlite3_wasm_extra_init(const char *z) {
16+
fprintf(stderr, "%s: %s()\n", __FILE__, __func__);
17+
return sqlite3_auto_extension((void *) sqlite3_cloudsync_init);
18+
}
19+
20+
#endif

0 commit comments

Comments
 (0)