|
| 1 | +#[=============================================================================[ |
| 2 | +# PHP/Package/Zlib |
| 3 | +
|
| 4 | +Finds or downloads the zlib library: |
| 5 | +
|
| 6 | +```cmake |
| 7 | +include(PHP/Package/Zlib) |
| 8 | +``` |
| 9 | +
|
| 10 | +This module is a wrapper for finding the `ZLIB` library. It first tries to find |
| 11 | +the `ZLIB` library on the system. If not successful it tries to download it from |
| 12 | +the upstream source with `ExternalProject` module and builds it together with |
| 13 | +the PHP build. |
| 14 | +
|
| 15 | +See: https://cmake.org/cmake/help/latest/module/FindZLIB.html |
| 16 | +
|
| 17 | +## Examples |
| 18 | +
|
| 19 | +Basic usage: |
| 20 | +
|
| 21 | +```cmake |
| 22 | +include(PHP/Package/ZLIB) |
| 23 | +target_link_libraries(php_ext_foo PRIVATE ZLIB::ZLIB) |
| 24 | +``` |
| 25 | +#]=============================================================================] |
| 26 | + |
| 27 | +include(FeatureSummary) |
| 28 | +include(ExternalProject) |
| 29 | + |
| 30 | +set_package_properties( |
| 31 | + ZLIB |
| 32 | + PROPERTIES |
| 33 | + URL "https://zlib.net/" |
| 34 | + DESCRIPTION "Compression library" |
| 35 | +) |
| 36 | + |
| 37 | +# Minimum required version for the zlib dependency. |
| 38 | +set(PHP_ZLIB_MIN_VERSION 1.2.0.4) |
| 39 | + |
| 40 | +# Download version when system dependency is not found. |
| 41 | +set(PHP_ZLIB_DOWNLOAD_VERSION 1.3.1) |
| 42 | + |
| 43 | +if(TARGET ZLIB::ZLIB) |
| 44 | + set(ZLIB_FOUND TRUE) |
| 45 | + get_property(ZLIB_DOWNLOADED GLOBAL PROPERTY _PHP_ZLIB_DOWNLOADED) |
| 46 | + return() |
| 47 | +endif() |
| 48 | + |
| 49 | +find_package(ZLIB ${PHP_ZLIB_MIN_VERSION}) |
| 50 | + |
| 51 | +if(NOT ZLIB_FOUND) |
| 52 | + message( |
| 53 | + STATUS |
| 54 | + "ZLIB ${PHP_ZLIB_DOWNLOAD_VERSION} will be downloaded at build phase" |
| 55 | + ) |
| 56 | + |
| 57 | + set(options "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>") |
| 58 | + |
| 59 | + if(PHP_ZLIB_DOWNLOAD_VERSION VERSION_LESS_EQUAL 1.3.1) |
| 60 | + list(APPEND options -DZLIB_BUILD_EXAMPLES=OFF) |
| 61 | + endif() |
| 62 | + |
| 63 | + if(PHP_ZLIB_DOWNLOAD_VERSION VERSION_GREATER 1.3.1) |
| 64 | + list(APPEND options -DZLIB_BUILD_TESTING=OFF) |
| 65 | + endif() |
| 66 | + |
| 67 | + ExternalProject_Add( |
| 68 | + ZLIB |
| 69 | + STEP_TARGETS build install |
| 70 | + URL |
| 71 | + https://github.com/madler/zlib/releases/download/v${PHP_ZLIB_DOWNLOAD_VERSION}/zlib-${PHP_ZLIB_DOWNLOAD_VERSION}.tar.gz |
| 72 | + CMAKE_ARGS ${options} |
| 73 | + INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/zlib-installation |
| 74 | + INSTALL_BYPRODUCTS <INSTALL_DIR>/lib/libz${CMAKE_STATIC_LIBRARY_SUFFIX} |
| 75 | + ) |
| 76 | + |
| 77 | + # Move dependency to PACKAGES_FOUND. |
| 78 | + block() |
| 79 | + get_cmake_property(packagesNotFound PACKAGES_NOT_FOUND) |
| 80 | + list(REMOVE_ITEM packagesNotFound ZLIB) |
| 81 | + set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND packagesNotFound) |
| 82 | + get_cmake_property(packagesFound PACKAGES_FOUND) |
| 83 | + set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND ZLIB) |
| 84 | + endblock() |
| 85 | + |
| 86 | + ExternalProject_Get_Property(ZLIB INSTALL_DIR) |
| 87 | + |
| 88 | + # Bypass issue with non-existing include directory for the imported target. |
| 89 | + file(MAKE_DIRECTORY ${INSTALL_DIR}/include) |
| 90 | + |
| 91 | + add_library(ZLIB::ZLIB STATIC IMPORTED GLOBAL) |
| 92 | + set_target_properties( |
| 93 | + ZLIB::ZLIB |
| 94 | + PROPERTIES |
| 95 | + IMPORTED_LOCATION "${INSTALL_DIR}/lib/libz${CMAKE_STATIC_LIBRARY_SUFFIX}" |
| 96 | + INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include" |
| 97 | + ) |
| 98 | + add_dependencies(ZLIB::ZLIB ZLIB-install) |
| 99 | + |
| 100 | + # Mark package as found. |
| 101 | + set(ZLIB_FOUND TRUE) |
| 102 | + |
| 103 | + define_property( |
| 104 | + GLOBAL |
| 105 | + PROPERTY _PHP_ZLIB_DOWNLOADED |
| 106 | + BRIEF_DOCS "Configuration header code with system extensions definitions" |
| 107 | + ) |
| 108 | + |
| 109 | + set_property(GLOBAL PROPERTY _PHP_ZLIB_DOWNLOADED TRUE) |
| 110 | + set(ZLIB_DOWNLOADED TRUE) |
| 111 | +endif() |
0 commit comments