From f7e29eb68dfcd7f1d6671403edef8341d65c623c Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 16 Apr 2020 21:22:59 +0200
Subject: [PATCH] [CMake] Added option OGS_USE_UNITY_BUILDS.

---
 CMakeLists.txt                         |  3 +++
 scripts/cmake/Functions.cmake          |  5 +++++
 scripts/cmake/UnityBuildSettings.cmake | 24 ++++++++++++++++--------
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 873a9a2be49..ccb9c84eff1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -31,6 +31,9 @@ option(OGS_USE_CONAN "Should Conan package manager be used?" ON)
 set(OGS_CONAN_BUILD "missing" CACHE STRING "Possible values: all, missing, \
     never or list of libs to build")
 option(OGS_DISABLE_CCACHE "Disables ccache compiler cache." OFF)
+if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
+    option(OGS_USE_UNITY_BUILDS "Enables Unity builds for faster compilation." OFF)
+endif()
 
 # Third-party libraries, names come from Conan package names
 set(OGS_LIBS
diff --git a/scripts/cmake/Functions.cmake b/scripts/cmake/Functions.cmake
index fc03396182d..8ef50c4b226 100644
--- a/scripts/cmake/Functions.cmake
+++ b/scripts/cmake/Functions.cmake
@@ -101,4 +101,9 @@ function(ogs_add_library targetName)
     if(OGS_USE_PCH AND NOT ${targetName} STREQUAL "ChemistryLib")
         cotire(${targetName})
     endif()
+
+    if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
+        set_target_properties(${targetName} PROPERTIES
+            UNITY_BUILD ${OGS_USE_UNITY_BUILDS})
+    endif()
 endfunction()
diff --git a/scripts/cmake/UnityBuildSettings.cmake b/scripts/cmake/UnityBuildSettings.cmake
index e78f060a733..a88a922199e 100644
--- a/scripts/cmake/UnityBuildSettings.cmake
+++ b/scripts/cmake/UnityBuildSettings.cmake
@@ -1,8 +1,16 @@
-set_target_properties(BaseLib PROPERTIES UNITY_BUILD ON
-    UNITY_BUILD_BATCH_SIZE 8)
-set_target_properties(GeoLib PROPERTIES UNITY_BUILD ON
-    UNITY_BUILD_BATCH_SIZE 40)
-set_target_properties(MathLib PROPERTIES UNITY_BUILD ON
-    UNITY_BUILD_BATCH_SIZE 10)
-set_target_properties(MeshLib PROPERTIES UNITY_BUILD ON
-    UNITY_BUILD_BATCH_SIZE 40)
+if(NOT OGS_USE_UNITY_BUILDS OR ${CMAKE_VERSION} VERSION_LESS 3.16)
+    return()
+endif()
+
+set_target_properties(BaseLib PROPERTIES UNITY_BUILD_BATCH_SIZE 8)
+set_target_properties(GeoLib PROPERTIES UNITY_BUILD_BATCH_SIZE 40)
+set_target_properties(MaterialLib PROPERTIES UNITY_BUILD_BATCH_SIZE 20)
+set_target_properties(MathLib PROPERTIES UNITY_BUILD_BATCH_SIZE 10)
+set_target_properties(MeshLib PROPERTIES UNITY_BUILD_BATCH_SIZE 20)
+# set_target_properties(ProcessLib PROPERTIES UNITY_BUILD_BATCH_SIZE 80) # breaks!
+
+if(TARGET testrunner)
+    # breaks!
+    # set_target_properties(testrunner PROPERTIES UNITY_BUILD ON)
+    # set_target_properties(testrunner PROPERTIES UNITY_BUILD_BATCH_SIZE 2)
+endif()
-- 
GitLab