In addition to unity builds, we’ve also experimented with precompiled-headers to improve compile times of non-JUCE code:
target_precompile_headers (plugin PRIVATE <juce_core/juce_core.h>
PRIVATE <juce_dsp/juce_dsp.h>
...)
This creates a binary intermediate representation of the headers, so the compiler doesn’t need to parse the header every time a file includes them. You don’t need to touch any sources for this to work, just add the cmake-command and during compilation the PCH is accessed.
One downside of this approach is that every time one of the headers specified in the command changes, all sources that include one of the PCH-headers will need to be recompiled. In case of the juce-sources this probably isn’t a big deal, but when you’re dealing with frequently-changing headers this is something you need to pay attention to.
Also, as with the unity builds, be sure to exclude the juce-sources from accessing the PCH:
file(GLOB_RECURSE JUCE_SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/ext/JUCE/*.cpp ${CMAKE_SOURCE_DIR}/ext/JUCE/*.mm ${CMAKE_SOURCE_DIR}/ext/JUCE/*.r)
set_source_files_properties(${JUCE_SOURCES} PROPERTIES SKIP_PRECOMPILE_HEADERS TRUE SKIP_UNITY_BUILD_INCLUSION TRUE)
You can also create a header-file that simply includes all other headers that you want to be in your PCH, and run the cmake-command on that one:
# pch.h
#include <juce_core/juce_core.h>
#include <juce_dsp/juce_dsp.h>
# CMakeLists.txt
target_precompile_headers (plugin PRIVATE pch.h)
With both approaches combined, we’ve managed to reduce our compile times by about 75%, without touching any source code:
target_precompile_headers(plugin PRIVATE pch.h)
set_target_properties(plugin PROPERTIES UNITY_BUILD ON)
file(GLOB_RECURSE JUCE_SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/ext/JUCE/*.cpp ${CMAKE_SOURCE_DIR}/ext/JUCE/*.mm ${CMAKE_SOURCE_DIR}/ext/JUCE/*.r)
set_source_files_properties(${JUCE_SOURCES} PROPERTIES SKIP_PRECOMPILE_HEADERS TRUE SKIP_UNITY_BUILD_INCLUSION TRUE)
One last thing to mention is that it seems that, at least in our setup, AppleClang that ships with XCode 12 crashes when using PCH, the issue seems to be resolved with XCode 13, but even only with unity builds we’ve reduced compile times by roughly 60%.