project.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. Optional.
  86. Sets the variables
  87. * :variable:`PROJECT_SPDX_LICENSE`,
  88. :variable:`<PROJECT-NAME>_SPDX_LICENSE`
  89. to ``<license-string>``, which shall be a |SPDX|_ (SPDX)
  90. `License Expression`_ that describes the license(s) of the project as a
  91. whole, including documentation, resources, or other materials distributed
  92. with the project, in addition to software artifacts. See the SPDX
  93. `License List`_ for a list of commonly used licenses and their identifiers.
  94. See the :prop_tgt:`SPDX_LICENSE` property for specifying the license(s) on
  95. individual software artifacts.
  96. .. _SPDX: https://spdx.dev/
  97. .. |SPDX| replace:: System Package Data Exchange
  98. .. _License Expression: https://spdx.github.io/spdx-spec/v3.0.1/annexes/spdx-license-expressions/
  99. .. _License List: https://spdx.org/licenses/
  100. ``DESCRIPTION <description-string>``
  101. .. versionadded:: 3.9
  102. Optional.
  103. Sets the variables
  104. * :variable:`PROJECT_DESCRIPTION`, :variable:`<PROJECT-NAME>_DESCRIPTION`
  105. to ``<description-string>``.
  106. It is recommended that this description is a relatively short string,
  107. usually no more than a few words.
  108. When the ``project()`` command is called from the top-level ``CMakeLists.txt``,
  109. then the description is also stored in the variable :variable:`CMAKE_PROJECT_DESCRIPTION`.
  110. .. versionadded:: 3.12
  111. Added the ``<PROJECT-NAME>_DESCRIPTION`` variable.
  112. ``HOMEPAGE_URL <url-string>``
  113. .. versionadded:: 3.12
  114. Optional.
  115. Sets the variables
  116. * :variable:`PROJECT_HOMEPAGE_URL`, :variable:`<PROJECT-NAME>_HOMEPAGE_URL`
  117. to ``<url-string>``, which should be the canonical home URL for the project.
  118. When the ``project()`` command is called from the top-level ``CMakeLists.txt``,
  119. then the URL also is stored in the variable :variable:`CMAKE_PROJECT_HOMEPAGE_URL`.
  120. ``LANGUAGES <language-name>...``
  121. Optional.
  122. Can also be specified without ``LANGUAGES`` keyword per the first, short signature.
  123. Selects which programming languages are needed to build the project.
  124. .. include:: include/SUPPORTED_LANGUAGES.rst
  125. By default ``C`` and ``CXX`` are enabled if no language options are given.
  126. Specify language ``NONE``, or use the ``LANGUAGES`` keyword and list no languages,
  127. to skip enabling any languages.
  128. The variables set through the ``VERSION``, ``COMPAT_VERSION``,
  129. ``SPDX_LICENSE``, ``DESCRIPTION`` and ``HOMEPAGE_URL`` options are
  130. intended for use as default values in package metadata and documentation.
  131. The :command:`export` and :command:`install` commands use these accordingly
  132. when generating |CPS| package descriptions.
  133. .. |CPS| replace:: Common Package Specification
  134. .. _`Code Injection`:
  135. Code Injection
  136. ^^^^^^^^^^^^^^
  137. A number of variables can be defined by the user to specify files to include
  138. at different points during the execution of the ``project()`` command.
  139. The following outlines the steps performed during a ``project()`` call:
  140. * .. versionadded:: 3.15
  141. For every ``project()`` call regardless of the project
  142. name, include the file(s) and module(s) named by
  143. :variable:`CMAKE_PROJECT_INCLUDE_BEFORE`, if set.
  144. * .. versionadded:: 3.17
  145. If the ``project()`` command specifies ``<PROJECT-NAME>`` as its project
  146. name, include the file(s) and module(s) named by
  147. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE`, if set.
  148. * Set the various project-specific variables detailed in the `Synopsis`_
  149. and `Options`_ sections above.
  150. * For the very first ``project()`` call only:
  151. * If :variable:`CMAKE_TOOLCHAIN_FILE` is set, read it at least once.
  152. It may be read multiple times and it may also be read again when
  153. enabling languages later (see below).
  154. * Set the variables describing the host and target platforms.
  155. Language-specific variables might or might not be set at this point.
  156. On the first run, the only language-specific variables that might be
  157. defined are those a toolchain file may have set. On subsequent runs,
  158. language-specific variables cached from a previous run may be set.
  159. * .. versionadded:: 3.24
  160. Include each file listed in :variable:`CMAKE_PROJECT_TOP_LEVEL_INCLUDES`,
  161. if set. The variable is ignored by CMake thereafter.
  162. * Enable any languages specified in the call, or the default languages if
  163. none were provided. The toolchain file may be re-read when enabling a
  164. language for the first time.
  165. * .. versionadded:: 3.15
  166. For every ``project()`` call regardless of the project
  167. name, include the file(s) and module(s) named by
  168. :variable:`CMAKE_PROJECT_INCLUDE`, if set.
  169. * If the ``project()`` command specifies ``<PROJECT-NAME>`` as its project
  170. name, include the file(s) and module(s) named by
  171. :variable:`CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE`, if set.
  172. Usage
  173. ^^^^^
  174. The top-level ``CMakeLists.txt`` file for a project must contain a
  175. literal, direct call to the ``project()`` command; loading one
  176. through the :command:`include` command is not sufficient. If no such
  177. call exists, CMake will issue a warning and pretend there is a
  178. ``project(Project)`` at the top to enable the default languages
  179. (``C`` and ``CXX``).
  180. .. note::
  181. Call the ``project()`` command near the top of the top-level
  182. ``CMakeLists.txt``, but *after* calling :command:`cmake_minimum_required`.
  183. It is important to establish version and policy settings before invoking
  184. other commands whose behavior they may affect and for this reason the
  185. ``project()`` command will issue a warning if this order is not kept.
  186. See also policy :policy:`CMP0000`.