experimental.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. CMake Experimental Features Guide
  2. *********************************
  3. The following is a guide to CMake experimental features that are
  4. under development and not yet included in official documentation.
  5. See documentation on `CMake Development`_ for more information.
  6. .. _`CMake Development`: README.rst
  7. Features are gated behind ``CMAKE_EXPERIMENTAL_`` variables which must be set
  8. to specific values in order to enable their gated behaviors. Note that the
  9. specific values will change over time to reinforce their experimental nature.
  10. When used, a warning will be generated to indicate that an experimental
  11. feature is in use and that the affected behavior in the project is not part of
  12. CMake's stability guarantees.
  13. C++20 Module APIs
  14. =================
  15. Variable: ``CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API``
  16. Value: ``2182bf5c-ef0d-489a-91da-49dbc3090d2a``
  17. In order to support C++20 modules, there are a number of behaviors that have
  18. CMake APIs to provide the required features to build and export them from a
  19. project.
  20. Limitations
  21. -----------
  22. There are a number of known limitations of the current C++20 module support in
  23. CMake. This does not document known limitations or bugs in compilers as these
  24. can change over time.
  25. For all generators:
  26. - Only in-project modules may be used. While there is some support for
  27. exporting module information, there is no mechanism for using it at the
  28. moment.
  29. For the Ninja Generators:
  30. - ``ninja`` 1.10 or newer is required.
  31. For the Visual Studio Generators:
  32. - Only Visual Studio 2022 and toolchains newer than 19.34 (Visual Studio
  33. 17.4).
  34. - No support for exporting or installing BMI or module information.
  35. - No diagnosis of using modules provided by ``PRIVATE`` sources from
  36. ``PUBLIC`` module sources.
  37. C++20 Module Dependencies
  38. =========================
  39. The Ninja generator has experimental infrastructure supporting C++20 module
  40. dependency scanning. This is similar to the Fortran modules support, but
  41. relies on external tools to scan C++20 translation units for module
  42. dependencies. The approach is described by Kitware's `D1483r1`_ paper.
  43. The ``CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP`` variable can be set to ``1``
  44. in order to activate this undocumented experimental infrastructure. This
  45. is **intended to make the functionality available to compiler writers** so
  46. they can use it to develop and test their dependency scanning tool.
  47. The ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` variable must also be set
  48. to tell CMake how to invoke the C++20 module dependency scanning tool.
  49. MSVC 19.34 (provided with Visual Studio 17.4) and above contains the support
  50. that CMake needs and has these variables already set up as required and only
  51. the UUID and the ``CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP`` variables need to be
  52. set.
  53. For example, add code like the following to a test project:
  54. .. code-block:: cmake
  55. set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
  56. string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
  57. "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE>"
  58. " -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
  59. " ${flags_to_scan_deps} -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
  60. )
  61. The tool specified by ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` is
  62. expected to process the translation unit, write preprocessor dependencies
  63. to the file specified by the ``<DEP_FILE>`` placeholder, and write module
  64. dependencies to the file specified by the ``<DYNDEP_FILE>`` placeholder. The
  65. ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_DEPFILE_FORMAT`` file may be set to ``msvc``
  66. for scandep rules which use ``msvc``-style dependency reporting.
  67. The module dependencies should be written in the format described
  68. by the `P1689r5`_ paper.
  69. Compiler writers may try out their scanning functionality using
  70. the `cxx-modules-sandbox`_ test project, modified to set variables
  71. as above for their compiler.
  72. For compilers that generate module maps, tell CMake as follows:
  73. .. code-block:: cmake
  74. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")
  75. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
  76. "${compiler_flags_for_module_map} -fmodule-mapper=<MODULE_MAP_FILE>")
  77. Currently, the only supported formats are, ``clang``, ``gcc``, and ``msvc``.
  78. The ``gcc`` format is described in the GCC documentation, but the relevant
  79. section for the purposes of CMake is:
  80. A mapping file consisting of space-separated module-name, filename
  81. pairs, one per line. Only the mappings for the direct imports and any
  82. module export name need be provided. If other mappings are provided,
  83. they override those stored in any imported CMI files. A repository
  84. root may be specified in the mapping file by using ``$root`` as the
  85. module name in the first active line.
  86. -- GCC module mapper documentation
  87. The ``msvc`` format is a response file containing flags required to compile
  88. any module interfaces properly as well as find any required files to satisfy
  89. ``import`` statements as required for Microsoft's Visual Studio toolchains.
  90. Similarly, the ``clang`` format is a response file containing flags using
  91. Clang's module flags.
  92. .. _`D1483r1`: https://mathstuf.fedorapeople.org/fortran-modules/fortran-modules.html
  93. .. _`P1689r5`: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html
  94. .. _`cxx-modules-sandbox`: https://github.com/mathstuf/cxx-modules-sandbox