cmake-cxxmodules.7.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .. cmake-manual-description: CMake C++ Modules Support Reference
  2. cmake-cxxmodules(7)
  3. *******************
  4. .. versionadded:: 3.28
  5. C++ 20 introduced the concept of "modules" to the language. The design
  6. requires build systems to order compilations among each other to satisfy
  7. ``import`` statements reliably. CMake's implementation asks the compiler
  8. to scan source files for module dependencies during the build, collates
  9. scanning results to infer ordering constraints, and tells the build tool
  10. how to dynamically update the build graph.
  11. Compilation Strategy
  12. ====================
  13. With C++ modules, compiling a set of C++ sources is no longer embarrassingly
  14. parallel. That is, any given source may first require the compilation of
  15. another source file first in order to provide a "CMI" (compiled module
  16. interface) or "BMI" (binary module interface) that C++ compilers use to
  17. satisfy ``import`` statements in other sources. With headers, sources could
  18. share their declarations so that any consumers could compile independently.
  19. With modules, declarations are now generated into these BMI files by the
  20. compiler during compilation based on the contents of the source file and its
  21. ``export`` statements.
  22. The order necessary for compilation requires build-time resolution of the
  23. ordering because the order is controlled by the contents of the sources. This
  24. means that the ordering needs extracted from the source during the build to
  25. avoid regenerating the build graph via a configure and generate phase for
  26. every source change to get a correct build.
  27. The general strategy is to use a "scanner" to extract the ordering dependency
  28. information and update the build graph with new edges between existing edges
  29. by taking the per-source scan results (represented by `P1689R5`_ files) and
  30. "collating" the dependencies within a target and to modules produced by
  31. targets visible to the target. The primary task is to generate "module map"
  32. files to pass to each compile rule with the paths to the BMIs needed to
  33. satisfy ``import`` statements. The collator also has tasks to use the
  34. build-time information to fill out information including ``install`` rules for
  35. the module interface units, their BMIs, and properties for any exported
  36. targets with C++ modules.
  37. .. _`P1689R5`: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html
  38. .. note::
  39. CMake is focusing on correct builds before looking at performance
  40. improvements. There are known tactics within the chosen strategy which may
  41. offer build performance improvements. However, they are being deferred
  42. until we have a working model against which to compare them. It is also
  43. important to note that a tactic useful in one situation (e.g., clean
  44. builds) may not be performant in a different situation (e.g., incremental
  45. builds). Finding a balance and offering controls to select the tactics is
  46. future work.
  47. Scanning Control
  48. ================
  49. Whether or not sources get scanned for C++ module usage is dependent on the
  50. following queries. The first query that provides a yes/no answer is used.
  51. - If the source file belongs to a file set of type ``CXX_MODULES``, it will
  52. be scanned.
  53. - If the target does not use at least C++ 20, it will not be scanned.
  54. - If the source file is not the language ``CXX``, it will not be scanned.
  55. - If the :prop_sf:`CXX_SCAN_FOR_MODULES` source file property is set, its
  56. value will be used.
  57. - If the :prop_tgt:`CXX_SCAN_FOR_MODULES` target property is set, its value
  58. will be used. Set the :variable:`CMAKE_CXX_SCAN_FOR_MODULES` variable
  59. to initialize this property on all targets as they are created.
  60. - Otherwise, the source file will be scanned if the compiler and generator
  61. support scanning. See policy :policy:`CMP0155`.
  62. Note that any scanned source will be excluded from any unity build (see
  63. :prop_tgt:`UNITY_BUILD`) because module-related statements can only happen at
  64. one place within a C++ translation unit.
  65. Compiler Support
  66. ================
  67. Compilers which CMake natively supports module dependency scanning include:
  68. * MSVC toolset 14.34 and newer (provided with Visual Studio 17.4 and newer)
  69. * LLVM/Clang 16.0 and newer
  70. * GCC 14 (for the in-development branch, after 2023-09-20) and newer
  71. ``import std`` Support
  72. ======================
  73. Support for ``import std`` is limited to the following toolchain and standard
  74. library combinations:
  75. * Clang 18.1.2 and newer with ``-stdlib=libc++`` or ``-stdlib=libstdc++``
  76. * MSVC toolset 14.36 and newer (provided with Visual Studio 17.6 Preview 2 and
  77. newer)
  78. * GCC 15 and newer.
  79. The :variable:`CMAKE_CXX_COMPILER_IMPORT_STD` variable may be used to detect
  80. support for a standard level with the active C++ toolchain.
  81. .. note::
  82. This support is provided only when experimental support for
  83. ``import std;`` has been enabled by the
  84. ``CMAKE_EXPERIMENTAL_CXX_IMPORT_STD`` gate.
  85. Generator Support
  86. =================
  87. The list of generators which support scanning sources for C++ modules include:
  88. - :generator:`Ninja`
  89. - :generator:`Ninja Multi-Config`
  90. - :generator:`Visual Studio 17 2022`
  91. Limitations
  92. -----------
  93. There are a number of known limitations of the current C++ module support in
  94. CMake. This does not document known limitations or bugs in compilers as these
  95. can change over time.
  96. For all generators:
  97. - Header units are not supported.
  98. - No builtin support for ``import std;`` or other compiler-provided modules.
  99. For the Ninja Generators:
  100. - ``ninja`` 1.11 or newer is required.
  101. For the :ref:`Visual Studio Generators`:
  102. - Only Visual Studio 2022 and MSVC toolsets 14.34 (Visual Studio
  103. 17.4) and newer.
  104. - No support for exporting or installing BMI or module information.
  105. - No support for compiling BMIs from ``IMPORTED`` targets with C++ modules
  106. (including ``import std``).
  107. - No diagnosis of using modules provided by ``PRIVATE`` sources from
  108. ``PUBLIC`` module sources.