source.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. CMake Source Code Guide
  2. ***********************
  3. The following is a guide to the CMake source code for developers.
  4. See documentation on `CMake Development`_ for more information.
  5. .. _`CMake Development`: README.rst
  6. C++ Code Style
  7. ==============
  8. We use `clang-format`_ version **6.0** to define our style for C++ code in
  9. the CMake source tree. See the `.clang-format`_ configuration file for our
  10. style settings. Use the `Utilities/Scripts/clang-format.bash`_ script to
  11. format source code. It automatically runs ``clang-format`` on the set of
  12. source files for which we enforce style. The script also has options to
  13. format only a subset of files, such as those that are locally modified.
  14. .. _`clang-format`: http://clang.llvm.org/docs/ClangFormat.html
  15. .. _`.clang-format`: ../../.clang-format
  16. .. _`Utilities/Scripts/clang-format.bash`: ../../Utilities/Scripts/clang-format.bash
  17. C++ Subset Permitted
  18. ====================
  19. CMake requires compiling as C++11 in order to support building on older
  20. toolchains. However, to facilitate development, some standard library
  21. features from more recent C++ standards are supported through a compatibility
  22. layer. These features are defined under the namespace ``cm`` and headers
  23. are accessible under the ``cm/`` directory. The headers under ``cm/`` can
  24. be used in place of the standard ones when extended features are needed.
  25. For example ``<cm/memory>`` can be used in place of ``<memory>``.
  26. Available features are:
  27. * From ``C++14``:
  28. * ``<cm/iomanip>``:
  29. ``cm::quoted``
  30. * ``<cm/iterator>``:
  31. ``cm::make_reverse_iterator``, ``cm::cbegin``, ``cm::cend``,
  32. ``cm::rbegin``, ``cm::rend``, ``cm::crbegin``, ``cm::crend``
  33. * ``<cm/memory>``:
  34. ``cm::make_unique``
  35. * ``<cm/shared_mutex>``:
  36. ``cm::shared_lock``
  37. * ``<cm/type_traits>``:
  38. ``cm::enable_if_t``
  39. * From ``C++17``:
  40. * ``<cm/algorithm>``:
  41. ``cm::clamp``
  42. * ``cm/filesystem>``:
  43. ``cm::filesystem::path``
  44. * ``<cm/iterator>``:
  45. ``cm::size``, ``cm::empty``, ``cm::data``
  46. * ``<cm/optional>``:
  47. ``cm::nullopt_t``, ``cm::nullopt``, ``cm::optional``,
  48. ``cm::make_optional``, ``cm::bad_optional_access``
  49. * ``<cm/shared_mutex>``:
  50. ``cm::shared_mutex``
  51. * ``<cm/string_view>``:
  52. ``cm::string_view``
  53. * ``<cm/type_traits>``:
  54. ``cm::bool_constant``, ``cm::invoke_result_t``, ``cm::invoke_result``,
  55. ``cm::void_t``
  56. * ``<cm/utility>``:
  57. ``cm::in_place_t``, ``cm::in_place``
  58. * From ``C++20``:
  59. * ``<cm/deque>``:
  60. ``cm::erase``, ``cm::erase_if``
  61. * ``<cm/list>``:
  62. ``cm::erase``, ``cm::erase_if``
  63. * ``<cm/map>`` :
  64. ``cm::erase_if``
  65. * ``<cm/set>`` :
  66. ``cm::erase_if``
  67. * ``<cm/string>``:
  68. ``cm::erase``, ``cm::erase_if``
  69. * ``<cm/unordered_map>``:
  70. ``cm::erase_if``
  71. * ``<cm/unordered_set>``:
  72. ``cm::erase_if``
  73. * ``<cm/vector>``:
  74. ``cm::erase``, ``cm::erase_if``
  75. Additionally, some useful non-standard extensions to the C++ standard library
  76. are available in headers under the directory ``cmext/`` in namespace ``cm``.
  77. These are:
  78. * ``<cmext/algorithm>``:
  79. * ``cm::append``:
  80. Append elements to a sequential container.
  81. * ``cm::contains``:
  82. Checks if element or key is contained in container.
  83. * ``<cmext/iterator>``:
  84. * ``cm::is_terator``:
  85. Checks if a type is an iterator type.
  86. * ``cm::is_input_iterator``:
  87. Checks if a type is an input iterator type.
  88. * ``cm::is_range``:
  89. Checks if a type is a range type: functions ``std::begin()`` and
  90. ``std::end()`` apply.
  91. * ``cm::is_input_range``:
  92. Checks if a type is an input range type: functions ``std::begin()`` and
  93. ``std::end()`` apply and return an input iterator.
  94. * ``<cmext/memory>``:
  95. * ``cm::static_reference_cast``:
  96. Apply a ``static_cast`` to a smart pointer.
  97. * ``cm::dynamic_reference_cast``:
  98. Apply a ``dynamic_cast`` to a smart pointer.
  99. * ``<cmext/type_traits>``:
  100. * ``cm::is_container``:
  101. Checks if a type is a container type.
  102. * ``cm::is_associative_container``:
  103. Checks if a type is an associative container type.
  104. * ``cm::is_unordered_associative_container``:
  105. Checks if a type is an unordered associative container type.
  106. * ``cm::is_sequence_container``:
  107. Checks if a type is a sequence container type.
  108. * ``cm::is_unique_ptr``:
  109. Checks if a type is a ``std::unique_ptr`` type.
  110. CMake assumes the compiler supports ``#pragma once``. Use this for all
  111. hand-written header files.
  112. Dynamic Memory Management
  113. =========================
  114. To ensure efficient memory management, i.e. no memory leaks, it is required
  115. to use smart pointers. Any dynamic memory allocation must be handled by a
  116. smart pointer such as ``std::unique_ptr`` or ``std::shared_ptr``.
  117. It is allowed to pass raw pointers between objects to enable objects sharing.
  118. A raw pointer **must** not be deleted. Only the object(s) owning the smart
  119. pointer are allowed to delete dynamically allocated memory.
  120. Third Parties
  121. =============
  122. To build CMake, some third parties are needed. Under ``Utilities``
  123. directory, are versions of these third parties which can be used as an
  124. alternate to the ones provided by the system.
  125. To enable the selection of the third parties between the system and CMake ones,
  126. in CMake sources, third parties headers must be prefixed by ``cm3p/``
  127. (for example: ``<cm3p/json/reader.h>``). These wrappers are located under
  128. ``Utilities/cm3p`` directory.
  129. Source Tree Layout
  130. ==================
  131. The CMake source tree is organized as follows.
  132. * ``Auxiliary/``:
  133. Shell and editor integration files.
  134. * ``Help/``:
  135. Documentation. See the `CMake Documentation Guide`_.
  136. * ``Help/dev/``:
  137. Developer documentation.
  138. * ``Help/release/dev/``:
  139. Release note snippets for development since last release.
  140. * ``Licenses/``:
  141. License files for third-party libraries in binary distributions.
  142. * ``Modules/``:
  143. CMake language modules installed with CMake.
  144. * ``Packaging/``:
  145. Files used for packaging CMake itself for distribution.
  146. * ``Source/``:
  147. Source code of CMake itself.
  148. * ``Templates/``:
  149. Files distributed with CMake as implementation details for generators,
  150. packagers, etc.
  151. * ``Tests/``:
  152. The test suite. See `Tests/README.rst`_.
  153. * ``Utilities/``:
  154. Scripts, third-party source code.
  155. * ``Utilities/std/cm``:
  156. Support files for various C++ standards.
  157. * ``Utilities/std/cmext``:
  158. Extensions to the C++ STL.
  159. * ``Utilities/cm3p``:
  160. Public headers for third parties needed to build CMake.
  161. * ``Utilities/Sphinx/``:
  162. Sphinx configuration to build CMake user documentation.
  163. * ``Utilities/Release/``:
  164. Scripts used to package CMake itself for distribution on ``cmake.org``.
  165. See `Utilities/Release/README.rst`_.
  166. .. _`CMake Documentation Guide`: documentation.rst
  167. .. _`Tests/README.rst`: ../../Tests/README.rst
  168. .. _`Utilities/Release/README.rst`: ../../Utilities/Release/README.rst