set.rst 4.3 KB

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