@@ -8,8 +8,8 @@ include(PHP/Package/LibXml2)
88```
99
1010This module first tries to find the `libxml2` library on the system. If not
11- successful it tries to download it from the upstream source with
12- `ExternalProject` module and build it together with the PHP build.
11+ successful it tries to download it from the upstream source and builds it
12+ together with the PHP build.
1313
1414See: https://cmake.org/cmake/help/latest/module/FindLibXml2.html
1515
@@ -24,8 +24,9 @@ target_link_libraries(example PRIVATE LibXml2::LibXml2)
2424```
2525#]=============================================================================]
2626
27- include (FeatureSummary)
2827include (ExternalProject)
28+ include (FeatureSummary)
29+ include (FetchContent)
2930
3031set_package_properties(
3132 LibXml2
@@ -40,36 +41,74 @@ set(PHP_LIBXML2_MIN_VERSION 2.9.0)
4041# Download version when system dependency is not found.
4142set (PHP_LIBXML2_DOWNLOAD_VERSION 2.14.4)
4243
43- if (TARGET LibXml2::LibXml2)
44- set (LibXml2_FOUND TRUE )
45- get_property (LibXml2_DOWNLOADED GLOBAL PROPERTY _PHP_LibXml2_DOWNLOADED)
46- return ()
47- endif ()
44+ macro (php_package_libxml2_find)
45+ if (TARGET LibXml2::LibXml2)
46+ set (LibXml2_FOUND TRUE )
47+ get_property (LibXml2_DOWNLOADED GLOBAL PROPERTY _PHP_LibXml2_DOWNLOADED)
48+ else ()
49+ # LibXml2 depends on ZLIB.
50+ include (PHP/Package/ZLIB)
51+
52+ find_package (LibXml2 ${PHP_LIBXML2_MIN_VERSION} )
4853
49- find_package (LibXml2 ${PHP_LIBXML2_MIN_VERSION} )
54+ if (NOT LibXml2_FOUND)
55+ _php_package_libxml2_download()
56+ endif ()
57+ endif ()
58+ endmacro ()
59+
60+ macro (_php_package_libxml2_download)
61+ message (STATUS "Downloading LibXml2 ${PHP_LIBXML2_DOWNLOAD_VERSION} " )
5062
51- if (NOT LibXml2_FOUND)
52- message (
53- STATUS
54- "LibXml2 ${PHP_LIBXML2_DOWNLOAD_VERSION} will be downloaded at build phase"
63+ FetchContent_Declare(
64+ LibXml2
65+ URL https://github.com/GNOME/libxml2/archive/refs/tags/v${PHP_LIBXML2_DOWNLOAD_VERSION} .tar.gz
66+ SOURCE_SUBDIR non-existing
67+ OVERRIDE_FIND_PACKAGE
5568 )
5669
57- include (PHP/Package/ZLIB )
70+ FetchContent_MakeAvailable(LibXml2 )
5871
5972 set (options "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>" )
60- list (APPEND options -DLIBXML2_WITH_PYTHON=OFF -DLIBXML2_WITH_LZMA=OFF )
73+ list (
74+ APPEND
75+ options
76+ -DLIBXML2_WITH_PYTHON=OFF
77+ -DLIBXML2_WITH_LZMA=OFF
78+ -DBUILD_SHARED_LIBS=OFF
79+ )
80+
81+ if (ZLIB_DOWNLOADED)
82+ ExternalProject_Get_Property(ZLIB INSTALL_DIR)
83+ list (APPEND options "-DZLIB_ROOT=${INSTALL_DIR} " )
84+ endif ()
6185
6286 ExternalProject_Add(
6387 LibXml2
64- STEP_TARGETS build install
65- URL
66- https://github.com/GNOME/libxml2/archive/refs/tags/v ${PHP_LIBXML2_DOWNLOAD_VERSION} .tar.gz
88+ STEP_TARGETS configure build install
89+ SOURCE_DIR ${libxml2_SOURCE_DIR}
90+ BINARY_DIR ${libxml2_BINARY_DIR}
6791 CMAKE_ARGS ${options}
68- INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR } /libxml2-installation
92+ INSTALL_DIR ${FETCHCONTENT_BASE_DIR } /libxml2-install
6993 INSTALL_BYPRODUCTS <INSTALL_DIR>/lib/libxml2${CMAKE_STATIC_LIBRARY_SUFFIX}
7094 )
7195
72- add_dependencies (LibXml2 ZLIB::ZLIB)
96+ add_dependencies (LibXml2-configure ZLIB::ZLIB)
97+
98+ ExternalProject_Get_Property(LibXml2 INSTALL_DIR)
99+
100+ # Bypass missing directory error for the imported target below.
101+ file (MAKE_DIRECTORY ${INSTALL_DIR} /include /libxml2)
102+
103+ add_library (LibXml2::LibXml2 STATIC IMPORTED GLOBAL )
104+ add_dependencies (LibXml2::LibXml2 LibXml2-install )
105+
106+ set_target_properties (
107+ LibXml2::LibXml2
108+ PROPERTIES
109+ INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR} /include /libxml2
110+ IMPORTED_LOCATION ${INSTALL_DIR} /lib/libxml2${CMAKE_STATIC_LIBRARY_SUFFIX}
111+ )
73112
74113 # Move dependency to PACKAGES_FOUND.
75114 block()
@@ -84,20 +123,6 @@ if(NOT LibXml2_FOUND)
84123 endif ()
85124 endblock()
86125
87- ExternalProject_Get_Property(LibXml2 INSTALL_DIR)
88-
89- # Bypass issue with non-existing include directory for the imported target.
90- file (MAKE_DIRECTORY ${INSTALL_DIR} /include )
91-
92- add_library (LibXml2::LibXml2 STATIC IMPORTED GLOBAL )
93- set_target_properties (
94- LibXml2::LibXml2
95- PROPERTIES
96- IMPORTED_LOCATION "${INSTALL_DIR} /lib/libxml2${CMAKE_STATIC_LIBRARY_SUFFIX} "
97- INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR} /include"
98- )
99- add_dependencies (LibXml2::LibXml2 LibXml2-install )
100-
101126 # Mark package as found.
102127 set (LibXml2_FOUND TRUE )
103128
@@ -108,5 +133,7 @@ if(NOT LibXml2_FOUND)
108133 )
109134
110135 set_property (GLOBAL PROPERTY _PHP_LibXml2_DOWNLOADED TRUE )
111- set (LibXml2_DOWNLOADED TRUE )
112- endif ()
136+ set (Libxml2_DOWNLOADED TRUE )
137+ endmacro ()
138+
139+ php_package_libxml2_find()
0 commit comments