experimental.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 and the ``CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP`` variables need to be
  35. set.
  36. For example, add code like the following to a test project:
  37. .. code-block:: cmake
  38. set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
  39. string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
  40. "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE>"
  41. " -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
  42. " ${flags_to_scan_deps} -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
  43. )
  44. The tool specified by ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE`` is
  45. expected to process the translation unit, write preprocessor dependencies
  46. to the file specified by the ``<DEP_FILE>`` placeholder, and write module
  47. dependencies to the file specified by the ``<DYNDEP_FILE>`` placeholder. The
  48. ``CMAKE_EXPERIMENTAL_CXX_SCANDEP_DEPFILE_FORMAT`` file may be set to ``msvc``
  49. for scandep rules which use ``msvc``-style dependency reporting.
  50. The module dependencies should be written in the format described
  51. by the `P1689r5`_ paper.
  52. Compiler writers may try out their scanning functionality using
  53. the `cxx-modules-sandbox`_ test project, modified to set variables
  54. as above for their compiler.
  55. For compilers that generate module maps, tell CMake as follows:
  56. .. code-block:: cmake
  57. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")
  58. set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
  59. "${compiler_flags_for_module_map} -fmodule-mapper=<MODULE_MAP_FILE>")
  60. Currently, the only supported formats are ``gcc`` and ``msvc``. The ``gcc``
  61. format is described in the GCC documentation, but the relevant section for the
  62. purposes of CMake is:
  63. A mapping file consisting of space-separated module-name, filename
  64. pairs, one per line. Only the mappings for the direct imports and any
  65. module export name need be provided. If other mappings are provided,
  66. they override those stored in any imported CMI files. A repository
  67. root may be specified in the mapping file by using ``$root`` as the
  68. module name in the first active line.
  69. -- GCC module mapper documentation
  70. The ``msvc`` format is a response file containing flags required to compile
  71. any module interfaces properly as well as find any required files to satisfy
  72. ``import`` statements as required for Microsoft's Visual Studio toolchains.
  73. .. _`D1483r1`: https://mathstuf.fedorapeople.org/fortran-modules/fortran-modules.html
  74. .. _`P1689r5`: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html
  75. .. _`cxx-modules-sandbox`: https://github.com/mathstuf/cxx-modules-sandbox