@@ -11,24 +11,54 @@ endmacro()
11
11
12
12
# this function is based on the unity build function described at: https://cheind.wordpress.com/2009/12/10/reducing-compilation-time-unity-builds/
13
13
function (enable_unity_build UNITY_SUFFIX SOURCE_FILES )
14
+ set (MAX_TRANSLATION_UNITS_PER_UNITY_UNIT 500 )
15
+
14
16
set (files ${${SOURCE_FILES}} )
17
+ list (LENGTH files length )
18
+
19
+ set (UNITY_FILE_COUNT "" )
20
+ if (length GREATER ${MAX_TRANSLATION_UNITS_PER_UNITY_UNIT} )
21
+ set (UNITY_FILE_COUNT 1 )
22
+ endif ()
23
+
24
+ set (SOURCE_FILE_COUNT 0 )
15
25
16
26
# Generate a unique filename for the unity build translation unit
17
- set (unity_build_file ${CMAKE_CURRENT_BINARY_DIR} /ub_ ${UNITY_SUFFIX}.cpp )
27
+ set (unity_build_file ${CMAKE_CURRENT_BINARY_DIR} /ub${UNITY_FILE_COUNT}_ ${UNITY_SUFFIX}.cpp )
18
28
19
29
# Exclude all translation units from compilation
20
30
set_source_files_properties (${files} PROPERTIES HEADER_FILE_ONLY true )
21
31
22
32
# Open the ub file
23
- FILE (WRITE ${unity_build_file} "// Unity Build generated by CMake\n " )
33
+ if (length GREATER ${MAX_TRANSLATION_UNITS_PER_UNITY_UNIT} )
34
+ FILE (WRITE ${unity_build_file} "// Unity Build Volume ${UNITY_FILE_COUNT} generated by CMake\n " )
35
+ else ()
36
+ FILE (WRITE ${unity_build_file} "// Unity Build generated by CMake\n " )
37
+ endif ()
38
+
39
+ # Complement list of translation units with the name of ub
40
+ list (APPEND ${SOURCE_FILES} "${unity_build_file} " )
41
+ set (${SOURCE_FILES} ${${SOURCE_FILES}} PARENT_SCOPE )
24
42
25
- # Add include statement for each translation unit
26
43
foreach (source_file ${files} )
44
+ if (SOURCE_FILE_COUNT EQUAL ${MAX_TRANSLATION_UNITS_PER_UNITY_UNIT} )
45
+ MATH (EXPR UNITY_FILE_COUNT "${UNITY_FILE_COUNT} +1" )
46
+ set (SOURCE_FILE_COUNT 0 )
47
+
48
+ set (unity_build_file ${CMAKE_CURRENT_BINARY_DIR} /ub${UNITY_FILE_COUNT}_${UNITY_SUFFIX}.cpp )
49
+ # Open the ub file
50
+ FILE (WRITE ${unity_build_file} "// Unity Build Volume ${UNITY_FILE_COUNT} generated by CMake\n " )
51
+
52
+ # Complement list of translation units with the name of ub
53
+ list (APPEND ${SOURCE_FILES} "${unity_build_file} " )
54
+ set (${SOURCE_FILES} ${${SOURCE_FILES}} PARENT_SCOPE )
55
+ endif ()
56
+
57
+ # Add include statement for each translation unit
27
58
FILE ( APPEND ${unity_build_file} "#include <${source_file} >\n " )
28
- endforeach (source_file )
29
59
30
- # Complement list of translation units with the name of ub
31
- set ( ${SOURCE_FILES} ${${SOURCE_FILES}} ${unity_build_file} PARENT_SCOPE )
60
+ MATH ( EXPR SOURCE_FILE_COUNT " ${SOURCE_FILE_COUNT} +1" )
61
+ endforeach ( source_file )
32
62
endfunction (enable_unity_build )
33
63
34
64
macro (setup_install )
0 commit comments