| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 | .. cmake-manual-description: CMake C++ Modules Support Referencecmake-cxxmodules(7)*******************.. versionadded:: 3.28C++ 20 introduced the concept of "modules" to the language.  The designrequires build systems to order compilations among each other to satisfy``import`` statements reliably.  CMake's implementation asks the compilerto scan source files for module dependencies during the build, collatesscanning results to infer ordering constraints, and tells the build toolhow to dynamically update the build graph.Compilation Strategy====================With C++ modules, compiling a set of C++ sources is no longer embarrassinglyparallel. That is, any given source may first require the compilation ofanother source file first in order to provide a "CMI" (compiled moduleinterface) or "BMI" (binary module interface) that C++ compilers use tosatisfy ``import`` statements in other sources. With headers, sources couldshare their declarations so that any consumers could compile independently.With modules, declarations are now generated into these BMI files by thecompiler during compilation based on the contents of the source file and its``export`` statements.The order necessary for compilation requires build-time resolution of theordering because the order is controlled by the contents of the sources. Thismeans that the ordering needs extracted from the source during the build toavoid regenerating the build graph via a configure and generate phase forevery source change to get a correct build.The general strategy is to use a "scanner" to extract the ordering dependencyinformation and update the build graph with new edges between existing edgesby taking the per-source scan results (represented by `P1689R5`_ files) and"collating" the dependencies within a target and to modules produced bytargets visible to the target. The primary task is to generate "module map"files to pass to each compile rule with the paths to the BMIs needed tosatisfy ``import`` statements. The collator also has tasks to use thebuild-time information to fill out information including ``install`` rules forthe module interface units, their BMIs, and properties for any exportedtargets with C++ modules... _`P1689R5`: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html.. note:   CMake is focusing on correct builds before looking at performance   improvements. There are known tactics within the chosen strategy which may   offer build performance improvements. However, they are being deferred   until we have a working model against which to compare them. It is also   important to note that a tactic useful in one situation (e.g., clean   builds) may not be performant in a different situation (e.g., incremental   builds). Finding a balance and offering controls to select the tactics is   future work.Scanning Control================Whether or not sources get scanned for C++ module usage is dependent on thefollowing queries. The first query that provides a yes/no answer is used.- If the source file belongs to a file set of type ``CXX_MODULES``, it will  be scanned.- If the target does not use at least C++ 20, it will not be scanned.- If the source file is not the language ``CXX``, it will not be scanned.- If the :prop_sf:`CXX_SCAN_FOR_MODULES` source file property is set, its  value will be used.- If the :prop_tgt:`CXX_SCAN_FOR_MODULES` target property is set, its value  will be used.  Set the :variable:`CMAKE_CXX_SCAN_FOR_MODULES` variable  to initialize this property on all targets as they are created.- Otherwise, the source file will be scanned if the compiler and generator  support scanning.  See policy :policy:`CMP0155`.Note that any scanned source will be excluded from any unity build (see:prop_tgt:`UNITY_BUILD`) because module-related statements can only happen atone place within a C++ translation unit.Compiler Support================Compilers which CMake natively supports module dependency scanning include:* MSVC toolset 14.34 and newer (provided with Visual Studio 17.4 and newer)* LLVM/Clang 16.0 and newer* GCC 14 (for the in-development branch, after 2023-09-20) and newer``import std`` Support======================Support for ``import std`` is limited to the following toolchain and standardlibrary combinations:Generator Support=================The list of generators which support scanning sources for C++ modules include:- :generator:`Ninja`- :generator:`Ninja Multi-Config`- :generator:`Visual Studio 17 2022`Limitations-----------There are a number of known limitations of the current C++ module support inCMake.  This does not document known limitations or bugs in compilers as thesecan change over time.For all generators:- Header units are not supported.- No builtin support for ``import std;`` or other compiler-provided modules.For the Ninja Generators:- ``ninja`` 1.11 or newer is required.For the :ref:`Visual Studio Generators`:- Only Visual Studio 2022 and MSVC toolsets 14.34 (Visual Studio  17.4) and newer.- No support for exporting or installing BMI or module information.- No support for compiling BMIs from ``IMPORTED`` targets with C++ modules  (including ``import std``).- No diagnosis of using modules provided by ``PRIVATE`` sources from  ``PUBLIC`` module sources.
 |