Skip to content

Commit caa9c3b

Browse files
committed
Use FetchContent and ExternalProject modules
This downloads dependencies at the configuration phase and configures and builds them at build phase.
1 parent 96b3747 commit caa9c3b

File tree

5 files changed

+274
-159
lines changed

5 files changed

+274
-159
lines changed

cmake/cmake/modules/PHP/Package/LibXml2.cmake

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ include(PHP/Package/LibXml2)
88
```
99
1010
This 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
1414
See: 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)
2827
include(ExternalProject)
28+
include(FeatureSummary)
29+
include(FetchContent)
2930

3031
set_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.
4142
set(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()

cmake/cmake/modules/PHP/Package/Oniguruma.cmake

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ include(PHP/Package/Oniguruma)
1010
Wrapper for finding the `Oniguruma` library.
1111
1212
Module first tries to find the `Oniguruma` library on the system. If not
13-
successful it tries to download it from the upstream source with
14-
`ExternalProject` module and build it together with the PHP build.
13+
successful it tries to download it from the upstream source and builds it
14+
together with the PHP build.
1515
1616
## Examples
1717
@@ -32,41 +32,66 @@ include(FeatureSummary)
3232
# Download version when system dependency is not found.
3333
set(PHP_ONIGURUMA_DOWNLOAD_VERSION 6.9.10)
3434

35-
if(TARGET Oniguruma::Oniguruma)
36-
set(Oniguruma_FOUND TRUE)
37-
get_property(Oniguruma_DOWNLOADED GLOBAL PROPERTY _PHP_Oniguruma_DOWNLOADED)
38-
set(PHP_ONIG_KOI8 FALSE)
39-
return()
40-
endif()
35+
macro(php_package_oniguruma_find)
36+
if(TARGET Oniguruma::Oniguruma)
37+
set(Oniguruma_FOUND TRUE)
38+
get_property(Oniguruma_DOWNLOADED GLOBAL PROPERTY _PHP_Oniguruma_DOWNLOADED)
39+
set(PHP_ONIG_KOI8 FALSE)
40+
else()
41+
find_package(Oniguruma ${PHP_ONIGURUMA_MIN_VERSION})
42+
43+
if(NOT Oniguruma_FOUND)
44+
_php_package_oniguruma_download()
45+
endif()
46+
endif()
47+
endmacro()
4148

42-
find_package(Oniguruma ${PHP_ONIGURUMA_MIN_VERSION})
49+
macro(_php_package_oniguruma_download)
50+
message(STATUS "Downloading Oniguruma ${PHP_ONIGURUMA_DOWNLOAD_VERSION}")
4351

44-
if(NOT Oniguruma_FOUND)
45-
message(
46-
STATUS
47-
"Oniguruma ${PHP_ONIGURUMA_DOWNLOAD_VERSION} will be downloaded at build phase"
52+
FetchContent_Declare(
53+
Oniguruma
54+
URL https://github.com/petk/oniguruma/archive/refs/tags/v${PHP_ONIGURUMA_DOWNLOAD_VERSION}.tar.gz
55+
SOURCE_SUBDIR non-existing
56+
OVERRIDE_FIND_PACKAGE
4857
)
4958

59+
FetchContent_MakeAvailable(Oniguruma)
60+
5061
set(options "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>")
5162

5263
list(
5364
APPEND
5465
options
55-
-DINSTALL_DOCUMENTATION=OFF
56-
-DBUILD_TEST=OFF
57-
-DBUILD_SHARED_LIBS=OFF
66+
-DINSTALL_DOCUMENTATION=OFF
67+
-DBUILD_TEST=OFF
68+
-DBUILD_SHARED_LIBS=OFF
5869
)
5970

6071
ExternalProject_Add(
6172
Oniguruma
6273
STEP_TARGETS build install
63-
URL
64-
https://github.com/petk/oniguruma/archive/refs/tags/v${PHP_ONIGURUMA_DOWNLOAD_VERSION}.tar.gz
74+
SOURCE_DIR ${oniguruma_SOURCE_DIR}
75+
BINARY_DIR ${oniguruma_BINARY_DIR}
6576
CMAKE_ARGS ${options}
66-
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/oniguruma-installation
77+
INSTALL_DIR ${FETCHCONTENT_BASE_DIR}/oniguruma-install
6778
INSTALL_BYPRODUCTS <INSTALL_DIR>/lib/libonig${CMAKE_STATIC_LIBRARY_SUFFIX}
6879
)
6980

81+
ExternalProject_Get_Property(Oniguruma INSTALL_DIR)
82+
83+
# Bypass missing directory error for the imported target below.
84+
file(MAKE_DIRECTORY ${INSTALL_DIR}/include)
85+
86+
add_library(Oniguruma::Oniguruma STATIC IMPORTED GLOBAL)
87+
add_dependencies(Oniguruma::Oniguruma Oniguruma-install)
88+
set_target_properties(
89+
Oniguruma::Oniguruma
90+
PROPERTIES
91+
INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include
92+
IMPORTED_LOCATION ${INSTALL_DIR}/lib/libonig${CMAKE_STATIC_LIBRARY_SUFFIX}
93+
)
94+
7095
# Move dependency to PACKAGES_FOUND.
7196
block()
7297
set(package "Oniguruma")
@@ -80,20 +105,6 @@ if(NOT Oniguruma_FOUND)
80105
endif()
81106
endblock()
82107

83-
ExternalProject_Get_Property(Oniguruma 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(Oniguruma::Oniguruma STATIC IMPORTED GLOBAL)
89-
set_target_properties(
90-
Oniguruma::Oniguruma
91-
PROPERTIES
92-
IMPORTED_LOCATION "${INSTALL_DIR}/lib/libonig${CMAKE_STATIC_LIBRARY_SUFFIX}"
93-
INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include"
94-
)
95-
add_dependencies(Oniguruma::Oniguruma Oniguruma-install)
96-
97108
# Mark package as found.
98109
set(Oniguruma_FOUND TRUE)
99110

@@ -107,4 +118,6 @@ if(NOT Oniguruma_FOUND)
107118
set(Oniguruma_DOWNLOADED TRUE)
108119

109120
set(PHP_ONIG_KOI8 FALSE)
110-
endif()
121+
endmacro()
122+
123+
php_package_oniguruma_find()

0 commit comments

Comments
 (0)