Skip to content

Commit 3591aee

Browse files
committed
Merge branch 'bundle-sqlite'
Use built-in SQLite if system library not found. See #1229.
2 parents 4963406 + 0fd87b6 commit 3591aee

File tree

12 files changed

+105
-62
lines changed

12 files changed

+105
-62
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "3rdparty/sqlite3"]
2+
path = 3rdparty/sqlite3
3+
url = https://github.com/vadz/sqlite-amalgamation.git

3rdparty/sqlite3

Submodule sqlite3 added at 46a41f1

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ project(SOCI
1414
VERSION ${SOCI_VERSION}
1515
DESCRIPTION "C++ database access library"
1616
HOMEPAGE_URL "https://soci.sourceforge.net/"
17-
LANGUAGES CXX
17+
LANGUAGES C CXX
1818
)
1919

2020
include(soci_utils)

appveyor.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ install:
7272
$env:SOCI_ODBC_SKIP_TESTS = $env:SOCI_ODBC_SKIP_TESTS + '|soci_odbc_test_postgresql'
7373
}
7474
Write-Output "To be skipped ODBC tests: $env:SOCI_ODBC_SKIP_TESTS"
75-
- git clone https://github.com/snikulov/sqlite.cmake.build.git C:\projects\sqlite\src
7675
# Ensure we have a recent cmake version available
7776
- choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
7877
- refreshenv
78+
- git submodule update --init
7979

8080
before_build:
81-
- set SQLITE_ROOT=C:\projects\sqlite\sqlite
8281
# dirty little hack - remove sh from Git to make generator happy
8382
- ps: |
8483
if ($env:G -eq "MinGW Makefiles")
@@ -91,12 +90,10 @@ before_build:
9190
Remove-Item $shellPath
9291
}
9392
}
94-
$env:SQLITE3_LIBRARY = $env:SQLITE_ROOT + '/lib/libsqlite3-static.a'
9593
$env:BUILD_TOOL_OPTIONS = '-j3'
9694
}
9795
else
9896
{
99-
$env:SQLITE3_LIBRARY = $env:SQLITE_ROOT + '/lib/sqlite3-static.lib'
10097
$env:BUILD_TOOL_OPTIONS = ''
10198
# Set up Visual Studio environment (MSVC compiler etc.)
10299
# Works by calling the respective bat script and then copying over the env vars
@@ -108,9 +105,6 @@ before_build:
108105
}
109106
}
110107
}
111-
- cd C:\projects\sqlite\src
112-
- mkdir build
113-
- cd build
114108
- set PATH=%MINGW_BIN%;%POSTGRESQL_ROOT%\bin;%MYSQL_DIR%\bin;%MYSQL_DIR%\lib;%PATH%
115109
- echo %PATH%
116110
- cmake --version
@@ -121,14 +115,12 @@ before_build:
121115
- set USER=root
122116
- mysql -e "create database soci_test;" --user=root
123117
- sqlcmd -U sa -P Password12! -S (local)\SQL%MSSQL_VER% -i C:\projects\soci\scripts\windows\mssql_db_create.sql
124-
- cmake .. -G"%G%" -DSQLITE_BUILD_SHARED=OFF -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_INSTALL_PREFIX=%SQLITE_ROOT% -DCMAKE_UNITY_BUILD=ON
125-
- cmake --build . --config %CONFIGURATION% --target install
126118

127119
build_script:
128120
- cd C:\projects\soci
129121
- mkdir build
130122
- cd build
131-
- cmake .. -G"%G%" -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_VERBOSE_MAKEFILE=ON -DSOCI_ENABLE_WERROR=ON -DCMAKE_PREFIX_PATH=%SQLITE_ROOT% -DSQLite3_LIBRARY=%SQLITE3_LIBRARY% -DCMAKE_UNITY_BUILD=ON
123+
- cmake .. -G"%G%" -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_VERBOSE_MAKEFILE=ON -DSOCI_ENABLE_WERROR=ON -DCMAKE_UNITY_BUILD=ON
132124
- cmake --build . --config %CONFIGURATION% -- %BUILD_TOOL_OPTIONS%
133125

134126
test_script:

cmake/soci_define_backend_target.cmake

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include(soci_utils)
99
# BACKEND_NAME <name> Name of the backend
1010
# ENABLED_VARIABLE <variable> CMake variable that indicates whether this backend is enabled. Will be set
1111
# to OFF if one of the dependencies are not satisfied.
12-
# MISSING_DEPENDENCY_BEHAVIOR <behavior> What to do if a dependency is not found. Valid values are "ERROR" and "DISABLE"
12+
# MISSING_DEPENDENCY_BEHAVIOR <behavior> What to do if a dependency is not found. Valid values are "ERROR", "DISABLE" and "BUILTIN".
1313
# TARGET_NAME <target> Name of the CMake target that shall be created for this backend
1414
# DEPENDENCIES <spec1> [... <specN>] List of dependency specifications. Each specification has to be a single
1515
# argument (single string) following the syntax
@@ -58,6 +58,9 @@ function(soci_define_backend_target)
5858
set(ERROR_ON_MISSING_DEPENDENCY ON)
5959
elseif(DEFINE_BACKEND_MISSING_DEPENDENCY_BEHAVIOR STREQUAL "DISABLE")
6060
set(DISABLE_ON_MISSING_DEPENDENCY ON)
61+
elseif(DEFINE_BACKEND_MISSING_DEPENDENCY_BEHAVIOR STREQUAL "BUILTIN")
62+
set(REQUIRE_FLAG "QUIET")
63+
set(BUILTIN_ON_MISSING_DEPENDENCY ON)
6164
else()
6265
message(FATAL_ERROR "Invalid value '${DEFINE_BACKEND_MISSING_DEPENDENCY_BEHAVIOR}' for option 'MISSING_DEPENDENCY_BEHAVIOR'")
6366
endif()
@@ -88,22 +91,29 @@ function(soci_define_backend_target)
8891
get_property(DESCRIPTION CACHE "${DEFINE_BACKEND_ENABLED_VARIABLE}" PROPERTY HELPSTRING)
8992
set(${DEFINE_BACKEND_ENABLED_VARIABLE} OFF CACHE STRING "${DESCRIPTION}" FORCE)
9093
return()
94+
elseif(BUILTIN_ON_MISSING_DEPENDENCY)
95+
message(STATUS "Falling back on built-in version of '${CURRENT_DEP}' for SOCI backend '${DEFINE_BACKEND_BACKEND_NAME}'")
9196
else()
9297
message(FATAL_ERROR "Unspecified handling of unmet dependency")
9398
endif()
94-
endif()
99+
else()
100+
# This is wasteful, but do it again without "QUIET" flag to show the result if it succeeded.
101+
if (REQUIRE_FLAG STREQUAL "QUIET")
102+
find_package(${CURRENT_DEP_SEARCH})
103+
endif()
95104

96-
foreach (CURRENT IN LISTS CURRENT_DEP_TARGETS)
97-
if (NOT TARGET "${CURRENT}")
98-
message(FATAL_ERROR "Expected successful find_package call with '${CURRENT_DEP_SEARCH}' to define target '${CURRENT}'")
105+
foreach (CURRENT IN LISTS CURRENT_DEP_TARGETS)
106+
if (NOT TARGET "${CURRENT}")
107+
message(FATAL_ERROR "Expected successful find_package call with '${CURRENT_DEP_SEARCH}' to define target '${CURRENT}'")
108+
endif()
109+
endforeach()
110+
if (CURRENT_DEP_DEFINES)
111+
set(MACRO_NAMES_ARG "MACRO_NAMES ${CURRENT_DEP_DEFINES}")
99112
endif()
100-
endforeach()
101-
if (CURRENT_DEP_DEFINES)
102-
set(MACRO_NAMES_ARG "MACRO_NAMES ${CURRENT_DEP_DEFINES}")
113+
list(APPEND PUBLIC_DEP_CALL_ARGS
114+
"NAME ${CURRENT_DEP} DEP_TARGETS ${CURRENT_DEP_TARGETS} TARGET SOCI::${DEFINE_BACKEND_ALIAS_NAME} ${MACRO_NAMES_ARG} REQUIRED"
115+
)
103116
endif()
104-
list(APPEND PUBLIC_DEP_CALL_ARGS
105-
"NAME ${CURRENT_DEP} DEP_TARGETS ${CURRENT_DEP_TARGETS} TARGET SOCI::${DEFINE_BACKEND_ALIAS_NAME} ${MACRO_NAMES_ARG} REQUIRED"
106-
)
107117
endforeach()
108118

109119

docs/backends/sqlite3.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,11 @@ SOCI backend for accessing SQLite 3 database.
66

77
### Supported Versions
88

9-
The SOCI SQLite3 backend is supported for use with SQLite3 >= 3.1
10-
11-
### Tested Platforms
12-
13-
|SQLite3|OS|Compiler|
14-
|--- |--- |--- |
15-
|3.12.1|Windows Server 2016|MSVC++ 14.1|
16-
|3.12.1|Windows Server 2012 R2|MSVC++ 14.0|
17-
|3.12.1|Windows Server 2012 R2|MSVC++ 12.0|
18-
|3.12.1|Windows Server 2012 R2|MSVC++ 11.0|
19-
|3.12.1|Windows Server 2012 R2|Mingw-w64/GCC 4.8|
20-
|3.7.9|Ubuntu 12.04|g++ 4.6.3|
21-
|3.4.0|Windows XP|(cygwin) g++ 3.4.4|
22-
|3.4.0|Windows XP|Visual C++ 2005 Express Edition|
23-
|3.3.8|Windows XP|Visual C++ 2005 Professional|
24-
|3.5.2|Mac OS X 10.5|g++ 4.0.1|
25-
|3.3.4|Ubuntu 5.1|g++ 4.0.2|
26-
|3.3.4|Windows XP|(cygwin) g++ 3.3.4|
27-
|3.3.4|Windows XP|Visual C++ 2005 Express Edition|
28-
|3.2.1|Linux i686 2.6.10-gentoo-r6|g++ 3.4.5|
29-
|3.1.3|Mac OS X 10.4|g++ 4.0.1|
30-
|3.24.0|macOS High Sierra 10.13.5|AppleClang 9.1.0.9020039|
9+
The oldest tested SQLite3 version that SOCI was tested with was 3.1.3 and the latest tested version is 3.49.1. Due to high degree of SQLite3 backwards compatibility, it is likely that newer versions can work as well.
3110

3211
### Required Client Libraries
3312

34-
The SOCI SQLite3 backend requires SQLite3's `libsqlite3` client library.
13+
The SOCI SQLite3 backend uses `libsqlite3` client library if available or the built-in version of SQLite3 included in the SOCI source code if this library cannot be found.
3514

3615
### Connecting to the Database
3716

@@ -171,6 +150,10 @@ The SQLite3 backend provides the following concrete classes for navite API acces
171150

172151
SQLite3 result code is provided via the backend specific `sqlite3_soci_error` class. Catching the backend specific error yields the value of SQLite3 result code via the `result()` method.
173152

153+
### SQLite3 version information
154+
155+
`sqlie3_session_backend` class, declared in `<soci/sqlite3/soci-sqlite3.h>`, provides static `libversion_number()` and `libversion()` functions which can be used to retrieve the SQLite3 version as a number (e.g. `3049001`) and as a string (e.g. `3.49.1`) respectively.
156+
174157
## Configuration options
175158

176159
None

docs/installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ where X.Y.Z is the version number. Unpack the archive.
2727
You can always clone SOCI from the Git repository:
2828

2929
```console
30-
git clone git://github.com/SOCI/soci.git
30+
git clone --recurse-submodules git://github.com/SOCI/soci.git
3131
```
3232

3333
## Building with CMake
@@ -145,7 +145,7 @@ Furthermore, the `MYSQL_DIR` _environment variable_ can be set to the MySQL inst
145145

146146
#### SQLite 3
147147

148-
* `SOCI_SQLITE3` - Enabler - Enables the [SQLite3](backends/sqlite3.md) backend.
148+
* `SOCI_SQLITE3` - Enabler - Enables the [SQLite3](backends/sqlite3.md) backend. Note that, unlike with all the other backends, if SQLite3 library is not found, built-in version of SQLite3 is used instead of the backend being disabled.
149149
* `SOCI_SQLITE3_TEST_CONNSTR` - string - Connection string is simply a file path where SQLite3 test database will be created (e.g. /home/john/soci_test.db). Check [SQLite3 backend reference](backends/sqlite3.md) for details. Example: `-DSOCI_SQLITE3_TEST_CONNSTR="my.db"` or `-DSOCI_SQLITE3_TEST_CONNSTR=":memory:"`.
150150
* `SOCI_SQLITE3_SKIP_TESTS` - boolean - Skips testing this backend.
151151

include/soci/sqlite3/soci-sqlite3.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,27 @@
3131
namespace sqlite_api
3232
{
3333

34-
#if SQLITE_VERSION_NUMBER < 3003010
35-
// The sqlite3_destructor_type typedef introduced in 3.3.10
36-
// https://www.sqlite.org/cvstrac/tktview?tn=2191
37-
typedef void (*sqlite3_destructor_type)(void*);
34+
// Don't include sqlite3.h from outside SOCI: this header might not be
35+
// available when using built-in SQLite3 as we don't install it in this case.
36+
#ifdef SOCI_SQLITE3_SOURCE
37+
#include <sqlite3.h>
38+
#else // !SOCI_SQLITE3_SOURCE
39+
// We need just a couple of forward declarations to make this header itself
40+
// compile.
41+
struct sqlite3;
42+
struct sqlite3_stmt;
43+
44+
#if defined(_MSC_VER)
45+
typedef __int64 sqlite3_int64;
46+
typedef unsigned __int64 sqlite3_uint64;
47+
#else
48+
typedef long long int sqlite3_int64;
49+
typedef unsigned long long int sqlite3_uint64;
3850
#endif
39-
40-
#include <sqlite3.h>
51+
#endif // SOCI_SQLITE3_SOURCE/!SOCI_SQLITE3_SOURCE
4152

4253
} // namespace sqlite_api
4354

44-
#undef SQLITE_STATIC
45-
#define SQLITE_STATIC ((sqlite_api::sqlite3_destructor_type)0)
46-
4755
#ifdef _MSC_VER
4856
#pragma warning(pop)
4957
#endif
@@ -362,6 +370,10 @@ struct SOCI_SQLITE3_DECL sqlite3_session_backend : details::session_backend
362370
}
363371
}
364372

373+
// Get information about SQLite3 version used.
374+
static const char* libversion();
375+
static int libversion_number();
376+
365377
sqlite_api::sqlite3 *conn_;
366378

367379
// This flag is set to true if the internal sqlite_sequence table exists in

src/backends/sqlite3/CMakeLists.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
include(soci_define_backend_target)
22

33
if (SOCI_SQLITE3_AUTO)
4-
set(DEPENDENCY_MODE "DISABLE")
4+
# Unlike most other backends, we can fall back on using the built-in
5+
# version of SQLite3 if the system version is not found.
6+
set(DEPENDENCY_MODE "BUILTIN")
57
else()
68
set(DEPENDENCY_MODE "ERROR")
79
endif()
@@ -32,3 +34,31 @@ soci_define_backend_target(
3234
if (NOT SOCI_SQLITE3)
3335
return()
3436
endif()
37+
38+
# If SQLite3 was found, this target must have been created.
39+
if (NOT TARGET SQLite::SQLite3)
40+
# But if it wasn't, use our built-in version, after checking that it is
41+
# available.
42+
if (NOT EXISTS "${PROJECT_SOURCE_DIR}/3rdparty/sqlite3/sqlite3.c")
43+
message(WARNING "SQLite3 not found and built-in version not available, have you cloned SOCI repository with --recurse-submodules?")
44+
45+
get_property(DESCRIPTION CACHE SOCI_SQLITE3 PROPERTY HELPSTRING)
46+
set(SOCI_SQLITE3 OFF CACHE STRING "${DESCRIPTION}" FORCE)
47+
return()
48+
endif()
49+
50+
add_library(soci_sqlite3_builtin)
51+
target_sources(soci_sqlite3_builtin
52+
PRIVATE
53+
"${PROJECT_SOURCE_DIR}/3rdparty/sqlite3/sqlite3.c"
54+
)
55+
target_include_directories(soci_sqlite3_builtin
56+
PUBLIC
57+
"${PROJECT_SOURCE_DIR}/3rdparty/sqlite3"
58+
)
59+
60+
target_link_libraries(soci_sqlite3
61+
PRIVATE
62+
soci_sqlite3_builtin
63+
)
64+
endif()

src/backends/sqlite3/session.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,15 @@ sqlite3_blob_backend * sqlite3_session_backend::make_blob_backend()
313313
{
314314
return new sqlite3_blob_backend(*this);
315315
}
316+
317+
// static
318+
const char* sqlite3_session_backend::libversion()
319+
{
320+
return sqlite3_libversion();
321+
}
322+
323+
// static
324+
int sqlite3_session_backend::libversion_number()
325+
{
326+
return sqlite3_libversion_number();
327+
}

0 commit comments

Comments
 (0)