|
| 1 | +# Declarative helpers that replace the repetitive option / source-URL / feature |
| 2 | +# / dependency if-chains in the top-level CMakeLists.txt with one readable line |
| 3 | +# each. Behavior is intentionally identical to the hand-written blocks they |
| 4 | +# supersede; they only remove boilerplate. |
| 5 | + |
| 6 | +# A user-facing cache knob, declared with one uniform form instead of mixing |
| 7 | +# option() (booleans) with set(... CACHE STRING) (string values). |
| 8 | +# photon_option(<name> BOOL|STRING <default> <help>) |
| 9 | +# option(X "help" OFF) is exactly set(X OFF CACHE BOOL "help"), so BOOL knobs |
| 10 | +# behave identically to the option() they replace. |
| 11 | +macro(photon_option name type default help) |
| 12 | + set(${name} ${default} CACHE ${type} "${help}") |
| 13 | +endmacro () |
| 14 | + |
| 15 | +# A build-from-source download location, collapsing the repetitive |
| 16 | +# set(PHOTON_..._SOURCE "url" CACHE STRING "") table into one line each. |
| 17 | +# <key> carries the suffix (SOURCE or GIT) so the resulting cache variable name |
| 18 | +# (PHOTON_<key>) matches the existing convention. |
| 19 | +macro(photon_source key url) |
| 20 | + set(PHOTON_${key} "${url}" CACHE STRING "") |
| 21 | +endmacro () |
| 22 | + |
| 23 | +# Append <name> to the `dependencies` list. Any trailing arguments form a |
| 24 | +# condition evaluated by if(); with no condition the dependency is |
| 25 | +# unconditional. Keeps each dependency on a single declarative line with its |
| 26 | +# enabling condition inline, in place of a long if() / list(APPEND) chain. |
| 27 | +macro(photon_dependency name) |
| 28 | + if (${ARGC} GREATER 1) |
| 29 | + if (${ARGN}) |
| 30 | + list(APPEND dependencies ${name}) |
| 31 | + endif () |
| 32 | + else () |
| 33 | + list(APPEND dependencies ${name}) |
| 34 | + endif () |
| 35 | +endmacro () |
| 36 | + |
| 37 | +# target_compile_definitions(photon_features INTERFACE <defs>), guarded by a |
| 38 | +# condition: |
| 39 | +# photon_feature(IF <cond...> DEFS <defs...>) |
| 40 | +# <cond...> is any expression if() accepts; the definitions are added only when |
| 41 | +# it holds. Mirrors the per-feature if() blocks with one line each. |
| 42 | +function(photon_feature) |
| 43 | + cmake_parse_arguments(_pf "" "" "IF;DEFS" ${ARGN}) |
| 44 | + if (${_pf_IF}) |
| 45 | + target_compile_definitions(photon_features INTERFACE ${_pf_DEFS}) |
| 46 | + endif () |
| 47 | +endfunction () |
| 48 | + |
| 49 | +# list(APPEND <list> <items...>), guarded by a condition: |
| 50 | +# photon_append_if(<list> IF <cond...> ITEMS <items...>) |
| 51 | +# The items (possibly empty, e.g. an unset *_LIBRARIES) are appended only when |
| 52 | +# <cond...> holds. Replaces the if() / list(APPEND) blocks that assemble the |
| 53 | +# link-time dependency lists while preserving their append order. |
| 54 | +function(photon_append_if listvar) |
| 55 | + cmake_parse_arguments(_pa "" "" "IF;ITEMS" ${ARGN}) |
| 56 | + if (${_pa_IF}) |
| 57 | + list(APPEND ${listvar} ${_pa_ITEMS}) |
| 58 | + set(${listvar} "${${listvar}}" PARENT_SCOPE) |
| 59 | + endif () |
| 60 | +endfunction () |
| 61 | + |
| 62 | +# list(REMOVE_ITEM <list> <items...>), guarded by a condition -- the opt-out |
| 63 | +# counterpart of photon_append_if: |
| 64 | +# photon_remove_if(<list> IF <cond...> ITEMS <items...>) |
| 65 | +# Used where a file is globbed in by default and dropped when a feature is off. |
| 66 | +function(photon_remove_if listvar) |
| 67 | + cmake_parse_arguments(_pr "" "" "IF;ITEMS" ${ARGN}) |
| 68 | + if (${_pr_IF}) |
| 69 | + list(REMOVE_ITEM ${listvar} ${_pr_ITEMS}) |
| 70 | + set(${listvar} "${${listvar}}" PARENT_SCOPE) |
| 71 | + endif () |
| 72 | +endfunction () |
0 commit comments