|
| 1 | +#[=============================================================================[ |
| 2 | +# FindPHP |
| 3 | +
|
| 4 | +Find PHP on the system, if installed. |
| 5 | +
|
| 6 | +## Result variables |
| 7 | +
|
| 8 | +* `PHP_FOUND` - Whether the package has been found. |
| 9 | +* `PHP_VERSION` - Package version, if found. |
| 10 | +
|
| 11 | +## Cache variables |
| 12 | +
|
| 13 | +* `PHP_EXECUTABLE` - PHP command-line executable, if available. |
| 14 | +#]=============================================================================] |
| 15 | + |
| 16 | +include(FeatureSummary) |
| 17 | +include(FindPackageHandleStandardArgs) |
| 18 | + |
| 19 | +set_package_properties( |
| 20 | + PHP |
| 21 | + PROPERTIES |
| 22 | + URL "https://www.php.net" |
| 23 | + DESCRIPTION "PHP: Hypertext Preprocessor" |
| 24 | +) |
| 25 | + |
| 26 | +set(_phpRequiredVars PHP_EXECUTABLE) |
| 27 | +set(_reason "") |
| 28 | + |
| 29 | +find_program( |
| 30 | + PHP_EXECUTABLE |
| 31 | + NAMES php |
| 32 | + DOC "The path to the PHP executable" |
| 33 | +) |
| 34 | +mark_as_advanced(PHP_EXECUTABLE) |
| 35 | + |
| 36 | +if(NOT PHP_EXECUTABLE) |
| 37 | + string(APPEND _reason "The php command-line executable could not be found. ") |
| 38 | +endif() |
| 39 | + |
| 40 | +unset(PHP_VERSION) |
| 41 | +block(PROPAGATE PHP_VERSION _reason _phpRequiredVars) |
| 42 | + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.29) |
| 43 | + set(test IS_EXECUTABLE) |
| 44 | + else() |
| 45 | + set(test EXISTS) |
| 46 | + endif() |
| 47 | + |
| 48 | + if(${test} ${PHP_EXECUTABLE}) |
| 49 | + list(APPEND _phpRequiredVars PHP_VERSION) |
| 50 | + |
| 51 | + execute_process( |
| 52 | + COMMAND ${PHP_EXECUTABLE} --version |
| 53 | + OUTPUT_VARIABLE version |
| 54 | + RESULT_VARIABLE result |
| 55 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 56 | + ERROR_QUIET |
| 57 | + ) |
| 58 | + |
| 59 | + if(NOT result EQUAL 0) |
| 60 | + string(APPEND _reason "Command '${PHP_EXECUTABLE} --version' failed. ") |
| 61 | + elseif(version MATCHES "PHP ([0-9]+[0-9.]+[^ ]+) \\(cli\\)") |
| 62 | + set(PHP_VERSION "${CMAKE_MATCH_1}") |
| 63 | + else() |
| 64 | + string(APPEND _reason "Invalid version format. ") |
| 65 | + endif() |
| 66 | + endif() |
| 67 | +endblock() |
| 68 | + |
| 69 | +find_package_handle_standard_args( |
| 70 | + PHP |
| 71 | + REQUIRED_VARS ${_phpRequiredVars} |
| 72 | + VERSION_VAR PHP_VERSION |
| 73 | + HANDLE_VERSION_RANGE |
| 74 | + REASON_FAILURE_MESSAGE "${_reason}" |
| 75 | +) |
| 76 | + |
| 77 | +unset(_phpRequiredVars) |
| 78 | +unset(_reason) |
0 commit comments