1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Macro to provide an option dependent on other options.
10 This macro presents an option to the user only if a set of other
13 .. command:: cmake_dependent_option
17 cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
19 Makes ``<option>`` available to the user if the
20 :ref:`semicolon-separated list <CMake Language Lists>` of conditions in
21 ``<depends>`` are all true. Otherwise, a local variable named ``<option>``
22 is set to ``<force>``.
24 When ``<option>`` is available, the given ``<help_text>`` and initial
25 ``<value>`` are used. Otherwise, any value set by the user is preserved for
26 when ``<depends>`` is satisfied in the future.
28 Note that the ``<option>`` variable only has a value which satisfies the
29 ``<depends>`` condition within the scope of the caller because it is a local
36 cmake_dependent_option(USE_FOO "Use Foo" ON "USE_BAR;NOT USE_ZOT" OFF)
38 If ``USE_BAR`` is true and ``USE_ZOT`` is false, this provides an option called
39 ``USE_FOO`` that defaults to ON. Otherwise, it sets ``USE_FOO`` to OFF and
40 hides the option from the user. If the status of ``USE_BAR`` or ``USE_ZOT``
41 ever changes, any value for the ``USE_FOO`` option is saved so that when the
42 option is re-enabled it retains its old value.
44 .. versionadded:: 3.22
46 Full :ref:`Condition Syntax` is now supported. See policy :policy:`CMP0127`.
48 #]=======================================================================]
50 macro(CMAKE_DEPENDENT_OPTION option doc default depends force)
51 cmake_policy(GET CMP0127 _CDO_CMP0127
52 PARENT_SCOPE # undocumented, do not use outside of CMake
54 if(${option}_ISSET MATCHES "^${option}_ISSET$")
55 set(${option}_AVAILABLE 1)
56 if("x${_CDO_CMP0127}x" STREQUAL "xNEWx")
58 cmake_language(EVAL CODE "
61 set(${option}_AVAILABLE 0)
67 string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
68 if(${CMAKE_DEPENDENT_OPTION_DEP})
70 set(${option}_AVAILABLE 0)
74 if(${option}_AVAILABLE)
75 option(${option} "${doc}" "${default}")
76 set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
78 if(${option} MATCHES "^${option}$")
80 set(${option} "${${option}}" CACHE INTERNAL "${doc}")
82 set(${option} ${force})
85 set(${option} "${${option}_ISSET}")
87 if("x${_CDO_CMP0127}x" STREQUAL "xx" AND "x${depends}x" MATCHES "[^A-Za-z0-9_; ]")
88 cmake_policy(GET_WARNING CMP0127 _CDO_CMP0127_WARNING)
89 message(AUTHOR_WARNING "${_CDO_CMP0127_WARNING}")