6
6
# or copy at http://opensource.org/licenses/MIT)
7
7
cmake_minimum_required (VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
8
8
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR} /cmake" ) # custom CMake modules like FindSQLiteCpp
9
+ include (GNUInstallDirs)
9
10
project (SQLiteCpp VERSION 3.1.1)
10
11
11
12
# SQLiteC++ 3.x requires C++11 features
@@ -178,8 +179,66 @@ if (SQLITECPP_INCLUDE_SCRIPT)
178
179
source_group (scripts FILES ${SQLITECPP_SCRIPT} )
179
180
endif ()
180
181
182
+ # All includes are relative to the "include" directory
183
+ include_directories ("${PROJECT_SOURCE_DIR} /include" )
184
+
185
+ option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
186
+ option (SQLITECPP_DOWNLOAD_SQLITE "Automatically download SQLite sources from sqlite.org" ON )
187
+ if (SQLITECPP_INTERNAL_SQLITE)
188
+ # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
189
+ if (SQLITECPP_DOWNLOAD_SQLITE)
190
+ include (FetchContent)
191
+ include (DownloadSQLite)
192
+ downloadSQLiteIfNeeded(sqlite3)
193
+ else ()
194
+ set (sqlite3_AMALGAMATION_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /sqlite3" )
195
+ endif ()
196
+ add_library (sqlite3 "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.c" )
197
+ if (SQLITE_ENABLE_COLUMN_METADATA)
198
+ # Enable the use of SQLite column metadata method
199
+ # Require that the sqlite3 library is also compiled with this flag:
200
+ target_compile_definitions (sqlite3 PUBLIC SQLITE_ENABLE_COLUMN_METADATA)
201
+ endif (SQLITE_ENABLE_COLUMN_METADATA)
202
+
203
+ if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
204
+ set_target_properties (sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC" )
205
+ endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
206
+
207
+ if (UNIX AND CMAKE_COMPILER_IS_GNUCXX)
208
+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
209
+ target_compile_options (sqlite3 PRIVATE "-Wimplicit-fallthrough=0" )
210
+ endif ()
211
+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0)
212
+ target_compile_options (sqlite3 PRIVATE "-Wno-cast-function-type" )
213
+ endif ()
214
+ endif ()
215
+ target_link_libraries (sqlite3 dl)
216
+ target_include_directories (sqlite3 PRIVATE "${sqlite3_AMALGAMATION_SOURCE_DIR} " )
217
+ target_include_directories (sqlite3
218
+ PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
219
+ PUBLIC $<INSTALL_INTERFACE:include />
220
+ )
221
+
222
+ install (TARGETS sqlite3
223
+ #EXPORT ${PROJECT_NAME}Targets
224
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
225
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
226
+ COMPONENT libsqlite3
227
+ )
228
+ install (FILES "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT libsqlite3_dev)
229
+ endif (SQLITECPP_INTERNAL_SQLITE)
230
+
181
231
# add sources of the wrapper as a "SQLiteCpp" static library
182
232
add_library (SQLiteCpp ${SQLITECPP_SRC} ${SQLITECPP_INC} ${SQLITECPP_DOC} ${SQLITECPP_SCRIPT} )
233
+ # make the sqlite3 library part of the interface of the SQLiteCpp wrapper itself (the client app does not need to link to sqlite3)
234
+ # PR https://github.com/SRombauts/SQLiteCpp/pull/111 "linked SQLiteCpp to sqlite3" commented out since it breaks install step from PR #118
235
+ target_include_directories (SQLiteCpp
236
+ PRIVATE
237
+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >
238
+ PUBLIC
239
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
240
+ $<INSTALL_INTERFACE:include />)
241
+ target_link_libraries (SQLiteCpp PUBLIC sqlite3)
183
242
184
243
# Options relative to SQLite and SQLiteC++ functions
185
244
@@ -230,83 +289,10 @@ if (SQLITECPP_USE_GCOV)
230
289
set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-fkeep-inline-functions -fkeep-static-functions" )
231
290
endif ()
232
291
233
- ## Build provided copy of SQLite3 C library ##
234
-
235
- option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
236
- if (SQLITECPP_INTERNAL_SQLITE)
237
- message (STATUS "Compile sqlite3 from source in subdirectory" )
238
- option (SQLITE_ENABLE_JSON1 "Enable JSON1 extension when building internal sqlite3 library." ON )
239
- # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
240
- add_subdirectory (sqlite3)
241
- target_link_libraries (SQLiteCpp PUBLIC sqlite3)
242
- else (SQLITECPP_INTERNAL_SQLITE)
243
- find_package (SQLite3 REQUIRED)
244
- message (STATUS "Link to sqlite3 system library" )
245
- target_link_libraries (SQLiteCpp PUBLIC SQLite::SQLite3)
246
- if (SQLite3_VERSION VERSION_LESS "3.19" )
247
- set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-DSQLITECPP_HAS_MEM_STRUCT" )
248
- endif ()
249
-
250
- # When using the SQLite codec, we need to link against the sqlcipher lib & include <sqlcipher/sqlite3.h>
251
- # So this gets the lib & header, and links/includes everything
252
- if (SQLITE_HAS_CODEC)
253
- # Make PkgConfig optional since Windows doesn't usually have it installed.
254
- find_package (PkgConfig QUIET )
255
- if (PKG_CONFIG_FOUND)
256
- # IMPORTED_TARGET was added in 3.6.3
257
- if (CMAKE_VERSION VERSION_LESS 3.6.3)
258
- pkg_check_modules(sqlcipher REQUIRED sqlcipher)
259
- # Only used in Database.cpp so PRIVATE to hide from end-user
260
- # Since we can't use IMPORTED_TARGET on this older Cmake version, manually link libs & includes
261
- target_link_libraries (SQLiteCpp PRIVATE ${sqlcipher_LIBRARIES} )
262
- target_include_directories (SQLiteCpp PRIVATE ${sqlcipher_INCLUDE_DIRS} )
263
- else ()
264
- pkg_check_modules(sqlcipher REQUIRED IMPORTED_TARGET sqlcipher)
265
- # Only used in Database.cpp so PRIVATE to hide from end-user
266
- target_link_libraries (SQLiteCpp PRIVATE PkgConfig::sqlcipher)
267
- endif ()
268
- else ()
269
- # Since we aren't using pkgconf here, find it manually
270
- find_library (sqlcipher_LIBRARY "sqlcipher" )
271
- find_path (sqlcipher_INCLUDE_DIR "sqlcipher/sqlite3.h"
272
- PATH_SUFFIXES
273
- "include"
274
- "includes"
275
- )
276
- # Hides it from the GUI
277
- mark_as_advanced (sqlcipher_LIBRARY sqlcipher_INCLUDE_DIR)
278
- if (NOT sqlcipher_INCLUDE_DIR)
279
- message (FATAL_ERROR "${PROJECT_NAME} requires the \" <sqlcipher/sqlite3.h>\" header to use the codec functionality but it wasn't found." )
280
- elseif (NOT sqlcipher_LIBRARY)
281
- message (FATAL_ERROR "${PROJECT_NAME} requires the sqlcipher library to use the codec functionality but it wasn't found." )
282
- endif ()
283
- # Only used in Database.cpp so PRIVATE to hide from end-user
284
- target_include_directories (SQLiteCpp PRIVATE "${sqlcipher_INCLUDE_DIR} /sqlcipher" )
285
- target_link_libraries (SQLiteCpp PRIVATE ${sqlcipher_LIBRARY} )
286
- endif ()
287
- endif ()
288
- endif (SQLITECPP_INTERNAL_SQLITE)
289
-
290
- # Link target with pthread and dl for Unix
291
- if (UNIX )
292
- set (THREADS_PREFER_PTHREAD_FLAG ON )
293
- find_package (Threads REQUIRED)
294
- target_link_libraries (SQLiteCpp PUBLIC Threads::Threads ${CMAKE_DL_LIBS} )
295
- endif (UNIX )
296
-
297
- # Set includes for target and transitive downstream targets
298
-
299
- target_include_directories (SQLiteCpp
300
- PRIVATE
301
- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>
302
- PUBLIC
303
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
304
- $<INSTALL_INTERFACE:include />)
305
-
306
292
# Allow the library to be installed via "make install" and found with "find_package"
307
293
308
294
include (GNUInstallDirs)
309
- install (TARGETS SQLiteCpp
295
+ install (TARGETS SQLiteCpp sqlite3
310
296
EXPORT ${PROJECT_NAME} Targets
311
297
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
312
298
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -381,11 +367,14 @@ option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF)
381
367
if (SQLITECPP_BUILD_EXAMPLES)
382
368
# add the basic example executable
383
369
add_executable (SQLiteCpp_example1 ${SQLITECPP_EXAMPLES} )
384
- target_link_libraries (SQLiteCpp_example1 SQLiteCpp)
385
- target_include_directories (SQLiteCpp_example1 PRIVATE
386
- ${CMAKE_CURRENT_SOURCE_DIR} /include
387
- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>)
388
- if (MSYS OR MINGW)
370
+ target_link_libraries (SQLiteCpp_example1 SQLiteCpp sqlite3)
371
+ # Link target with pthread and dl for Linux
372
+ if (UNIX )
373
+ target_link_libraries (SQLiteCpp_example1 pthread)
374
+ if (NOT APPLE )
375
+ target_link_libraries (SQLiteCpp_example1 dl)
376
+ endif ()
377
+ elseif (MSYS OR MINGW)
389
378
target_link_libraries (SQLiteCpp_example1 ssp)
390
379
endif ()
391
380
else (SQLITECPP_BUILD_EXAMPLES)
@@ -399,12 +388,12 @@ if (SQLITECPP_BUILD_TESTS)
399
388
target_link_libraries (SQLiteCpp_tests SQLiteCpp)
400
389
target_include_directories (SQLiteCpp_tests PRIVATE
401
390
${CMAKE_CURRENT_SOURCE_DIR} /include
402
- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3 >)
391
+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >)
403
392
404
393
find_package (GTest)
405
394
if (GTEST_FOUND)
406
395
message (STATUS "Link to GTest system library" )
407
- target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main)
396
+ target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main SQLiteCpp sqlite3 )
408
397
else (GTEST_FOUND)
409
398
message (STATUS "Compile googletest from source in submodule" )
410
399
# deactivate some warnings for compiling the googletest library
@@ -429,7 +418,7 @@ if (SQLITECPP_BUILD_TESTS)
429
418
endif (MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1919)
430
419
endif (MSVC )
431
420
432
- target_link_libraries (SQLiteCpp_tests gtest_main)
421
+ target_link_libraries (SQLiteCpp_tests gtest_main SQLiteCpp sqlite3 )
433
422
endif (GTEST_FOUND)
434
423
435
424
# add a "test" target:
0 commit comments