experimental.rst 3.9 KB

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