Ninja Multi-Config.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Ninja Multi-Config
  2. ------------------
  3. .. versionadded:: 3.17
  4. Generates multiple ``build-<Config>.ninja`` files.
  5. This generator is very much like the :generator:`Ninja` generator, but with
  6. some key differences. Only these differences will be discussed in this
  7. document.
  8. Unlike the :generator:`Ninja` generator, ``Ninja Multi-Config`` generates
  9. multiple configurations at once with :variable:`CMAKE_CONFIGURATION_TYPES`
  10. instead of only one configuration with :variable:`CMAKE_BUILD_TYPE`. One
  11. ``build-<Config>.ninja`` file will be generated for each of these
  12. configurations (with ``<Config>`` being the configuration name.) These files
  13. are intended to be run with ``ninja -f build-<Config>.ninja``. A
  14. ``build.ninja`` file is also generated, using the configuration from either
  15. :variable:`CMAKE_DEFAULT_BUILD_TYPE` or the first item from
  16. :variable:`CMAKE_CONFIGURATION_TYPES`.
  17. ``cmake --build . --config <Config>`` will always use ``build-<Config>.ninja``
  18. to build. If no ``--config`` argument is specified, ``cmake --build .`` will
  19. use ``build.ninja``.
  20. Each ``build-<Config>.ninja`` file contains ``<target>`` targets as well as
  21. ``<target>:<Config>`` targets, where ``<Config>`` is the same as the
  22. configuration specified in ``build-<Config>.ninja`` Additionally, if
  23. cross-config mode is enabled, ``build-<Config>.ninja`` may contain
  24. ``<target>:<OtherConfig>`` targets, where ``<OtherConfig>`` is a cross-config,
  25. as well as ``<target>:all``, which builds the target in all cross-configs. See
  26. below for how to enable cross-config mode.
  27. The ``Ninja Multi-Config`` generator recognizes the following variables:
  28. :variable:`CMAKE_CONFIGURATION_TYPES`
  29. Specifies the total set of configurations to build.
  30. :variable:`CMAKE_CROSS_CONFIGS`
  31. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  32. configurations available from all ``build-<Config>.ninja`` files.
  33. :variable:`CMAKE_DEFAULT_BUILD_TYPE`
  34. Specifies the configuration to use by default in a ``build.ninja`` file.
  35. :variable:`CMAKE_DEFAULT_CONFIGS`
  36. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  37. configurations to build for a target in ``build.ninja``
  38. if no ``:<Config>`` suffix is specified.
  39. Consider the following example:
  40. .. code-block:: cmake
  41. cmake_minimum_required(VERSION 3.16)
  42. project(MultiConfigNinja C)
  43. add_executable(generator generator.c)
  44. add_custom_command(OUTPUT generated.c COMMAND generator generated.c)
  45. add_library(generated ${CMAKE_BINARY_DIR}/generated.c)
  46. Now assume you configure the project with ``Ninja Multi-Config`` and run one of
  47. the following commands:
  48. .. code-block:: shell
  49. ninja -f build-Debug.ninja generated
  50. # OR
  51. cmake --build . --config Debug --target generated
  52. This would build the ``Debug`` configuration of ``generator``, which would be
  53. used to generate ``generated.c``, which would be used to build the ``Debug``
  54. configuration of ``generated``.
  55. But if :variable:`CMAKE_CROSS_CONFIGS` is set to ``all``, and you run the
  56. following instead:
  57. .. code-block:: shell
  58. ninja -f build-Release.ninja generated:Debug
  59. # OR
  60. cmake --build . --config Release --target generated:Debug
  61. This would build the ``Release`` configuration of ``generator``, which would be
  62. used to generate ``generated.c``, which would be used to build the ``Debug``
  63. configuration of ``generated``. This is useful for running a release-optimized
  64. version of a generator utility while still building the debug version of the
  65. targets built with the generated code.
  66. Custom Commands
  67. ^^^^^^^^^^^^^^^
  68. .. versionadded:: 3.20
  69. The ``Ninja Multi-Config`` generator adds extra capabilities to
  70. :command:`add_custom_command` and :command:`add_custom_target` through its
  71. cross-config mode. The ``COMMAND``, ``DEPENDS``, and ``WORKING_DIRECTORY``
  72. arguments can be evaluated in the context of either the "command config" (the
  73. "native" configuration of the ``build-<Config>.ninja`` file in use) or the
  74. "output config" (the configuration used to evaluate the ``OUTPUT`` and
  75. ``BYPRODUCTS``).
  76. If either ``OUTPUT`` or ``BYPRODUCTS`` names a path that is common to
  77. more than one configuration (e.g. it does not use any generator expressions),
  78. all arguments are evaluated in the command config by default.
  79. If all ``OUTPUT`` and ``BYPRODUCTS`` paths are unique to each configuration
  80. (e.g. by using the ``$<CONFIG>`` generator expression), the first argument of
  81. ``COMMAND`` is still evaluated in the command config by default, while all
  82. subsequent arguments, as well as the arguments to ``DEPENDS`` and
  83. ``WORKING_DIRECTORY``, are evaluated in the output config. These defaults can
  84. be overridden with the ``$<OUTPUT_CONFIG:...>`` and ``$<COMMAND_CONFIG:...>``
  85. generator-expressions. Note that if a target is specified by its name in
  86. ``DEPENDS``, or as the first argument of ``COMMAND``, it is always evaluated
  87. in the command config, even if it is wrapped in ``$<OUTPUT_CONFIG:...>``
  88. (because its plain name is not a generator expression).
  89. As an example, consider the following:
  90. .. code-block:: cmake
  91. add_custom_command(
  92. OUTPUT "$<CONFIG>.txt"
  93. COMMAND generator "$<CONFIG>.txt" "$<OUTPUT_CONFIG:$<CONFIG>>" "$<COMMAND_CONFIG:$<CONFIG>>"
  94. DEPENDS tgt1 "$<TARGET_FILE:tgt2>" "$<OUTPUT_CONFIG:$<TARGET_FILE:tgt3>>" "$<COMMAND_CONFIG:$<TARGET_FILE:tgt4>>"
  95. )
  96. Assume that ``generator``, ``tgt1``, ``tgt2``, ``tgt3``, and ``tgt4`` are all
  97. executable targets, and assume that ``$<CONFIG>.txt`` is built in the ``Debug``
  98. output config using the ``Release`` command config. The ``Release`` build of
  99. the ``generator`` target is called with ``Debug.txt Debug Release`` as
  100. arguments. The command depends on the ``Release`` builds of ``tgt1`` and
  101. ``tgt4``, and the ``Debug`` builds of ``tgt2`` and ``tgt3``.