Skip to content

Update from upstream #10

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 13 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
2 changes: 1 addition & 1 deletion .github/actions/build-wasm/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ runs:
working-directory: sqlite3
shell: bash
run: |
cmake -Dwasi_sysroot=/opt/wasi-sysroot -Dclang=/opt/homebrew/opt/llvm@18/bin/clang -S assets/wasm -B .dart_tool/sqlite3_build --toolchain toolchain.cmake
cmake -Dwasi_sysroot=/opt/wasi-sysroot -Dclang=/opt/homebrew/opt/llvm@18/bin/clang -S assets/wasm -B .dart_tool/sqlite3_build
cmake --build .dart_tool/sqlite3_build/ -t output -j
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ jobs:

- name: Web tests
run: |
curl https://storage.googleapis.com/simon-public-euw3/assets/sqlite3/wasm/2.4.3/sqlite3.wasm -o example/web/sqlite3.wasm
curl https://storage.googleapis.com/simon-public-euw3/assets/sqlite3/wasm/2.4.6/sqlite3.wasm -o example/web/sqlite3.wasm
dart test -P web -r expanded
# If browsers behave differently on different platforms, surely that's not our fault...
# So, only run browser tests on Linux to be faster.
Expand Down
12 changes: 11 additions & 1 deletion integration_tests/sqlcipher_flutter/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 34
namespace "com.example.sqlcipher_flutter"

compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

kotlinOptions {
jvmTarget = "1.8"
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/sqlcipher_flutter/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.6.10'
ext.kotlin_version = '2.0.10'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath 'com.android.tools.build:gradle:8.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
4 changes: 4 additions & 0 deletions sqlcipher_flutter_libs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.4

- Fix compilation on Android by upgrading `compileSdk` version.

## 0.6.3

- Enable extension loading on Windows and Linux to match the compile options
Expand Down
7 changes: 3 additions & 4 deletions sqlcipher_flutter_libs/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:8.0.1'
classpath 'com.android.tools.build:gradle:8.5.1'
}
}

Expand All @@ -22,15 +22,14 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 28

// Conditional for compatibility with AGP <4.2.
if (project.android.hasProperty("namespace")) {
namespace 'eu.simonbinder.sqlite3_flutter_libs'
}

defaultConfig {
minSdkVersion 16
minSdkVersion 21
compileSdk 34
}
lintOptions {
disable 'InvalidPackage'
Expand Down
2 changes: 1 addition & 1 deletion sqlcipher_flutter_libs/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sqlcipher_flutter_libs
description: Flutter plugin to include native SQLCipher libraries in your app
version: 0.6.3
version: 0.6.4
homepage: https://github.com/simolus3/sqlite3.dart/tree/main/sqlcipher_flutter_libs
issue_tracker: https://github.com/simolus3/sqlite3.dart/issues

Expand Down
6 changes: 4 additions & 2 deletions sqlite3/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
## 2.4.6

- Fix selecting large integers (being represented as a `BigInt` in Dart)
not working when compiled with dartdevc.
- WebAssembly: Call `_initialize` function of sqlite3 module if one is present.
- Support version 1.0.0 of `package:web`.

## 2.4.5

- Fix a bug in the OPFS-locks implementation causing a deadlock when the `xSleep`
VFS call is issued.
- Fix selecting large integers (being represented as a `BigInt` in Dart)
not working when compiled with dartdevc.

## 2.4.4

Expand Down
4 changes: 2 additions & 2 deletions sqlite3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ With wasi in `/usr/share/wasi-sysroot` and the default clang compiler having the
required builtins, you can setup the build with:

```
cmake -S assets/wasm -B .dart_tool/sqlite3_build --toolchain toolchain.cmake
cmake -S assets/wasm -B .dart_tool/sqlite3_build
```

##### macOS
Expand All @@ -196,7 +196,7 @@ Replace `clang/18` with the correct directory if you're using a different versio
Then, set up the build with

```
cmake -Dwasi_sysroot=/opt/wasi-sysroot -Dclang=/opt/homebrew/opt/llvm/bin/clang -S assets/wasm -B .dart_tool/sqlite3_build --toolchain toolchain.cmake
cmake -Dwasi_sysroot=/opt/wasi-sysroot -Dclang=/opt/homebrew/opt/llvm/bin/clang -S assets/wasm -B .dart_tool/sqlite3_build
```

#### Building
Expand Down
131 changes: 80 additions & 51 deletions sqlite3/assets/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.24)

set(PROJECT_NAME "sqlite3_web")
project(${PROJECT_NAME} LANGUAGES C)

set(triple wasm32-unknown-wasi)
set(wasi_sysroot "/usr/share/wasi-sysroot" CACHE PATH "Path to wasi sysroot")
set(clang "clang" CACHE FILEPATH "Path to wasm-capable clang executable")

include(FetchContent)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
# cmake 3.24.0 added the `DOWNLOAD_EXTRACT_TIMESTAMP` and prints an ugly warning when
# the default is used, so override it to the recommended behavior.
# We can't really ask users to use a cmake that recent, so there's this if here.
FetchContent_Declare(

FetchContent_Declare(
sqlite3
URL https://sqlite.org/2023/sqlite-autoconf-3440000.tar.gz
URL https://sqlite.org/2024/sqlite-autoconf-3460000.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP NEW
)
else()
FetchContent_Declare(
sqlite3
URL https://sqlite.org/2023/sqlite-autoconf-3440000.tar.gz
)
endif()
)

FetchContent_MakeAvailable(sqlite3)

set(wasm_visibility "__attribute__((visibility(\"default\")))")
file(DOWNLOAD https://raw.githubusercontent.com/sqlite/sqlite/master/src/test_vfstrace.c "${CMAKE_BINARY_DIR}/vfstrace.c")

get_filename_component(RS_LIB_DIR "${CMAKE_BINARY_DIR}/../../powersync-sqlite-core/" ABSOLUTE)
set(RS_LIB "powersync")
Expand All @@ -31,28 +27,74 @@ set(RS_WASM_TGT_DIR "${RS_LIB_DIR}/target/${RS_WASM_TGT}")
set(RS_RELEASE_OUT "${RS_WASM_TGT_DIR}/wasm/")
set(RS_RELEASE_OUT_DEPS "${RS_WASM_TGT_DIR}/wasm/deps")
set(RS_RELEASE_EXTENSION_OUT "${RS_RELEASE_OUT}/powersync-extension.o")
set(RS_DEBUG_BC "${RS_WASM_TGT_DIR}/debug/deps/${RS_LIB}.bc")
set(RS_BUILD_COMMAND "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 ${RS_WASM_TGT}")

file(GLOB BYTECODE_FILES "${RS_WASM_TGT_DIR}/wasm/deps/*.bc")
file(GLOB OBJ_FILES "${RS_WASM_TGT_DIR}/wasm/deps/*.o" CONFIGURE_DEPENDS "*.o")
# Generate symbols we need to export from the sqlite3.wasm build
add_custom_command(
OUTPUT required_symbols.txt
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../
COMMAND dart run tool/wasm_symbols.dart ${CMAKE_CURRENT_BINARY_DIR}/required_symbols.txt
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../../tool/wasm_symbols.dart
VERBATIM
)
add_custom_target(required_symbols DEPENDS required_symbols.txt)

macro(base_sqlite3_target name)
add_executable(${name}
"${sqlite3_SOURCE_DIR}/sqlite3.c"
os_web.c
helpers.c
)
macro(base_sqlite3_target name debug)
set(clang_output ${name}.clang.wasm)
set(init_output ${name}.init.wasm)
set(output ${init_output})

target_link_libraries(${name} ${RS_RELEASE_EXTENSION_OUT})
target_link_options(${name} PRIVATE -nostartfiles -Wl,--import-memory -Wl,--no-entry -Wl,--export-dynamic)
target_include_directories(${name} PRIVATE "${PROJECT_SOURCE_DIR}/")
target_include_directories(${name} PRIVATE ${sqlite3_SOURCE_DIR})
target_compile_definitions(${name} PRIVATE
_HAVE_SQLITE_CONFIG_H
SQLITE_API=${wasm_visibility}
set(sources
${CMAKE_CURRENT_SOURCE_DIR}/os_web.c
${CMAKE_CURRENT_SOURCE_DIR}/helpers.c
${sqlite3_SOURCE_DIR}/sqlite3.c
${RS_RELEASE_EXTENSION_OUT}
)
set(flags -Wall -Wextra -Wno-unused-parameter -Wno-unused-function)

if(${debug})
list(APPEND sources "${CMAKE_BINARY_DIR}/vfstrace.c")
list(APPEND flags "-g" "-DDEBUG")
else()
list(APPEND flags "-Oz" "-DNDEBUG" "-flto")
endif()

add_custom_command(
OUTPUT ${clang_output}
COMMAND ${clang} --target=${triple} -std=c23
${flags}
-o ${clang_output}
-I ${PROJECT_SOURCE_DIR} -I ${sqlite3_SOURCE_DIR}
-D_HAVE_SQLITE_CONFIG_H
-mcpu=generic
-mexec-model=reactor
-fno-stack-protector -fno-stack-clash-protection
-Wl,--import-memory
--sysroot ${wasi_sysroot}
${sources}
@${CMAKE_CURRENT_BINARY_DIR}/required_symbols.txt
DEPENDS ${sources} required_symbols
VERBATIM
)

add_custom_command(
OUTPUT ${init_output}
COMMAND wasm-ctor-eval -c _initialize ${clang_output} -o ${init_output}
VERBATIM
DEPENDS ${clang_output}
)

if(NOT ${debug})
set(output ${name}.wasm)

add_custom_command(
OUTPUT ${output}
COMMAND wasm-opt --strip --strip-producers -c -O4 ${init_output} -o ${output}
VERBATIM
DEPENDS ${init_output}
)
set_property(TARGET ${name} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

add_custom_target(${name} DEPENDS ${output})
endmacro()

# Script to use llc to get object code from bytecode
Expand All @@ -73,26 +115,13 @@ add_custom_target(
COMMAND sh ${objectcode_script}
)

base_sqlite3_target(sqlite3_debug)
file(DOWNLOAD https://raw.githubusercontent.com/sqlite/sqlite/version-3.44.0/src/test_vfstrace.c "${CMAKE_BINARY_DIR}/vfstrace.c")
target_sources(sqlite3_debug PRIVATE "${CMAKE_BINARY_DIR}/vfstrace.c")
target_compile_options(sqlite3_debug PRIVATE -g)
target_compile_definitions(sqlite3_debug PRIVATE SQLITE_ENABLE_VFSTRACE SQLITE_ENABLE_API_ARMOR)
set_target_properties(sqlite3_debug PROPERTIES OUTPUT_NAME "sqlite3" SUFFIX ".debug.wasm")

base_sqlite3_target(sqlite3_opt)
target_compile_options(sqlite3_opt PRIVATE -Oz)
set_target_properties(sqlite3_opt PROPERTIES OUTPUT_NAME "sqlite3" SUFFIX ".tmp.wasm")
add_custom_command(TARGET sqlite3_opt POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../
COMMAND dart run tool/wasm_dce.dart ${CMAKE_CURRENT_BINARY_DIR}/sqlite3.tmp.wasm ${CMAKE_CURRENT_BINARY_DIR}/sqlite3.dce.wasm
COMMAND wasm-opt ${CMAKE_CURRENT_BINARY_DIR}/sqlite3.dce.wasm -O4 -o ${CMAKE_CURRENT_BINARY_DIR}/sqlite3.wasm
)
base_sqlite3_target(sqlite3_debug true)
base_sqlite3_target(sqlite3_opt false)

add_dependencies(sqlite3_opt powersync_core_bytecode)
add_dependencies(sqlite3_debug powersync_core_bytecode)

add_custom_target(output)
add_custom_command(TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/sqlite3.wasm ${PROJECT_SOURCE_DIR}/../../example/web/sqlite3.wasm DEPENDS)
add_custom_command(TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/sqlite3.debug.wasm ${PROJECT_SOURCE_DIR}/../../example/web/sqlite3.debug.wasm)
add_dependencies(output sqlite3_debug sqlite3_opt powersync_core_bytecode)
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 powersync_core_bytecode)
10 changes: 0 additions & 10 deletions sqlite3/assets/wasm/toolchain.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion sqlite3/example/custom_wasm_build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rustup target add wasm32-wasi
```

Additionally, you need to download WASI compiler builtins and the associated libc
as described in the [build instructions](https://github.com/simolus3/sqlite3.dart/tree/rust-wasm-build/sqlite3#compiling).
as described in the [build instructions](https://github.com/simolus3/sqlite3.dart/tree/main/sqlite3#compiling).

## Building

Expand Down
3 changes: 1 addition & 2 deletions sqlite3/example/custom_wasm_build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ fn main() {
env::var("WASI_SYSROOT").unwrap_or_else(|_| "/usr/share/wasi-sysroot".to_string());

let cmake_dir = Config::new("../../assets/wasm/")
.configure_arg("--toolchain")
.configure_arg(std::fs::canonicalize("../../assets/wasm/toolchain.cmake").unwrap())
.define("wasi_sysroot", &sysroot)
.define("CMAKE_C_COMPILER_WORKS", "1")
.build_target("sqlite3_opt_lib")
.build_target("help") // We only need the sources
.build();
Expand Down
2 changes: 1 addition & 1 deletion sqlite3/example/web/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {

if (data.equals('start'.toJS).toDart) {
final options = WasmVfs.createOptions();
final worker = web.Worker(''); // Clone this worker
final worker = web.Worker(''.toJS); // Clone this worker
worker.postMessage(options);

// Now, wait for the worker to report that it has been initialized.
Expand Down
8 changes: 8 additions & 0 deletions sqlite3/lib/src/wasm/js_interop/wasm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class WasmInstance {
});

final native = await _instantiateStreaming(response, importsJs).toDart;

// If the module has an `_initialize` export, it needs to be called to run
// C constructors and set up memory.
final exports = native.instance.exports;
if (exports.has('_initialize')) {
(exports['_initialize'] as JSFunction).callAsFunction();
}

return WasmInstance._(native.instance);
}
}
Expand Down
4 changes: 2 additions & 2 deletions sqlite3/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ dependencies:
ffi: '>=1.2.1 <3.0.0'
meta: ^1.3.0
path: ^1.8.0
web: ^0.5.0
web: ^1.0.0

dev_dependencies:
analyzer: ^6.4.1
build_daemon: ^4.0.0
build_runner: ^2.1.7
build_web_compilers: ^4.0.3
ffigen: ^12.0.0
ffigen: ^13.0.0
http: ^1.2.1
lints: ^4.0.0
shelf: ^1.4.0
Expand Down
2 changes: 1 addition & 1 deletion sqlite3/test/wasm/sqlite3_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void main() {
test(
backend,
() async {
final worker = web.Worker(workerUri);
final worker = web.Worker(workerUri.toJS);

web.EventStreamProviders.errorEvent
.forTarget(worker)
Expand Down
2 changes: 1 addition & 1 deletion sqlite3/test/wasm/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Future<void> _startTest(String fsImplementation, Uri wasmUri) async {
// server needed for synchronous access.
final options = WasmVfs.createOptions();

final worker = web.Worker(scope.location.href);
final worker = web.Worker(scope.location.href.toJS);
worker.postMessage(options);

// Wait for the worker to acknowledge it being ready
Expand Down
Loading