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
@@ -172,8 +173,66 @@ set(SQLITECPP_SCRIPT
172173)
173174source_group (scripts FILES ${SQLITECPP_SCRIPT} )
174175
176+ # All includes are relative to the "include" directory
177+ include_directories ("${PROJECT_SOURCE_DIR} /include" )
178+
179+ option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
180+ option (SQLITECPP_DOWNLOAD_SQLITE "Automatically download SQLite sources from sqlite.org" ON )
181+ if (SQLITECPP_INTERNAL_SQLITE)
182+ # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
183+ if (SQLITECPP_DOWNLOAD_SQLITE)
184+ include (FetchContent)
185+ include (DownloadSQLite)
186+ downloadSQLiteIfNeeded(sqlite3)
187+ else ()
188+ set (sqlite3_AMALGAMATION_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /sqlite3" )
189+ endif ()
190+ add_library (sqlite3 "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.c" )
191+ if (SQLITE_ENABLE_COLUMN_METADATA)
192+ # Enable the use of SQLite column metadata method
193+ # Require that the sqlite3 library is also compiled with this flag:
194+ target_compile_definitions (sqlite3 PUBLIC SQLITE_ENABLE_COLUMN_METADATA)
195+ endif (SQLITE_ENABLE_COLUMN_METADATA)
196+
197+ if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
198+ set_target_properties (sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC" )
199+ endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
200+
201+ if (UNIX AND CMAKE_COMPILER_IS_GNUCXX)
202+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
203+ target_compile_options (sqlite3 PRIVATE "-Wimplicit-fallthrough=0" )
204+ endif ()
205+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0)
206+ target_compile_options (sqlite3 PRIVATE "-Wno-cast-function-type" )
207+ endif ()
208+ endif ()
209+ target_link_libraries (sqlite3 dl)
210+ target_include_directories (sqlite3 PRIVATE "${sqlite3_AMALGAMATION_SOURCE_DIR} " )
211+ target_include_directories (sqlite3
212+ PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
213+ PUBLIC $<INSTALL_INTERFACE:include />
214+ )
215+
216+ install (TARGETS sqlite3
217+ #EXPORT ${PROJECT_NAME}Targets
218+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
219+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
220+ COMPONENT libsqlite3
221+ )
222+ install (FILES "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT libsqlite3_dev)
223+ endif (SQLITECPP_INTERNAL_SQLITE)
224+
175225# add sources of the wrapper as a "SQLiteCpp" static library
176226add_library (SQLiteCpp ${SQLITECPP_SRC} ${SQLITECPP_INC} ${SQLITECPP_DOC} ${SQLITECPP_SCRIPT} )
227+ # make the sqlite3 library part of the interface of the SQLiteCpp wrapper itself (the client app does not need to link to sqlite3)
228+ # PR https://github.com/SRombauts/SQLiteCpp/pull/111 "linked SQLiteCpp to sqlite3" commented out since it breaks install step from PR #118
229+ target_include_directories (SQLiteCpp
230+ PRIVATE
231+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >
232+ PUBLIC
233+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
234+ $<INSTALL_INTERFACE:include />)
235+ target_link_libraries (SQLiteCpp PUBLIC sqlite3)
177236
178237# Options relative to SQLite and SQLiteC++ functions
179238
@@ -224,83 +283,10 @@ if (SQLITECPP_USE_GCOV)
224283 set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-fkeep-inline-functions -fkeep-static-functions" )
225284endif ()
226285
227- ## Build provided copy of SQLite3 C library ##
228-
229- option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
230- if (SQLITECPP_INTERNAL_SQLITE)
231- message (STATUS "Compile sqlite3 from source in subdirectory" )
232- option (SQLITE_ENABLE_JSON1 "Enable JSON1 extension when building internal sqlite3 library." ON )
233- # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
234- add_subdirectory (sqlite3)
235- target_link_libraries (SQLiteCpp PUBLIC sqlite3)
236- else (SQLITECPP_INTERNAL_SQLITE)
237- find_package (SQLite3 REQUIRED)
238- message (STATUS "Link to sqlite3 system library" )
239- target_link_libraries (SQLiteCpp PUBLIC SQLite::SQLite3)
240- if (SQLite3_VERSION VERSION_LESS "3.19" )
241- set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-DSQLITECPP_HAS_MEM_STRUCT" )
242- endif ()
243-
244- # When using the SQLite codec, we need to link against the sqlcipher lib & include <sqlcipher/sqlite3.h>
245- # So this gets the lib & header, and links/includes everything
246- if (SQLITE_HAS_CODEC)
247- # Make PkgConfig optional since Windows doesn't usually have it installed.
248- find_package (PkgConfig QUIET )
249- if (PKG_CONFIG_FOUND)
250- # IMPORTED_TARGET was added in 3.6.3
251- if (CMAKE_VERSION VERSION_LESS 3.6.3)
252- pkg_check_modules(sqlcipher REQUIRED sqlcipher)
253- # Only used in Database.cpp so PRIVATE to hide from end-user
254- # Since we can't use IMPORTED_TARGET on this older Cmake version, manually link libs & includes
255- target_link_libraries (SQLiteCpp PRIVATE ${sqlcipher_LIBRARIES} )
256- target_include_directories (SQLiteCpp PRIVATE ${sqlcipher_INCLUDE_DIRS} )
257- else ()
258- pkg_check_modules(sqlcipher REQUIRED IMPORTED_TARGET sqlcipher)
259- # Only used in Database.cpp so PRIVATE to hide from end-user
260- target_link_libraries (SQLiteCpp PRIVATE PkgConfig::sqlcipher)
261- endif ()
262- else ()
263- # Since we aren't using pkgconf here, find it manually
264- find_library (sqlcipher_LIBRARY "sqlcipher" )
265- find_path (sqlcipher_INCLUDE_DIR "sqlcipher/sqlite3.h"
266- PATH_SUFFIXES
267- "include"
268- "includes"
269- )
270- # Hides it from the GUI
271- mark_as_advanced (sqlcipher_LIBRARY sqlcipher_INCLUDE_DIR)
272- if (NOT sqlcipher_INCLUDE_DIR)
273- message (FATAL_ERROR "${PROJECT_NAME} requires the \" <sqlcipher/sqlite3.h>\" header to use the codec functionality but it wasn't found." )
274- elseif (NOT sqlcipher_LIBRARY)
275- message (FATAL_ERROR "${PROJECT_NAME} requires the sqlcipher library to use the codec functionality but it wasn't found." )
276- endif ()
277- # Only used in Database.cpp so PRIVATE to hide from end-user
278- target_include_directories (SQLiteCpp PRIVATE "${sqlcipher_INCLUDE_DIR} /sqlcipher" )
279- target_link_libraries (SQLiteCpp PRIVATE ${sqlcipher_LIBRARY} )
280- endif ()
281- endif ()
282- endif (SQLITECPP_INTERNAL_SQLITE)
283-
284- # Link target with pthread and dl for Unix
285- if (UNIX )
286- set (THREADS_PREFER_PTHREAD_FLAG ON )
287- find_package (Threads REQUIRED)
288- target_link_libraries (SQLiteCpp PUBLIC Threads::Threads ${CMAKE_DL_LIBS} )
289- endif (UNIX )
290-
291- # Set includes for target and transitive downstream targets
292-
293- target_include_directories (SQLiteCpp
294- PRIVATE
295- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>
296- PUBLIC
297- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
298- $<INSTALL_INTERFACE:include />)
299-
300286# Allow the library to be installed via "make install" and found with "find_package"
301287
302288include (GNUInstallDirs)
303- install (TARGETS SQLiteCpp
289+ install (TARGETS SQLiteCpp sqlite3
304290 EXPORT ${PROJECT_NAME} Targets
305291 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
306292 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -375,11 +361,14 @@ option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF)
375361if (SQLITECPP_BUILD_EXAMPLES)
376362 # add the basic example executable
377363 add_executable (SQLiteCpp_example1 ${SQLITECPP_EXAMPLES} )
378- target_link_libraries (SQLiteCpp_example1 SQLiteCpp)
379- target_include_directories (SQLiteCpp_example1 PRIVATE
380- ${CMAKE_CURRENT_SOURCE_DIR} /include
381- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>)
382- if (MSYS OR MINGW)
364+ target_link_libraries (SQLiteCpp_example1 SQLiteCpp sqlite3)
365+ # Link target with pthread and dl for Linux
366+ if (UNIX )
367+ target_link_libraries (SQLiteCpp_example1 pthread)
368+ if (NOT APPLE )
369+ target_link_libraries (SQLiteCpp_example1 dl)
370+ endif ()
371+ elseif (MSYS OR MINGW)
383372 target_link_libraries (SQLiteCpp_example1 ssp)
384373 endif ()
385374else (SQLITECPP_BUILD_EXAMPLES)
@@ -393,12 +382,12 @@ if (SQLITECPP_BUILD_TESTS)
393382 target_link_libraries (SQLiteCpp_tests SQLiteCpp)
394383 target_include_directories (SQLiteCpp_tests PRIVATE
395384 ${CMAKE_CURRENT_SOURCE_DIR} /include
396- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3 >)
385+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >)
397386
398387 find_package (GTest)
399388 if (GTEST_FOUND)
400389 message (STATUS "Link to GTest system library" )
401- target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main)
390+ target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main SQLiteCpp sqlite3 )
402391 else (GTEST_FOUND)
403392 message (STATUS "Compile googletest from source in submodule" )
404393 # deactivate some warnings for compiling the googletest library
@@ -423,7 +412,7 @@ if (SQLITECPP_BUILD_TESTS)
423412 endif (MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1919)
424413 endif (MSVC )
425414
426- target_link_libraries (SQLiteCpp_tests gtest_main)
415+ target_link_libraries (SQLiteCpp_tests gtest_main SQLiteCpp sqlite3 )
427416 endif (GTEST_FOUND)
428417
429418 # add a "test" target:
0 commit comments