|
| 1 | +#[=============================================================================[ |
| 2 | +# PHP/Package/SQLite3 |
| 3 | +
|
| 4 | +Finds or downloads the SQLite library: |
| 5 | +
|
| 6 | +```cmake |
| 7 | +include(PHP/Package/SQLite3) |
| 8 | +``` |
| 9 | +
|
| 10 | +This module is a wrapper for finding the `SQLite` library. It first tries to |
| 11 | +find the `SQLite` library on the system. If not successful it tries to download |
| 12 | +it from the upstream source and builds it together with the PHP build. |
| 13 | +
|
| 14 | +See: https://cmake.org/cmake/help/latest/module/FindSQLite3.html |
| 15 | +
|
| 16 | +## Examples |
| 17 | +
|
| 18 | +Basic usage: |
| 19 | +
|
| 20 | +```cmake |
| 21 | +include(PHP/Package/SQLite3) |
| 22 | +php_package_sqlite3_find() |
| 23 | +target_link_libraries(php_ext_foo PRIVATE SQLite::SQLite3) |
| 24 | +``` |
| 25 | +#]=============================================================================] |
| 26 | + |
| 27 | +include(ExternalProject) |
| 28 | +include(FeatureSummary) |
| 29 | +include(FetchContent) |
| 30 | + |
| 31 | +set_package_properties( |
| 32 | + SQLite3 |
| 33 | + PROPERTIES |
| 34 | + URL "https://www.sqlite.org/" |
| 35 | + DESCRIPTION "SQL database engine library" |
| 36 | +) |
| 37 | + |
| 38 | +# Minimum required version for the SQLite dependency. |
| 39 | +set(PHP_SQLITE3_MIN_VERSION 3.7.7) |
| 40 | + |
| 41 | +# Download version when system dependency is not found. |
| 42 | +set(PHP_SQLITE3_DOWNLOAD_VERSION 3.50.2) |
| 43 | + |
| 44 | +macro(php_package_sqlite3_find) |
| 45 | + if(TARGET SQLite::SQLite3) |
| 46 | + set(SQLite3_FOUND TRUE) |
| 47 | + get_property(SQLite3_DOWNLOADED GLOBAL PROPERTY _PHP_SQLite3_DOWNLOADED) |
| 48 | + else() |
| 49 | + find_package(SQLite3 ${PHP_SQLITE3_MIN_VERSION}) |
| 50 | + |
| 51 | + if(NOT SQLite3_FOUND) |
| 52 | + _php_package_sqlite3_download() |
| 53 | + endif() |
| 54 | + endif() |
| 55 | +endmacro() |
| 56 | + |
| 57 | +macro(_php_package_sqlite3_download) |
| 58 | + message(STATUS "Downloading SQLite ${PHP_SQLITE3_DOWNLOAD_VERSION}") |
| 59 | + |
| 60 | + FetchContent_Declare( |
| 61 | + SQLite3 |
| 62 | + URL https://github.com/sjinks/sqlite3-cmake/archive/refs/tags/v${PHP_SQLITE3_DOWNLOAD_VERSION}.tar.gz |
| 63 | + SOURCE_SUBDIR non-existing |
| 64 | + OVERRIDE_FIND_PACKAGE |
| 65 | + ) |
| 66 | + |
| 67 | + FetchContent_MakeAvailable(SQLite3) |
| 68 | + |
| 69 | + set(options "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>") |
| 70 | + |
| 71 | + ExternalProject_Add( |
| 72 | + SQLite3 |
| 73 | + STEP_TARGETS build install |
| 74 | + SOURCE_DIR ${sqlite3_SOURCE_DIR} |
| 75 | + BINARY_DIR ${sqlite3_BINARY_DIR} |
| 76 | + CMAKE_ARGS ${options} |
| 77 | + INSTALL_DIR ${FETCHCONTENT_BASE_DIR}/sqlite3-install |
| 78 | + INSTALL_BYPRODUCTS <INSTALL_DIR>/lib/libsqlite3${CMAKE_STATIC_LIBRARY_SUFFIX} |
| 79 | + ) |
| 80 | + |
| 81 | + ExternalProject_Get_Property(SQLite3 INSTALL_DIR) |
| 82 | + |
| 83 | + # Bypass missing directory error for the imported target below. |
| 84 | + file(MAKE_DIRECTORY ${INSTALL_DIR}/include) |
| 85 | + |
| 86 | + add_library(SQLite::SQLite3 STATIC IMPORTED GLOBAL) |
| 87 | + add_dependencies(SQLite::SQLite3 SQLite3-install) |
| 88 | + set_target_properties( |
| 89 | + SQLite::SQLite3 |
| 90 | + PROPERTIES |
| 91 | + INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include |
| 92 | + IMPORTED_LOCATION ${INSTALL_DIR}/lib/libsqlite3${CMAKE_STATIC_LIBRARY_SUFFIX} |
| 93 | + ) |
| 94 | + |
| 95 | + # Move dependency to PACKAGES_FOUND. |
| 96 | + block() |
| 97 | + set(package "SQLite3") |
| 98 | + get_property(packagesNotFound GLOBAL PROPERTY PACKAGES_NOT_FOUND) |
| 99 | + list(REMOVE_ITEM packagesNotFound ${package}) |
| 100 | + set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND ${packagesNotFound}) |
| 101 | + get_property(packagesFound GLOBAL PROPERTY PACKAGES_FOUND) |
| 102 | + list(FIND packagesFound ${package} found) |
| 103 | + if(found EQUAL -1) |
| 104 | + set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND ${package}) |
| 105 | + endif() |
| 106 | + endblock() |
| 107 | + |
| 108 | + # Mark package as found. |
| 109 | + set(SQLite3_FOUND TRUE) |
| 110 | + |
| 111 | + define_property( |
| 112 | + GLOBAL |
| 113 | + PROPERTY _PHP_SQLite3_DOWNLOADED |
| 114 | + BRIEF_DOCS "Marker that SQLite3 library will be downloaded" |
| 115 | + ) |
| 116 | + |
| 117 | + set_property(GLOBAL PROPERTY _PHP_SQLite3_DOWNLOADED TRUE) |
| 118 | + set(SQLite3_DOWNLOADED TRUE) |
| 119 | +endmacro() |
| 120 | + |
| 121 | +php_package_sqlite3_find() |
0 commit comments