UNITY_BUILD

Should the target source files be processed into batches for faster compilation. This feature is known as “Unity build”, or “Jumbo build”.

The C and CXX source files are grouped separately.

This property is initialized by the value of the CMAKE_UNITY_BUILD variable if it is set when a target is created.

Note

It’s not recommended to directly set UNITY_BUILD to ON, but to instead set CMAKE_UNITY_BUILD from the command line. However, it IS recommended to set UNITY_BUILD to OFF if you need to ensure that a target doesn’t get a unity build.

The batch size can be specified by setting UNITY_BUILD_BATCH_SIZE.

The batching of source files is done by adding new sources files wich will #include the source files, and exclude them from building by setting HEADER_FILE_ONLY to ON.

ODR (One definition rule) errors

Since multiple source files are included into one source file, it can lead to ODR errors. This section contains properties which help fixing these errors.

The source files marked by GENERATED will be skipped from unity build. This applies also for the source files marked with SKIP_UNITY_BUILD_INCLUSION.

The source files that have COMPILE_OPTIONS, COMPILE_DEFINITIONS, COMPILE_FLAGS, or INCLUDE_DIRECTORIES will also be skipped.

With the UNITY_BUILD_CODE_BEFORE_INCLUDE and UNITY_BUILD_CODE_AFTER_INCLUDE one can specify code to be injected in the unity source file before and after every #include statement.

Note

The order of source files defined in the CMakeLists.txt will be preserved into the generated unity source files. This can be used to manually enforce a specific grouping based on the UNITY_BUILD_BATCH_SIZE.