project.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. project
  2. -------
  3. Set the name of the project.
  4. Synopsis
  5. ^^^^^^^^
  6. .. code-block:: cmake
  7. project(<PROJECT-NAME> [<language-name>...])
  8. project(<PROJECT-NAME>
  9. [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
  10. [COMPAT_VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
  11. [SPDX_LICENSE <license-string>]
  12. [DESCRIPTION <description-string>]
  13. [HOMEPAGE_URL <url-string>]
  14. [LANGUAGES <language-name>...])
  15. Sets the name of the project, and stores it in the variable
  16. :variable:`PROJECT_NAME`. When called from the top-level
  17. ``CMakeLists.txt`` also stores the project name in the
  18. variable :variable:`CMAKE_PROJECT_NAME`.
  19. Also sets the variables:
  20. :variable:`PROJECT_SOURCE_DIR`, :variable:`<PROJECT-NAME>_SOURCE_DIR`
  21. Absolute path to the source directory for the project.
  22. :variable:`PROJECT_BINARY_DIR`, :variable:`<PROJECT-NAME>_BINARY_DIR`
  23. Absolute path to the binary directory for the project.
  24. :variable:`PROJECT_IS_TOP_LEVEL`, :variable:`<PROJECT-NAME>_IS_TOP_LEVEL`
  25. .. versionadded:: 3.21
  26. Boolean value indicating whether the project is top-level.
  27. Further variables are set by the optional arguments described in `Options`_
  28. further below. Where an option is not given, its corresponding variable is
  29. set to the empty string.
  30. Note that variables of the form ``<name>_SOURCE_DIR`` and ``<name>_BINARY_DIR``
  31. may also be set by other commands before ``project()`` is called (see the
  32. :command:`FetchContent_MakeAvailable` command for one example).
  33. Projects should not rely on ``<PROJECT-NAME>_SOURCE_DIR`` or
  34. ``<PROJECT-NAME>_BINARY_DIR`` holding a particular value outside of the scope
  35. of the call to ``project()`` or one of its child scopes.
  36. .. versionchanged:: 3.30
  37. ``<PROJECT-NAME>_SOURCE_DIR``, ``<PROJECT-NAME>_BINARY_DIR``, and
  38. ``<PROJECT-NAME>_IS_TOP_LEVEL``, if already set as normal variables when
  39. ``project(<PROJECT-NAME> ...)`` is called, are updated by the call.
  40. Cache entries by the same names are always set as before.
  41. See release notes for 3.30.3, 3.30.4, and 3.30.5 for details.
  42. .. versionchanged:: 3.31
  43. ``<PROJECT-NAME>_SOURCE_DIR``, ``<PROJECT-NAME>_BINARY_DIR``, and
  44. ``<PROJECT-NAME>_IS_TOP_LEVEL`` are always set as normal variables by
  45. ``project(<PROJECT-NAME> ...)``. See policy :policy:`CMP0180`.
  46. Cache entries by the same names are always set as before.
  47. Options
  48. ^^^^^^^
  49. The options are:
  50. ``VERSION <version>``
  51. Optional; may not be used unless policy :policy:`CMP0048` is
  52. set to ``NEW``.
  53. Takes a ``<version>`` argument composed of non-negative integer components,
  54. i.e. ``<major>[.<minor>[.<patch>[.<tweak>]]]``,
  55. and sets the variables
  56. * :variable:`PROJECT_VERSION`,
  57. :variable:`<PROJECT-NAME>_VERSION`
  58. * :variable:`PROJECT_VERSION_MAJOR`,
  59. :variable:`<PROJECT-NAME>_VERSION_MAJOR`
  60. * :variable:`PROJECT_VERSION_MINOR`,
  61. :variable:`<PROJECT-NAME>_VERSION_MINOR`
  62. * :variable:`PROJECT_VERSION_PATCH`,
  63. :variable:`<PROJECT-NAME>_VERSION_PATCH`
  64. * :variable:`PROJECT_VERSION_TWEAK`,
  65. :variable:`<PROJECT-NAME>_VERSION_TWEAK`.
  66. .. versionadded:: 3.12
  67. When the ``project()`` command is called from the top-level
  68. ``CMakeLists.txt``, then the version is also stored in the variable
  69. :variable:`CMAKE_PROJECT_VERSION`.
  70. ``COMPAT_VERSION <version>``
  71. .. versionadded:: 4.1
  72. .. note::
  73. Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``.
  74. Optional; requires ``VERSION`` also be set.
  75. Takes a ``<version>`` argument composed of non-negative integer components,
  76. i.e. ``<major>[.<minor>[.<patch>[.<tweak>]]]``,
  77. and sets the variables
  78. * :variable:`PROJECT_COMPAT_VERSION`,
  79. :variable:`<PROJECT-NAME>_COMPAT_VERSION`
  80. When the ``project()`` command is called from the top-level
  81. ``CMakeLists.txt``, then the compatibility version is also stored in the
  82. variable :variable:`CMAKE_PROJECT_COMPAT_VERSION`.
  83. ``SPDX_LICENSE <license-string>``
  84. .. versionadded:: 4.2
  85. .. note::
  86. Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``.
  87. Optional.
  88. Sets the variables
  89. * :variable:`PROJECT_SPDX_LICENSE`,
  90. :variable:`<PROJECT-NAME>_SPDX_LICENSE`
  91. to ``<license-string>``, which shall be a |SPDX|_ (SPDX)
  92. `License Expression`_ that describes the license(s) of the project as a
  93. whole, including documentation, resources, or other materials distributed
  94. with the project, in addition to software artifacts. See the SPDX
  95. `License List`_ for a list of commonly used licenses and their identifiers.
  96. See the :prop_tgt:`SPDX_LICENSE` property for specifying the license(s) on
  97. individual software artifacts.
  98. .. note::
  99. The project license is *not* used to initialize the
  100. :prop_tgt:`SPDX_LICENSE` property of individual targets. This allows the
  101. package license and default component license, which are specified when
  102. exporting package information, to be meaningful. Only |CPS| exports make
  103. use of this information.
  104. The project license *is* inherited as the package license in some cases.
  105. Refer to the ``PROJECT`` option and related documentation of the
  106. :command:`export` and :command:`install` commands for more information.
  107. .. _SPDX: https://spdx.dev/
  108. .. |SPDX| replace:: System Package Data Exchange
  109. .. _License Expression: https://spdx.github.io/spdx-spec/v3.0.1/annexes/spdx-license-expressions/
  110. .. _License List: https://spdx.org/licenses/
  111. ``DESCRIPTION <description-string>``
  112. .. versionadded:: 3.9
  113. Optional.
  114. Sets the variables
  115. * :variable:`PROJECT_DESCRIPTION`, :variable:`<PROJECT-NAME>_DESCRIPTION`
  116. to ``<description-string>``.
  117. It is recommended that this description is a relatively short string,
  118. usually no more than a few words.
  119. When the ``project()`` command is called from the top-level ``CMakeLists.txt``,
  120. then the description is also stored in the variable :variable:`CMAKE_PROJECT_DESCRIPTION`.
  121. .. versionadded:: 3.12
  122. Added the ``<PROJECT-NAME>_DESCRIPTION`` variable.
  123. ``HOMEPAGE_URL <url-string>``
  124. .. versionadded:: 3.12
  125. Optional.
  126. Sets the variables
  127. * :variable:`PROJECT_HOMEPAGE_URL`, :variable:`<PROJECT-NAME>_HOMEPAGE_URL`
  128. to ``<url-string>``, which should be the canonical home URL for the project.
  129. When the ``project()`` command is called from the top-level ``CMakeLists.txt``,
  130. then the URL also is stored in the variable :variable:`CMAKE_PROJECT_HOMEPAGE_URL`.
  131. ``LANGUAGES <language-name>...``
  132. Optional.
  133. Can also be specified without ``LANGUAGES`` keyword per the first, short signature.
  134. Selects which programming languages are needed to build the project.
  135. .. include:: include/SUPPORTED_LANGUAGES.rst
  136. By default ``C`` and ``CXX`` are enabled if no language options are given.
  137. Specify language ``NONE``, or use the ``LANGUAGES`` keyword and list no languages,
  138. to skip enabling any languages.
  139. The variables set through the ``VERSION``, ``COMPAT_VERSION``,
  140. ``SPDX_LICENSE``, ``DESCRIPTION`` and ``HOMEPAGE_URL`` options are
  141. intended for use as default values in package metadata and documentation.
  142. The :command:`export` and :command:`install` commands use these accordingly
  143. when generating |CPS| package descriptions.
  144. .. _`Code Injection`:
  145. Code Injection
  146. ^^^^^^^^^^^^^^
  147. A number of variables can be defined by the user to specify files to include
  148. at different points during the execution of the ``project()`` command.
  149. The following outlines the steps performed during a ``project()`` call:
  150. * .. versionadded:: 3.15
  151. For every ``project()`` call regardless of the project
  152. name, include the file(s) and module(s) named by
  153. :variable:`CMAKE_PROJECT_INCLUDE_BEFORE`, if set.
  154. * .. versionadded:: 3.17
  155. If the ``project()`` command specifies ``<PROJECT-NAME>`` as its project
  156. name, include the file(s) and module(s) named by
  157. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE`, if set.
  158. * Set the various project-specific variables detailed in the `Synopsis`_
  159. and `Options`_ sections above.
  160. * For the very first ``project()`` call only:
  161. * If :variable:`CMAKE_TOOLCHAIN_FILE` is set, read it at least once.
  162. It may be read multiple times and it may also be read again when
  163. enabling languages later (see below).
  164. * Set the variables describing the host and target platforms.
  165. Language-specific variables might or might not be set at this point.
  166. On the first run, the only language-specific variables that might be
  167. defined are those a toolchain file may have set. On subsequent runs,
  168. language-specific variables cached from a previous run may be set.
  169. * .. versionadded:: 3.24
  170. Include each file listed in :variable:`CMAKE_PROJECT_TOP_LEVEL_INCLUDES`,
  171. if set. The variable is ignored by CMake thereafter.
  172. * Enable any languages specified in the call, or the default languages if
  173. none were provided. The toolchain file may be re-read when enabling a
  174. language for the first time.
  175. * .. versionadded:: 3.15
  176. For every ``project()`` call regardless of the project
  177. name, include the file(s) and module(s) named by
  178. :variable:`CMAKE_PROJECT_INCLUDE`, if set.
  179. * If the ``project()`` command specifies ``<PROJECT-NAME>`` as its project
  180. name, include the file(s) and module(s) named by
  181. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, if set.
  182. Usage
  183. ^^^^^
  184. The top-level ``CMakeLists.txt`` file for a project must contain a
  185. literal, direct call to the ``project()`` command; loading one
  186. through the :command:`include` command is not sufficient. If no such
  187. call exists, CMake will issue a warning and pretend there is a
  188. ``project(Project)`` at the top to enable the default languages
  189. (``C`` and ``CXX``).
  190. .. note::
  191. Call the ``project()`` command near the top of the top-level
  192. ``CMakeLists.txt``, but *after* calling :command:`cmake_minimum_required`.
  193. It is important to establish version and policy settings before invoking
  194. other commands whose behavior they may affect and for this reason the
  195. ``project()`` command will issue a warning if this order is not kept.
  196. See also policy :policy:`CMP0000`.
  197. .. |CPS| replace:: Common Package Specification