summaryrefslogtreecommitdiffstats
path: root/cmake/modules/DefaultOption.cmake
blob: b8cc640d056bbe7aa64ededebe95e9084e636c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#[=======================================================================[.rst:
DefaultOption
-------------

Macros to provide an option with defaults dependent on other options.

Usage:

.. code-block:: cmake

  default_option(<option> "<help_text>" <depends>)

Where ``<option>`` uses the given ``<help_text>`` and is ``ON`` by default if
``<depends>`` is true.  If the ``<depends>`` condition is not true,
``<option>`` will be ``OFF`` by default.  Each element in the ``<depends>``
parameter is evaluated as an if-condition, so :ref:`Condition Syntax` can be
used.

Example invocation:

.. code-block:: cmake

  default_option(USE_FOO "Use Foo" "USE_BAR;NOT USE_ZOT")

If ``USE_BAR`` is true and ``USE_ZOT`` is false, the option called ``USE_FOO``
will default to ``ON``, otherwise it will default to ``OFF``.
#]=======================================================================]

macro(DEFAULT_OPTION option doc depends)
  set(${option}_DEFAULT_ON 1)
  foreach(d ${depends})
    string(REGEX REPLACE " +" ";" DEFAULT_OPTION_DEP "${d}")
    if(${DEFAULT_OPTION_DEP})
    else()
      set(${option}_DEFAULT_ON 0)
    endif()
  endforeach()
  if(${option}_DEFAULT_ON)
    option(${option} "${doc}" "ON")
  else()
    option(${option} "${doc}" "OFF")
  endif()
endmacro()