ac0e2624a336b166d2f21de285f68cf4dd3d4598
[platform/upstream/cmake.git] / Modules / CMakeDependentOption.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 CMakeDependentOption
6 --------------------
7
8 Macro to provide an option dependent on other options.
9
10 This macro presents an option to the user only if a set of other
11 conditions are true.
12
13 .. command:: cmake_dependent_option
14
15   .. code-block:: cmake
16
17     cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
18
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>``.
23
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.
27
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
30   variable.
31
32 Example invocation:
33
34 .. code-block:: cmake
35
36   cmake_dependent_option(USE_FOO "Use Foo" ON "USE_BAR;NOT USE_ZOT" OFF)
37
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.
43
44 .. versionadded:: 3.22
45
46   Full :ref:`Condition Syntax` is now supported.  See policy :policy:`CMP0127`.
47
48 #]=======================================================================]
49
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
53     )
54   if(${option}_ISSET MATCHES "^${option}_ISSET$")
55     set(${option}_AVAILABLE 1)
56     if("x${_CDO_CMP0127}x" STREQUAL "xNEWx")
57       foreach(d ${depends})
58         cmake_language(EVAL CODE "
59           if (${d})
60           else()
61             set(${option}_AVAILABLE 0)
62           endif()"
63         )
64       endforeach()
65     else()
66       foreach(d ${depends})
67         string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
68         if(${CMAKE_DEPENDENT_OPTION_DEP})
69         else()
70           set(${option}_AVAILABLE 0)
71         endif()
72       endforeach()
73     endif()
74     if(${option}_AVAILABLE)
75       option(${option} "${doc}" "${default}")
76       set(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
77     else()
78       if(${option} MATCHES "^${option}$")
79       else()
80         set(${option} "${${option}}" CACHE INTERNAL "${doc}")
81       endif()
82       set(${option} ${force})
83     endif()
84   else()
85     set(${option} "${${option}_ISSET}")
86   endif()
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}")
90   endif()
91   unset(_CDO_CMP0127)
92 endmacro()