af862e4da3efa44e1911c5c3c19d010bed6125db
[platform/upstream/cmake.git] / Help / command / set.rst
1 set
2 ---
3
4 Set a normal, cache, or environment variable to a given value.
5 See the :ref:`cmake-language(7) variables <CMake Language Variables>`
6 documentation for the scopes and interaction of normal variables
7 and cache entries.
8
9 Signatures of this command that specify a ``<value>...`` placeholder
10 expect zero or more arguments.  Multiple arguments will be joined as
11 a :ref:`semicolon-separated list <CMake Language Lists>` to form the actual variable
12 value to be set.  Zero arguments will cause normal variables to be
13 unset.  See the :command:`unset` command to unset variables explicitly.
14
15 Set Normal Variable
16 ^^^^^^^^^^^^^^^^^^^
17
18 .. code-block:: cmake
19
20   set(<variable> <value>... [PARENT_SCOPE])
21
22 Sets the given ``<variable>`` in the current function or directory scope.
23
24 If the ``PARENT_SCOPE`` option is given the variable will be set in
25 the scope above the current scope.  Each new directory or function
26 creates a new scope.  This command will set the value of a variable
27 into the parent directory or calling function (whichever is applicable
28 to the case at hand). The previous state of the variable's value stays the
29 same in the current scope (e.g., if it was undefined before, it is still
30 undefined and if it had a value, it is still that value).
31
32 Set Cache Entry
33 ^^^^^^^^^^^^^^^
34
35 .. code-block:: cmake
36
37   set(<variable> <value>... CACHE <type> <docstring> [FORCE])
38
39 Sets the given cache ``<variable>`` (cache entry).  Since cache entries
40 are meant to provide user-settable values this does not overwrite
41 existing cache entries by default.  Use the ``FORCE`` option to
42 overwrite existing entries.
43
44 The ``<type>`` must be specified as one of:
45
46 ``BOOL``
47   Boolean ``ON/OFF`` value.  :manual:`cmake-gui(1)` offers a checkbox.
48
49 ``FILEPATH``
50   Path to a file on disk.  :manual:`cmake-gui(1)` offers a file dialog.
51
52 ``PATH``
53   Path to a directory on disk.  :manual:`cmake-gui(1)` offers a file dialog.
54
55 ``STRING``
56   A line of text.  :manual:`cmake-gui(1)` offers a text field or a
57   drop-down selection if the :prop_cache:`STRINGS` cache entry
58   property is set.
59
60 ``INTERNAL``
61   A line of text.  :manual:`cmake-gui(1)` does not show internal entries.
62   They may be used to store variables persistently across runs.
63   Use of this type implies ``FORCE``.
64
65 The ``<docstring>`` must be specified as a line of text providing
66 a quick summary of the option for presentation to :manual:`cmake-gui(1)`
67 users.
68
69 If the cache entry does not exist prior to the call or the ``FORCE``
70 option is given then the cache entry will be set to the given value.
71
72 .. note::
73
74   The content of the cache variable will not be directly accessible if a normal
75   variable of the same name already exists (see :ref:`rules of variable
76   evaluation <CMake Language Variables>`). If policy :policy:`CMP0126` is set
77   to ``OLD``, any normal variable binding in the current scope will be removed.
78
79 It is possible for the cache entry to exist prior to the call but
80 have no type set if it was created on the :manual:`cmake(1)` command
81 line by a user through the ``-D<var>=<value>`` option without
82 specifying a type.  In this case the ``set`` command will add the
83 type.  Furthermore, if the ``<type>`` is ``PATH`` or ``FILEPATH``
84 and the ``<value>`` provided on the command line is a relative path,
85 then the ``set`` command will treat the path as relative to the
86 current working directory and convert it to an absolute path.
87
88 Set Environment Variable
89 ^^^^^^^^^^^^^^^^^^^^^^^^
90
91 .. code-block:: cmake
92
93   set(ENV{<variable>} [<value>])
94
95 Sets an :manual:`Environment Variable <cmake-env-variables(7)>`
96 to the given value.
97 Subsequent calls of ``$ENV{<variable>}`` will return this new value.
98
99 This command affects only the current CMake process, not the process
100 from which CMake was called, nor the system environment at large,
101 nor the environment of subsequent build or test processes.
102
103 If no argument is given after ``ENV{<variable>}`` or if ``<value>`` is
104 an empty string, then this command will clear any existing value of the
105 environment variable.
106
107 Arguments after ``<value>`` are ignored. If extra arguments are found,
108 then an author warning is issued.