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