From c8c050b526477f4a26e3ba852537bb04ed1bbe39 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Mon, 11 Feb 2019 13:05:38 +0300 Subject: [PATCH] Implementing downloading SQLite sources from the official website. --- CMakeLists.txt | 113 +++++++++++++++++++++++-------------- cmake/DownloadSQLite.cmake | 62 ++++++++++++++++++++ sqlite3/CMakeLists.txt | 45 --------------- 3 files changed, 133 insertions(+), 87 deletions(-) create mode 100644 cmake/DownloadSQLite.cmake delete mode 100644 sqlite3/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9363c0d8..43933fd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ # or copy at http://opensource.org/licenses/MIT) cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp +include(GNUInstallDirs) project(SQLiteCpp VERSION 3.0.0) # SQLiteC++ 3.x requires C++11 features @@ -166,8 +167,66 @@ set(SQLITECPP_SCRIPT ) source_group(scripts FILES ${SQLITECPP_SCRIPT}) +# All includes are relative to the "include" directory +include_directories("${PROJECT_SOURCE_DIR}/include") + +option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON) +option(SQLITECPP_DOWNLOAD_SQLITE "Automatically download SQLite sources from sqlite.org" ON) +if (SQLITECPP_INTERNAL_SQLITE) + # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package + if(SQLITECPP_DOWNLOAD_SQLITE) + include(FetchContent) + include(DownloadSQLite) + downloadSQLiteIfNeeded(sqlite3) + else() + set(sqlite3_AMALGAMATION_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/sqlite3") + endif() + add_library(sqlite3 "${sqlite3_AMALGAMATION_SOURCE_DIR}/sqlite3.c") + if (SQLITE_ENABLE_COLUMN_METADATA) + # Enable the use of SQLite column metadata method + # Require that the sqlite3 library is also compiled with this flag: + target_compile_definitions(sqlite3 PUBLIC SQLITE_ENABLE_COLUMN_METADATA) + endif (SQLITE_ENABLE_COLUMN_METADATA) + + if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) + set_target_properties(sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC") + endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) + + if (UNIX AND CMAKE_COMPILER_IS_GNUCXX) + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) + target_compile_options(sqlite3 PRIVATE "-Wimplicit-fallthrough=0") + endif() + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0) + target_compile_options(sqlite3 PRIVATE "-Wno-cast-function-type") + endif() + endif() + target_link_libraries(sqlite3 dl) + target_include_directories(sqlite3 PRIVATE "${sqlite3_AMALGAMATION_SOURCE_DIR}") + target_include_directories(sqlite3 + PRIVATE $ + PUBLIC $ + ) + + install(TARGETS sqlite3 + #EXPORT ${PROJECT_NAME}Targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT libsqlite3 + ) + install(FILES "${sqlite3_AMALGAMATION_SOURCE_DIR}/sqlite3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT libsqlite3_dev) +endif (SQLITECPP_INTERNAL_SQLITE) + # add sources of the wrapper as a "SQLiteCpp" static library add_library(SQLiteCpp ${SQLITECPP_SRC} ${SQLITECPP_INC} ${SQLITECPP_DOC} ${SQLITECPP_SCRIPT}) +# make the sqlite3 library part of the interface of the SQLiteCpp wrapper itself (the client app does not need to link to sqlite3) +# PR https://github.com/SRombauts/SQLiteCpp/pull/111 "linked SQLiteCpp to sqlite3" commented out since it breaks install step from PR #118 +target_include_directories(SQLiteCpp + PRIVATE + $<$:${sqlite3_AMALGAMATION_SOURCE_DIR}> + PUBLIC + $ + $) +target_link_libraries(SQLiteCpp PUBLIC sqlite3) # Options relative to SQLite and SQLiteC++ functions @@ -218,43 +277,10 @@ if (SQLITECPP_USE_GCOV) set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-fkeep-inline-functions -fkeep-static-functions") endif () -## Build provided copy of SQLite3 C library ## - -option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON) -if (SQLITECPP_INTERNAL_SQLITE) - message(STATUS "Compile sqlite3 from source in subdirectory") - # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package - add_subdirectory(sqlite3) - target_link_libraries(SQLiteCpp PUBLIC sqlite3) -else (SQLITECPP_INTERNAL_SQLITE) - find_package (SQLite3 REQUIRED) - message(STATUS "Link to sqlite3 system library") - target_link_libraries(SQLiteCpp PUBLIC SQLite::SQLite3) - if(SQLite3_VERSION VERSION_LESS "3.19") - set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-DSQLITECPP_HAS_MEM_STRUCT") - endif() -endif (SQLITECPP_INTERNAL_SQLITE) - -# Link target with pthread and dl for Unix -if (UNIX) - set(THREADS_PREFER_PTHREAD_FLAG ON) - find_package(Threads REQUIRED) - target_link_libraries(SQLiteCpp PUBLIC Threads::Threads ${CMAKE_DL_LIBS}) -endif (UNIX) - -# Set includes for target and transitive downstream targets - -target_include_directories(SQLiteCpp - PRIVATE - $<$:${CMAKE_CURRENT_SOURCE_DIR}/sqlite3> - PUBLIC - $ - $) - # Allow the library to be installed via "make install" and found with "find_package" include(GNUInstallDirs) -install(TARGETS SQLiteCpp +install(TARGETS SQLiteCpp sqlite3 EXPORT ${PROJECT_NAME}Targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -329,11 +355,14 @@ option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF) if (SQLITECPP_BUILD_EXAMPLES) # add the basic example executable add_executable(SQLiteCpp_example1 ${SQLITECPP_EXAMPLES}) - target_link_libraries(SQLiteCpp_example1 SQLiteCpp) - target_include_directories(SQLiteCpp_example1 PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/include - $<$:${CMAKE_CURRENT_SOURCE_DIR}/sqlite3>) - if (MSYS OR MINGW) + target_link_libraries(SQLiteCpp_example1 SQLiteCpp sqlite3) + # Link target with pthread and dl for Linux + if (UNIX) + target_link_libraries(SQLiteCpp_example1 pthread) + if (NOT APPLE) + target_link_libraries(SQLiteCpp_example1 dl) + endif () + elseif (MSYS OR MINGW) target_link_libraries(SQLiteCpp_example1 ssp) endif () else (SQLITECPP_BUILD_EXAMPLES) @@ -347,12 +376,12 @@ if (SQLITECPP_BUILD_TESTS) target_link_libraries(SQLiteCpp_tests SQLiteCpp) target_include_directories(SQLiteCpp_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include - $<$:${CMAKE_CURRENT_SOURCE_DIR}/sqlite3>) + $<$:${sqlite3_AMALGAMATION_SOURCE_DIR}>) find_package(GTest) if (GTEST_FOUND) message(STATUS "Link to GTest system library") - target_link_libraries(SQLiteCpp_tests GTest::GTest GTest::Main) + target_link_libraries(SQLiteCpp_tests GTest::GTest GTest::Main SQLiteCpp sqlite3) else (GTEST_FOUND) message(STATUS "Compile googletest from source in submodule") # deactivate some warnings for compiling the googletest library @@ -377,7 +406,7 @@ if (SQLITECPP_BUILD_TESTS) endif (MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1919) endif (MSVC) - target_link_libraries(SQLiteCpp_tests gtest_main) + target_link_libraries(SQLiteCpp_tests gtest_main SQLiteCpp sqlite3) endif (GTEST_FOUND) # add a "test" target: diff --git a/cmake/DownloadSQLite.cmake b/cmake/DownloadSQLite.cmake new file mode 100644 index 00000000..73bac103 --- /dev/null +++ b/cmake/DownloadSQLite.cmake @@ -0,0 +1,62 @@ +#This is free and unencumbered software released into the public domain. +#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. +#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +#For more information, please refer to + +set(SQLITE_BASE_URI "https://sqlite.org") +set(SQLITE_DOWNLOAD_PAGE_URI "${SQLITE_BASE_URI}/download.html") +set(SQLITE_USE_PRERELEASE ON) + + +function(extractLinkFromHTML pathRegexp DOWNLOADED_DOWNLOAD_HTML resVar) + set(ARCHIVE_REGEXP ",'(${pathRegexp})'\\)") + string(REGEX MATCH "${ARCHIVE_REGEXP}" "" "${DOWNLOADED_DOWNLOAD_HTML}") + set("${resVar}" "${CMAKE_MATCH_1}" PARENT_SCOPE) +endfunction() + + +function(downloadSQLiteIfNeeded projectName) + set("DOWNLOADED_DOWNLOAD_HTML_FILE_NAME" "${CMAKE_BINARY_DIR}/sqlite_download.html") + if(EXISTS "${${projectName}_DOWNLOADED_DOWNLOAD_HTML_FILE_NAME}") + else() + file(DOWNLOAD "${SQLITE_DOWNLOAD_PAGE_URI}" "${DOWNLOADED_DOWNLOAD_HTML_FILE_NAME}" TLS_VERIFY SHOW_PROGRESS) + endif() + + file(READ "${DOWNLOADED_DOWNLOAD_HTML_FILE_NAME}" DOWNLOADED_DOWNLOAD_HTML) + if(SQLITE_USE_PRERELEASE) + extractLinkFromHTML("snapshot/sqlite-snapshot-[0-9]+.tar.gz" "${DOWNLOADED_DOWNLOAD_HTML}" SQLITE_PRERELEASE_URI_PART) + endif() + + if(SQLITE_PRERELEASE_URI_PART) + set(SQLITE_URI_PART "${SQLITE_PRERELEASE_URI_PART}") + else() + if(SQLITE_USE_PRERELEASE) + message(STATUS "no prereleases are available, release is used") + endif() + extractLinkFromHTML("20[0-9][0-9]/sqlite-amalgamation-[0-9]+.zip" "${DOWNLOADED_DOWNLOAD_HTML}" SQLITE_RELEASE_URI_PART) + set(SQLITE_URI_PART "${SQLITE_RELEASE_URI_PART}") + endif() + + set(SQLITE_AMALGAMATION_URI "${SQLITE_BASE_URI}/${SQLITE_URI_PART}") + + message(STATUS "${projectName} amalgamation file URI: ${SQLITE_BASE_URI}/${SQLITE_URI_PART}") + + set(VAR_NAME "${projectName}_download") + FetchContent_Declare( + "${VAR_NAME}" + URL "${SQLITE_AMALGAMATION_URI}" + #SOURCE_DIR "${SQLITE_AMALGAMATION_SOURCE_DIR}" + #CONFIGURE_COMMAND "" + #BUILD_COMMAND "" + #INSTALL_COMMAND "" + TLS_VERIFY 1 + ) + + FetchContent_MakeAvailable("${VAR_NAME}") + FetchContent_GetProperties("${VAR_NAME}" + SOURCE_DIR "${projectName}_AMALGAMATION_SOURCE_DIR" + ) + set("${projectName}_AMALGAMATION_SOURCE_DIR" "${${projectName}_AMALGAMATION_SOURCE_DIR}" PARENT_SCOPE) + message(STATUS "${projectName} amalgamation dir: ${${projectName}_AMALGAMATION_SOURCE_DIR}") +endfunction() diff --git a/sqlite3/CMakeLists.txt b/sqlite3/CMakeLists.txt deleted file mode 100644 index de1de0ba..00000000 --- a/sqlite3/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ -# CMake file for compiling the sqlite3 static library under Windows (for ease of use) -# -# Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com) -# -# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt -# or copy at http://opensource.org/licenses/MIT) - -# add sources of the "sqlite3" static library -add_library(sqlite3 - sqlite3.c - sqlite3.h -) - -target_include_directories(sqlite3 - PRIVATE $ - PUBLIC $) - -if (SQLITE_ENABLE_COLUMN_METADATA) - # Enable the use of SQLite column metadata method - # Require that the sqlite3 library is also compiled with this flag: - target_compile_definitions(sqlite3 PUBLIC SQLITE_ENABLE_COLUMN_METADATA) -endif (SQLITE_ENABLE_COLUMN_METADATA) - -if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) - set_target_properties(sqlite3 PROPERTIES COMPILE_FLAGS "-fPIC") -endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")) - -if (UNIX AND CMAKE_COMPILER_IS_GNUCXX) - if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) - target_compile_options(sqlite3 PRIVATE "-Wimplicit-fallthrough=0") - endif() - if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0) - target_compile_options(sqlite3 PRIVATE "-Wno-cast-function-type") - endif() -endif() - -# Allow the library to be installed via "make install" and found with "find_package" - -include(GNUInstallDirs) -install(TARGETS sqlite3 - EXPORT ${PROJECT_NAME}Targets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT libraries) -install(FILES sqlite3.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT headers)