Skip to content

Commit 812be2f

Browse files
committed
Use ExternalProject module
1 parent c6a7af3 commit 812be2f

File tree

7 files changed

+115
-94
lines changed

7 files changed

+115
-94
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
```cmake
20+
include(PHP/Package/ZLIB)
21+
target_link_libraries(php_ext_foo PRIVATE ZLIB::ZLIB)
22+
#]=============================================================================]
23+
24+
include(FeatureSummary)
25+
include(ExternalProject)
26+
27+
set_package_properties(
28+
ZLIB
29+
PROPERTIES
30+
URL "https://zlib.net/"
31+
DESCRIPTION "Compression library"
32+
)
33+
34+
# Minimum required version for the zlib dependency.
35+
set(PHP_ZLIB_MIN_VERSION 1.2.0.4)
36+
#set(PHP_ZLIB_MIN_VERSION 1.3.1)
37+
38+
# Download version when system dependency is not found.
39+
set(PHP_ZLIB_DOWNLOAD_VERSION 1.3.1)
40+
41+
if(TARGET ZLIB::ZLIB)
42+
set(ZLIB_FOUND TRUE)
43+
return()
44+
endif()
45+
46+
find_package(ZLIB ${PHP_ZLIB_MIN_VERSION})
47+
48+
if(NOT ZLIB_FOUND)
49+
message(
50+
STATUS
51+
"ZLIB ${PHP_ZLIB_DOWNLOAD_VERSION} will be downloaded at build phase"
52+
)
53+
54+
set(zlib_options "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>")
55+
56+
if(PHP_ZLIB_DOWNLOAD_VERSION VERSION_LESS_EQUAL 1.3.1)
57+
list(APPEND zlib_options -DZLIB_BUILD_EXAMPLES=OFF)
58+
endif()
59+
60+
if(PHP_ZLIB_DOWNLOAD_VERSION VERSION_GREATER 1.3.1)
61+
list(APPEND zlib_options -DZLIB_BUILD_TESTING=OFF)
62+
endif()
63+
64+
ExternalProject_Add(
65+
ZLIB
66+
STEP_TARGETS build install
67+
URL
68+
https://github.com/madler/zlib/releases/download/v${PHP_ZLIB_DOWNLOAD_VERSION}/zlib-${PHP_ZLIB_DOWNLOAD_VERSION}.tar.gz
69+
CMAKE_ARGS ${zlib_options}
70+
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/zlib-installation
71+
INSTALL_BYPRODUCTS <INSTALL_DIR>/lib/libz.so
72+
)
73+
74+
# Move dependency to PACKAGES_FOUND.
75+
block()
76+
get_cmake_property(packagesNotFound PACKAGES_NOT_FOUND)
77+
list(REMOVE_ITEM packagesNotFound ZLIB)
78+
set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND packagesNotFound)
79+
get_cmake_property(packagesFound PACKAGES_FOUND)
80+
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND ZLIB)
81+
endblock()
82+
83+
ExternalProject_Get_Property(ZLIB INSTALL_DIR)
84+
85+
# Bypass issue with non-existing include directory for the imported target.
86+
file(MAKE_DIRECTORY ${INSTALL_DIR}/include)
87+
88+
add_library(ZLIB::ZLIB STATIC IMPORTED GLOBAL)
89+
set_target_properties(
90+
ZLIB::ZLIB
91+
PROPERTIES
92+
IMPORTED_LOCATION "${INSTALL_DIR}/lib/libz.${CMAKE_STATIC_LIBRARY_SUFFIX}"
93+
INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include"
94+
)
95+
add_dependencies(ZLIB::ZLIB ZLIB-install)
96+
97+
# Mark package as found.
98+
set(ZLIB_FOUND TRUE)
99+
endif()

cmake/cmake/modules/Packages/LibXml2.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ FetchContent_Declare(
5454
find_package(LibXml2 ${PHP_LIBXML2_MIN_VERSION})
5555

5656
if(NOT LibXml2_FOUND)
57-
include(Packages/ZLIB)
57+
include(PHP/Package/ZLIB)
5858

5959
set(FETCHCONTENT_QUIET NO)
6060
set(LIBXML2_WITH_PYTHON OFF)

cmake/cmake/modules/Packages/ZLIB.cmake

Lines changed: 0 additions & 90 deletions
This file was deleted.

cmake/ext/gd/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ if(NOT PHP_EXT_GD_EXTERNAL)
316316
)
317317
endif()
318318

319-
include(Packages/ZLIB)
319+
include(PHP/Package/ZLIB)
320320
set_package_properties(
321321
ZLIB
322322
PROPERTIES

cmake/ext/mysqlnd/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ target_link_libraries(
170170
)
171171

172172
if(PHP_EXT_MYSQLND_COMPRESSION)
173-
include(Packages/ZLIB)
173+
include(PHP/Package/ZLIB)
174174
set_package_properties(
175175
ZLIB
176176
PROPERTIES

cmake/ext/standard/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Use external crypt library, such as libcrypt or libxcrypt, instead of the
2222
bundled PHP crypt.
2323
#]=============================================================================]
2424

25+
# TODO: ext/standard/image.c depends on zlib extension and its macros but it
26+
# should only depend on zlib library.
27+
2528
project(
2629
PhpExtensionStandard
2730
LANGUAGES C

cmake/ext/zlib/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,25 @@ endif()
7777

7878
target_compile_definitions(php_ext_zlib PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE)
7979

80-
include(Packages/ZLIB)
80+
include(PHP/Package/ZLIB)
8181
set_package_properties(
8282
ZLIB
8383
PROPERTIES
8484
TYPE REQUIRED
8585
PURPOSE "Necessary to enable the zlib extension."
8686
)
8787

88+
# Link publicly for internal_functions files and ext/standard/image.c.
8889
target_link_libraries(php_ext_zlib PUBLIC ZLIB::ZLIB)
8990

91+
# TODO: zlib.h must exist when building php_main* targets.
92+
add_dependencies(
93+
php_ext_zlib
94+
php_main
95+
php_main_internal_functions
96+
php_main_internal_functions_cli
97+
)
98+
9099
set(HAVE_ZLIB TRUE)
91100

92101
configure_file(cmake/config.h.in config.h)

0 commit comments

Comments
 (0)