Skip to content

Commit 2244de3

Browse files
committed
Add initial FindPHP module
This replaces the weird FindPHPSystem module with simpler FindPHP, which can be also extended in the future with components and other features for developing PHP extensions. Using the hint variable PHP_ARTIFACTS_PREFIX both project(PHP) and find_package(PHP) can coexist and provide result variables such as PHP_VERSION and PHP_<artifacts-prefix>_VERSION.
1 parent 8211b60 commit 2244de3

File tree

6 files changed

+167
-78
lines changed

6 files changed

+167
-78
lines changed

cmake/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ block()
164164
endblock()
165165

166166
# Rebuild all targets as needed.
167-
if(NOT PHPSystem_FOUND)
167+
if(NOT PHP_HOST_FOUND)
168168
include(cmake/Rebuild.cmake)
169169
endif()
170170

cmake/cmake/Requirements.cmake

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ endif()
6363
find_package(Sendmail)
6464

6565
################################################################################
66-
# Find PHP installed on the system for generating stub files (*_arginfo.h),
67-
# Zend/zend_vm_gen.php, and similar where it can be used. Otherwise the built
68-
# cli SAPI is used at the build phase. Minimum supported version for
69-
# gen_stub.php is PHP 7.4.
66+
# Find PHP installed on the system and set PHP_HOST_EXECUTABLE for development
67+
# such as generating stubs (*_arginfo.h) with build/gen_stub.php, running PHP
68+
# scripts Zend/zend_vm_gen.php and similar. Otherwise the sapi/cli executable
69+
# will be used at the build phase, where possible. The minimum version should
70+
# match the version required to run these PHP scripts.
7071
################################################################################
71-
find_package(PHPSystem 7.4)
72+
set(PHP_ARTIFACTS_PREFIX "_HOST")
73+
find_package(PHP 7.4)
74+
unset(PHP_ARTIFACTS_PREFIX)

cmake/cmake/modules/FindPHP.cmake

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#[=============================================================================[
2+
# FindPHP
3+
4+
Finds PHP, the general-purpose scripting language:
5+
6+
```cmake
7+
find_package(PHP [<version>] [...])
8+
```
9+
10+
## Result variables
11+
12+
This module defines the following variables:
13+
14+
* `PHP_FOUND` - Boolean indicating whether (the requested version of) package
15+
was found.
16+
* `PHP_VERSION` - The version of package found.
17+
18+
## Cache variables
19+
20+
The following cache variables may also be set:
21+
22+
* `PHP_EXECUTABLE` - PHP command-line tool, if available.
23+
24+
## Hints
25+
26+
* `PHP_ARTIFACTS_PREFIX` - A prefix that will be used for all result and cache
27+
variables.
28+
29+
To comply with standard find modules, the `PHP_FOUND` result variable is also
30+
defined, even if prefix has been specified.
31+
32+
## Examples
33+
34+
### Example: Finding PHP
35+
36+
```cmake
37+
# CMakeLists.txt
38+
39+
find_package(PHP)
40+
41+
if(PHP_FOUND)
42+
message(STATUS "PHP_EXECUTABLE=${PHP_EXECUTABLE}")
43+
message(STATUS "PHP_VERSION=${PHP_VERSION}")
44+
endif()
45+
```
46+
47+
### Example: Using hint variables
48+
49+
Finding PHP on the host and prefixing the module result/cache variables:
50+
51+
```cmake
52+
set(PHP_ARTIFACTS_PREFIX "_HOST")
53+
find_package(PHP)
54+
unset(PHP_ARTIFACTS_PREFIX)
55+
56+
if(PHP_HOST_FOUND)
57+
message(STATUS "PHP_HOST_EXECUTABLE=${PHP_HOST_EXECUTABLE}")
58+
message(STATUS "PHP_HOST_VERSION=${PHP_HOST_VERSION}")
59+
endif()
60+
```
61+
#]=============================================================================]
62+
63+
cmake_minimum_required(VERSION 4.2...4.3)
64+
65+
include(FeatureSummary)
66+
include(FindPackageHandleStandardArgs)
67+
68+
##############################################################################
69+
# Configuration.
70+
##############################################################################
71+
72+
set_package_properties(
73+
PHP
74+
PROPERTIES
75+
URL "https://www.php.net"
76+
DESCRIPTION "PHP: Hypertext Preprocessor"
77+
)
78+
79+
if(PHP_ARTIFACTS_PREFIX)
80+
set(_php_prefix "${PHP_ARTIFACTS_PREFIX}")
81+
else()
82+
set(_php_prefix "")
83+
endif()
84+
85+
block(
86+
PROPAGATE
87+
PHP_FOUND
88+
PHP${_php_prefix}_FOUND
89+
PHP${_php_prefix}_VERSION
90+
)
91+
set(reason "")
92+
93+
##############################################################################
94+
# Find the PHP executable.
95+
##############################################################################
96+
97+
find_program(
98+
PHP${_php_prefix}_EXECUTABLE
99+
NAMES php
100+
DOC "Path to the PHP executable"
101+
)
102+
mark_as_advanced(PHP${_php_prefix}_EXECUTABLE)
103+
104+
if(NOT PHP${_php_prefix}_EXECUTABLE)
105+
string(APPEND reason "The php command-line executable not found. ")
106+
endif()
107+
108+
##############################################################################
109+
# Check version.
110+
##############################################################################
111+
112+
if(IS_EXECUTABLE "${PHP${_php_prefix}_EXECUTABLE}")
113+
execute_process(
114+
COMMAND "${PHP${_php_prefix}_EXECUTABLE}" --version
115+
OUTPUT_VARIABLE version
116+
RESULT_VARIABLE result
117+
ERROR_QUIET
118+
OUTPUT_STRIP_TRAILING_WHITESPACE
119+
)
120+
121+
if(NOT result EQUAL 0)
122+
string(
123+
APPEND
124+
reason
125+
"Command '${PHP${_php_prefix}_EXECUTABLE} --version' failed. "
126+
)
127+
elseif(version MATCHES "PHP ([^ ]+) ")
128+
set(PHP${_php_prefix}_VERSION "${CMAKE_MATCH_1}")
129+
else()
130+
string(APPEND reason "Invalid version format. ")
131+
endif()
132+
endif()
133+
134+
##############################################################################
135+
# Handle result.
136+
##############################################################################
137+
138+
find_package_handle_standard_args(
139+
PHP
140+
REQUIRED_VARS
141+
PHP${_php_prefix}_EXECUTABLE
142+
VERSION_VAR PHP${_php_prefix}_VERSION
143+
HANDLE_VERSION_RANGE
144+
REASON_FAILURE_MESSAGE "${reason}"
145+
)
146+
147+
set(PHP${_php_prefix}_FOUND ${PHP_FOUND})
148+
endblock()
149+
150+
unset(_php_prefix)

cmake/cmake/modules/FindPHPSystem.cmake

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

cmake/cmake/modules/PHP/AddCustomCommand.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ function(php_add_custom_command)
8282
set(verbatim "")
8383
endif()
8484

85-
if(PHPSystem_FOUND)
85+
if(PHP_HOST_FOUND)
8686
add_custom_command(
8787
OUTPUT ${parsed_OUTPUT}
88-
COMMAND ${PHPSystem_EXECUTABLE} ${parsed_PHP_COMMAND}
88+
COMMAND ${PHP_HOST_EXECUTABLE} ${parsed_PHP_COMMAND}
8989
DEPENDS ${parsed_DEPENDS}
9090
COMMENT "${parsed_COMMENT}"
9191
${verbatim}

cmake/cmake/modules/PHP/Stubs.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function(_php_stubs_get_php_command result)
2222
# If PHP is not found on the system, the PHP cli SAPI will be used with the
2323
# tokenizer extension.
2424
if(
25-
NOT PHPSystem_FOUND
25+
NOT PHP_HOST_FOUND
2626
AND (
2727
NOT TARGET PHP::sapi::cli
2828
OR (TARGET PHP::sapi::cli AND NOT TARGET PHP::ext::tokenizer)
@@ -32,16 +32,16 @@ function(_php_stubs_get_php_command result)
3232
endif()
3333

3434
# If external PHP is available, check for the required tokenizer extension.
35-
if(PHPSystem_FOUND)
35+
if(PHP_HOST_FOUND)
3636
execute_process(
37-
COMMAND ${PHPSystem_EXECUTABLE} --ri tokenizer
37+
COMMAND ${PHP_HOST_EXECUTABLE} --ri tokenizer
3838
RESULT_VARIABLE code
3939
OUTPUT_QUIET
4040
ERROR_QUIET
4141
)
4242

4343
if(code EQUAL 0)
44-
set(${result} ${PHPSystem_EXECUTABLE})
44+
set(${result} ${PHP_HOST_EXECUTABLE})
4545
return(PROPAGATE ${result})
4646
endif()
4747
endif()
@@ -120,7 +120,7 @@ block()
120120
$<PATH:ABSOLUTE_PATH,NORMALIZE,$<LIST:FILTER,$<TARGET_PROPERTY:${target},SOURCES>,INCLUDE,\.stub\.php$>,$<TARGET_PROPERTY:${target},SOURCE_DIR>>
121121
)
122122

123-
if(PHPSystem_FOUND)
123+
if(PHP_HOST_FOUND)
124124
add_dependencies(${target} php_stubs)
125125
endif()
126126
endforeach()
@@ -135,7 +135,7 @@ block()
135135
)
136136

137137
set(target_options "")
138-
if(NOT PHPSystem_FOUND)
138+
if(NOT PHP_HOST_FOUND)
139139
set(target_options ALL DEPENDS ${targets})
140140
endif()
141141

0 commit comments

Comments
 (0)