experimental.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: ``3c375311-a3c9-4396-a187-3227ef642046``
  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. C++20 Module Dependencies
  21. =========================
  22. The Ninja generator has experimental infrastructure supporting C++20 module
  23. dependency scanning. This is similar to the Fortran modules support, but
  24. relies on external tools to scan C++20 translation units for module
  25. dependencies. The approach is described by Kitware's `D1483r1`_ paper.
  26. The ``CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP`` variable can be set to ``1``
  27. in order to activate this undocumented experimental infrastructure. This
  28. is **intended to make the functionality available to compiler writers** so
  29. they can use it to develop and test their dependency scanning tool.
  30. The ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` variable must also be set
  31. to tell CMake how to invoke the C++20 module dependency scanning tool.
  32. MSVC 19.34 (provided with Visual Studio 17.4) and above contains the support
  33. that CMake needs and has these variables already set up as required and only
  34. the UUID variable needs to be set.
  35. For example, add code like the following to a test project:
  36. .. code-block:: cmake
  37. set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
  38. string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
  39. "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE>"
  40. " -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
  41. " ${flags_to_scan_deps} -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
  42. )
  43. The tool specified by ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` is
  44. expected to process the translation unit, write preprocessor dependencies
  45. to the file specified by the ``<DEP_FILE>`` placeholder, and write module
  46. dependencies to the file specified by the ``<DYNDEP_FILE>`` placeholder. The
  47. ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_DEPFILE_FORMAT`` file may be set to ``msvc``
  48. for scandep rules which use ``msvc``-style dependency reporting.
  49. The module dependencies should be written in the format described
  50. by the `P1689r5`_ paper.
  51. Compiler writers may try out their scanning functionality using
  52. the `cxx-modules-sandbox`_ test project, modified to set variables
  53. as above for their compiler.
  54. For compilers that generate module maps, tell CMake as follows:
  55. .. code-block:: cmake
  56. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")
  57. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
  58. "${compiler_flags_for_module_map} -fmodule-mapper=<MODULE_MAP_FILE>")
  59. Currently, the only supported format is ``gcc``. The format is described in
  60. the GCC documentation, but the relevant section for the purposes of CMake is:
  61. A mapping file consisting of space-separated module-name, filename
  62. pairs, one per line. Only the mappings for the direct imports and any
  63. module export name need be provided. If other mappings are provided,
  64. they override those stored in any imported CMI files. A repository
  65. root may be specified in the mapping file by using ``$root`` as the
  66. module name in the first active line.
  67. -- GCC module mapper documentation
  68. .. _`D1483r1`: https://mathstuf.fedorapeople.org/fortran-modules/fortran-modules.html
  69. .. _`P1689r5`: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html
  70. .. _`cxx-modules-sandbox`: https://github.com/mathstuf/cxx-modules-sandbox