set.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. set
  2. ---
  3. Set a CMake, cache or environment variable to a given value.
  4. ::
  5. set(<variable> <value>
  6. [[CACHE <type> <docstring> [FORCE]] | PARENT_SCOPE])
  7. Within CMake sets <variable> to the value <value>. <value> is
  8. expanded before <variable> is set to it. Normally, set will set a
  9. regular CMake variable. If CACHE is present, then the <variable> is
  10. put in the cache instead, unless it is already in the cache. See
  11. section 'Variable types in CMake' below for details of regular and
  12. cache variables and their interactions. If CACHE is used, <type> and
  13. <docstring> are required. <type> is used by the CMake GUI to choose a
  14. widget with which the user sets a value. The value for <type> may be
  15. one of
  16. ::
  17. FILEPATH = File chooser dialog.
  18. PATH = Directory chooser dialog.
  19. STRING = Arbitrary string.
  20. BOOL = Boolean ON/OFF checkbox.
  21. INTERNAL = No GUI entry (used for persistent variables).
  22. If <type> is INTERNAL, the cache variable is marked as internal, and
  23. will not be shown to the user in tools like cmake-gui. This is
  24. intended for values that should be persisted in the cache, but which
  25. users should not normally change. INTERNAL implies FORCE.
  26. Normally, set(...CACHE...) creates cache variables, but does not
  27. modify them. If FORCE is specified, the value of the cache variable
  28. is set, even if the variable is already in the cache. This should
  29. normally be avoided, as it will remove any changes to the cache
  30. variable's value by the user.
  31. If PARENT_SCOPE is present, the variable will be set in the scope
  32. above the current scope. Each new directory or function creates a new
  33. scope. This command will set the value of a variable into the parent
  34. directory or calling function (whichever is applicable to the case at
  35. hand). PARENT_SCOPE cannot be combined with CACHE.
  36. If <value> is not specified then the variable is removed instead of
  37. set. See also: the unset() command.
  38. ::
  39. set(<variable> <value1> ... <valueN>)
  40. In this case <variable> is set to a semicolon separated list of
  41. values.
  42. <variable> can be an environment variable such as:
  43. ::
  44. set( ENV{PATH} /home/martink )
  45. in which case the environment variable will be set.
  46. *** Variable types in CMake ***
  47. In CMake there are two types of variables: normal variables and cache
  48. variables. Normal variables are meant for the internal use of the
  49. script (just like variables in most programming languages); they are
  50. not persisted across CMake runs. Cache variables (unless set with
  51. INTERNAL) are mostly intended for configuration settings where the
  52. first CMake run determines a suitable default value, which the user
  53. can then override, by editing the cache with tools such as ccmake or
  54. cmake-gui. Cache variables are stored in the CMake cache file, and
  55. are persisted across CMake runs.
  56. Both types can exist at the same time with the same name but different
  57. values. When ${FOO} is evaluated, CMake first looks for a normal
  58. variable 'FOO' in scope and uses it if set. If and only if no normal
  59. variable exists then it falls back to the cache variable 'FOO'.
  60. Some examples:
  61. The code 'set(FOO "x")' sets the normal variable 'FOO'. It does not
  62. touch the cache, but it will hide any existing cache value 'FOO'.
  63. The code 'set(FOO "x" CACHE ...)' checks for 'FOO' in the cache,
  64. ignoring any normal variable of the same name. If 'FOO' is in the
  65. cache then nothing happens to either the normal variable or the cache
  66. variable. If 'FOO' is not in the cache, then it is added to the
  67. cache.
  68. Finally, whenever a cache variable is added or modified by a command,
  69. CMake also *removes* the normal variable of the same name from the
  70. current scope so that an immediately following evaluation of it will
  71. expose the newly cached value.
  72. Normally projects should avoid using normal and cache variables of the
  73. same name, as this interaction can be hard to follow. However, in
  74. some situations it can be useful. One example (used by some
  75. projects):
  76. A project has a subproject in its source tree. The child project has
  77. its own CMakeLists.txt, which is included from the parent
  78. CMakeLists.txt using add_subdirectory(). Now, if the parent and the
  79. child project provide the same option (for example a compiler option),
  80. the parent gets the first chance to add a user-editable option to the
  81. cache. Normally, the child would then use the same value that the
  82. parent uses. However, it may be necessary to hard-code the value for
  83. the child project's option while still allowing the user to edit the
  84. value used by the parent project. The parent project can achieve this
  85. simply by setting a normal variable with the same name as the option
  86. in a scope sufficient to hide the option's cache variable from the
  87. child completely. The parent has already set the cache variable, so
  88. the child's set(...CACHE...) will do nothing, and evaluating the
  89. option variable will use the value from the normal variable, which
  90. hides the cache variable.