|
| 1 | +#[=============================================================================[ |
| 2 | +Add subdirectories of PHP SAPIs via `add_subdirectory()`. |
| 3 | +
|
| 4 | +This module is responsible for traversing `CMakeLists.txt` files of PHP SAPIs |
| 5 | +and adding them via `add_subdirectory()`. |
| 6 | +
|
| 7 | +## Exposed macro |
| 8 | +
|
| 9 | +```cmake |
| 10 | +php_sapis_add(subdirectory) |
| 11 | +``` |
| 12 | +
|
| 13 | +## Custom CMake properties |
| 14 | +
|
| 15 | +* `PHP_ALL_SAPIS` |
| 16 | +
|
| 17 | + Global property with a list of all PHP SAPIs in the sapi directory. |
| 18 | +
|
| 19 | +* `PHP_SAPIS` |
| 20 | +
|
| 21 | + This global property contains a list of all enabled PHP SAPIs for the current |
| 22 | + configuration. |
| 23 | +#]=============================================================================] |
| 24 | + |
| 25 | +macro(php_sapis_add directory) |
| 26 | + _php_sapis_get(${directory} directories) |
| 27 | + |
| 28 | + # Add subdirectories of PHP SAPIs. |
| 29 | + foreach(dir ${directories}) |
| 30 | + cmake_path(GET dir FILENAME sapi) |
| 31 | + message(STATUS "Checking ${sapi} SAPI") |
| 32 | + list(APPEND CMAKE_MESSAGE_CONTEXT "sapi/${sapi}") |
| 33 | + unset(sapi) |
| 34 | + |
| 35 | + add_subdirectory("${dir}") |
| 36 | + |
| 37 | + list(POP_BACK CMAKE_MESSAGE_CONTEXT) |
| 38 | + |
| 39 | + _php_sapis_post_configure("${dir}") |
| 40 | + endforeach() |
| 41 | + |
| 42 | + _php_sapis_validate() |
| 43 | + |
| 44 | + unset(directories) |
| 45 | + unset(sapis) |
| 46 | +endmacro() |
| 47 | + |
| 48 | +# Get a list of subdirectories related to PHP SAPIs. |
| 49 | +function(_php_sapis_get directory result) |
| 50 | + file(GLOB paths ${directory}/*/CMakeLists.txt) |
| 51 | + |
| 52 | + set(directories "") |
| 53 | + |
| 54 | + foreach(path ${paths}) |
| 55 | + cmake_path(GET path PARENT_PATH dir) |
| 56 | + list(APPEND directories "${dir}") |
| 57 | + |
| 58 | + # Add SAPI name to a list of all SAPIs. |
| 59 | + cmake_path(GET dir FILENAME sapi) |
| 60 | + set_property(GLOBAL APPEND PROPERTY PHP_ALL_SAPIS ${module}) |
| 61 | + endforeach() |
| 62 | + |
| 63 | + set(${result} ${directories} PARENT_SCOPE) |
| 64 | +endfunction() |
| 65 | + |
| 66 | +# Configure SAPI after its CMakeLists.txt is added. |
| 67 | +function(_php_sapis_post_configure directory) |
| 68 | + cmake_path(GET directory FILENAME sapi) |
| 69 | + |
| 70 | + if(NOT TARGET php_${sapi}) |
| 71 | + return() |
| 72 | + endif() |
| 73 | + |
| 74 | + set_property(GLOBAL APPEND PROPERTY PHP_SAPIS ${sapi}) |
| 75 | + |
| 76 | + if(NOT TARGET PHP::${sapi}) |
| 77 | + get_target_property(type php_${sapi} TYPE) |
| 78 | + |
| 79 | + if(type STREQUAL "EXECUTABLE") |
| 80 | + add_executable(PHP::${sapi} ALIAS php_${sapi}) |
| 81 | + else() |
| 82 | + add_library(PHP::${sapi} ALIAS php_${sapi}) |
| 83 | + endif() |
| 84 | + endif() |
| 85 | +endfunction() |
| 86 | + |
| 87 | +# Check if at least one SAPI is enabled. |
| 88 | +function(_php_sapis_validate) |
| 89 | + get_cmake_property(sapis PHP_SAPIS) |
| 90 | + if(NOT sapis) |
| 91 | + message( |
| 92 | + WARNING |
| 93 | + "None of the PHP SAPIs have been enabled. If this is intentional, you " |
| 94 | + "can disregard this warning." |
| 95 | + ) |
| 96 | + endif() |
| 97 | +endfunction() |
0 commit comments