Skip to content

Commit 4434d82

Browse files
Split generated unity build translation units into volumes of max 500 real translation units each
1 parent d974efe commit 4434d82

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

cmake/utilities.cmake

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,54 @@ endmacro()
1111

1212
# this function is based on the unity build function described at: https://cheind.wordpress.com/2009/12/10/reducing-compilation-time-unity-builds/
1313
function(enable_unity_build UNITY_SUFFIX SOURCE_FILES)
14+
set(MAX_TRANSLATION_UNITS_PER_UNITY_UNIT 500)
15+
1416
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)
1525

1626
# 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)
1828

1929
# Exclude all translation units from compilation
2030
set_source_files_properties(${files} PROPERTIES HEADER_FILE_ONLY true)
2131

2232
# 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)
2442

25-
# Add include statement for each translation unit
2643
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
2758
FILE( APPEND ${unity_build_file} "#include <${source_file}>\n")
28-
endforeach(source_file)
2959

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)
3262
endfunction(enable_unity_build)
3363

3464
macro(setup_install)

0 commit comments

Comments
 (0)