# Is there any way to use UNITY\_BUILD with juce cmake products?

**URL:** <https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140>\
**Category:** CMake\
**Created:** [May 8, 2023, 10:04pm UTC](https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140 "2023-05-08T22:04:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [May 8, 2023, 10:04pm UTC](https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140/1 "2023-05-08T22:04:28Z")

</div>

I tried it, and it adds all the juce module source to unity files, which is obviously not what I want, since they already use their own unity files. I want it to just apply to my own sources.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [May 8, 2023, 10:26pm UTC](https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140/2 "2023-05-08T22:26:49Z")

</div>

You can use the property UNITY\_BUILD to enable or disable unity builds on a per-target basis. You can also set the property SKIP\_UNITY\_BUILD\_INCLUSION on individual source files to ensure that they are compiled individually.

One option might be to put all of the JUCE modules into a staticlib target with UNITY\_BUILD explicitly disabled. Then, targets that depend on JUCE can just link that staticlib target. You can find out how (and also find out about the drawbacks of this approach) here:

> [@Using CMake flow to build libraries, Android apps](https://forum.juce.com/t/using-cmake-flow-to-build-libraries-android-apps/40492/2):
>
> At the moment, the CMake support focuses on building plugins, gui apps, and console apps, using the JUCE ‘build everything directly into the final binary’ model. Unfortunately we don’t have android or library examples, yet. There are good reasons in both cases: The Android/Gradle build system expects to ‘own’ the build, and will itself invoke CMake. That means it’s not really possible to write a CMake-only Android build - Gradle is required too. There’s also a lot of stuff that the Projucer n…

---

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [May 11, 2023, 3:44pm UTC](https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140/3 "2023-05-11T15:44:58Z")

</div>

```auto
set_target_properties(cloud PROPERTIES UNITY_BUILD ON UNITY_BUILD_MODE BATCH)

file (GLOB_RECURSE module_files CONFIGURE_DEPENDS
    ${CMAKE_CURRENT_SOURCE_DIR}/../modules/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/../modules/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../modules/*.h
	${CMAKE_CURRENT_SOURCE_DIR}/../modules/*.mm)

foreach (FILE IN LISTS module_files)
    set_property(SOURCE ${FILE} PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)
endforeach()

```

Thanks, setting `SKIP_UNITY_BUILD_INCLUSION` for everything in my modules folder is the easiest way to do it.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [August 6, 2023, 12:13pm UTC](https://forum.juce.com/t/is-there-any-way-to-use-unity-build-with-juce-cmake-products/56140/4 "2023-08-06T12:13:39Z")

</div>

Interesting… I found this post right after reading [this article on compilation times in blender](https://devtalk.blender.org/t/speed-up-c-compilation/30508), in particular the unity build section, first learning about `SKIP_UNITY_BUILD_INCLUSION`.

> [@RolandMR](#):
>
> for everything in my modules folder

Is it only third-party modules in that folder, or is JUCE in there too? I’m assuming they aren’t their own cmake targets?

A few of my modules are their own CMake targets. Some don’t depend on juce modules like [perfetto](https://github.com/sudara/melatonin_perfetto), so I’m assuming they might benefit from being excluded.

I’m less clear what’s happening in modules that rely on juce like [the inspector](https://github.com/sudara/melatonin_inspector) — If `SKIP_UNITY_BUILD_INCLUSION` is true, is it going to add another copy of the juce module it relies on (like @reuk warns in his post)? Bit confused…
