CMakeDependentOption.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CMakeDependentOption
  5. --------------------
  6. Macro to provide an option dependent on other options.
  7. This macro presents an option to the user only if a set of other
  8. conditions are true.
  9. .. command:: cmake_dependent_option
  10. .. code-block:: cmake
  11. cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
  12. Makes ``<option>`` available to the user if the
  13. :ref:`semicolon-separated list <CMake Language Lists>` of conditions in
  14. ``<depends>`` are all true. Otherwise, a local variable named ``<option>``
  15. is set to ``<force>``.
  16. When ``<option>`` is available, the given ``<help_text>`` and initial
  17. ``<value>`` are used. Otherwise, any value set by the user is preserved for
  18. when ``<depends>`` is satisfied in the future.
  19. Note that the ``<option>`` variable only has a value which satisfies the
  20. ``<depends>`` condition within the scope of the caller because it is a local
  21. variable.
  22. .. versionadded:: 3.22
  23. Full :ref:`Condition Syntax` is now supported. See policy :policy:`CMP0127`.
  24. Examples
  25. ^^^^^^^^
  26. Semicolon-separated list of conditions:
  27. .. code-block:: cmake
  28. cmake_dependent_option(USE_FOO "Use Foo" ON "USE_BAR;NOT USE_ZOT" OFF)
  29. If ``USE_BAR`` is true and ``USE_ZOT`` is false, this provides an option called
  30. ``USE_FOO`` that defaults to ON. Otherwise, it sets ``USE_FOO`` to OFF and
  31. hides the option from the user. If the status of ``USE_BAR`` or ``USE_ZOT``
  32. ever changes, any value for the ``USE_FOO`` option is saved so that when the
  33. option is re-enabled it retains its old value.
  34. Full condition syntax:
  35. .. code-block:: cmake
  36. cmake_dependent_option(USE_FOO "Use Foo" ON "USE_A AND (USE_B OR USE_C)" OFF)
  37. Similar to the previous example, if the argument with full condition syntax
  38. evaluates to true, this provides an option called ``USE_FOO`` that defaults to
  39. ON. Otherwise, it sets ``USE_FOO`` to OFF and hides the option from the user in
  40. the GUI. When condition changes, option is saved in similar way as described
  41. above. This enables using entire condition syntax as being the ``if`` clause
  42. argument, such as grouping conditions with parens and similar.
  43. #]=======================================================================]
  44. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  45. cmake_policy(GET CMP0127 _CDO_CMP0127
  46. PARENT_SCOPE # undocumented, do not use outside of CMake
  47. )
  48. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  49. set(${option}_AVAILABLE 1)
  50. if("x${_CDO_CMP0127}x" STREQUAL "xNEWx")
  51. foreach(d ${depends})
  52. cmake_language(EVAL CODE "
  53. if (${d})
  54. else()
  55. set(${option}_AVAILABLE 0)
  56. endif()"
  57. )
  58. endforeach()
  59. else()
  60. foreach(d ${depends})
  61. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  62. if(${CMAKE_DEPENDENT_OPTION_DEP})
  63. else()
  64. set(${option}_AVAILABLE 0)
  65. endif()
  66. endforeach()
  67. endif()
  68. if(${option}_AVAILABLE)
  69. option(${option} "${doc}" "${default}")
  70. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  71. else()
  72. if(${option} MATCHES "^${option}$")
  73. else()
  74. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  75. endif()
  76. set(${option} ${force})
  77. endif()
  78. else()
  79. set(${option} "${${option}_ISSET}")
  80. endif()
  81. if("x${_CDO_CMP0127}x" STREQUAL "xx" AND "x${depends}x" MATCHES "[^A-Za-z0-9_.; ]")
  82. cmake_policy(GET_WARNING CMP0127 _CDO_CMP0127_WARNING)
  83. message(AUTHOR_WARNING "${_CDO_CMP0127_WARNING}")
  84. endif()
  85. unset(_CDO_CMP0127)
  86. endmacro()