CMakeDependentOption.cmake 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt 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. Usage:
  10. .. code-block:: cmake
  11. cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
  12. Where ``<option>`` is available to the user if ``<depends>`` is true. When
  13. ``<option>`` is available, the given ``<help_text>`` and initial ``<value>``
  14. are used. If the ``<depends>`` condition is not true, ``<option>`` will not be
  15. presented and will always have the value given by ``<force>``. Any value set by
  16. the user is preserved for when the option is presented again. Each element in
  17. the fourth parameter is evaluated as an if-condition, so
  18. :ref:`Condition Syntax` can be used.
  19. Example invocation:
  20. .. code-block:: cmake
  21. cmake_dependent_option(USE_FOO "Use Foo" ON
  22. "USE_BAR;NOT USE_ZOT" OFF)
  23. If ``USE_BAR`` is true and ``USE_ZOT`` is false, this provides an option called
  24. ``USE_FOO`` that defaults to ON. Otherwise, it sets ``USE_FOO`` to OFF and
  25. hides the option from the user. If the status of ``USE_BAR`` or ``USE_ZOT``
  26. ever changes, any value for the ``USE_FOO`` option is saved so that when the
  27. option is re-enabled it retains its old value.
  28. #]=======================================================================]
  29. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  30. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  31. set(${option}_AVAILABLE 1)
  32. foreach(d ${depends})
  33. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  34. if(${CMAKE_DEPENDENT_OPTION_DEP})
  35. else()
  36. set(${option}_AVAILABLE 0)
  37. endif()
  38. endforeach()
  39. if(${option}_AVAILABLE)
  40. option(${option} "${doc}" "${default}")
  41. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  42. else()
  43. if(${option} MATCHES "^${option}$")
  44. else()
  45. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  46. endif()
  47. set(${option} ${force})
  48. endif()
  49. else()
  50. set(${option} "${${option}_ISSET}")
  51. endif()
  52. endmacro()