CMakeDependentOption.cmake 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ::
  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.
  19. #]=======================================================================]
  20. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  21. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  22. set(${option}_AVAILABLE 1)
  23. foreach(d ${depends})
  24. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  25. if(${CMAKE_DEPENDENT_OPTION_DEP})
  26. else()
  27. set(${option}_AVAILABLE 0)
  28. endif()
  29. endforeach()
  30. if(${option}_AVAILABLE)
  31. option(${option} "${doc}" "${default}")
  32. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  33. else()
  34. if(${option} MATCHES "^${option}$")
  35. else()
  36. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  37. endif()
  38. set(${option} ${force})
  39. endif()
  40. else()
  41. set(${option} "${${option}_ISSET}")
  42. endif()
  43. endmacro()