project.rst 7.5 KB

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