|
| 1 | +# See LICENSE_ENZO file for license and copyright information |
| 2 | + |
| 3 | +#.rst: |
| 4 | +# GenericOptionCommand |
| 5 | +# -------------------- |
| 6 | +# |
| 7 | +# Defines a command, ``generic_option``, that acts like a more general form of |
| 8 | +# the built-in ``option`` command. |
| 9 | +# |
| 10 | +# In slightly more detail, it provides a variable that the user can optionally |
| 11 | +# specify:: |
| 12 | +# |
| 13 | +# generic_option(<variable> <type> <help_text> |
| 14 | +# DEFAULT_VALUE <default_value> |
| 15 | +# [FORBID_EXISTING_NONCACHE_VAR]) |
| 16 | +# |
| 17 | +# Similar to the ``option`` command, the default behavior is to |
| 18 | +# - do nothing if ``<variable>`` is already defined (as a normal or cache |
| 19 | +# variable). |
| 20 | +# - initialize ``<variable>`` as a cache variable of type ``TYPE`` |
| 21 | +# with the specified ``<default_value>``. If ``DEFAULT_VALUE`` is not |
| 22 | +# specified, then the default is ``OFF`` for a ``BOOL`` variable and |
| 23 | +# ``""`` in other cases. |
| 24 | +# |
| 25 | +# When the ``FORBID_EXISTING_NONCACHE_VAR`` option is specified, this command |
| 26 | +# will immediately abort cmake with an error if ``<variable>`` is already |
| 27 | +# exists as a non-cache variable. NOTE: if it exists as a cache variable and as |
| 28 | +# a normal variable that is okay (there isn't any way to identify this case |
| 29 | +# anyways) |
| 30 | + |
| 31 | +if(__GenericOptionCommand) |
| 32 | + return() |
| 33 | +endif() |
| 34 | +set(__GenericOptionCommand YES) |
| 35 | + |
| 36 | + |
| 37 | +function(generic_option variable type help_text) |
| 38 | + |
| 39 | + # on the off-chance that the variable-name matches a value used in this |
| 40 | + # function, let's check if it already exists (BEFORE DEFINING ANY VARIABLES) |
| 41 | + if (DEFINED "${variable}") |
| 42 | + set(is_already_defined TRUE) |
| 43 | + else() |
| 44 | + set(is_already_defined FALSE) |
| 45 | + endif() |
| 46 | + |
| 47 | + # parse args |
| 48 | + set(options FORBID_EXISTING_NONCACHE_VAR) |
| 49 | + set(oneValueArgs DEFAULT_VALUE) |
| 50 | + set(multiValueArgs "") |
| 51 | + cmake_parse_arguments(PARSE_ARGV 3 __GENERIC_OPTION "${options}" "${oneValueArgs}" |
| 52 | + "${multiValueArgs}" )#${ARGN} ) |
| 53 | + |
| 54 | + # some basic error-handling |
| 55 | + set(_funcname "generic_option") |
| 56 | + if (DEFINED __GENERIC_OPTION_UNPARSED_ARGUMENTS) |
| 57 | + message(FATAL_ERROR |
| 58 | + "${_funcname} recieved invalid arguments: " |
| 59 | + "\"${__GENERIC_OPTION_UNPARSED_ARGUMENTS}\"") |
| 60 | + elseif (DEFINED __GENERIC_OPTION_KEYWORDS_MISSING_VALUES) |
| 61 | + message(FATAL_ERROR |
| 62 | + "${_funcname} received the ${__GENERIC_OPTION_KEYWORDS_MISSING_VALUES} " |
| 63 | + "keyword(s) without any associated arguments.") |
| 64 | + elseif ("${type}" STREQUAL "INTERNAL") |
| 65 | + message(FATAL_ERROR "${_funcname} does not support the INTERNAL type") |
| 66 | + elseif ("${type}" MATCHES "^PATH|FILEPATH$") |
| 67 | + # these types have some weird handling related to relative-paths |
| 68 | + message(FATAL_ERROR |
| 69 | + "${_funcname} doesn't currently support the ${type} type") |
| 70 | + elseif (NOT "${type}" MATCHES "^BOOL|STRING$") |
| 71 | + message(FATAL_ERROR "invalid type passed to ${_funcname}") |
| 72 | + endif() |
| 73 | + |
| 74 | + if ("${__GENERIC_OPTION_FORBID_EXISTING_NONCACHE_VAR}" AND |
| 75 | + "${is_already_defined}" AND (NOT DEFINED CACHE{${variable}})) |
| 76 | + # by trial and error, the output format now seems nicer (not sure why we |
| 77 | + # need to escape some line-breaks but not others) |
| 78 | + message(FATAL_ERROR "\ |
| 79 | +The \"${variable}\" variable is expressly forbidden from being defined as a \ |
| 80 | +regular variable before the appropriate call to ${_funcname}. \ |
| 81 | +You probably made the mistake of writing some cmake-code with ``set(...)`` \ |
| 82 | +somewhere to define the variable. |
| 83 | + -> if you are a user, you should assign a value to the variable on the |
| 84 | + the command-line with the -D<var>=<value> flag or by modifying values |
| 85 | + in the CMakeCache.txt file (by modifying it directly OR using some kind |
| 86 | + of gui-program) |
| 87 | + -> if you are a developer, you are probably trying to assign a default value |
| 88 | + to \"${variable}\". In this case, you should prefer to specify the |
| 89 | + initial-value to ${_func_name} by using the DEFAULT_VALUE keyword. If you |
| 90 | + instead choose to refactor, keep in mind that it's a little tricky to get |
| 91 | + this \"right\" (it can be tricky to allow users to override the value)") |
| 92 | + endif() |
| 93 | + |
| 94 | + cmake_policy(GET CMP0077 _CMP0077_val) |
| 95 | + if ("${_CMP0077_val}" STREQUAL "OLD") |
| 96 | + message(FATAL_ERROR "${_func_name} needs NEW behavior of CMP0077") |
| 97 | + endif() |
| 98 | + |
| 99 | + if (NOT "${is_already_defined}") |
| 100 | + if (DEFINED __GENERIC_OPTION_DEFAULT_VALUE) |
| 101 | + set("${variable}" "${__GENERIC_OPTION_DEFAULT_VALUE}" CACHE "${type}" |
| 102 | + "${help_text}") |
| 103 | + elseif("${type}" STREQUAL "BOOL") |
| 104 | + set("${variable}" "OFF" CACHE BOOL "${help_text}") |
| 105 | + else() |
| 106 | + set("${variable}" "" CACHE "${type}" "${help_text}") |
| 107 | + endif() |
| 108 | + endif() |
| 109 | + |
| 110 | +endfunction() |
| 111 | + |
0 commit comments