source.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 **18** 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`: https://clang.llvm.org/docs/ClangFormat.html
  15. .. _`.clang-format`: ../../.clang-format
  16. .. _`Utilities/Scripts/clang-format.bash`: ../../Utilities/Scripts/clang-format.bash
  17. In addition, we have a few coding conventions that we prefer for new code to
  18. follow (which are not enforced by other tooling):
  19. * Name local variables using ``camelCase``.
  20. * Name class members and free functions using ``PascalCase``.
  21. * Reference class members using ``this->Member``.
  22. * Use east ``const`` style, e.g., ``int const`` instead of ``const int``.
  23. * Declare variables using a locally-specified type:
  24. .. code-block:: c++
  25. T x = f();
  26. Use ``auto`` only if the real type explicitly appears in the initializer:
  27. .. code-block:: c++
  28. auto y = cm::make_unique<T>();
  29. auto z = []() -> T {...}();
  30. Exceptions:
  31. * Iterators: ``auto i = myMap.find(myKey);``
  32. * Lambdas: ``auto f = []() {...};``
  33. C++ Subset Permitted
  34. ====================
  35. CMake requires compiling as C++11 in order to support building on older
  36. toolchains. However, to facilitate development, some standard library
  37. features from more recent C++ standards are supported through a compatibility
  38. layer. These features are defined under the namespace ``cm`` and headers
  39. are accessible under the ``cm/`` directory. The headers under ``cm/`` can
  40. be used in place of the standard ones when extended features are needed.
  41. For example ``<cm/memory>`` can be used in place of ``<memory>``.
  42. Available features are:
  43. * From ``C++14``:
  44. * ``<cm/array>``:
  45. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  46. ``cm::crbegin``, ``cm::crend``
  47. * ``<cm/deque>``:
  48. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  49. ``cm::crbegin``, ``cm::crend``
  50. * ``<cm/forward_list>``:
  51. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  52. ``cm::crbegin``, ``cm::crend``
  53. * ``<cm/iomanip>``:
  54. ``cm::quoted``
  55. * ``<cm/iterator>``:
  56. ``cm::make_reverse_iterator``, ``cm::cbegin``, ``cm::cend``,
  57. ``cm::rbegin``, ``cm::rend``, ``cm::crbegin``, ``cm::crend``
  58. * ``<cm/list>``:
  59. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  60. ``cm::crbegin``, ``cm::crend``
  61. * ``<cm/map>``:
  62. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  63. ``cm::crbegin``, ``cm::crend``
  64. * ``<cm/memory>``:
  65. ``cm::make_unique``
  66. * ``<cm/set>``:
  67. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  68. ``cm::crbegin``, ``cm::crend``
  69. * ``<cm/string>``:
  70. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  71. ``cm::crbegin``, ``cm::crend``
  72. * ``<cm/string_view>``:
  73. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  74. ``cm::crbegin``, ``cm::crend``
  75. * ``<cm/shared_mutex>``:
  76. ``cm::shared_lock``
  77. * ``<cm/type_traits>``:
  78. ``cm::enable_if_t``
  79. * ``<cm/unordered_map>``:
  80. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  81. ``cm::crbegin``, ``cm::crend``
  82. * ``<cm/unordered_set>``:
  83. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  84. ``cm::crbegin``, ``cm::crend``
  85. * ``<cm/vector>``:
  86. ``cm::cbegin``, ``cm::cend``, ``cm::rbegin``, ``cm::rend``,
  87. ``cm::crbegin``, ``cm::crend``
  88. * From ``C++17``:
  89. * ``<cm/algorithm>``:
  90. ``cm::clamp``
  91. * ``<cm/array>``:
  92. ``cm::size``, ``cm::empty``, ``cm::data``
  93. * ``<cm/deque>``:
  94. ``cm::size``, ``cm::empty``, ``cm::data``
  95. * ``cm/filesystem>``:
  96. ``cm::filesystem::path``
  97. * ``<cm/forward_list>``:
  98. ``cm::size``, ``cm::empty``, ``cm::data``
  99. * ``<cm/iterator>``:
  100. ``cm::size``, ``cm::empty``, ``cm::data``
  101. * ``<cm/list>``:
  102. ``cm::size``, ``cm::empty``, ``cm::data``
  103. * ``<cm/map>``:
  104. ``cm::size``, ``cm::empty``, ``cm::data``
  105. * ``<cm/optional>``:
  106. ``cm::nullopt_t``, ``cm::nullopt``, ``cm::optional``,
  107. ``cm::make_optional``, ``cm::bad_optional_access``
  108. * ``<cm/set>``:
  109. ``cm::size``, ``cm::empty``, ``cm::data``
  110. * ``<cm/shared_mutex>``:
  111. ``cm::shared_mutex``
  112. * ``<cm/string>``:
  113. ``cm::size``, ``cm::empty``, ``cm::data``
  114. * ``<cm/string_view>``:
  115. ``cm::string_view``, ``cm::size``, ``cm::empty``, ``cm::data``
  116. * ``<cm/type_traits>``:
  117. ``cm::bool_constant``, ``cm::invoke_result_t``, ``cm::invoke_result``,
  118. ``cm::void_t``
  119. * ``<cm/unordered_map>``:
  120. ``cm::size``, ``cm::empty``, ``cm::data``
  121. * ``<cm/unordered_set>``:
  122. ``cm::size``, ``cm::empty``, ``cm::data``
  123. * ``<cm/utility>``:
  124. ``cm::in_place_t``, ``cm::in_place``
  125. * ``<cm/vector>``:
  126. ``cm::size``, ``cm::empty``, ``cm::data``
  127. * From ``C++20``:
  128. * ``<cm/array>``:
  129. ``cm::ssize``
  130. * ``<cm/deque>``:
  131. ``cm::erase``, ``cm::erase_if``, ``cm::ssize``
  132. * ``<cm/forward_list>``:
  133. ``cm::ssize``
  134. * ``<cm/iterator>``:
  135. ``cm::ssize``
  136. * ``<cm/list>``:
  137. ``cm::erase``, ``cm::erase_if``, ``cm::ssize``
  138. * ``<cm/map>`` :
  139. ``cm::erase_if``, ``cm::ssize``
  140. * ``<cm/set>`` :
  141. ``cm::erase_if``, ``cm::ssize``
  142. * ``<cm/string_view>``:
  143. ``cm::ssize``
  144. * ``<cm/string>``:
  145. ``cm::erase``, ``cm::erase_if``, ``cm::ssize``
  146. * ``<cm/unordered_map>``:
  147. ``cm::erase_if``, ``cm::ssize``
  148. * ``<cm/unordered_set>``:
  149. ``cm::erase_if``, ``cm::ssize``
  150. * ``<cm/vector>``:
  151. ``cm::erase``, ``cm::erase_if``, ``cm::ssize``
  152. * From ``C++23``:
  153. * ``<cm/type_traits>``:
  154. ``cm::is_scoped_enum``
  155. Additionally, some useful non-standard extensions to the C++ standard library
  156. are available in headers under the directory ``cmext/`` in namespace ``cm``.
  157. These are:
  158. * ``<cmext/algorithm>``:
  159. * ``cm::append``:
  160. Append elements to a sequential container.
  161. * ``cm::contains``:
  162. Checks if element or key is contained in container.
  163. * ``<cmext/enum_set>``
  164. * ``cm::enum_set``:
  165. Container to manage set of elements from an ``enum class`` definition.
  166. * ``<cmext/iterator>``:
  167. * ``cm::is_iterator``:
  168. Checks if a type is an iterator type.
  169. * ``cm::is_input_iterator``:
  170. Checks if a type is an input iterator type.
  171. * ``cm::is_range``:
  172. Checks if a type is a range type: functions ``std::begin()`` and
  173. ``std::end()`` apply.
  174. * ``cm::is_input_range``:
  175. Checks if a type is an input range type: functions ``std::begin()`` and
  176. ``std::end()`` apply and return an input iterator.
  177. * ``<cmext/memory>``:
  178. * ``cm::static_reference_cast``:
  179. Apply a ``static_cast`` to a smart pointer.
  180. * ``cm::dynamic_reference_cast``:
  181. Apply a ``dynamic_cast`` to a smart pointer.
  182. * ``<cmext/type_traits>``:
  183. * ``cm::is_container``:
  184. Checks if a type is a container type.
  185. * ``cm::is_associative_container``:
  186. Checks if a type is an associative container type.
  187. * ``cm::is_unordered_associative_container``:
  188. Checks if a type is an unordered associative container type.
  189. * ``cm::is_sequence_container``:
  190. Checks if a type is a sequence container type.
  191. * ``cm::is_unique_ptr``:
  192. Checks if a type is a ``std::unique_ptr`` type.
  193. * ``cm::remove_member_pointer``
  194. Produces the underlying type of a member-pointer type, ie, given ``T C::*``,
  195. returns ``T``.
  196. * ``cm::member_pointer_class``
  197. Produces the class associated with a member-pointer type, ie, given
  198. ``T C::*``, returns ``C``.
  199. CMake assumes the compiler supports ``#pragma once``. Use this for all
  200. hand-written header files.
  201. Dynamic Memory Management
  202. =========================
  203. To ensure efficient memory management, i.e. no memory leaks, it is required
  204. to use smart pointers. Any dynamic memory allocation must be handled by a
  205. smart pointer such as ``std::unique_ptr`` or ``std::shared_ptr``.
  206. It is allowed to pass raw pointers between objects to enable objects sharing.
  207. A raw pointer **must** not be deleted. Only the object(s) owning the smart
  208. pointer are allowed to delete dynamically allocated memory.
  209. Third Parties
  210. =============
  211. To build CMake, some third parties are needed. Under ``Utilities``
  212. directory, are versions of these third parties which can be used as an
  213. alternate to the ones provided by the system.
  214. To enable the selection of the third parties between the system and CMake ones,
  215. in CMake sources, third parties headers must be prefixed by ``cm3p/``
  216. (for example: ``<cm3p/json/reader.h>``). These wrappers are located under
  217. ``Utilities/cm3p`` directory.
  218. Source Tree Layout
  219. ==================
  220. The CMake source tree is organized as follows.
  221. * ``Auxiliary/``:
  222. Shell and editor integration files.
  223. * ``Help/``:
  224. Documentation. See the `CMake Documentation Guide`_.
  225. * ``Help/dev/``:
  226. Developer documentation.
  227. * ``Help/release/dev/``:
  228. Release note snippets for development since last release.
  229. * ``Licenses/``:
  230. License files for third-party libraries in binary distributions.
  231. * ``Modules/``:
  232. CMake language modules installed with CMake.
  233. * ``Packaging/``:
  234. Files used for packaging CMake itself for distribution.
  235. * ``Source/``:
  236. Source code of CMake itself.
  237. * ``Templates/``:
  238. Files distributed with CMake as implementation details for generators,
  239. packagers, etc.
  240. * ``Tests/``:
  241. The test suite. See `Tests/README.rst`_.
  242. To run the tests, see the `CMake Testing Guide`_.
  243. * ``Utilities/``:
  244. Scripts, third-party source code.
  245. * ``Utilities/std/cm``:
  246. Support files for various C++ standards.
  247. * ``Utilities/std/cmext``:
  248. Extensions to the C++ STL.
  249. * ``Utilities/cm3p``:
  250. Public headers for third parties needed to build CMake.
  251. * ``Utilities/Sphinx/``:
  252. Sphinx configuration to build CMake user documentation.
  253. * ``Utilities/Release/``:
  254. Scripts used to package CMake itself for distribution on ``cmake.org``.
  255. See `Utilities/Release/README.rst`_.
  256. .. _`CMake Documentation Guide`: documentation.rst
  257. .. _`CMake Testing Guide`: testing.rst
  258. .. _`Tests/README.rst`: ../../Tests/README.rst
  259. .. _`Utilities/Release/README.rst`: ../../Utilities/Release/README.rst