experimental.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: ``ac01f462-0f5f-432a-86aa-acef252918a6``
  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. In order to activate CMake's experimental support for C++20 module
  44. dependencies, set the following variables:
  45. ``CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API``
  46. Set this to the UUID documented above.
  47. Some compilers already have support for module dependency scanning:
  48. * MSVC 19.34 and newer (provided with Visual Studio 17.4 and newer)
  49. * LLVM/Clang 16.0 and newer
  50. For those, only the above variables need to be set by project code.
  51. For compilers with in-development support, additional variables must
  52. be set as follows.
  53. ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE``
  54. Set this to tell CMake how to invoke the C++20 module dependency
  55. scanning tool.
  56. ``CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT``
  57. Set this for compilers that generate module maps. See below.
  58. ``CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG``
  59. Set this for compilers that generate module maps. See below.
  60. For example, add code like the following to a test project:
  61. .. code-block:: cmake
  62. string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
  63. "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE>"
  64. " -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
  65. " ${flags_to_scan_deps} -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
  66. )
  67. The tool specified by ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` is
  68. expected to process the translation unit, write preprocessor dependencies
  69. to the file specified by the ``<DEP_FILE>`` placeholder, and write module
  70. dependencies to the file specified by the ``<DYNDEP_FILE>`` placeholder. The
  71. ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_DEPFILE_FORMAT`` file may be set to ``msvc``
  72. for scandep rules which use ``msvc``-style dependency reporting.
  73. In order to support ``IMPORTED`` targets with associated C++20 module sources,
  74. the ``CMAKE_EXPERIMENTAL_CXX_MODULE_BMI_ONLY_FLAG`` variable must be provided
  75. to have the compiler only output a BMI instead of a BMI and an object file.
  76. The module dependencies should be written in the format described
  77. by the `P1689r5`_ paper.
  78. Compiler writers may try out their scanning functionality using
  79. the `cxx-modules-sandbox`_ test project, modified to set variables
  80. as above for their compiler.
  81. For compilers that generate module maps, tell CMake as follows:
  82. .. code-block:: cmake
  83. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")
  84. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
  85. "${compiler_flags_for_module_map} -fmodule-mapper=<MODULE_MAP_FILE>")
  86. set(CMAKE_EXPERIMENTAL_CXX_MODULE_BMI_ONLY_FLAG
  87. "-fmodule-only")
  88. Currently, the only supported formats are, ``clang``, ``gcc``, and ``msvc``.
  89. The ``gcc`` format is described in the GCC documentation, but the relevant
  90. section for the purposes of CMake is:
  91. A mapping file consisting of space-separated module-name, filename
  92. pairs, one per line. Only the mappings for the direct imports and any
  93. module export name need be provided. If other mappings are provided,
  94. they override those stored in any imported CMI files. A repository
  95. root may be specified in the mapping file by using ``$root`` as the
  96. module name in the first active line.
  97. -- GCC module mapper documentation
  98. The ``msvc`` format is a response file containing flags required to compile
  99. any module interfaces properly as well as find any required files to satisfy
  100. ``import`` statements as required for Microsoft's Visual Studio toolchains.
  101. Similarly, the ``clang`` format is a response file containing flags using
  102. Clang's module flags.
  103. .. _`D1483r1`: https://mathstuf.fedorapeople.org/fortran-modules/fortran-modules.html
  104. .. _`P1689r5`: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html
  105. .. _`cxx-modules-sandbox`: https://github.com/mathstuf/cxx-modules-sandbox