Skip to content

Add install procedure to CMake and make finding tests optional #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.22)
option(ENABLE_SQLCIPHER_TESTS "enable sqlchipher test")

# Creates the file compile_commands.json in the build directory.
Expand All @@ -7,6 +7,13 @@ set(CMAKE_CXX_STANDARD 17)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
set(HUNTER_TLS_VERIFY ON)

option(HUNTER_ENABLED "Enable Hunter package manager support" ON)

set(IS_MAIN_FILE CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
option(BUILD_TESTING "Enable build of tests" IS_MAIN_FILE)
option(ENABLE_INSTALL "Enable the installation of the library" "NOT IS_MAIN_FILE")

include("cmake/HunterGate.cmake")
include("cmake/Catch.cmake")

Expand All @@ -15,42 +22,70 @@ HunterGate(
SHA1 "8010d63d5ae611c564889d5fe12d3cb7a45703ac"
)

project(SqliteModernCpp)
project(SQLiteModernCpp LANGUAGES CXX)

hunter_add_package(Catch)
hunter_add_package(sqlite3)

find_package(Catch2 CONFIG REQUIRED)
find_package(sqlite3 CONFIG REQUIRED)

set(TEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
file(GLOB TEST_SOURCES ${TEST_SOURCE_DIR}/*.cc)
add_library(SQLiteModernCpp INTERFACE)

IF(NOT ENABLE_SQLCIPHER_TESTS)
list(REMOVE_ITEM TEST_SOURCES ${TEST_SOURCE_DIR}/sqlcipher.cc)
ENDIF(NOT ENABLE_SQLCIPHER_TESTS)
target_include_directories(SQLiteModernCpp INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/hdr>
)

include(GNUInstallDirs)

enable_testing()
if(ENABLE_INSTALL)
install(
TARGETS SQLiteModernCpp
EXPORT SQLiteModernCppConfig
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

add_library (sqlite_modern_cpp INTERFACE)
target_include_directories(sqlite_modern_cpp INTERFACE hdr/)
install(
EXPORT SQLiteModernCppConfig
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/SQLiteModernCpp"
NAMESPACE SQLiteModernCpp::
)

add_executable(tests_runner ${TEST_SOURCES})
target_include_directories(tests_runner INTERFACE ${SQLITE3_INCLUDE_DIRS})
if(ENABLE_SQLCIPHER_TESTS)
target_link_libraries(tests_runner Catch2::Catch2 sqlite_modern_cpp sqlite3::sqlite3 -lsqlcipher)
else()
target_link_libraries(tests_runner Catch2::Catch2 sqlite_modern_cpp sqlite3::sqlite3)
install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/hdr/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()

catch_discover_tests(tests_runner)
target_compile_options(tests_runner PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/Zc:__cplusplus> )
if(BUILD_TESTING)
hunter_add_package(Catch)
find_package(Catch2 CONFIG REQUIRED)

set(TEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
file(GLOB TEST_SOURCES ${TEST_SOURCE_DIR}/*.cc)

IF(NOT ENABLE_SQLCIPHER_TESTS)
list(REMOVE_ITEM TEST_SOURCES ${TEST_SOURCE_DIR}/sqlcipher.cc)
ENDIF(NOT ENABLE_SQLCIPHER_TESTS)

enable_testing()

add_executable(tests_runner ${TEST_SOURCES})
target_include_directories(tests_runner INTERFACE ${SQLITE3_INCLUDE_DIRS})

if(ENABLE_SQLCIPHER_TESTS)
target_link_libraries(tests_runner Catch2::Catch2 sqlite_modern_cpp sqlite3::sqlite3 -lsqlcipher)
else()
target_link_libraries(tests_runner Catch2::Catch2 sqlite_modern_cpp sqlite3::sqlite3)
endif()

catch_discover_tests(tests_runner)

target_compile_options(tests_runner PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/Zc:__cplusplus>)
endif()

# Place the file in the source directory, permitting us to place a single configuration file for YCM there.
# YCM is the code-completion engine for (neo)vim https://github.com/Valloric/YouCompleteMe
IF(EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
EXECUTE_PROCESS( COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
)
ENDIF()
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
)
ENDIF()
1 change: 0 additions & 1 deletion cmake/HunterGate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
# * https://github.com/hunter-packages/gate/
# * https://github.com/ruslo/hunter

option(HUNTER_ENABLED "Enable Hunter package manager support" ON)
if(HUNTER_ENABLED)
if(CMAKE_VERSION VERSION_LESS "3.0")
message(FATAL_ERROR "At least CMake version 3.0 required for hunter dependency management."
Expand Down