Skip to content

Commit bc314f6

Browse files
authored
fix(common): added a flag to opt out of bundling sqlite3 for windows apps (#5680)
1 parent 2ee74bb commit bc314f6

File tree

3 files changed

+74
-63
lines changed

3 files changed

+74
-63
lines changed

packages/common/amplify_db_common/example/pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ dependencies:
1111
drift: ">=2.18.0 <2.19.0"
1212
flutter:
1313
sdk: flutter
14-
# Unused in example app, rather included to validate
15-
# windows app will build when there is a downstream dependency on sqlite3
14+
# Included to validate windows app will build when there is a downstream
15+
# dependency on sqlite3. Also requires the USE_CUSTOM_SQLITE3=ON flag set
16+
# in the consuming App's CMakeLists.txt
1617
# https://github.com/aws-amplify/amplify-flutter/issues/5477
17-
sqlite3: ">=2.0.0 <2.4.7"
18+
# powersync: 1.4.2
1819

1920
dev_dependencies:
2021
amplify_lints: ^2.0.0

packages/common/amplify_db_common/example/windows/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/common/amplify_db_common/windows/CMakeLists.txt

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ add_library(${PLUGIN_NAME} SHARED
1616
"amplify_db_common_plugin.cpp"
1717
)
1818

19-
# ##
19+
###
2020
# Below here, keep in sync with: https://github.com/simolus3/sqlite3.dart/blob/main/sqlite3_flutter_libs/windows/CMakeLists.txt
21-
# ##
21+
###
2222

2323
# Essentially, the idea of this build script is to compile a sqlite3.dll
24-
# and make Fluter bundle that with the final app.
24+
# and make Flutter bundle that with the final app.
2525
# It looks like we can't avoid building a sqlite3_flutter_libs.dll too,
2626
# but that's not on me.
27+
2728
apply_standard_settings(${PLUGIN_NAME})
2829
set_target_properties(${PLUGIN_NAME} PROPERTIES
2930
CXX_VISIBILITY_PRESET hidden)
@@ -32,71 +33,77 @@ target_include_directories(${PLUGIN_NAME} INTERFACE
3233
"${CMAKE_CURRENT_SOURCE_DIR}/include")
3334
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
3435

35-
include(FetchContent)
36-
37-
# Only add the sqlite3 library if it hasn't been defined already.
38-
if(NOT TARGET sqlite3)
39-
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
40-
# cmake 3.24.0 added the `DOWNLOAD_EXTRACT_TIMESTAMP` and prints an ugly warning when
41-
# the default is used, so override it to the recommended behavior.
42-
# We can't really ask users to use a cmake that recent, so there's this if here.
43-
FetchContent_Declare(
44-
sqlite3
45-
URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz
46-
DOWNLOAD_EXTRACT_TIMESTAMP NEW
47-
)
48-
else()
49-
FetchContent_Declare(
50-
sqlite3
51-
URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz
52-
)
53-
endif()
36+
# Option to allow users to opt out of the internal sqlite3 definition
37+
option(USE_CUSTOM_SQLITE3 "Disable internal sqlite3 definition to allow downstream dependencies to define their own" OFF)
5438

55-
FetchContent_MakeAvailable(sqlite3)
39+
if (NOT USE_CUSTOM_SQLITE3)
40+
# Include and define sqlite3 if not already defined
41+
if (NOT TARGET sqlite3)
42+
include(FetchContent)
43+
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
44+
# cmake 3.24.0 added the `DOWNLOAD_EXTRACT_TIMESTAMP` and prints an ugly warning when
45+
# the default is used, so override it to the recommended behavior.
46+
# We can't really ask users to use a cmake that recent, so there's this if here.
47+
FetchContent_Declare(
48+
sqlite3
49+
URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz
50+
DOWNLOAD_EXTRACT_TIMESTAMP NEW
51+
)
52+
else()
53+
FetchContent_Declare(
54+
sqlite3
55+
URL https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz
56+
)
57+
endif()
58+
FetchContent_MakeAvailable(sqlite3)
5659

57-
# Define the sqlite3 library only if it wasn't already defined.
58-
add_library(sqlite3 SHARED "sqlite3_flutter.c")
60+
add_library(sqlite3 SHARED "sqlite3_flutter.c")
5961

60-
target_include_directories(sqlite3 PRIVATE "${sqlite3_SOURCE_DIR}")
61-
target_compile_options(sqlite3 PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O2>" "/w")
62+
target_include_directories(sqlite3 PRIVATE "${sqlite3_SOURCE_DIR}")
63+
target_compile_options(sqlite3 PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O2>" "/w")
6264

63-
# Note: Keep in sync with https://github.com/simolus3/sqlite-native-libraries/blob/master/sqlite3-native-library/cpp/CMakeLists.txt
64-
target_compile_definitions(sqlite3 PRIVATE
65-
SQLITE_ENABLE_FTS5
66-
SQLITE_ENABLE_RTREE
67-
SQLITE_DQS=0
68-
SQLITE_DEFAULT_MEMSTATUS=0
69-
SQLITE_TEMP_STORE=2
70-
SQLITE_MAX_EXPR_DEPTH=0
71-
SQLITE_OMIT_AUTHORIZATION
72-
SQLITE_OMIT_DECLTYPE
73-
SQLITE_OMIT_DEPRECATED
74-
SQLITE_OMIT_GET_TABLE
75-
SQLITE_OMIT_LOAD_EXTENSION
76-
SQLITE_OMIT_PROGRESS_CALLBACK
77-
SQLITE_OMIT_SHARED_CACHE
78-
SQLITE_OMIT_TCL_VARIABLE
79-
SQLITE_OMIT_TRACE
80-
SQLITE_USE_ALLOCA
81-
SQLITE_UNTESTABLE
82-
SQLITE_HAVE_ISNAN
83-
SQLITE_HAVE_LOCALTIME_R
84-
SQLITE_HAVE_LOCALTIME_S
85-
)
65+
# Note: Keep in sync with https://github.com/simolus3/sqlite-native-libraries/blob/master/sqlite3-native-library/cpp/CMakeLists.txt
66+
target_compile_definitions(sqlite3 PRIVATE
67+
SQLITE_ENABLE_FTS5
68+
SQLITE_ENABLE_RTREE
69+
SQLITE_DQS=0
70+
SQLITE_DEFAULT_MEMSTATUS=0
71+
SQLITE_TEMP_STORE=2
72+
SQLITE_MAX_EXPR_DEPTH=0
73+
SQLITE_OMIT_AUTHORIZATION
74+
SQLITE_OMIT_DECLTYPE
75+
SQLITE_OMIT_DEPRECATED
76+
SQLITE_OMIT_GET_TABLE
77+
SQLITE_OMIT_LOAD_EXTENSION
78+
SQLITE_OMIT_PROGRESS_CALLBACK
79+
SQLITE_OMIT_SHARED_CACHE
80+
SQLITE_OMIT_TCL_VARIABLE
81+
SQLITE_OMIT_TRACE
82+
SQLITE_USE_ALLOCA
83+
SQLITE_UNTESTABLE
84+
SQLITE_HAVE_ISNAN
85+
SQLITE_HAVE_LOCALTIME_R
86+
SQLITE_HAVE_LOCALTIME_S
87+
)
88+
else()
89+
# Add recovery suggestion when a duplicate sqlite3 dependency is detected
90+
message(FATAL_ERROR
91+
"The sqlite3 target already exists, causing a conflict. This issue may occur if another dependency also defines a sqlite3 target.
8692
87-
# Create an alias for this version of sqlite3.
88-
add_library(sqlite3_amplify_db_common ALIAS sqlite3)
89-
else()
90-
# If sqlite3 already exists, create an alias for amplify plugin to avoid duplication.
91-
add_library(sqlite3_amplify_db_common ALIAS sqlite3)
93+
Recovery suggestions:
94+
Set the 'USE_CUSTOM_SQLITE3' option to ON within YOUR CMakeList.txt to disable the internal sqlite3 definition:
95+
set(USE_CUSTOM_SQLITE3 ON)"
96+
)
97+
endif()
9298
endif()
9399

94-
target_link_libraries(${PLUGIN_NAME} PRIVATE sqlite3_amplify_db_common)
95-
96-
add_dependencies(${PLUGIN_NAME} sqlite3_amplify_db_common)
100+
# Ensure sqlite3 actually gets built
101+
if (NOT USE_CUSTOM_SQLITE3)
102+
add_dependencies(${PLUGIN_NAME} sqlite3)
103+
endif()
97104

98-
# List of absolute paths to libraries that should be bundled with the plugin.
105+
# List of absolute paths to libraries that should be bundled with the plugin
99106
set(amplify_db_common_bundled_libraries
100-
"$<TARGET_FILE:sqlite3_amplify_db_common>"
107+
"$<TARGET_FILE:sqlite3>"
101108
PARENT_SCOPE
102109
)

0 commit comments

Comments
 (0)