Skip to content

Commit c97f6f4

Browse files
committed
Add installation configuration
This adds the config files for finding PHP when using find_package(PHP) without find module (the CONFIG mode). Further improvements and refactorings in the next commits...
1 parent 5bdaa94 commit c97f6f4

File tree

18 files changed

+223
-52
lines changed

18 files changed

+223
-52
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,8 @@ jobs:
146146
- name: Setup shared standalone extension
147147
run: |
148148
php php-build/all-enabled/php-src/ext/ext_skel.php --ext phantom
149-
mkdir -p php-build/all-enabled/php-src/ext/phantom/cmake/modules
150-
cp cmake/cmake/modules/FindPHP.cmake \
151-
php-build/all-enabled/php-src/ext/phantom/cmake/modules
152-
cmake \
153-
-S php-build/all-enabled/php-src/ext/phantom \
154-
-B php-build/all-enabled/php-src/ext/phantom/cmake-build
155-
cmake --build php-build/all-enabled/php-src/ext/phantom/cmake-build -j
156-
cmake --install php-build/all-enabled/php-src/ext/phantom/cmake-build
149+
cd php-build/all-enabled/php-src/ext/phantom
150+
cmake -B cmake-build
151+
cmake --build cmake-build -j
152+
cmake --install cmake-build
157153
php -d extension=phantom -m | grep phantom

cmake/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ block()
6161
endforeach()
6262
endblock()
6363

64+
# Configure PHP installation.
65+
include(cmake/installation/Install.cmake)
66+
6467
# Rebuild all targets as needed.
6568
if(NOT PHP_FOUND)
6669
include(PHP/Rebuild)

cmake/cmake/Requirements.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ find_package(Sendmail)
5757
# version required to run these PHP scripts.
5858
################################################################################
5959
find_package(PHP 7.4 COMPONENTS Interpreter)
60+
set(CMAKE_DISABLE_FIND_PACKAGE_PHP TRUE)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
include_guard(GLOBAL)
2+
3+
if(NOT PROJECT_NAME STREQUAL "PHP")
4+
message(
5+
AUTHOR_WARNING
6+
"${CMAKE_CURRENT_LIST_FILE} should be used in the project(PHP) scope."
7+
)
8+
return()
9+
endif()
10+
11+
include(CMakePackageConfigHelpers)
12+
13+
write_basic_package_version_file(
14+
PHPConfigVersion.cmake
15+
COMPATIBILITY AnyNewerVersion
16+
)
17+
18+
set(INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX})
19+
20+
configure_package_config_file(
21+
cmake/installation/PHPConfig.cmake.in
22+
PHPConfig.cmake
23+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PHP
24+
PATH_VARS
25+
INSTALL_INCLUDE_DIR
26+
)
27+
28+
add_library(php_development INTERFACE)
29+
add_library(PHP::Development ALIAS php_development)
30+
set_target_properties(php_development PROPERTIES EXPORT_NAME Development)
31+
target_include_directories(
32+
php_development
33+
INTERFACE
34+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
35+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/main>
36+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Zend>
37+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/TSRM>
38+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}>
39+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/main>
40+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/Zend>
41+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/TSRM>
42+
)
43+
set_target_properties(php_development PROPERTIES EXPORT_NAME Development)
44+
45+
install(
46+
TARGETS php_development
47+
EXPORT PHP::Development
48+
)
49+
50+
install(
51+
EXPORT PHP::Development
52+
FILE PHP_Development.cmake
53+
NAMESPACE PHP::
54+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PHP
55+
COMPONENT PHP::Development
56+
)
57+
58+
install(
59+
FILES
60+
${CMAKE_CURRENT_BINARY_DIR}/PHPConfig.cmake
61+
${CMAKE_CURRENT_BINARY_DIR}/PHPConfigVersion.cmake
62+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PHP
63+
COMPONENT PHP::Development
64+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
if(CMAKE_VERSION VERSION_LESS 3.25)
2+
set(
3+
${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
4+
"PHP ${${CMAKE_FIND_PACKAGE_NAME}_VERSION} requires CMake 3.25 or later."
5+
)
6+
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
7+
return()
8+
endif()
9+
10+
cmake_minimum_required(VERSION 3.25...3.31)
11+
12+
# Set PHP components.
13+
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
14+
set(
15+
${CMAKE_FIND_PACKAGE_NAME}_components
16+
${${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS}
17+
)
18+
else()
19+
# No components given, look for default components.
20+
set(${CMAKE_FIND_PACKAGE_NAME}_components Interpreter)
21+
endif()
22+
23+
# Ensure all required components are available before trying to load any.
24+
foreach(component IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_components)
25+
if(
26+
${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED_${component}
27+
AND NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/PHP_${component}.cmake
28+
)
29+
set(
30+
${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
31+
"PHP doesn't have the required component installed: ${component}"
32+
)
33+
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
34+
35+
return()
36+
endif()
37+
endforeach()
38+
39+
foreach(component IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_components)
40+
# All required components are known to exist. The OPTIONAL keyword allows the
41+
# non-required components to be missing without error.
42+
include(${CMAKE_CURRENT_LIST_DIR}/PHP_${component}.cmake OPTIONAL)
43+
endforeach()
44+
45+
@PACKAGE_INIT@
46+
47+
set_and_check(PHP_INCLUDE_DIR "@PACKAGE_INSTALL_INCLUDE_DIR@")
48+
49+
check_required_components(${CMAKE_FIND_PACKAGE_NAME})

cmake/cmake/modules/FindPHP.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ Run application executable:
349349

350350
cmake_minimum_required(VERSION 3.25...3.31)
351351

352+
if(
353+
CMAKE_PARENT_LIST_FILE STREQUAL "CMakeLists.txt"
354+
AND PROJECT_NAME MATCHES "^php_ext_.+"
355+
)
356+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/modules)
357+
endif()
358+
352359
include(FeatureSummary)
353360
include(FindPackageHandleStandardArgs)
354361

cmake/ext/bcmath/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Enable the extension.
2020
Build extension as shared.
2121
#]=============================================================================]
2222

23+
cmake_minimum_required(VERSION 3.25...3.31)
24+
2325
project(
2426
PhpExtensionBcMath
2527
LANGUAGES C
2628
)
2729

30+
find_package(PHP COMPONENTS Development)
31+
2832
include(CMakeDependentOption)
2933
include(FeatureSummary)
3034

@@ -84,6 +88,8 @@ target_sources(
8488

8589
target_compile_definitions(php_ext_bcmath PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE)
8690

91+
target_link_libraries(php_ext_bcmath PRIVATE PHP::Development)
92+
8793
set(HAVE_BCMATH TRUE)
8894

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

cmake/ext/intl/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Enable the extension.
2121
Build extension as shared.
2222
#]=============================================================================]
2323

24+
cmake_minimum_required(VERSION 3.25...3.31)
25+
2426
project(
2527
PhpExtensionIntl
2628
LANGUAGES C

cmake/ext/skeleton/CMakeLists.txt.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ target_compile_definitions(
115115
)
116116

117117
# Find PHP on the system.
118-
find_package(PHP REQUIRED)
118+
find_package(PHP REQUIRED COMPONENTS Development)
119119

120120
# Link the PHP::Development imported library which provides the PHP include
121121
# directories and configuration for the extension.

cmake/main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ install(
415415
ARCHIVE EXCLUDE_FROM_ALL
416416
FILE_SET HEADERS
417417
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/main
418+
COMPONENT PHP::Development
418419
FILE_SET generated
419420
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/main
421+
COMPONENT PHP::Development
420422
)

0 commit comments

Comments
 (0)