CMakeDependentOption.cmake 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. When the option is not presented a default value
  9. is used, but any value set by the user is preserved for when the
  10. option is presented again. Example invocation:
  11. .. code-block:: cmake
  12. CMAKE_DEPENDENT_OPTION(USE_FOO "Use Foo" ON
  13. "USE_BAR;NOT USE_ZOT" OFF)
  14. If USE_BAR is true and USE_ZOT is false, this provides an option
  15. called USE_FOO that defaults to ON. Otherwise, it sets USE_FOO to
  16. OFF. If the status of USE_BAR or USE_ZOT ever changes, any value for
  17. the USE_FOO option is saved so that when the option is re-enabled it
  18. retains its old value. Each element in the fourth parameter is
  19. evaluated as an if-condition, so :ref:`Condition Syntax` can be used.
  20. #]=======================================================================]
  21. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  22. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  23. set(${option}_AVAILABLE 1)
  24. foreach(d ${depends})
  25. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  26. if(${CMAKE_DEPENDENT_OPTION_DEP})
  27. else()
  28. set(${option}_AVAILABLE 0)
  29. endif()
  30. endforeach()
  31. if(${option}_AVAILABLE)
  32. option(${option} "${doc}" "${default}")
  33. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  34. else()
  35. if(${option} MATCHES "^${option}$")
  36. else()
  37. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  38. endif()
  39. set(${option} ${force})
  40. endif()
  41. else()
  42. set(${option} "${${option}_ISSET}")
  43. endif()
  44. endmacro()