CMakeDependentOption.cmake 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #
  7. # Macro to provide an option dependent on other options.
  8. #
  9. # This macro presents an option to the user only if a set of other
  10. # conditions are true. When the option is not presented a default value
  11. # is used, but any value set by the user is preserved for when the
  12. # option is presented again. Example invocation:
  13. #
  14. # ::
  15. #
  16. # CMAKE_DEPENDENT_OPTION(USE_FOO "Use Foo" ON
  17. # "USE_BAR;NOT USE_ZOT" OFF)
  18. #
  19. # If USE_BAR is true and USE_ZOT is false, this provides an option
  20. # called USE_FOO that defaults to ON. Otherwise, it sets USE_FOO to
  21. # OFF. If the status of USE_BAR or USE_ZOT ever changes, any value for
  22. # the USE_FOO option is saved so that when the option is re-enabled it
  23. # retains its old value.
  24. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  25. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  26. set(${option}_AVAILABLE 1)
  27. foreach(d ${depends})
  28. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  29. if(${CMAKE_DEPENDENT_OPTION_DEP})
  30. else()
  31. set(${option}_AVAILABLE 0)
  32. endif()
  33. endforeach()
  34. if(${option}_AVAILABLE)
  35. option(${option} "${doc}" "${default}")
  36. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  37. else()
  38. if(${option} MATCHES "^${option}$")
  39. else()
  40. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  41. endif()
  42. set(${option} ${force})
  43. endif()
  44. else()
  45. set(${option} "${${option}_ISSET}")
  46. endif()
  47. endmacro()