Ninja Multi-Config.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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``. No
  13. ``build.ninja`` file is generated by default (see below for how to generate
  14. it.)
  15. ``cmake --build . --config <Config>`` will always use ``build-<Config>.ninja``
  16. to build. If no ``--config`` argument is specified, ``cmake --build .`` will
  17. default to ``build-Debug.ninja``, unless a ``build.ninja`` is generated (see
  18. below), in which case that will be used instead.
  19. Each ``build-<Config>.ninja`` file contains ``<target>`` targets as well as
  20. ``<target>:<Config>`` targets, where ``<Config>`` is the same as the
  21. configuration specified in ``build-<Config>.ninja`` Additionally, if
  22. cross-config mode is enabled, ``build-<Config>.ninja`` may contain
  23. ``<target>:<OtherConfig>`` targets, where ``<OtherConfig>`` is a cross-config,
  24. as well as ``<target>:all``, which builds the target in all cross-configs. See
  25. below for how to enable cross-config mode.
  26. The ``Ninja Multi-Config`` generator recognizes the following variables:
  27. :variable:`CMAKE_CONFIGURATION_TYPES`
  28. Specifies the total set of configurations to build. See the variable's
  29. documentation for more information.
  30. :variable:`CMAKE_NMC_CROSS_CONFIGS`
  31. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  32. configurations available from all ``build-<Config>.ninja`` files.
  33. This variable activates cross-config mode.
  34. Targets from each config specified in this variable can be built from any
  35. ``build-<Config>.ninja`` file. Custom commands will use the configuration
  36. native to ``build-<Config>.ninja``. If it is set to ``all``, all
  37. configurations from :variable:`CMAKE_CONFIGURATION_TYPES` are cross-configs.
  38. If it is not specified, or empty, each ``build-<Config>.ninja`` file will
  39. only contain build rules for its own configuration.
  40. The value of this variable must be a subset of
  41. :variable:`CMAKE_CONFIGURATION_TYPES`.
  42. :variable:`CMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG`
  43. Specifies the configuration to use by default in a ``build.ninja`` file. If
  44. this variable is specified, a ``build.ninja`` file is generated which uses
  45. build rules from ``build-<Config>.ninja`` by default. All custom commands are
  46. executed with this configuration. If the variable is not specified, no
  47. ``build.ninja`` file is generated.
  48. The value of this variable must be one of the items from
  49. :variable:`CMAKE_CONFIGURATION_TYPES`.
  50. :variable:`CMAKE_NMC_DEFAULT_CONFIGS`
  51. Specifies a :ref:`semicolon-separated list <CMake Language Lists>` of
  52. configurations to build for a target in ``build.ninja``
  53. if no ``:<Config>`` suffix is specified. If it is set to ``all``, all
  54. configurations from :variable:`CMAKE_NMC_CROSS_CONFIGS` are used. If
  55. it is not specified, it defaults to
  56. :variable:`CMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG`.
  57. For example, if you set
  58. :variable:`CMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG` to ``Release``, but
  59. set :variable:`CMAKE_NMC_DEFAULT_CONFIGS` to ``Debug`` or ``all``,
  60. all ``<target>`` aliases in ``build.ninja`` will resolve to
  61. ``<target>:Debug`` or ``<target>:all``, but custom commands will still use
  62. the ``Release`` configuration.
  63. The value of this variable must be a subset of
  64. :variable:`CMAKE_NMC_CROSS_CONFIGS` or be the same as
  65. :variable:`CMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG`. It must not be
  66. specified if :variable:`CMAKE_NMC_DEFAULT_BUILD_FILE_CONFIG` or
  67. :variable:`CMAKE_NMC_CROSS_CONFIGS` is not used.
  68. Consider the following example:
  69. .. code-block:: cmake
  70. cmake_minimum_required(VERSION 3.16)
  71. project(MultiConfigNinja C)
  72. add_executable(generator generator.c)
  73. add_custom_command(OUTPUT generated.c COMMAND generator generated.c)
  74. add_library(generated ${CMAKE_BINARY_DIR}/generated.c)
  75. Now assume you configure the project with ``Ninja Multi-Config`` and run one of
  76. the following commands:
  77. .. code-block:: shell
  78. ninja -f build-Debug.ninja generated
  79. # OR
  80. cmake --build . --config Debug --target generated
  81. This would build the ``Debug`` configuration of ``generator``, which would be
  82. used to generate ``generated.c``, which would be used to build the ``Debug``
  83. configuration of ``generated``.
  84. But if :variable:`CMAKE_NMC_CROSS_CONFIGS` is set to ``all``, and you
  85. run the following instead:
  86. .. code-block:: shell
  87. ninja -f build-Release.ninja generated:Debug
  88. # OR
  89. cmake --build . --config Release --target generated:Debug
  90. This would build the ``Release`` configuration of ``generator``, which would be
  91. used to generate ``generated.c``, which would be used to build the ``Debug``
  92. configuration of ``generated``. This is useful for running a release-optimized
  93. version of a generator utility while still building the debug version of the
  94. targets built with the generated code.