Skip to content

Commit

Permalink
Fix linking with system library (libsqlite3)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SRombauts committed Jan 7, 2020
1 parent a7d9456 commit ab8ff73
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
37 changes: 18 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ os: linux
cache:
apt: true

env:
global:
- BUILD_TYPE=Debug
- ASAN=ON
- INTERNAL_SQLITE=ON
- VALGRIND=OFF

matrix:
include:

Expand All @@ -31,15 +38,14 @@ matrix:
- dist: bionic
env:
- cc=gcc cxx=g++
- BUILD_TYPE=Debug
- ASAN=ON GCOV=ON
- GCOV=ON
- COVERALLS=true

# GCC Debug build with Valgrind instead of Address Sanitizer
- dist: bionic
env:
- cc=gcc cxx=g++
- BUILD_TYPE=Debug
- ASAN=OFF
- VALGRIND=true

# GCC Release build
Expand All @@ -48,17 +54,19 @@ matrix:
- cc=gcc cxx=g++
- BUILD_TYPE=Release

# GCC test linking with libsqlite3-dev package
- dist: bionic
env:
- cc=gcc cxx=g++
- INTERNAL_SQLITE=OFF

- dist: xenial
env:
- cc=gcc cxx=g++
- BUILD_TYPE=Debug
- ASAN=ON

- dist: trusty
env:
- cc=gcc cxx=g++
- BUILD_TYPE=Debug
- ASAN=ON

##########################################################################
# Clang on Linux
Expand All @@ -67,20 +75,14 @@ matrix:
- dist: bionic
env:
- cc=clang cxx=clang++
- BUILD_TYPE=Debug
- ASAN=ON

- dist: xenial
env:
- cc=clang cxx=clang++
- BUILD_TYPE=Debug
- ASAN=ON

- dist: trusty
env:
- cc=clang cxx=clang++
- BUILD_TYPE=Debug
- ASAN=ON

##########################################################################
# Clang on OSX
Expand All @@ -90,23 +92,20 @@ matrix:
- os: osx
env:
- cc=clang cxx=clang++
- BUILD_TYPE=Debug
- ASAN=ON

# XCode 8.3
- os: osx
osx_image: xcode8.3
env:
- cc=clang cxx=clang++
- BUILD_TYPE=Debug
- ASAN=ON

before_install:
# Coverity: don't use addons.coverity_scan since it run on every job of the build matrix, which waste resources and exhausts quotas
# 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.)
- if [[ -n "$COVERITY_SCAN_PROJECT_NAME" ]] ; then curl -s https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh | bash ; exit 0 ; fi

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

# Set the compiler environment variables properly
Expand All @@ -117,7 +116,7 @@ before_install:
before_script:
- mkdir build
- cd build
- 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 ..
- 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 ..

# build examples, and run tests (ie make & make test)
script:
Expand Down
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
# or copy at http://opensource.org/licenses/MIT)
cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp
project(SQLiteCpp VERSION "2.99")

# SQLiteC++ 3.x now requires C++11 compiler
Expand Down Expand Up @@ -154,6 +155,8 @@ set(SQLITECPP_SCRIPT
cpplint.py
Doxyfile
cmake/FindSQLiteCpp.cmake
cmake/FindSQLite3.cmake
cmake/SQLiteCppConfig.cmake.in
)
source_group(scripts FILES ${SQLITECPP_SCRIPT})

Expand Down Expand Up @@ -246,15 +249,17 @@ install(FILES

option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON)
if (SQLITECPP_INTERNAL_SQLITE)
message(STATUS "Compile sqlite3 from source in subdirectory")
# build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
add_subdirectory(sqlite3)
target_include_directories(sqlite3 PUBLIC "${PROJECT_SOURCE_DIR}/sqlite3")
target_include_directories(SQLiteCpp PRIVATE "${PROJECT_SOURCE_DIR}/sqlite3")
else (SQLITECPP_INTERNAL_SQLITE)
find_package (SQLite3 REQUIRED)
if (SQLITE3_FOUND)
message(STATUS "Link to sqlite3 system library")
include_directories(${SQLITE3_INCLUDE_DIRS})
target_link_libraries (SQLiteCpp ${SQLITE3_LIBRARIES})
target_link_libraries(SQLiteCpp ${SQLITE3_LIBRARIES})
endif (SQLITE3_FOUND)
endif (SQLITECPP_INTERNAL_SQLITE)

Expand Down Expand Up @@ -336,7 +341,7 @@ if (SQLITECPP_BUILD_TESTS)
else (GTEST_FOUND)
# deactivate some warnings for compiling the googletest library
if (NOT MSVC)
add_compile_options(-Wno-variadic-macros -Wno-long-long -Wno-switch-enum -Wno-float-equal -Wno-conversion-null -Wno-switch-default -Wno-pedantic)
add_compile_options(-Wno-switch-enum)
endif (NOT MSVC)

# add the subdirectory containing the CMakeLists.txt for the googletest library
Expand Down
66 changes: 66 additions & 0 deletions cmake/FindSQLite3.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindSQLite3
-----------
Find the SQLite libraries, v3
IMPORTED targets
^^^^^^^^^^^^^^^^
This module defines the following :prop_tgt:`IMPORTED` target:
``SQLite::SQLite3``
Result variables
^^^^^^^^^^^^^^^^
This module will set the following variables if found:
``SQLite3_INCLUDE_DIRS``
where to find sqlite3.h, etc.
``SQLite3_LIBRARIES``
the libraries to link against to use SQLite3.
``SQLite3_VERSION``
version of the SQLite3 library found
``SQLite3_FOUND``
TRUE if found
#]=======================================================================]

# Look for the necessary header
find_path(SQLite3_INCLUDE_DIR NAMES sqlite3.h)
mark_as_advanced(SQLite3_INCLUDE_DIR)

# Look for the necessary library
find_library(SQLite3_LIBRARY NAMES sqlite3 sqlite)
mark_as_advanced(SQLite3_LIBRARY)

# Extract version information from the header file
if(SQLite3_INCLUDE_DIR)
file(STRINGS ${SQLite3_INCLUDE_DIR}/sqlite3.h _ver_line
REGEX "^#define SQLITE_VERSION *\"[0-9]+\\.[0-9]+\\.[0-9]+\""
LIMIT_COUNT 1)
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+"
SQLite3_VERSION "${_ver_line}")
unset(_ver_line)
endif()

include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
find_package_handle_standard_args(SQLite3
REQUIRED_VARS SQLite3_INCLUDE_DIR SQLite3_LIBRARY
VERSION_VAR SQLite3_VERSION)

# Create the imported target
if(SQLite3_FOUND)
set(SQLite3_INCLUDE_DIRS ${SQLite3_INCLUDE_DIR})
set(SQLite3_LIBRARIES ${SQLite3_LIBRARY})
if(NOT TARGET SQLite::SQLite3)
add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
set_target_properties(SQLite::SQLite3 PROPERTIES
IMPORTED_LOCATION "${SQLite3_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}")
endif()
endif()

0 comments on commit ab8ff73

Please sign in to comment.