set.rst 5.6 KB

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