UNITY_BUILD.rst 3.6 KB

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