project.rst 8.1 KB

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