From 9010ff4e2470e086d469af1ad1ecb61f9054e6b7 Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 19 Nov 2024 11:50:24 -0600 Subject: [PATCH 01/17] fix(common): namespace db_common windows cmake --- .../amplify_db_common/windows/CMakeLists.txt | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/packages/common/amplify_db_common/windows/CMakeLists.txt b/packages/common/amplify_db_common/windows/CMakeLists.txt index b9788c4158..d068e0c17a 100644 --- a/packages/common/amplify_db_common/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/windows/CMakeLists.txt @@ -34,12 +34,11 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) include(FetchContent) -# Only add the sqlite3 library if it hasn't been defined already. -if(NOT TARGET sqlite3) - 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. +# Add a custom namespace for the sqlite3 target to prevent naming collisions. +set(SQLITE3_AMPLIFY_TARGET_NAME sqlite3_amplify) + +if (NOT TARGET ${SQLITE3_AMPLIFY_TARGET_NAME}) + if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") FetchContent_Declare( sqlite3 URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz @@ -54,14 +53,13 @@ if(NOT TARGET sqlite3) FetchContent_MakeAvailable(sqlite3) - # Define the sqlite3 library only if it wasn't already defined. - add_library(sqlite3 SHARED "sqlite3_flutter.c") - - target_include_directories(sqlite3 PRIVATE "${sqlite3_SOURCE_DIR}") - target_compile_options(sqlite3 PRIVATE "$<$>:-O2>" "/w") + # Define the sqlite3 library under a custom name. + add_library(${SQLITE3_AMPLIFY_TARGET_NAME} SHARED "sqlite3_flutter.c") + target_include_directories(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE "${sqlite3_SOURCE_DIR}") + target_compile_options(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE "$<$>:-O2>" "/w") # Note: Keep in sync with https://github.com/simolus3/sqlite-native-libraries/blob/master/sqlite3-native-library/cpp/CMakeLists.txt - target_compile_definitions(sqlite3 PRIVATE + target_compile_definitions(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE SQLITE_ENABLE_FTS5 SQLITE_ENABLE_RTREE SQLITE_DQS=0 @@ -83,20 +81,13 @@ if(NOT TARGET sqlite3) SQLITE_HAVE_LOCALTIME_R SQLITE_HAVE_LOCALTIME_S ) - - # Create an alias for this version of sqlite3. - add_library(sqlite3_amplify_db_common ALIAS sqlite3) -else() - # If sqlite3 already exists, create an alias for amplify plugin to avoid duplication. - add_library(sqlite3_amplify_db_common ALIAS sqlite3) endif() -target_link_libraries(${PLUGIN_NAME} PRIVATE sqlite3_amplify_db_common) - -add_dependencies(${PLUGIN_NAME} sqlite3_amplify_db_common) +# Link the custom sqlite3 target to your plugin. +target_link_libraries(${PLUGIN_NAME} PRIVATE ${SQLITE3_AMPLIFY_TARGET_NAME}) # List of absolute paths to libraries that should be bundled with the plugin. set(amplify_db_common_bundled_libraries - "$" + "$" PARENT_SCOPE ) From 1c18831f85a69666b6d7fd4f1d0fb72839fd5a89 Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 19 Nov 2024 11:56:00 -0600 Subject: [PATCH 02/17] fix comments --- packages/common/amplify_db_common/windows/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common/windows/CMakeLists.txt b/packages/common/amplify_db_common/windows/CMakeLists.txt index d068e0c17a..5940c89b94 100644 --- a/packages/common/amplify_db_common/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/windows/CMakeLists.txt @@ -39,6 +39,9 @@ set(SQLITE3_AMPLIFY_TARGET_NAME sqlite3_amplify) if (NOT TARGET ${SQLITE3_AMPLIFY_TARGET_NAME}) 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( sqlite3 URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz @@ -83,7 +86,7 @@ if (NOT TARGET ${SQLITE3_AMPLIFY_TARGET_NAME}) ) endif() -# Link the custom sqlite3 target to your plugin. +# Link the custom sqlite3 target to the amplify plugin. target_link_libraries(${PLUGIN_NAME} PRIVATE ${SQLITE3_AMPLIFY_TARGET_NAME}) # List of absolute paths to libraries that should be bundled with the plugin. From cc28db7198e46ba4338c2b97b409f936759cf0b7 Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 19 Nov 2024 12:42:18 -0600 Subject: [PATCH 03/17] temp fix integration tests --- .../example/integration_test/main_test.dart | 15 +++++++++--- .../lib/src/connect_io.dart | 23 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/common/amplify_db_common/example/integration_test/main_test.dart b/packages/common/amplify_db_common/example/integration_test/main_test.dart index c072e06be1..24f451e1b6 100644 --- a/packages/common/amplify_db_common/example/integration_test/main_test.dart +++ b/packages/common/amplify_db_common/example/integration_test/main_test.dart @@ -13,12 +13,21 @@ void main() { late AppDb db; setUp(() async { - db = AppDb(connect(name: 'app_test')); + try { + db = AppDb(connect(name: 'app_test')); + expect(db, isNotNull); + } catch (e) { + fail('Error during setup: $e'); + } }); tearDown(() async { - await db.delete(db.countTable).go(); - await db.close(); + try { + await db.delete(db.countTable).go(); + await db.close(); + } catch (e) { + fail('Error during teardown: $e'); + } }); testWidgets('can decrement', (_) async { diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index 5d65aee014..92059c475b 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -25,7 +25,12 @@ QueryExecutor connect({ return DatabaseConnection.delayed( Future.sync(() async { final resolvedPath = await path; - final dbPath = p.join(resolvedPath!, 'com.amplify.$name.sqlite'); + if (resolvedPath == null || !Directory(resolvedPath).existsSync()) { + throw ArgumentError('Invalid or non-existent path: $resolvedPath'); + } + + final dbPath = p.join(resolvedPath, 'com.amplify.$name.sqlite'); + print('Spawning drift isolate with path: $dbPath'); final receiveDriftIsolate = ReceivePort(); await Isolate.spawn( @@ -33,7 +38,21 @@ QueryExecutor connect({ _IsolateStartRequest(receiveDriftIsolate.sendPort, dbPath), ); - final driftIsolate = await receiveDriftIsolate.first as DriftIsolate; + final driftIsolateOrError = await receiveDriftIsolate.first; + + if (driftIsolateOrError is IsolateSpawnException) { + throw Exception( + 'Error in drift isolate: ${driftIsolateOrError.message}', + ); + } + + if (driftIsolateOrError is! DriftIsolate) { + throw Exception( + 'Unexpected type received from isolate: ${driftIsolateOrError.runtimeType}', + ); + } + + final driftIsolate = driftIsolateOrError; return driftIsolate.connect(); }), ).executor; From 42b3c390a291b8b98b903329ac023fa10c4dc3c7 Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 19 Nov 2024 12:46:00 -0600 Subject: [PATCH 04/17] fix print lint error --- packages/common/amplify_db_common_dart/lib/src/connect_io.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index 92059c475b..5f18d99e8e 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:isolate'; +import 'package:amplify_core/amplify_core.dart'; import 'package:drift/drift.dart'; import 'package:drift/isolate.dart'; import 'package:drift/native.dart'; @@ -30,7 +31,7 @@ QueryExecutor connect({ } final dbPath = p.join(resolvedPath, 'com.amplify.$name.sqlite'); - print('Spawning drift isolate with path: $dbPath'); + safePrint('Spawning drift isolate with path: $dbPath'); final receiveDriftIsolate = ReceivePort(); await Isolate.spawn( From a7213b2b7c7efff594fd4266f495ae6176ff22dc Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 19 Nov 2024 13:01:21 -0600 Subject: [PATCH 05/17] revert test changes --- .../example/integration_test/main_test.dart | 16 ++++--------- .../lib/src/connect_io.dart | 24 ++----------------- 2 files changed, 6 insertions(+), 34 deletions(-) diff --git a/packages/common/amplify_db_common/example/integration_test/main_test.dart b/packages/common/amplify_db_common/example/integration_test/main_test.dart index 24f451e1b6..503076b63d 100644 --- a/packages/common/amplify_db_common/example/integration_test/main_test.dart +++ b/packages/common/amplify_db_common/example/integration_test/main_test.dart @@ -13,21 +13,13 @@ void main() { late AppDb db; setUp(() async { - try { - db = AppDb(connect(name: 'app_test')); - expect(db, isNotNull); - } catch (e) { - fail('Error during setup: $e'); - } + db = AppDb(connect(name: 'app_test')); + expect(db, isNotNull); }); tearDown(() async { - try { - await db.delete(db.countTable).go(); - await db.close(); - } catch (e) { - fail('Error during teardown: $e'); - } + await db.delete(db.countTable).go(); + await db.close(); }); testWidgets('can decrement', (_) async { diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index 5f18d99e8e..5d65aee014 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:io'; import 'dart:isolate'; -import 'package:amplify_core/amplify_core.dart'; import 'package:drift/drift.dart'; import 'package:drift/isolate.dart'; import 'package:drift/native.dart'; @@ -26,12 +25,7 @@ QueryExecutor connect({ return DatabaseConnection.delayed( Future.sync(() async { final resolvedPath = await path; - if (resolvedPath == null || !Directory(resolvedPath).existsSync()) { - throw ArgumentError('Invalid or non-existent path: $resolvedPath'); - } - - final dbPath = p.join(resolvedPath, 'com.amplify.$name.sqlite'); - safePrint('Spawning drift isolate with path: $dbPath'); + final dbPath = p.join(resolvedPath!, 'com.amplify.$name.sqlite'); final receiveDriftIsolate = ReceivePort(); await Isolate.spawn( @@ -39,21 +33,7 @@ QueryExecutor connect({ _IsolateStartRequest(receiveDriftIsolate.sendPort, dbPath), ); - final driftIsolateOrError = await receiveDriftIsolate.first; - - if (driftIsolateOrError is IsolateSpawnException) { - throw Exception( - 'Error in drift isolate: ${driftIsolateOrError.message}', - ); - } - - if (driftIsolateOrError is! DriftIsolate) { - throw Exception( - 'Unexpected type received from isolate: ${driftIsolateOrError.runtimeType}', - ); - } - - final driftIsolate = driftIsolateOrError; + final driftIsolate = await receiveDriftIsolate.first as DriftIsolate; return driftIsolate.connect(); }), ).executor; From 8276de357aebb1dece887eaa0c535914d48db027 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 09:38:31 -0600 Subject: [PATCH 06/17] test fix? --- .github/workflows/e2e_windows.yaml | 2 +- .../example/integration_test/main_test.dart | 2 ++ .../lib/src/connect_io.dart | 25 ++++++++++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.github/workflows/e2e_windows.yaml b/.github/workflows/e2e_windows.yaml index de1c66627b..7e1ac99734 100644 --- a/.github/workflows/e2e_windows.yaml +++ b/.github/workflows/e2e_windows.yaml @@ -63,7 +63,7 @@ jobs: timeout-minutes: 60 run: | flutter config --enable-windows-desktop - dart pub global run aft exec --include=${{ inputs.package-name }} -- flutter test integration_test/main_test.dart -d windows + dart pub global run aft exec --include=${{ inputs.package-name }} -- flutter test integration_test/main_test.dart -d windows --timeout=120 - name: Log success/failure if: always() diff --git a/packages/common/amplify_db_common/example/integration_test/main_test.dart b/packages/common/amplify_db_common/example/integration_test/main_test.dart index 503076b63d..f98fabd390 100644 --- a/packages/common/amplify_db_common/example/integration_test/main_test.dart +++ b/packages/common/amplify_db_common/example/integration_test/main_test.dart @@ -23,11 +23,13 @@ void main() { }); testWidgets('can decrement', (_) async { + print('Running "can decrement" test'); expect(await db.getLatestCount(), 0); await Future.wait([ for (var i = 0; i < 10; i++) db.decrementCount(), ]); expect(await db.getLatestCount(), -10); + print('"can decrement" test passed'); }); testWidgets('can increment', (_) async { diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index 5d65aee014..c8dbc998b0 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -33,7 +33,16 @@ QueryExecutor connect({ _IsolateStartRequest(receiveDriftIsolate.sendPort, dbPath), ); - final driftIsolate = await receiveDriftIsolate.first as DriftIsolate; + final driftIsolateOrError = await receiveDriftIsolate.first; + + if (driftIsolateOrError is Map && + driftIsolateOrError.containsKey('error')) { + throw Exception( + 'Error in isolate: ${driftIsolateOrError['message']}}', + ); + } + + final driftIsolate = driftIsolateOrError as DriftIsolate; return driftIsolate.connect(); }), ).executor; @@ -47,11 +56,15 @@ class _IsolateStartRequest { } void _entrypointForDriftIsolate(_IsolateStartRequest request) { - final databaseImpl = NativeDatabase(File(request.databasePath)); + try { + final databaseImpl = NativeDatabase(File(request.databasePath)); - final driftServer = DriftIsolate.inCurrent( - () => DatabaseConnection(databaseImpl), - ); + final driftServer = DriftIsolate.inCurrent( + () => DatabaseConnection(databaseImpl), + ); - request.talkToMain.send(driftServer); + request.talkToMain.send(driftServer); + } catch (e, stack) { + request.talkToMain.send({'error': e.toString(), 'stack': stack.toString()}); + } } From c82e8043015e5fd97903daff397f01c0293d3df7 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 09:44:34 -0600 Subject: [PATCH 07/17] fix lint --- packages/common/amplify_db_common_dart/lib/src/connect_io.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index c8dbc998b0..8ac264b137 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -64,7 +64,7 @@ void _entrypointForDriftIsolate(_IsolateStartRequest request) { ); request.talkToMain.send(driftServer); - } catch (e, stack) { + } on Object catch (e, stack) { request.talkToMain.send({'error': e.toString(), 'stack': stack.toString()}); } } From ccbe1ab8973c576f3acd123dcc5808b4e61a6e27 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 09:49:59 -0600 Subject: [PATCH 08/17] fix lint --- .../amplify_db_common/example/integration_test/main_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/common/amplify_db_common/example/integration_test/main_test.dart b/packages/common/amplify_db_common/example/integration_test/main_test.dart index f98fabd390..7167107705 100644 --- a/packages/common/amplify_db_common/example/integration_test/main_test.dart +++ b/packages/common/amplify_db_common/example/integration_test/main_test.dart @@ -1,6 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +// ignore_for_file: avoid_print + import 'package:amplify_db_common/amplify_db_common.dart'; import 'package:amplify_db_common_example/db.dart'; import 'package:flutter_test/flutter_test.dart'; From c17b551ffc36bdb008815f507ce4d688051f0560 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 10:09:29 -0600 Subject: [PATCH 09/17] remove timeout --- .github/workflows/e2e_windows.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e_windows.yaml b/.github/workflows/e2e_windows.yaml index 7e1ac99734..de1c66627b 100644 --- a/.github/workflows/e2e_windows.yaml +++ b/.github/workflows/e2e_windows.yaml @@ -63,7 +63,7 @@ jobs: timeout-minutes: 60 run: | flutter config --enable-windows-desktop - dart pub global run aft exec --include=${{ inputs.package-name }} -- flutter test integration_test/main_test.dart -d windows --timeout=120 + dart pub global run aft exec --include=${{ inputs.package-name }} -- flutter test integration_test/main_test.dart -d windows - name: Log success/failure if: always() From 8b37d8e39d2e052502f5a3ab2b7366b4a429cf57 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 11:56:42 -0600 Subject: [PATCH 10/17] revert debugging --- .../example/integration_test/main_test.dart | 5 ---- .../lib/src/connect_io.dart | 25 +++++-------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/packages/common/amplify_db_common/example/integration_test/main_test.dart b/packages/common/amplify_db_common/example/integration_test/main_test.dart index 7167107705..c072e06be1 100644 --- a/packages/common/amplify_db_common/example/integration_test/main_test.dart +++ b/packages/common/amplify_db_common/example/integration_test/main_test.dart @@ -1,8 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -// ignore_for_file: avoid_print - import 'package:amplify_db_common/amplify_db_common.dart'; import 'package:amplify_db_common_example/db.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -16,7 +14,6 @@ void main() { setUp(() async { db = AppDb(connect(name: 'app_test')); - expect(db, isNotNull); }); tearDown(() async { @@ -25,13 +22,11 @@ void main() { }); testWidgets('can decrement', (_) async { - print('Running "can decrement" test'); expect(await db.getLatestCount(), 0); await Future.wait([ for (var i = 0; i < 10; i++) db.decrementCount(), ]); expect(await db.getLatestCount(), -10); - print('"can decrement" test passed'); }); testWidgets('can increment', (_) async { diff --git a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart index 8ac264b137..5d65aee014 100644 --- a/packages/common/amplify_db_common_dart/lib/src/connect_io.dart +++ b/packages/common/amplify_db_common_dart/lib/src/connect_io.dart @@ -33,16 +33,7 @@ QueryExecutor connect({ _IsolateStartRequest(receiveDriftIsolate.sendPort, dbPath), ); - final driftIsolateOrError = await receiveDriftIsolate.first; - - if (driftIsolateOrError is Map && - driftIsolateOrError.containsKey('error')) { - throw Exception( - 'Error in isolate: ${driftIsolateOrError['message']}}', - ); - } - - final driftIsolate = driftIsolateOrError as DriftIsolate; + final driftIsolate = await receiveDriftIsolate.first as DriftIsolate; return driftIsolate.connect(); }), ).executor; @@ -56,15 +47,11 @@ class _IsolateStartRequest { } void _entrypointForDriftIsolate(_IsolateStartRequest request) { - try { - final databaseImpl = NativeDatabase(File(request.databasePath)); + final databaseImpl = NativeDatabase(File(request.databasePath)); - final driftServer = DriftIsolate.inCurrent( - () => DatabaseConnection(databaseImpl), - ); + final driftServer = DriftIsolate.inCurrent( + () => DatabaseConnection(databaseImpl), + ); - request.talkToMain.send(driftServer); - } on Object catch (e, stack) { - request.talkToMain.send({'error': e.toString(), 'stack': stack.toString()}); - } + request.talkToMain.send(driftServer); } From 37248a6d78dfbbe61aa1ba99977ea38d8237759b Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 11:57:05 -0600 Subject: [PATCH 11/17] refactor: opt in approach --- .../amplify_db_common/windows/CMakeLists.txt | 98 ++++++++----------- 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/packages/common/amplify_db_common/windows/CMakeLists.txt b/packages/common/amplify_db_common/windows/CMakeLists.txt index 5940c89b94..bc4d965675 100644 --- a/packages/common/amplify_db_common/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/windows/CMakeLists.txt @@ -32,65 +32,53 @@ target_include_directories(${PLUGIN_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) -include(FetchContent) +# Option to disable internal sqlite3 definition +option(USE_CUSTOM_SQLITE3 "Disable internal sqlite3 definition to allow downstream dependencies to define their own" OFF) -# Add a custom namespace for the sqlite3 target to prevent naming collisions. -set(SQLITE3_AMPLIFY_TARGET_NAME sqlite3_amplify) - -if (NOT TARGET ${SQLITE3_AMPLIFY_TARGET_NAME}) - 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( - sqlite3 - URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz - DOWNLOAD_EXTRACT_TIMESTAMP NEW - ) - else() - FetchContent_Declare( - sqlite3 - URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz +if (NOT USE_CUSTOM_SQLITE3) + # Define the sqlite3_amplify target + if (NOT TARGET sqlite3_amplify) + add_library(sqlite3_amplify SHARED "sqlite3_flutter.c") + + # Configure sqlite3 properties and definitions + target_include_directories(sqlite3_amplify PRIVATE "${sqlite3_SOURCE_DIR}") + target_compile_options(sqlite3_amplify PRIVATE "$<$>:-O2>" "/w") + target_compile_definitions(sqlite3_amplify PRIVATE + SQLITE_ENABLE_FTS5 + SQLITE_ENABLE_RTREE + SQLITE_DQS=0 + SQLITE_DEFAULT_MEMSTATUS=0 + SQLITE_TEMP_STORE=2 + SQLITE_MAX_EXPR_DEPTH=0 + SQLITE_OMIT_AUTHORIZATION + SQLITE_OMIT_DECLTYPE + SQLITE_OMIT_DEPRECATED + SQLITE_OMIT_GET_TABLE + SQLITE_OMIT_LOAD_EXTENSION + SQLITE_OMIT_PROGRESS_CALLBACK + SQLITE_OMIT_SHARED_CACHE + SQLITE_OMIT_TCL_VARIABLE + SQLITE_OMIT_TRACE + SQLITE_USE_ALLOCA + SQLITE_UNTESTABLE + SQLITE_HAVE_ISNAN + SQLITE_HAVE_LOCALTIME_R + SQLITE_HAVE_LOCALTIME_S ) endif() - FetchContent_MakeAvailable(sqlite3) - - # Define the sqlite3 library under a custom name. - add_library(${SQLITE3_AMPLIFY_TARGET_NAME} SHARED "sqlite3_flutter.c") - - target_include_directories(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE "${sqlite3_SOURCE_DIR}") - target_compile_options(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE "$<$>:-O2>" "/w") - # Note: Keep in sync with https://github.com/simolus3/sqlite-native-libraries/blob/master/sqlite3-native-library/cpp/CMakeLists.txt - target_compile_definitions(${SQLITE3_AMPLIFY_TARGET_NAME} PRIVATE - SQLITE_ENABLE_FTS5 - SQLITE_ENABLE_RTREE - SQLITE_DQS=0 - SQLITE_DEFAULT_MEMSTATUS=0 - SQLITE_TEMP_STORE=2 - SQLITE_MAX_EXPR_DEPTH=0 - SQLITE_OMIT_AUTHORIZATION - SQLITE_OMIT_DECLTYPE - SQLITE_OMIT_DEPRECATED - SQLITE_OMIT_GET_TABLE - SQLITE_OMIT_LOAD_EXTENSION - SQLITE_OMIT_PROGRESS_CALLBACK - SQLITE_OMIT_SHARED_CACHE - SQLITE_OMIT_TCL_VARIABLE - SQLITE_OMIT_TRACE - SQLITE_USE_ALLOCA - SQLITE_UNTESTABLE - SQLITE_HAVE_ISNAN - SQLITE_HAVE_LOCALTIME_R - SQLITE_HAVE_LOCALTIME_S - ) + # Create a global alias for sqlite3 + if (NOT TARGET sqlite3) + add_library(sqlite3 ALIAS sqlite3_amplify) + endif() endif() -# Link the custom sqlite3 target to the amplify plugin. -target_link_libraries(${PLUGIN_NAME} PRIVATE ${SQLITE3_AMPLIFY_TARGET_NAME}) +# Link your plugin to the custom sqlite3 target +if (TARGET sqlite3_amplify) + target_link_libraries(${PLUGIN_NAME} PRIVATE sqlite3_amplify) +endif() -# List of absolute paths to libraries that should be bundled with the plugin. -set(amplify_db_common_bundled_libraries - "$" - PARENT_SCOPE -) +# Add dependencies to ensure proper build order +if (NOT USE_CUSTOM_SQLITE3 AND TARGET sqlite3_amplify) + add_dependencies(${PLUGIN_NAME} sqlite3_amplify) +endif() From 2b5eea51aaa4bfb8093317244d9ffcc3a2976c8d Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 12:28:50 -0600 Subject: [PATCH 12/17] refactor: opt flag --- .../amplify_db_common/example/pubspec.yaml | 2 +- .../example/windows/CMakeLists.txt | 3 + .../amplify_db_common/windows/CMakeLists.txt | 71 +++++++++++++------ 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/packages/common/amplify_db_common/example/pubspec.yaml b/packages/common/amplify_db_common/example/pubspec.yaml index aee1e97d19..b29e2ab36b 100644 --- a/packages/common/amplify_db_common/example/pubspec.yaml +++ b/packages/common/amplify_db_common/example/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: # Unused in example app, rather included to validate # windows app will build when there is a downstream dependency on sqlite3 # https://github.com/aws-amplify/amplify-flutter/issues/5477 - sqlite3: ">=2.0.0 <2.4.7" + # powersync: ^1.8.2 dev_dependencies: amplify_lints: ^2.0.0 diff --git a/packages/common/amplify_db_common/example/windows/CMakeLists.txt b/packages/common/amplify_db_common/example/windows/CMakeLists.txt index 188b7ea59c..ff569a0c62 100644 --- a/packages/common/amplify_db_common/example/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/example/windows/CMakeLists.txt @@ -6,6 +6,9 @@ project(amplify_db_common_example LANGUAGES CXX) # the on-disk name of your application. set(BINARY_NAME "amplify_db_common_example") +# Example for having SQLITE3 not bundled at build time due to another dep exposing it globally +# set(USE_CUSTOM_SQLITE3 ON) + # Explicitly opt in to modern CMake behaviors to avoid warnings with recent # versions of CMake. cmake_policy(SET CMP0063 NEW) diff --git a/packages/common/amplify_db_common/windows/CMakeLists.txt b/packages/common/amplify_db_common/windows/CMakeLists.txt index bc4d965675..e03a0f84d4 100644 --- a/packages/common/amplify_db_common/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/windows/CMakeLists.txt @@ -16,14 +16,15 @@ add_library(${PLUGIN_NAME} SHARED "amplify_db_common_plugin.cpp" ) -# ## +### # Below here, keep in sync with: https://github.com/simolus3/sqlite3.dart/blob/main/sqlite3_flutter_libs/windows/CMakeLists.txt -# ## +### # Essentially, the idea of this build script is to compile a sqlite3.dll -# and make Fluter bundle that with the final app. +# and make Flutter bundle that with the final app. # It looks like we can't avoid building a sqlite3_flutter_libs.dll too, # but that's not on me. + apply_standard_settings(${PLUGIN_NAME}) set_target_properties(${PLUGIN_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden) @@ -32,18 +33,37 @@ target_include_directories(${PLUGIN_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) -# Option to disable internal sqlite3 definition +# Option to allow users to opt out of the internal sqlite3 definition option(USE_CUSTOM_SQLITE3 "Disable internal sqlite3 definition to allow downstream dependencies to define their own" OFF) if (NOT USE_CUSTOM_SQLITE3) - # Define the sqlite3_amplify target - if (NOT TARGET sqlite3_amplify) - add_library(sqlite3_amplify SHARED "sqlite3_flutter.c") - - # Configure sqlite3 properties and definitions - target_include_directories(sqlite3_amplify PRIVATE "${sqlite3_SOURCE_DIR}") - target_compile_options(sqlite3_amplify PRIVATE "$<$>:-O2>" "/w") - target_compile_definitions(sqlite3_amplify PRIVATE + # Include and define sqlite3 if not already defined + if (NOT TARGET sqlite3) + 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( + sqlite3 + URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz + DOWNLOAD_EXTRACT_TIMESTAMP NEW + ) + else() + FetchContent_Declare( + sqlite3 + URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz + ) + endif() + FetchContent_MakeAvailable(sqlite3) + + add_library(sqlite3 SHARED "sqlite3_flutter.c") + + target_include_directories(sqlite3 PRIVATE "${sqlite3_SOURCE_DIR}") + target_compile_options(sqlite3 PRIVATE "$<$>:-O2>" "/w") + + # Note: Keep in sync with https://github.com/simolus3/sqlite-native-libraries/blob/master/sqlite3-native-library/cpp/CMakeLists.txt + target_compile_definitions(sqlite3 PRIVATE SQLITE_ENABLE_FTS5 SQLITE_ENABLE_RTREE SQLITE_DQS=0 @@ -65,20 +85,25 @@ if (NOT USE_CUSTOM_SQLITE3) SQLITE_HAVE_LOCALTIME_R SQLITE_HAVE_LOCALTIME_S ) - endif() + else() + # Add recovery suggestion when a duplicate sqlite3 dependency is detected + message(FATAL_ERROR + "The sqlite3 target already exists, causing a conflict. This issue may occur if another dependency also defines a sqlite3 target. - # Create a global alias for sqlite3 - if (NOT TARGET sqlite3) - add_library(sqlite3 ALIAS sqlite3_amplify) + Recovery suggestions: + Set the 'USE_CUSTOM_SQLITE3' option to ON within YOUR CMakeList.txt to disable the internal sqlite3 definition: + set(USE_CUSTOM_SQLITE3 ON)" + ) endif() endif() -# Link your plugin to the custom sqlite3 target -if (TARGET sqlite3_amplify) - target_link_libraries(${PLUGIN_NAME} PRIVATE sqlite3_amplify) +# Ensure sqlite3 actually gets built +if (NOT USE_CUSTOM_SQLITE3) + add_dependencies(${PLUGIN_NAME} sqlite3) endif() -# Add dependencies to ensure proper build order -if (NOT USE_CUSTOM_SQLITE3 AND TARGET sqlite3_amplify) - add_dependencies(${PLUGIN_NAME} sqlite3_amplify) -endif() +# List of absolute paths to libraries that should be bundled with the plugin +set(amplify_db_common_bundled_libraries + "$" + PARENT_SCOPE +) From a0c3ee433ba20ac226252c6678ada863a95a3ee2 Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 14:21:16 -0600 Subject: [PATCH 13/17] Flag off, powersync added, tests should fail --- packages/common/amplify_db_common/example/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common/example/pubspec.yaml b/packages/common/amplify_db_common/example/pubspec.yaml index b29e2ab36b..05951760b6 100644 --- a/packages/common/amplify_db_common/example/pubspec.yaml +++ b/packages/common/amplify_db_common/example/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: # Unused in example app, rather included to validate # windows app will build when there is a downstream dependency on sqlite3 # https://github.com/aws-amplify/amplify-flutter/issues/5477 - # powersync: ^1.8.2 + powersync: ^1.8.2 dev_dependencies: amplify_lints: ^2.0.0 From d9b1c5af36cbda7cedfc868d0afdef65024196cb Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 14:34:07 -0600 Subject: [PATCH 14/17] bump example app sdk --- packages/common/amplify_db_common/example/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common/example/pubspec.yaml b/packages/common/amplify_db_common/example/pubspec.yaml index 05951760b6..ed0f1766ff 100644 --- a/packages/common/amplify_db_common/example/pubspec.yaml +++ b/packages/common/amplify_db_common/example/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: "none" environment: flutter: ">=3.19.0" - sdk: ^3.3.0 + sdk: ^3.4.0 dependencies: amplify_db_common: ">=0.4.5 <0.5.0" From d6914f478b5f3bd9bc0f94739953e307ee83c97d Mon Sep 17 00:00:00 2001 From: equartey Date: Wed, 20 Nov 2024 14:48:59 -0600 Subject: [PATCH 15/17] downgraded powersync for ci --- packages/common/amplify_db_common/example/pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/amplify_db_common/example/pubspec.yaml b/packages/common/amplify_db_common/example/pubspec.yaml index ed0f1766ff..89a94e3dc3 100644 --- a/packages/common/amplify_db_common/example/pubspec.yaml +++ b/packages/common/amplify_db_common/example/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: "none" environment: flutter: ">=3.19.0" - sdk: ^3.4.0 + sdk: ^3.3.0 dependencies: amplify_db_common: ">=0.4.5 <0.5.0" @@ -14,7 +14,7 @@ dependencies: # Unused in example app, rather included to validate # windows app will build when there is a downstream dependency on sqlite3 # https://github.com/aws-amplify/amplify-flutter/issues/5477 - powersync: ^1.8.2 + powersync: 1.4.2 dev_dependencies: amplify_lints: ^2.0.0 From ff0a6756fe2da058533e46c6392c99ddda76ce7c Mon Sep 17 00:00:00 2001 From: Elijah Quartey Date: Wed, 20 Nov 2024 15:25:49 -0600 Subject: [PATCH 16/17] power sync build should pass --- .../common/amplify_db_common/example/windows/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/amplify_db_common/example/windows/CMakeLists.txt b/packages/common/amplify_db_common/example/windows/CMakeLists.txt index ff569a0c62..0ce7b87687 100644 --- a/packages/common/amplify_db_common/example/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/example/windows/CMakeLists.txt @@ -7,7 +7,7 @@ project(amplify_db_common_example LANGUAGES CXX) set(BINARY_NAME "amplify_db_common_example") # Example for having SQLITE3 not bundled at build time due to another dep exposing it globally -# set(USE_CUSTOM_SQLITE3 ON) +set(USE_CUSTOM_SQLITE3 ON) # Explicitly opt in to modern CMake behaviors to avoid warnings with recent # versions of CMake. From 9e33e343f6c7bcc97217476a16fc1b7c852b30b2 Mon Sep 17 00:00:00 2001 From: Elijah Quartey Date: Thu, 21 Nov 2024 09:12:23 -0600 Subject: [PATCH 17/17] Disabled downstream dep & flag. Tests should pass --- packages/common/amplify_db_common/example/pubspec.yaml | 7 ++++--- .../amplify_db_common/example/windows/CMakeLists.txt | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/common/amplify_db_common/example/pubspec.yaml b/packages/common/amplify_db_common/example/pubspec.yaml index 89a94e3dc3..a5b6ab272c 100644 --- a/packages/common/amplify_db_common/example/pubspec.yaml +++ b/packages/common/amplify_db_common/example/pubspec.yaml @@ -11,10 +11,11 @@ dependencies: drift: ">=2.18.0 <2.19.0" flutter: sdk: flutter - # Unused in example app, rather included to validate - # windows app will build when there is a downstream dependency on sqlite3 + # Included to validate windows app will build when there is a downstream + # dependency on sqlite3. Also requires the USE_CUSTOM_SQLITE3=ON flag set + # in the consuming App's CMakeLists.txt # https://github.com/aws-amplify/amplify-flutter/issues/5477 - powersync: 1.4.2 + # powersync: 1.4.2 dev_dependencies: amplify_lints: ^2.0.0 diff --git a/packages/common/amplify_db_common/example/windows/CMakeLists.txt b/packages/common/amplify_db_common/example/windows/CMakeLists.txt index 0ce7b87687..ff569a0c62 100644 --- a/packages/common/amplify_db_common/example/windows/CMakeLists.txt +++ b/packages/common/amplify_db_common/example/windows/CMakeLists.txt @@ -7,7 +7,7 @@ project(amplify_db_common_example LANGUAGES CXX) set(BINARY_NAME "amplify_db_common_example") # Example for having SQLITE3 not bundled at build time due to another dep exposing it globally -set(USE_CUSTOM_SQLITE3 ON) +# set(USE_CUSTOM_SQLITE3 ON) # Explicitly opt in to modern CMake behaviors to avoid warnings with recent # versions of CMake.