Ninja Multi-Config.rst 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. default to ``build-Debug.ninja``, unless a ``build.ninja`` is generated (see
  20. below), in which case that will be used instead.
  21. Each ``build-<Config>.ninja`` file contains ``<target>`` targets as well as
  22. ``<target>:<Config>`` targets, where ``<Config>`` is the same as the
  23. configuration specified in ``build-<Config>.ninja`` Additionally, if
  24. cross-config mode is enabled, ``build-<Config>.ninja`` may contain
  25. ``<target>:<OtherConfig>`` targets, where ``<OtherConfig>`` is a cross-config,
  26. as well as ``<target>:all``, which builds the target in all cross-configs. See
  27. below for how to enable cross-config mode.
  28. The ``Ninja Multi-Config`` generator recognizes the following variables:
  29. :variable:`CMAKE_CONFIGURATION_TYPES`
  30. Specifies the total set of configurations to build.
  31. :variable:`CMAKE_CROSS_CONFIGS`
  32. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  33. configurations available from all ``build-<Config>.ninja`` files.
  34. :variable:`CMAKE_DEFAULT_BUILD_TYPE`
  35. Specifies the configuration to use by default in a ``build.ninja`` file.
  36. :variable:`CMAKE_DEFAULT_CONFIGS`
  37. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  38. configurations to build for a target in ``build.ninja``
  39. if no ``:<Config>`` suffix is specified.
  40. Consider the following example:
  41. .. code-block:: cmake
  42. cmake_minimum_required(VERSION 3.16)
  43. project(MultiConfigNinja C)
  44. add_executable(generator generator.c)
  45. add_custom_command(OUTPUT generated.c COMMAND generator generated.c)
  46. add_library(generated ${CMAKE_BINARY_DIR}/generated.c)
  47. Now assume you configure the project with ``Ninja Multi-Config`` and run one of
  48. the following commands:
  49. .. code-block:: shell
  50. ninja -f build-Debug.ninja generated
  51. # OR
  52. cmake --build . --config Debug --target generated
  53. This would build the ``Debug`` configuration of ``generator``, which would be
  54. used to generate ``generated.c``, which would be used to build the ``Debug``
  55. configuration of ``generated``.
  56. But if :variable:`CMAKE_CROSS_CONFIGS` is set to ``all``, and you run the
  57. following instead:
  58. .. code-block:: shell
  59. ninja -f build-Release.ninja generated:Debug
  60. # OR
  61. cmake --build . --config Release --target generated:Debug
  62. This would build the ``Release`` configuration of ``generator``, which would be
  63. used to generate ``generated.c``, which would be used to build the ``Debug``
  64. configuration of ``generated``. This is useful for running a release-optimized
  65. version of a generator utility while still building the debug version of the
  66. targets built with the generated code.