1- # RCDB C++ is a header-only library - nothing here is required to USE it (just #include the headers
2- # and link sqlite3 and/or the mysql client). This CMake project builds the unit tests and examples.
1+ # CMake project for the RCDB C++ tests and examples.
2+ #
3+ # SQLiteCpp is fetched automatically (pinned by SQLITECPP_FETCH_TAG). To build against an
4+ # already-installed copy instead, pass its location: -DSQLiteCpp_ROOT=/install/prefix
35#
46# Build & run the SQLite unit tests:
57# cmake -S . -B build -DWITH_SQLITE=ON -DWITH_MYSQL=OFF
68# cmake --build build --target test_rcdb_cpp
79# RCDB_TEST_CONNECTION="sqlite:///<path-to>.sqlite" ./build/test_rcdb_cpp
810# Create the test database with: rcdb -c sqlite:///<path-to>.sqlite db init --add-cpp-tests --confirm
911
10- cmake_minimum_required (VERSION 3.10 )
12+ cmake_minimum_required (VERSION 3.14 ) # FetchContent_MakeAvailable
1113project (rcdb_cpp)
1214
1315option (WITH_MYSQL "Compile with MySQL support" OFF )
1416option (WITH_SQLITE "Compile with SQLite support" ON )
1517
18+ set (SQLITECPP_FETCH_TAG "3.3.3" CACHE STRING "SQLiteCpp git tag to fetch (used unless -DSQLiteCpp_ROOT is given)" )
1619
1720# Location of additional CMake modules
1821SET (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH } "${CMAKE_SOURCE_DIR } /cmake/" )
1922
2023set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS } -std=c++11 -g -O0" )
2124
22-
2325include_directories (include )
2426
2527set (DB_REQIRED_LIBRARIES dl pthread)
@@ -28,24 +30,42 @@ if(WITH_MYSQL)
2830 add_definitions (-DRCDB_MYSQL )
2931 include_directories (/usr/include/mysql )
3032 set (DB_REQIRED_LIBRARIES mysqlclient ${DB_REQIRED_LIBRARIES} )
31-
3233endif ()
3334
34-
35-
3635if (WITH_SQLITE)
3736 add_definitions (-DRCDB_SQLITE )
38- include_directories (include /SQLiteCpp )
39- set (DB_REQIRED_LIBRARIES sqlite3 ${DB_REQIRED_LIBRARIES} )
37+
38+ # Fetch a known-good SQLiteCpp by default. If the user points CMake at an existing
39+ # install (-DSQLiteCpp_ROOT=/prefix, CMake's standard package-location hint), use that.
40+ if (SQLiteCpp_ROOT)
41+ find_package (SQLiteCpp CONFIG REQUIRED )
42+ message (STATUS "Using SQLiteCpp from ${SQLiteCpp_ROOT} " )
43+ else ()
44+ message (STATUS "Fetching SQLiteCpp ${SQLITECPP_FETCH_TAG} " )
45+ include (FetchContent )
46+ # By default SQLiteCpp compiles its own bundled SQLite3 amalgamation. We forward
47+ # this knob (keeping SQLiteCpp's own default, ON) so it can be turned off to link
48+ # the platform's libsqlite3 instead: -DSQLITECPP_INTERNAL_SQLITE=OFF
49+ set (SQLITECPP_INTERNAL_SQLITE ON CACHE BOOL "Compile SQLiteCpp's bundled SQLite3 (OFF links system libsqlite3)" )
50+ set (SQLITECPP_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
51+ set (SQLITECPP_BUILD_TESTS OFF CACHE BOOL "" FORCE )
52+ set (SQLITECPP_RUN_CPPLINT OFF CACHE BOOL "" FORCE )
53+ set (SQLITECPP_RUN_CPPCHECK OFF CACHE BOOL "" FORCE )
54+ FetchContent_Declare (SQLiteCpp
55+ GIT_REPOSITORY https://github.com/SRombauts/SQLiteCpp.git
56+ GIT_TAG ${SQLITECPP_FETCH_TAG} )
57+ FetchContent_MakeAvailable (SQLiteCpp)
58+ endif ()
59+
60+ # The SQLiteCpp target carries its own sqlite3 dependency and include dirs
61+ # (transitively exposing <sqlite3.h>), so we do not link a separate sqlite3.
62+ set (DB_REQIRED_LIBRARIES SQLiteCpp ${DB_REQIRED_LIBRARIES} )
4063endif ()
4164
4265MESSAGE (STATUS "WITH_MYSQL " ${WITH_MYSQL} )
4366MESSAGE (STATUS "WITH_SQLITE " ${WITH_SQLITE} )
4467
45-
46- set (SOURCE_FILES
47- /usr/include/sqlite3.h
48- include/RCDB/SQLiteCpp.h
68+ set (RCDB_HEADERS
4969 include/RCDB/ConditionType.h
5070 include/RCDB/MySqlProvider.h
5171 include/RCDB/SqLiteProvider.h
@@ -59,20 +79,8 @@ set(SOURCE_FILES
5979 include/RCDB/RcdbFile.h
6080 )
6181
62-
6382SET (TEST_SOURCE_FILES
64- ${SOURCE_FILES}
65-
66- include/RCDB/ConditionType.h
67- include/RCDB/MySqlProvider.h
68- include/RCDB/SqLiteProvider.h
69- include/RCDB/DataProvider.h
70- include/RCDB/Condition.h
71- include/RCDB/Exceptions.h
72- include/RCDB/MySqlConnectionInfo.h
73- include/RCDB/Connection.h
74- include/RCDB/StringUtils.h
75- include/RCDB/ConfigParser.h
83+ ${RCDB_HEADERS}
7684
7785 tests/test_ConditionType.cpp
7886
@@ -88,11 +96,8 @@ if(WITH_MYSQL)
8896 list (APPEND TEST_SOURCE_FILES tests/test_MySqlProvider.cpp)
8997endif ()
9098
91- # The Connection and SqLiteProvider tests both pull in the SQLiteCpp amalgamation,
92- # whose out-of-line definitions are not `inline`. Compile them as a single unity
93- # translation unit (tests/test_sqlite_unity.cpp) so the definitions appear once.
9499if (WITH_SQLITE)
95- list (APPEND TEST_SOURCE_FILES tests/test_sqlite_unity .cpp)
100+ list (APPEND TEST_SOURCE_FILES tests/test_SqLiteProvider.cpp tests/test_Connection .cpp)
96101endif ()
97102
98103# Read examples and tests build with either backend (SQLite is the default).
@@ -101,10 +106,10 @@ add_executable(examples_simple examples/simple.cpp)
101106add_executable (examples_trigger_params examples/get_trigger_params.cpp )
102107add_executable (examples_fadc_masks examples/get_fadc_masks.cpp )
103108
104- target_link_libraries (test_rcdb_cpp ${DB_REQIRED_LIBRARIES} dl pthread sqlite3 )
105- target_link_libraries (examples_trigger_params ${DB_REQIRED_LIBRARIES} dl pthread )
106- target_link_libraries (examples_simple ${DB_REQIRED_LIBRARIES} dl pthread )
107- target_link_libraries (examples_fadc_masks ${DB_REQIRED_LIBRARIES} dl pthread )
109+ target_link_libraries (test_rcdb_cpp ${DB_REQIRED_LIBRARIES} )
110+ target_link_libraries (examples_trigger_params ${DB_REQIRED_LIBRARIES} )
111+ target_link_libraries (examples_simple ${DB_REQIRED_LIBRARIES} )
112+ target_link_libraries (examples_fadc_masks ${DB_REQIRED_LIBRARIES} )
108113
109114# Writing to RCDB from C++ goes through WritingConnection, which is MySQL-only
110115# (the SQLite provider is read-only). So the write examples are built only when
@@ -114,7 +119,7 @@ if(WITH_MYSQL)
114119 add_executable (examples_write_array_to_json examples/write_array_to_json.cpp )
115120 add_executable (examples_write_objects_to_json examples/write_objects_to_json.cpp )
116121
117- target_link_libraries (examples_write_conditions ${DB_REQIRED_LIBRARIES} dl pthread )
118- target_link_libraries (examples_write_array_to_json ${DB_REQIRED_LIBRARIES} dl pthread )
119- target_link_libraries (examples_write_objects_to_json ${DB_REQIRED_LIBRARIES} dl pthread )
120- endif ()
122+ target_link_libraries (examples_write_conditions ${DB_REQIRED_LIBRARIES} )
123+ target_link_libraries (examples_write_array_to_json ${DB_REQIRED_LIBRARIES} )
124+ target_link_libraries (examples_write_objects_to_json ${DB_REQIRED_LIBRARIES} )
125+ endif ()
0 commit comments