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

+4-8
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

+3
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

+1
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)
+64
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+
)
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

+7
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

+6
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

+2
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

+1-1
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

+2
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
)

cmake/sapi/apache2handler/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ set_package_properties(
7878
target_link_libraries(
7979
php_sapi_apache2handler
8080
PRIVATE
81-
PHP::sapi
81+
$<BUILD_INTERFACE:PHP::sapi>
8282
Apache::Apache
8383
)
8484

cmake/sapi/cgi/CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ target_compile_definitions(php_sapi_cgi PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE)
4141
target_link_libraries(
4242
php_sapi_cgi
4343
PRIVATE
44-
PHP::sapi
44+
$<BUILD_INTERFACE:PHP::sapi>
4545
$<$<PLATFORM_ID:Windows>:ws2_32;kernel32;advapi32>
4646
)
4747

@@ -55,8 +55,7 @@ set_target_properties(
5555
php_sapi_cgi
5656
PROPERTIES
5757
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php-cgi${PHP_PROGRAM_SUFFIX}
58-
# TODO: Check if there's a better solution here:
59-
ENABLE_EXPORTS TRUE
58+
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
6059
PHP_CLI TRUE
6160
)
6261

cmake/sapi/cli/CMakeLists.txt

+57-24
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ if(NOT PHP_SAPI_CLI)
5252
return()
5353
endif()
5454

55+
################################################################################
56+
# Configuration checks.
57+
################################################################################
58+
5559
check_symbol_exists(setproctitle "unistd.h;stdlib.h" HAVE_SETPROCTITLE)
5660
check_include_file(sys/pstat.h HAVE_SYS_PSTAT_H)
5761

@@ -74,6 +78,10 @@ else()
7478
message(CHECK_FAIL "no")
7579
endif()
7680

81+
################################################################################
82+
# The cli SAPI.
83+
################################################################################
84+
7785
add_executable(php_sapi_cli)
7886
add_executable(PHP::sapi::cli ALIAS php_sapi_cli)
7987

@@ -102,7 +110,7 @@ target_compile_definitions(
102110
target_link_libraries(
103111
php_sapi_cli
104112
PRIVATE
105-
PHP::sapi
113+
$<BUILD_INTERFACE:PHP::sapi>
106114
$<$<PLATFORM_ID:Windows>:ws2_32;shell32>
107115
)
108116

@@ -116,8 +124,8 @@ set_target_properties(
116124
php_sapi_cli
117125
PROPERTIES
118126
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}
119-
# TODO: Check if there's a better solution here:
120-
ENABLE_EXPORTS TRUE
127+
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
128+
EXPORT_NAME Interpreter
121129
PHP_CLI TRUE
122130
)
123131

@@ -130,6 +138,42 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
130138
)
131139
endif()
132140

141+
# Man documentation.
142+
block()
143+
set(program_prefix "${PHP_PROGRAM_PREFIX}")
144+
configure_file(php.1.in php.1 @ONLY)
145+
endblock()
146+
147+
install(
148+
TARGETS php_sapi_cli
149+
EXPORT PHP::Interpreter
150+
RUNTIME
151+
DESTINATION ${CMAKE_INSTALL_BINDIR}
152+
COMPONENT PHP::Interpreter
153+
FILE_SET HEADERS
154+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/sapi/cli
155+
COMPONENT PHP::Interpreter
156+
)
157+
158+
install(
159+
EXPORT PHP::Interpreter
160+
FILE PHP_Interpreter.cmake
161+
NAMESPACE PHP::
162+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PHP
163+
COMPONENT PHP::Interpreter
164+
)
165+
166+
install(
167+
FILES ${CMAKE_CURRENT_BINARY_DIR}/php.1
168+
RENAME ${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}.1
169+
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1
170+
COMPONENT PHP::Interpreter
171+
)
172+
173+
################################################################################
174+
# The cli SAPI without console on Windows.
175+
################################################################################
176+
133177
if(PHP_SAPI_CLI_WIN_NO_CONSOLE)
134178
add_executable(php_sapi_cli_win)
135179
add_executable(PHP::sapi::cli_win ALIAS php_sapi_cli_win)
@@ -147,6 +191,7 @@ if(PHP_SAPI_CLI_WIN_NO_CONSOLE)
147191
php_sapi_cli_win
148192
PROPERTIES
149193
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php-win${PHP_PROGRAM_SUFFIX}
194+
EXPORT_NAME InterpreterNoConsole
150195
)
151196

152197
target_compile_definitions(
@@ -158,7 +203,7 @@ if(PHP_SAPI_CLI_WIN_NO_CONSOLE)
158203
target_link_libraries(
159204
php_sapi_cli_win
160205
PRIVATE
161-
PHP::sapi
206+
$<BUILD_INTERFACE:PHP::sapi>
162207
shell32
163208
)
164209

@@ -167,26 +212,14 @@ if(PHP_SAPI_CLI_WIN_NO_CONSOLE)
167212
PRIVATE
168213
/stack:67108864
169214
)
170-
endif()
171-
172-
# Man documentation.
173-
block()
174-
set(program_prefix "${PHP_PROGRAM_PREFIX}")
175-
configure_file(php.1.in php.1 @ONLY)
176-
endblock()
177-
178-
install(
179-
TARGETS php_sapi_cli
180-
RUNTIME
181-
DESTINATION ${CMAKE_INSTALL_BINDIR}
182-
FILE_SET HEADERS
183-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/sapi/cli
184-
)
185215

186-
install(
187-
FILES ${CMAKE_CURRENT_BINARY_DIR}/php.1
188-
RENAME ${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}.1
189-
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1
190-
)
216+
install(
217+
TARGETS php_sapi_cli_win
218+
EXPORT PHP::Interpreter
219+
RUNTIME
220+
DESTINATION ${CMAKE_INSTALL_BINDIR}
221+
COMPONENT PHP::Interpreter
222+
)
223+
endif()
191224

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

0 commit comments

Comments
 (0)