Ninja Multi-Config.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Ninja Multi-Config
  2. ------------------
  3. Generates multiple ``build-<Config>.ninja`` files.
  4. This generator is very much like the :generator:`Ninja` generator, but with
  5. some key differences. Only these differences will be discussed in this
  6. document.
  7. Unlike the :generator:`Ninja` generator, ``Ninja Multi-Config`` generates
  8. multiple configurations at once with :variable:`CMAKE_CONFIGURATION_TYPES`
  9. instead of only one configuration with :variable:`CMAKE_BUILD_TYPE`. One
  10. ``build-<Config>.ninja`` file will be generated for each of these
  11. configurations (with ``<Config>`` being the configuration name.) These files
  12. are intended to be run with ``ninja -f build-<Config>.ninja``. A
  13. ``build.ninja`` file is also generated, using the configuration from either
  14. :variable:`CMAKE_DEFAULT_BUILD_TYPE` or the first item from
  15. :variable:`CMAKE_CONFIGURATION_TYPES`.
  16. ``cmake --build . --config <Config>`` will always use ``build-<Config>.ninja``
  17. to build. If no ``--config`` argument is specified, ``cmake --build .`` will
  18. default to ``build-Debug.ninja``, unless a ``build.ninja`` is generated (see
  19. below), in which case that will be used instead.
  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. See the variable's
  30. documentation for more information.
  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. This variable activates cross-config mode.
  35. Targets from each config specified in this variable can be built from any
  36. ``build-<Config>.ninja`` file. Custom commands will use the configuration
  37. native to ``build-<Config>.ninja``. If it is set to ``all``, all
  38. configurations from :variable:`CMAKE_CONFIGURATION_TYPES` are cross-configs.
  39. If it is not specified, or empty, each ``build-<Config>.ninja`` file will
  40. only contain build rules for its own configuration.
  41. The value of this variable must be a subset of
  42. :variable:`CMAKE_CONFIGURATION_TYPES`.
  43. :variable:`CMAKE_DEFAULT_BUILD_TYPE`
  44. Specifies the configuration to use by default in a ``build.ninja`` file. If
  45. this variable is specified, ``build.ninja`` uses build rules from
  46. ``build-<Config>.ninja`` by default. All custom commands are executed with
  47. this configuration. If the variable is not specified, the first item from
  48. :variable:`CMAKE_CONFIGURATION_TYPES` is used instead.
  49. The value of this variable must be one of the items from
  50. :variable:`CMAKE_CONFIGURATION_TYPES`.
  51. :variable:`CMAKE_DEFAULT_CONFIGS`
  52. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  53. configurations to build for a target in ``build.ninja``
  54. if no ``:<Config>`` suffix is specified. If it is set to ``all``, all
  55. configurations from :variable:`CMAKE_CROSS_CONFIGS` are used. If
  56. it is not specified, it defaults to
  57. :variable:`CMAKE_DEFAULT_BUILD_TYPE`.
  58. For example, if you set
  59. :variable:`CMAKE_DEFAULT_BUILD_TYPE` to ``Release``, but
  60. set :variable:`CMAKE_DEFAULT_CONFIGS` to ``Debug`` or ``all``,
  61. all ``<target>`` aliases in ``build.ninja`` will resolve to
  62. ``<target>:Debug`` or ``<target>:all``, but custom commands will still use
  63. the ``Release`` configuration.
  64. The value of this variable must be a subset of
  65. :variable:`CMAKE_CROSS_CONFIGS` or be the same as
  66. :variable:`CMAKE_DEFAULT_BUILD_TYPE`. It must not be
  67. specified if :variable:`CMAKE_DEFAULT_BUILD_TYPE` or
  68. :variable:`CMAKE_CROSS_CONFIGS` is not used.
  69. Consider the following example:
  70. .. code-block:: cmake
  71. cmake_minimum_required(VERSION 3.16)
  72. project(MultiConfigNinja C)
  73. add_executable(generator generator.c)
  74. add_custom_command(OUTPUT generated.c COMMAND generator generated.c)
  75. add_library(generated ${CMAKE_BINARY_DIR}/generated.c)
  76. Now assume you configure the project with ``Ninja Multi-Config`` and run one of
  77. the following commands:
  78. .. code-block:: shell
  79. ninja -f build-Debug.ninja generated
  80. # OR
  81. cmake --build . --config Debug --target generated
  82. This would build the ``Debug`` configuration of ``generator``, which would be
  83. used to generate ``generated.c``, which would be used to build the ``Debug``
  84. configuration of ``generated``.
  85. But if :variable:`CMAKE_CROSS_CONFIGS` is set to ``all``, and you run the
  86. following instead:
  87. .. code-block:: shell
  88. ninja -f build-Release.ninja generated:Debug
  89. # OR
  90. cmake --build . --config Release --target generated:Debug
  91. This would build the ``Release`` configuration of ``generator``, which would be
  92. used to generate ``generated.c``, which would be used to build the ``Debug``
  93. configuration of ``generated``. This is useful for running a release-optimized
  94. version of a generator utility while still building the debug version of the
  95. targets built with the generated code.