From bc1ddb8ce0253d1545725dd2f1ed501af93f788c 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 | 153 +++++++++++++++++-------------------- cmake/DownloadSQLite.cmake | 62 +++++++++++++++ sqlite3/CMakeLists.txt | 57 -------------- 3 files changed, 133 insertions(+), 139 deletions(-) create mode 100644 cmake/DownloadSQLite.cmake delete mode 100644 sqlite3/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 85c10619..866230f6 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.1.1) # SQLiteC++ 3.x requires C++11 features @@ -170,8 +171,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 @@ -222,83 +281,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") - option(SQLITE_ENABLE_JSON1 "Enable JSON1 extension when building internal sqlite3 library." ON) - # 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() - - # When using the SQLite codec, we need to link against the sqlcipher lib & include - # So this gets the lib & header, and links/includes everything - if(SQLITE_HAS_CODEC) - # Make PkgConfig optional since Windows doesn't usually have it installed. - find_package(PkgConfig QUIET) - if(PKG_CONFIG_FOUND) - # IMPORTED_TARGET was added in 3.6.3 - if(CMAKE_VERSION VERSION_LESS 3.6.3) - pkg_check_modules(sqlcipher REQUIRED sqlcipher) - # Only used in Database.cpp so PRIVATE to hide from end-user - # Since we can't use IMPORTED_TARGET on this older Cmake version, manually link libs & includes - target_link_libraries(SQLiteCpp PRIVATE ${sqlcipher_LIBRARIES}) - target_include_directories(SQLiteCpp PRIVATE ${sqlcipher_INCLUDE_DIRS}) - else() - pkg_check_modules(sqlcipher REQUIRED IMPORTED_TARGET sqlcipher) - # Only used in Database.cpp so PRIVATE to hide from end-user - target_link_libraries(SQLiteCpp PRIVATE PkgConfig::sqlcipher) - endif() - else() - # Since we aren't using pkgconf here, find it manually - find_library(sqlcipher_LIBRARY "sqlcipher") - find_path(sqlcipher_INCLUDE_DIR "sqlcipher/sqlite3.h" - PATH_SUFFIXES - "include" - "includes" - ) - # Hides it from the GUI - mark_as_advanced(sqlcipher_LIBRARY sqlcipher_INCLUDE_DIR) - if(NOT sqlcipher_INCLUDE_DIR) - message(FATAL_ERROR "${PROJECT_NAME} requires the \"\" header to use the codec functionality but it wasn't found.") - elseif(NOT sqlcipher_LIBRARY) - message(FATAL_ERROR "${PROJECT_NAME} requires the sqlcipher library to use the codec functionality but it wasn't found.") - endif() - # Only used in Database.cpp so PRIVATE to hide from end-user - target_include_directories(SQLiteCpp PRIVATE "${sqlcipher_INCLUDE_DIR}/sqlcipher") - target_link_libraries(SQLiteCpp PRIVATE ${sqlcipher_LIBRARY}) - endif() - 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} @@ -373,11 +359,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) @@ -391,12 +380,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 @@ -421,7 +410,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 7ee9df30..00000000 --- a/sqlite3/CMakeLists.txt +++ /dev/null @@ -1,57 +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 (SQLITE_ENABLE_JSON1) - # Enable JSON1 extension when building sqlite3 - # See more here: https://www.sqlite.org/json1.html - target_compile_definitions(sqlite3 PUBLIC SQLITE_ENABLE_JSON1) -endif (SQLITE_ENABLE_JSON1) - -if(SQLITE_ENABLE_RTREE) - # Enable RTree extension when building sqlite3 - # See more here: https://sqlite.org/rtree.html - target_compile_definitions(sqlite3 PUBLIC SQLITE_ENABLE_RTREE) -endif (SQLITE_ENABLE_RTREE) - -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)