project.rst 6.3 KB

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