|
| 1 | +#[=============================================================================[ |
| 2 | +Wrapper for finding the `ZLIB` library. |
| 3 | +
|
| 4 | +Module first tries to find the `ZLIB` library on the system. If not successful |
| 5 | +it tries to download it from the upstream source with `FetchContent` module and |
| 6 | +build it together with the PHP build. |
| 7 | +
|
| 8 | +See: https://cmake.org/cmake/help/latest/module/FindZLIB.html |
| 9 | +
|
| 10 | +The `FetchContent` CMake module does things differently compared to the |
| 11 | +`find_package()` flow: |
| 12 | +* By default, it uses `QUIET` in its `find_package()` call when calling the |
| 13 | + `FetchContent_MakeAvailable()` |
| 14 | +* When using `FeatureSummary`, dependencies must be moved manually to |
| 15 | + `PACKAGES_FOUND` from the `PACKAGES_NOT_FOUND` global property |
| 16 | +
|
| 17 | +TODO: Improve this. This is for now only initial `FetchContent` integration for |
| 18 | +testing purposes and will be changed in the future. |
| 19 | +#]=============================================================================] |
| 20 | + |
| 21 | +include(FeatureSummary) |
| 22 | +include(FetchContent) |
| 23 | + |
| 24 | +set_package_properties( |
| 25 | + ZLIB |
| 26 | + PROPERTIES |
| 27 | + URL "https://zlib.net/" |
| 28 | + DESCRIPTION "Compression library" |
| 29 | +) |
| 30 | + |
| 31 | +# Minimum required version for the zlib dependency. |
| 32 | +set(PHP_ZLIB_MIN_VERSION 1.2.0.4) |
| 33 | + |
| 34 | +# Download version when system dependency is not found. |
| 35 | +set(PHP_ZLIB_DOWNLOAD_VERSION 1.3.1) |
| 36 | + |
| 37 | +FetchContent_Declare( |
| 38 | + ZLIB |
| 39 | + URL https://github.com/madler/zlib/releases/download/v${PHP_ZLIB_DOWNLOAD_VERSION}/zlib-${PHP_ZLIB_DOWNLOAD_VERSION}.tar.gz |
| 40 | + EXCLUDE_FROM_ALL |
| 41 | + SYSTEM |
| 42 | + FIND_PACKAGE_ARGS |
| 43 | +) |
| 44 | + |
| 45 | +find_package(ZLIB ${PHP_ZLIB_MIN_VERSION}) |
| 46 | + |
| 47 | +if(NOT ZLIB_FOUND) |
| 48 | + set(FETCHCONTENT_QUIET NO) |
| 49 | + |
| 50 | + # The above EXCLUDE_FROM_ALL was introduced in CMake 3.28. |
| 51 | + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) |
| 52 | + FetchContent_MakeAvailable(ZLIB) |
| 53 | + else() |
| 54 | + FetchContent_GetProperties(ZLIB) |
| 55 | + if(NOT ZLIB_POPULATED) |
| 56 | + FetchContent_Populate(ZLIB) |
| 57 | + |
| 58 | + add_subdirectory( |
| 59 | + ${zlib_SOURCE_DIR} |
| 60 | + ${zlib_BINARY_DIR} |
| 61 | + EXCLUDE_FROM_ALL |
| 62 | + ) |
| 63 | + endif() |
| 64 | + endif() |
| 65 | + |
| 66 | + # Move dependency to PACKAGES_FOUND. |
| 67 | + block() |
| 68 | + get_cmake_property(packagesNotFound PACKAGES_NOT_FOUND) |
| 69 | + list(REMOVE_ITEM packagesNotFound ZLIB) |
| 70 | + set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND packagesNotFound) |
| 71 | + get_cmake_property(packagesFound PACKAGES_FOUND) |
| 72 | + set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND ZLIB) |
| 73 | + endblock() |
| 74 | + |
| 75 | + set_target_properties(zlibstatic PROPERTIES POSITION_INDEPENDENT_CODE TRUE) |
| 76 | + add_library(ZLIB::ZLIB INTERFACE IMPORTED GLOBAL) |
| 77 | + target_link_libraries(ZLIB::ZLIB INTERFACE zlibstatic) |
| 78 | + target_include_directories( |
| 79 | + zlibstatic |
| 80 | + PUBLIC |
| 81 | + $<BUILD_INTERFACE:${zlib_BINARY_DIR}> |
| 82 | + $<INSTALL_INTERFACE:include> |
| 83 | + ) |
| 84 | + |
| 85 | + # Mark package as found. |
| 86 | + set(ZLIB_FOUND TRUE) |
| 87 | + |
| 88 | + # Clean used variables. |
| 89 | + unset(FETCHCONTENT_QUIET) |
| 90 | +endif() |
0 commit comments