CMakeDependentOption.cmake 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #.rst:
  2. # CMakeDependentOption
  3. # --------------------
  4. #
  5. # Macro to provide an option dependent on other options.
  6. #
  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. # ::
  13. #
  14. # CMAKE_DEPENDENT_OPTION(USE_FOO "Use Foo" ON
  15. # "USE_BAR;NOT USE_ZOT" OFF)
  16. #
  17. # If USE_BAR is true and USE_ZOT is false, this provides an option
  18. # called USE_FOO that defaults to ON. Otherwise, it sets USE_FOO to
  19. # OFF. If the status of USE_BAR or USE_ZOT ever changes, any value for
  20. # the USE_FOO option is saved so that when the option is re-enabled it
  21. # retains its old value.
  22. #=============================================================================
  23. # Copyright 2006-2009 Kitware, Inc.
  24. #
  25. # Distributed under the OSI-approved BSD License (the "License");
  26. # see accompanying file Copyright.txt for details.
  27. #
  28. # This software is distributed WITHOUT ANY WARRANTY; without even the
  29. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. # See the License for more information.
  31. #=============================================================================
  32. # (To distribute this file outside of CMake, substitute the full
  33. # License text for the above reference.)
  34. macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
  35. if(${option}_ISSET MATCHES "^${option}_ISSET$")
  36. set(${option}_AVAILABLE 1)
  37. foreach(d ${depends})
  38. string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
  39. if(${CMAKE_DEPENDENT_OPTION_DEP})
  40. else()
  41. set(${option}_AVAILABLE 0)
  42. endif()
  43. endforeach()
  44. if(${option}_AVAILABLE)
  45. option(${option} "${doc}" "${default}")
  46. set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
  47. else()
  48. if(${option} MATCHES "^${option}$")
  49. else()
  50. set(${option} "${${option}}" CACHE INTERNAL "${doc}")
  51. endif()
  52. set(${option} ${force})
  53. endif()
  54. else()
  55. set(${option} "${${option}_ISSET}")
  56. endif()
  57. endmacro()