|
| 1 | +#[=============================================================================[ |
| 2 | +Set a CACHE variable that depends on a set of conditions. |
| 3 | +
|
| 4 | +In CMake there are 3 main ways to create non-internal cache variables that can |
| 5 | +be also customized using the `-D` command-line option, through CMake presets, or |
| 6 | +similar: |
| 7 | +* `option()` |
| 8 | +* `set(<variable> <value> CACHE <type> <docstring>)` |
| 9 | +* `cmake_dependent_option()` |
| 10 | +
|
| 11 | +Ideally, these are the recommended ways to set configuration variables. However, |
| 12 | +there are many cases where a `CACHE` variable of a type other than `BOOL` |
| 13 | +depends on certain conditions. Additionally, an edge-case issue with |
| 14 | +`cmake_dependent_option()` is that it sets a local variable if the conditions |
| 15 | +are not met. Local variables in such edge cases can be difficult to work with |
| 16 | +when using `add_subdirectory()`. In the parent scope, instead of the local |
| 17 | +variable with a forced value, the cached variable is still defined as |
| 18 | +`INTERNAL`, which can lead to bugs in the build process. |
| 19 | +
|
| 20 | +This module exposes the following function: |
| 21 | +
|
| 22 | +```cmake |
| 23 | +php_set( |
| 24 | + <variable> |
| 25 | + <default> |
| 26 | + CACHE <type> |
| 27 | + [STRINGS <string>...] |
| 28 | + [DOC <docstring>...] |
| 29 | + IF <condition> |
| 30 | + FORCED <forced> |
| 31 | +) |
| 32 | +``` |
| 33 | +
|
| 34 | +It sets the given CACHE `<variable>` of `<type>` to a `<value>` if `<condition>` |
| 35 | +is met. Otherwise it sets the `<variable>` to `<default>` value and hides it in |
| 36 | +the GUI. |
| 37 | +
|
| 38 | +* The `CACHE` `<type>` can be `BOOL`, `FILEPATH`, `PATH`, or `STRING`. |
| 39 | +
|
| 40 | +* `STRINGS` is an optional list of items when `CACHE` `STRING` is used to create |
| 41 | + a list of supported options to pick in the GUI. |
| 42 | +
|
| 43 | +* `DOC` is a short variable help text visible in the GUIs. Multiple strings are |
| 44 | + joined together. |
| 45 | +
|
| 46 | +* `IF` behaves the same as the `<depends>` argument in |
| 47 | + `cmake_dependent_option()`. If conditions `<condition>` are met, the variable |
| 48 | + is set to `<default>` value. Otherwise, it is set to `<forced>` value and |
| 49 | + hidden in the GUIs. This supports both full condition sytanx and |
| 50 | + semicolon-separated list of conditions. |
| 51 | +
|
| 52 | +* `FORCED` is a value that is set when `IF <conditions>` are not met. |
| 53 | +#]=============================================================================] |
| 54 | + |
| 55 | +include_guard(GLOBAL) |
| 56 | + |
| 57 | +function(php_set) |
| 58 | + cmake_parse_arguments( |
| 59 | + PARSE_ARGV |
| 60 | + 2 |
| 61 | + parsed # prefix |
| 62 | + "" # options |
| 63 | + "CACHE;IF" # one-value keywords |
| 64 | + "STRINGS;DOC;FORCED" # multi-value keywords |
| 65 | + ) |
| 66 | + |
| 67 | + if(parsed_UNPARSED_ARGUMENTS) |
| 68 | + message(FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS}") |
| 69 | + endif() |
| 70 | + |
| 71 | + if(NOT parsed_CACHE) |
| 72 | + message(FATAL_ERROR "Missing CACHE type argument") |
| 73 | + elseif(NOT parsed_CACHE MATCHES "^(BOOL|FILEPATH|PATH|STRING)$") |
| 74 | + message(FATAL_ERROR "Unknown CACHE type argument: ${parsed_CACHE}") |
| 75 | + endif() |
| 76 | + |
| 77 | + if(NOT parsed_IF) |
| 78 | + message(FATAL_ERROR "Missing IF argument with condition") |
| 79 | + endif() |
| 80 | + |
| 81 | + if(NOT DEFINED parsed_FORCED) |
| 82 | + message(FATAL_ERROR "Missing FORCED argument") |
| 83 | + endif() |
| 84 | + |
| 85 | + set(doc "") |
| 86 | + foreach(string ${parsed_DOC}) |
| 87 | + string(APPEND doc "${string}") |
| 88 | + endforeach() |
| 89 | + |
| 90 | + set(condition TRUE) |
| 91 | + foreach(d ${parsed_IF}) |
| 92 | + cmake_language(EVAL CODE " |
| 93 | + if(${d}) |
| 94 | + else() |
| 95 | + set(condition FALSE) |
| 96 | + endif()" |
| 97 | + ) |
| 98 | + endforeach() |
| 99 | + |
| 100 | + set(var "${ARGV0}") |
| 101 | + set(internal ___PHP_SET_${var}) |
| 102 | + |
| 103 | + if(NOT DEFINED ${internal} AND DEFINED ${var}) |
| 104 | + set(${internal} "${${var}}" CACHE INTERNAL "Internal storage for ${var}") |
| 105 | + elseif(NOT DEFINED ${internal}) |
| 106 | + set(${internal} "${ARGV1}" CACHE INTERNAL "Internal storage for ${var}") |
| 107 | + elseif(DEFINED ${internal} AND NOT ${internal}_FORCED) |
| 108 | + set(${internal} "${${var}}" CACHE INTERNAL "Internal storage for ${var}") |
| 109 | + endif() |
| 110 | + |
| 111 | + if(condition) |
| 112 | + set(${var} "${${internal}}" CACHE ${parsed_CACHE} "${doc}" FORCE) |
| 113 | + if(parsed_CACHE STREQUAL "STRING" AND parsed_STRINGS) |
| 114 | + set_property(CACHE ${var} PROPERTY STRINGS ${parsed_STRINGS}) |
| 115 | + endif() |
| 116 | + unset(${internal} CACHE) |
| 117 | + unset(${internal}_FORCED CACHE) |
| 118 | + else() |
| 119 | + set(${var} "${parsed_FORCED}" CACHE INTERNAL "${doc}") |
| 120 | + set( |
| 121 | + ${internal}_FORCED |
| 122 | + TRUE |
| 123 | + CACHE INTERNAL |
| 124 | + "Internal marker that ${var} has a forced value." |
| 125 | + ) |
| 126 | + endif() |
| 127 | +endfunction() |
0 commit comments