UNITY_BUILD.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. UNITY_BUILD
  2. -----------
  3. .. versionadded:: 3.16
  4. When this property is set to true, the target source files will be combined
  5. into batches for faster compilation. This is done by creating a (set of)
  6. unity sources which ``#include`` the original sources, then compiling these
  7. unity sources instead of the originals. This is known as a *Unity* or *Jumbo*
  8. build.
  9. CMake provides different algorithms for selecting which sources are grouped
  10. together into a *bucket*. Algorithm selection is decided by the
  11. :prop_tgt:`UNITY_BUILD_MODE` target property, which has the following acceptable
  12. values:
  13. * ``BATCH``
  14. When in this mode CMake determines which files are grouped together.
  15. The :prop_tgt:`UNITY_BUILD_BATCH_SIZE` property controls the upper limit on
  16. how many sources can be combined per unity source file.
  17. * ``GROUP``
  18. When in this mode each target explicitly specifies how to group
  19. source files. Each source file that has the same
  20. :prop_sf:`UNITY_GROUP` value will be grouped together. Any sources
  21. that don't have this property will be compiled individually. The
  22. :prop_tgt:`UNITY_BUILD_BATCH_SIZE` property is ignored when using
  23. this mode.
  24. If no explicit :prop_tgt:`UNITY_BUILD_MODE` has been specified, CMake will
  25. default to ``BATCH``.
  26. Unity builds are not currently supported for all languages. CMake version
  27. |release| supports combining ``C`` and ``CXX`` source files. For targets that
  28. mix source files from more than one language, CMake will separate the languages
  29. such that each generated unity source file only contains sources for a single
  30. language.
  31. This property is initialized by the value of the :variable:`CMAKE_UNITY_BUILD`
  32. variable when a target is created.
  33. .. note::
  34. Projects should not directly set the ``UNITY_BUILD`` property or its
  35. associated :variable:`CMAKE_UNITY_BUILD` variable to true. Depending
  36. on the capabilities of the build machine and compiler used, it might or
  37. might not be appropriate to enable unity builds. Therefore, this feature
  38. should be under developer control, which would normally be through the
  39. developer choosing whether or not to set the :variable:`CMAKE_UNITY_BUILD`
  40. variable on the :manual:`cmake(1)` command line or some other equivalent
  41. method. However, it IS recommended to set the ``UNITY_BUILD`` target
  42. property to false if it is known that enabling unity builds for the
  43. target can lead to problems.
  44. ODR (One definition rule) errors
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. When multiple source files are included into one source file, as is done
  47. for unity builds, it can potentially lead to ODR errors. CMake provides
  48. a number of measures to help address such problems:
  49. * Any source file that has a non-empty :prop_sf:`COMPILE_OPTIONS`,
  50. :prop_sf:`COMPILE_DEFINITIONS`, :prop_sf:`COMPILE_FLAGS`, or
  51. :prop_sf:`INCLUDE_DIRECTORIES` source property will not be combined
  52. into a unity source.
  53. * Projects can prevent an individual source file from being combined into
  54. a unity source by setting its :prop_sf:`SKIP_UNITY_BUILD_INCLUSION`
  55. source property to true. This can be a more effective way to prevent
  56. problems with specific files than disabling unity builds for an entire
  57. target.
  58. * Projects can set :prop_tgt:`UNITY_BUILD_UNIQUE_ID` to cause a valid
  59. C-identifier to be generated which is unique per file in a unity
  60. build. This can be used to avoid problems with anonymous namespaces
  61. in unity builds.
  62. * The :prop_tgt:`UNITY_BUILD_CODE_BEFORE_INCLUDE` and
  63. :prop_tgt:`UNITY_BUILD_CODE_AFTER_INCLUDE` target properties can be used
  64. to inject code into the unity source files before and after every
  65. ``#include`` statement.
  66. * The order of source files added to the target via commands like
  67. :command:`add_library`, :command:`add_executable` or
  68. :command:`target_sources` will be preserved in the generated unity source
  69. files. This can be used to manually enforce a specific grouping based on
  70. the :prop_tgt:`UNITY_BUILD_BATCH_SIZE` target property.