Skip to content

Commit ab8ff73

Browse files
committed
Fix linking with system library (libsqlite3)
Add FindSQLite3 from CMake v3.14 in cmake subdir custom CMake modules like FindSQLiteCpp Add this cmake subdir to CMAKE_MODULE_PATH Add a Travis CI build configuration using the libsqlite3-dev packagee from the Linux/Ubuntu distribution
1 parent a7d9456 commit ab8ff73

File tree

3 files changed

+91
-21
lines changed

3 files changed

+91
-21
lines changed

.travis.yml

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ os: linux
99
cache:
1010
apt: true
1111

12+
env:
13+
global:
14+
- BUILD_TYPE=Debug
15+
- ASAN=ON
16+
- INTERNAL_SQLITE=ON
17+
- VALGRIND=OFF
18+
1219
matrix:
1320
include:
1421

@@ -31,15 +38,14 @@ matrix:
3138
- dist: bionic
3239
env:
3340
- cc=gcc cxx=g++
34-
- BUILD_TYPE=Debug
35-
- ASAN=ON GCOV=ON
41+
- GCOV=ON
3642
- COVERALLS=true
3743

3844
# GCC Debug build with Valgrind instead of Address Sanitizer
3945
- dist: bionic
4046
env:
4147
- cc=gcc cxx=g++
42-
- BUILD_TYPE=Debug
48+
- ASAN=OFF
4349
- VALGRIND=true
4450

4551
# GCC Release build
@@ -48,17 +54,19 @@ matrix:
4854
- cc=gcc cxx=g++
4955
- BUILD_TYPE=Release
5056

57+
# GCC test linking with libsqlite3-dev package
58+
- dist: bionic
59+
env:
60+
- cc=gcc cxx=g++
61+
- INTERNAL_SQLITE=OFF
62+
5163
- dist: xenial
5264
env:
5365
- cc=gcc cxx=g++
54-
- BUILD_TYPE=Debug
55-
- ASAN=ON
5666

5767
- dist: trusty
5868
env:
5969
- cc=gcc cxx=g++
60-
- BUILD_TYPE=Debug
61-
- ASAN=ON
6270

6371
##########################################################################
6472
# Clang on Linux
@@ -67,20 +75,14 @@ matrix:
6775
- dist: bionic
6876
env:
6977
- cc=clang cxx=clang++
70-
- BUILD_TYPE=Debug
71-
- ASAN=ON
7278

7379
- dist: xenial
7480
env:
7581
- cc=clang cxx=clang++
76-
- BUILD_TYPE=Debug
77-
- ASAN=ON
7882

7983
- dist: trusty
8084
env:
8185
- cc=clang cxx=clang++
82-
- BUILD_TYPE=Debug
83-
- ASAN=ON
8486

8587
##########################################################################
8688
# Clang on OSX
@@ -90,23 +92,20 @@ matrix:
9092
- os: osx
9193
env:
9294
- cc=clang cxx=clang++
93-
- BUILD_TYPE=Debug
94-
- ASAN=ON
9595

9696
# XCode 8.3
9797
- os: osx
9898
osx_image: xcode8.3
9999
env:
100100
- cc=clang cxx=clang++
101-
- BUILD_TYPE=Debug
102-
- ASAN=ON
103101

104102
before_install:
105103
# Coverity: don't use addons.coverity_scan since it run on every job of the build matrix, which waste resources and exhausts quotas
106104
# Note: the job dedicated to Coverity need to only run the shell script and then exit (to not try to build and run unit tests etc.)
107105
- if [[ -n "$COVERITY_SCAN_PROJECT_NAME" ]] ; then curl -s https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh | bash ; exit 0 ; fi
108106

109-
- if [[ "$VALGRIND" == "true" ]]; then sudo apt-get install -qq valgrind ; fi
107+
- if [[ "$INTERNAL_SQLITE" == "OFF" ]]; then sudo apt-get install libsqlite3-dev ; fi
108+
- if [[ "$VALGRIND" == "true" ]]; then sudo apt-get install valgrind ; fi
110109
- if [[ "$COVERALLS" == "true" ]]; then pip install --user cpp-coveralls ; fi
111110

112111
# Set the compiler environment variables properly
@@ -117,7 +116,7 @@ before_install:
117116
before_script:
118117
- mkdir build
119118
- cd build
120-
- cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSQLITECPP_USE_ASAN=$ASAN -DSQLITECPP_USE_GCOV=$GCOV -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON ..
119+
- cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DSQLITECPP_INTERNAL_SQLITE=$INTERNAL_SQLITE -DSQLITECPP_USE_ASAN=$ASAN -DSQLITECPP_USE_GCOV=$GCOV -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON ..
121120

122121
# build examples, and run tests (ie make & make test)
123122
script:

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
66
# or copy at http://opensource.org/licenses/MIT)
77
cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
8+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp
89
project(SQLiteCpp VERSION "2.99")
910

1011
# SQLiteC++ 3.x now requires C++11 compiler
@@ -154,6 +155,8 @@ set(SQLITECPP_SCRIPT
154155
cpplint.py
155156
Doxyfile
156157
cmake/FindSQLiteCpp.cmake
158+
cmake/FindSQLite3.cmake
159+
cmake/SQLiteCppConfig.cmake.in
157160
)
158161
source_group(scripts FILES ${SQLITECPP_SCRIPT})
159162

@@ -246,15 +249,17 @@ install(FILES
246249

247250
option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON)
248251
if (SQLITECPP_INTERNAL_SQLITE)
252+
message(STATUS "Compile sqlite3 from source in subdirectory")
249253
# build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
250254
add_subdirectory(sqlite3)
251255
target_include_directories(sqlite3 PUBLIC "${PROJECT_SOURCE_DIR}/sqlite3")
252256
target_include_directories(SQLiteCpp PRIVATE "${PROJECT_SOURCE_DIR}/sqlite3")
253257
else (SQLITECPP_INTERNAL_SQLITE)
254258
find_package (SQLite3 REQUIRED)
255259
if (SQLITE3_FOUND)
260+
message(STATUS "Link to sqlite3 system library")
256261
include_directories(${SQLITE3_INCLUDE_DIRS})
257-
target_link_libraries (SQLiteCpp ${SQLITE3_LIBRARIES})
262+
target_link_libraries(SQLiteCpp ${SQLITE3_LIBRARIES})
258263
endif (SQLITE3_FOUND)
259264
endif (SQLITECPP_INTERNAL_SQLITE)
260265

@@ -336,7 +341,7 @@ if (SQLITECPP_BUILD_TESTS)
336341
else (GTEST_FOUND)
337342
# deactivate some warnings for compiling the googletest library
338343
if (NOT MSVC)
339-
add_compile_options(-Wno-variadic-macros -Wno-long-long -Wno-switch-enum -Wno-float-equal -Wno-conversion-null -Wno-switch-default -Wno-pedantic)
344+
add_compile_options(-Wno-switch-enum)
340345
endif (NOT MSVC)
341346

342347
# add the subdirectory containing the CMakeLists.txt for the googletest library

cmake/FindSQLite3.cmake

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file Copyright.txt or https://cmake.org/licensing for details.
3+
4+
#[=======================================================================[.rst:
5+
FindSQLite3
6+
-----------
7+
8+
Find the SQLite libraries, v3
9+
10+
IMPORTED targets
11+
^^^^^^^^^^^^^^^^
12+
13+
This module defines the following :prop_tgt:`IMPORTED` target:
14+
15+
``SQLite::SQLite3``
16+
17+
Result variables
18+
^^^^^^^^^^^^^^^^
19+
20+
This module will set the following variables if found:
21+
22+
``SQLite3_INCLUDE_DIRS``
23+
where to find sqlite3.h, etc.
24+
``SQLite3_LIBRARIES``
25+
the libraries to link against to use SQLite3.
26+
``SQLite3_VERSION``
27+
version of the SQLite3 library found
28+
``SQLite3_FOUND``
29+
TRUE if found
30+
31+
#]=======================================================================]
32+
33+
# Look for the necessary header
34+
find_path(SQLite3_INCLUDE_DIR NAMES sqlite3.h)
35+
mark_as_advanced(SQLite3_INCLUDE_DIR)
36+
37+
# Look for the necessary library
38+
find_library(SQLite3_LIBRARY NAMES sqlite3 sqlite)
39+
mark_as_advanced(SQLite3_LIBRARY)
40+
41+
# Extract version information from the header file
42+
if(SQLite3_INCLUDE_DIR)
43+
file(STRINGS ${SQLite3_INCLUDE_DIR}/sqlite3.h _ver_line
44+
REGEX "^#define SQLITE_VERSION *\"[0-9]+\\.[0-9]+\\.[0-9]+\""
45+
LIMIT_COUNT 1)
46+
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+"
47+
SQLite3_VERSION "${_ver_line}")
48+
unset(_ver_line)
49+
endif()
50+
51+
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
52+
find_package_handle_standard_args(SQLite3
53+
REQUIRED_VARS SQLite3_INCLUDE_DIR SQLite3_LIBRARY
54+
VERSION_VAR SQLite3_VERSION)
55+
56+
# Create the imported target
57+
if(SQLite3_FOUND)
58+
set(SQLite3_INCLUDE_DIRS ${SQLite3_INCLUDE_DIR})
59+
set(SQLite3_LIBRARIES ${SQLite3_LIBRARY})
60+
if(NOT TARGET SQLite::SQLite3)
61+
add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
62+
set_target_properties(SQLite::SQLite3 PROPERTIES
63+
IMPORTED_LOCATION "${SQLite3_LIBRARY}"
64+
INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}")
65+
endif()
66+
endif()

0 commit comments

Comments
 (0)