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.0.0)
1011
1112# SQLiteC++ 3.x requires C++11 features
@@ -166,8 +167,66 @@ set(SQLITECPP_SCRIPT
166167)
167168source_group (scripts FILES ${SQLITECPP_SCRIPT} )
168169
170+ # All includes are relative to the "include" directory
171+ include_directories ("${PROJECT_SOURCE_DIR} /include" )
172+
173+ option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
174+ option (SQLITECPP_DOWNLOAD_SQLITE "Automatically download SQLite sources from sqlite.org" ON )
175+ if (SQLITECPP_INTERNAL_SQLITE)
176+ # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
177+ if (SQLITECPP_DOWNLOAD_SQLITE)
178+ include (FetchContent)
179+ include (DownloadSQLite)
180+ downloadSQLiteIfNeeded(sqlite3)
181+ else ()
182+ set (sqlite3_AMALGAMATION_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /sqlite3" )
183+ endif ()
184+ add_library (sqlite3 "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.c" )
185+ if (SQLITE_ENABLE_COLUMN_METADATA)
186+ # Enable the use of SQLite column metadata method
187+ # Require that the sqlite3 library is also compiled with this flag:
188+ target_compile_definitions (sqlite3 PUBLIC SQLITE_ENABLE_COLUMN_METADATA)
189+ endif (SQLITE_ENABLE_COLUMN_METADATA)
190+
191+ if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
192+ set_target_properties (sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC" )
193+ endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" ))
194+
195+ if (UNIX AND CMAKE_COMPILER_IS_GNUCXX)
196+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
197+ target_compile_options (sqlite3 PRIVATE "-Wimplicit-fallthrough=0" )
198+ endif ()
199+ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0)
200+ target_compile_options (sqlite3 PRIVATE "-Wno-cast-function-type" )
201+ endif ()
202+ endif ()
203+ target_link_libraries (sqlite3 dl)
204+ target_include_directories (sqlite3 PRIVATE "${sqlite3_AMALGAMATION_SOURCE_DIR} " )
205+ target_include_directories (sqlite3
206+ PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
207+ PUBLIC $<INSTALL_INTERFACE:include />
208+ )
209+
210+ install (TARGETS sqlite3
211+ #EXPORT ${PROJECT_NAME}Targets
212+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
213+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
214+ COMPONENT libsqlite3
215+ )
216+ install (FILES "${sqlite3_AMALGAMATION_SOURCE_DIR} /sqlite3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT libsqlite3_dev)
217+ endif (SQLITECPP_INTERNAL_SQLITE)
218+
169219# add sources of the wrapper as a "SQLiteCpp" static library
170220add_library (SQLiteCpp ${SQLITECPP_SRC} ${SQLITECPP_INC} ${SQLITECPP_DOC} ${SQLITECPP_SCRIPT} )
221+ # make the sqlite3 library part of the interface of the SQLiteCpp wrapper itself (the client app does not need to link to sqlite3)
222+ # PR https://github.com/SRombauts/SQLiteCpp/pull/111 "linked SQLiteCpp to sqlite3" commented out since it breaks install step from PR #118
223+ target_include_directories (SQLiteCpp
224+ PRIVATE
225+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >
226+ PUBLIC
227+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
228+ $<INSTALL_INTERFACE:include />)
229+ target_link_libraries (SQLiteCpp PUBLIC sqlite3)
171230
172231# Options relative to SQLite and SQLiteC++ functions
173232
@@ -218,43 +277,10 @@ if (SQLITECPP_USE_GCOV)
218277 set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-fkeep-inline-functions -fkeep-static-functions" )
219278endif ()
220279
221- ## Build provided copy of SQLite3 C library ##
222-
223- option (SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON )
224- if (SQLITECPP_INTERNAL_SQLITE)
225- message (STATUS "Compile sqlite3 from source in subdirectory" )
226- # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
227- add_subdirectory (sqlite3)
228- target_link_libraries (SQLiteCpp PUBLIC sqlite3)
229- else (SQLITECPP_INTERNAL_SQLITE)
230- find_package (SQLite3 REQUIRED)
231- message (STATUS "Link to sqlite3 system library" )
232- target_link_libraries (SQLiteCpp PUBLIC SQLite::SQLite3)
233- if (SQLite3_VERSION VERSION_LESS "3.19" )
234- set_target_properties (SQLiteCpp PROPERTIES COMPILE_FLAGS "-DSQLITECPP_HAS_MEM_STRUCT" )
235- endif ()
236- endif (SQLITECPP_INTERNAL_SQLITE)
237-
238- # Link target with pthread and dl for Unix
239- if (UNIX )
240- set (THREADS_PREFER_PTHREAD_FLAG ON )
241- find_package (Threads REQUIRED)
242- target_link_libraries (SQLiteCpp PUBLIC Threads::Threads ${CMAKE_DL_LIBS} )
243- endif (UNIX )
244-
245- # Set includes for target and transitive downstream targets
246-
247- target_include_directories (SQLiteCpp
248- PRIVATE
249- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>
250- PUBLIC
251- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
252- $<INSTALL_INTERFACE:include />)
253-
254280# Allow the library to be installed via "make install" and found with "find_package"
255281
256282include (GNUInstallDirs)
257- install (TARGETS SQLiteCpp
283+ install (TARGETS SQLiteCpp sqlite3
258284 EXPORT ${PROJECT_NAME} Targets
259285 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
260286 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -329,11 +355,14 @@ option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF)
329355if (SQLITECPP_BUILD_EXAMPLES)
330356 # add the basic example executable
331357 add_executable (SQLiteCpp_example1 ${SQLITECPP_EXAMPLES} )
332- target_link_libraries (SQLiteCpp_example1 SQLiteCpp)
333- target_include_directories (SQLiteCpp_example1 PRIVATE
334- ${CMAKE_CURRENT_SOURCE_DIR} /include
335- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3>)
336- if (MSYS OR MINGW)
358+ target_link_libraries (SQLiteCpp_example1 SQLiteCpp sqlite3)
359+ # Link target with pthread and dl for Linux
360+ if (UNIX )
361+ target_link_libraries (SQLiteCpp_example1 pthread)
362+ if (NOT APPLE )
363+ target_link_libraries (SQLiteCpp_example1 dl)
364+ endif ()
365+ elseif (MSYS OR MINGW)
337366 target_link_libraries (SQLiteCpp_example1 ssp)
338367 endif ()
339368else (SQLITECPP_BUILD_EXAMPLES)
@@ -347,12 +376,12 @@ if (SQLITECPP_BUILD_TESTS)
347376 target_link_libraries (SQLiteCpp_tests SQLiteCpp)
348377 target_include_directories (SQLiteCpp_tests PRIVATE
349378 ${CMAKE_CURRENT_SOURCE_DIR} /include
350- $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${CMAKE_CURRENT_SOURCE_DIR} /sqlite3 >)
379+ $<$<BOOL :${SQLITECPP_INTERNAL_SQLITE} >:${sqlite3_AMALGAMATION_SOURCE_DIR} >)
351380
352381 find_package (GTest)
353382 if (GTEST_FOUND)
354383 message (STATUS "Link to GTest system library" )
355- target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main)
384+ target_link_libraries (SQLiteCpp_tests GTest::GTest GTest::Main SQLiteCpp sqlite3 )
356385 else (GTEST_FOUND)
357386 message (STATUS "Compile googletest from source in submodule" )
358387 # deactivate some warnings for compiling the googletest library
@@ -377,7 +406,7 @@ if (SQLITECPP_BUILD_TESTS)
377406 endif (MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1919)
378407 endif (MSVC )
379408
380- target_link_libraries (SQLiteCpp_tests gtest_main)
409+ target_link_libraries (SQLiteCpp_tests gtest_main SQLiteCpp sqlite3 )
381410 endif (GTEST_FOUND)
382411
383412 # add a "test" target:
0 commit comments