mirror of
https://github.com/Kitware/CMake.git
synced 2026-05-07 22:59:56 -05:00
Unity Builds: Support explicit specification of sources to groups
Instead of having CMake determine which files should go into each unity file, the user can now use explicitly state the mapping.
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
include(RunCMake)
|
||||
|
||||
run_cmake(unitybuild_c)
|
||||
run_cmake(unitybuild_c_batch)
|
||||
run_cmake(unitybuild_c_group)
|
||||
run_cmake(unitybuild_cxx)
|
||||
run_cmake(unitybuild_cxx_group)
|
||||
run_cmake(unitybuild_c_and_cxx)
|
||||
run_cmake(unitybuild_c_and_cxx_group)
|
||||
run_cmake(unitybuild_batchsize)
|
||||
run_cmake(unitybuild_default_batchsize)
|
||||
run_cmake(unitybuild_skip)
|
||||
run_cmake(unitybuild_code_before_and_after_include)
|
||||
run_cmake(unitybuild_c_no_unity_build)
|
||||
run_cmake(unitybuild_c_no_unity_build_group)
|
||||
run_cmake(unitybuild_order)
|
||||
run_cmake(unitybuild_invalid_mode)
|
||||
|
||||
function(run_test name)
|
||||
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${name}-build)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
int f(int x)
|
||||
{
|
||||
(void)x;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
set(unitybuild_a_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_a_c.c")
|
||||
if(NOT EXISTS "${unitybuild_a_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_a_c} does not exist.")
|
||||
return()
|
||||
else()
|
||||
#verify that the 4 c file is part of this grouping and therefore UNITY_BUILD_BATCH_SIZE
|
||||
#was ignored
|
||||
file(STRINGS ${unitybuild_a_c} unitybuild_a_c_strings)
|
||||
string(REGEX MATCH ".*#include.*s1.c.*#include.*s2.c.*#include.*s3.c.*#include.*s4.c.*" matched_code ${unitybuild_a_c_strings})
|
||||
if(NOT matched_code)
|
||||
set(RunCMake_TEST_FAILED "Generated unity file doesn't include expected source files")
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(unitybuild_b_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_b_c.c")
|
||||
if(NOT EXISTS "${unitybuild_b_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_b_c} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
|
||||
set(unitybuild_a_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_a_cxx.cxx")
|
||||
if(NOT EXISTS "${unitybuild_a_cxx}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_a_cxx} does not exist.")
|
||||
return()
|
||||
else()
|
||||
#verify that the 4 cxx file are part of this grouping and therefore UNITY_BUILD_BATCH_SIZE
|
||||
#was ignored
|
||||
file(STRINGS ${unitybuild_a_cxx} unitybuild_a_cxx_strings)
|
||||
string(REGEX MATCH ".*#include.*s1.cxx.*#include.*s2.cxx.*#include.*s3.cxx.*#include.*s4.cxx.*" matched_code ${unitybuild_a_cxx_strings})
|
||||
if(NOT matched_code)
|
||||
set(RunCMake_TEST_FAILED "Generated unity file doesn't include expected source files")
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(unitybuild_b_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_b_cxx.cxx")
|
||||
if(NOT EXISTS "${unitybuild_b_cxx}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_b_cxx} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,39 @@
|
||||
project(unitybuild_c_and_cxx C CXX)
|
||||
|
||||
set(srcs f.c)
|
||||
foreach(s RANGE 1 8)
|
||||
set(src_c "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src_c}" "
|
||||
int f(int);\n
|
||||
int s${s}(void) { return f(${s}); }\n"
|
||||
)
|
||||
|
||||
set(src_cxx "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
|
||||
file(WRITE "${src_cxx}" "
|
||||
extern \"C\" { \n
|
||||
int f(int); \n
|
||||
}\n
|
||||
int s${s}(void) { return f(${s}); }\n"
|
||||
)
|
||||
|
||||
list(APPEND srcs "${src_c}")
|
||||
list(APPEND srcs "${src_cxx}")
|
||||
endforeach()
|
||||
|
||||
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
|
||||
UNITY_BUILD_MODE GROUP
|
||||
#UNITY_BUILD_BATCH_SIZE will be ignored
|
||||
UNITY_BUILD_BATCH_SIZE 2)
|
||||
|
||||
set_source_files_properties(s1.c s2.c s3.c s4.c
|
||||
s1.cxx s2.cxx s3.cxx s4.cxx
|
||||
PROPERTIES UNITY_GROUP "a"
|
||||
)
|
||||
set_source_files_properties(s5.c s6.c s7.c s8.c
|
||||
s5.cxx s6.cxx s7.cxx s8.cxx
|
||||
PROPERTIES UNITY_GROUP "b"
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
|
||||
if(NOT EXISTS "${unitybuild_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,15 @@
|
||||
project(unitybuild_c C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_MODE BATCH
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
set(unitybuild_a_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_a_c.c")
|
||||
if(NOT EXISTS "${unitybuild_a_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_a_c} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(unitybuild_b_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_b_c.c")
|
||||
if(NOT EXISTS "${unitybuild_b_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_b_c} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,17 @@
|
||||
project(unitybuild_c C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
|
||||
UNITY_BUILD_MODE GROUP)
|
||||
|
||||
set_source_files_properties(s1.c PROPERTIES UNITY_GROUP "a")
|
||||
set_source_files_properties(s2.c PROPERTIES UNITY_GROUP "a")
|
||||
set_source_files_properties(s3.c s4.c PROPERTIES UNITY_GROUP "b")
|
||||
@@ -0,0 +1,5 @@
|
||||
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_a_c.c")
|
||||
if(EXISTS "${unitybuild_c}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} should not exist.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,16 @@
|
||||
project(unitybuild_c_no_unity_build C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
#These should be ignored as UNITY_BUILD is off
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD_MODE GROUP)
|
||||
set_source_files_properties(s1.c s2.c s3.c s4.c s5.c s6.c s7.c s8.c
|
||||
PROPERTIES UNITY_GROUP "a"
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
set(unitybuild_a_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_a_cxx.cxx")
|
||||
if(NOT EXISTS "${unitybuild_a_cxx}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_a_cxx} does not exist.")
|
||||
return()
|
||||
else()
|
||||
#verify that odr2 is not part of this source set
|
||||
file(STRINGS ${unitybuild_a_cxx} unitybuild_a_cxx_strings)
|
||||
string(REGEX MATCH ".*#include.*odr2.cxx" matched_code ${unitybuild_a_cxx_strings})
|
||||
if(matched_code)
|
||||
set(RunCMake_TEST_FAILED "Generated unity file includes un-expected ord2.cxx source file")
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(unitybuild_b_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_b_cxx.cxx")
|
||||
if(NOT EXISTS "${unitybuild_b_cxx}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_b_cxx} does not exist.")
|
||||
return()
|
||||
else()
|
||||
#verify that odr1 is not part of this source set
|
||||
file(STRINGS ${unitybuild_b_cxx} unitybuild_b_cxx_strings)
|
||||
string(REGEX MATCH ".*#include.*odr1.cxx" matched_code ${unitybuild_b_cxx_strings})
|
||||
if(matched_code)
|
||||
set(RunCMake_TEST_FAILED "Generated unity file includes un-expected ord1.cxx source file")
|
||||
return()
|
||||
endif()
|
||||
endif()
|
||||
@@ -0,0 +1,27 @@
|
||||
project(unitybuild_cxx CXX)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 4)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
foreach(s RANGE 1 2)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/odr${s}.cxx")
|
||||
file(WRITE "${src}" "namespace odr { int s${s}(void) { return 0; } }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
|
||||
UNITY_BUILD_MODE GROUP
|
||||
)
|
||||
|
||||
set_source_files_properties(s1.cxx s2.cxx odr1.cxx
|
||||
PROPERTIES UNITY_GROUP "a"
|
||||
)
|
||||
set_source_files_properties(s3.cxx s4.cxx odr2.cxx
|
||||
PROPERTIES UNITY_GROUP "b"
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
^CMake Error in CMakeLists.txt:
|
||||
Invalid UNITY_BUILD_MODE value of INVALID assigned to target tgt\.
|
||||
Acceptable values are BATCH and GROUP\.
|
||||
.*
|
||||
CMake Generate step failed\. Build files cannot be regenerated correctly\.$
|
||||
@@ -0,0 +1,12 @@
|
||||
project(unitybuild_c C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON UNITY_BUILD_MODE INVALID)
|
||||
@@ -56,6 +56,7 @@ if (RunCMake_GENERATOR MATCHES "Visual Studio 1[0-4] 201[0-5]" OR
|
||||
run_cmake(UnityBuildPre2017)
|
||||
else()
|
||||
run_cmake(UnityBuildNative)
|
||||
run_cmake(UnityBuildNativeGrouped)
|
||||
endif()
|
||||
|
||||
run_cmake(VsDotnetTargetFramework)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_poolA_c.c")
|
||||
if(NOT EXISTS "${unitybuild_c0}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c0} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(unitybuild_c1 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_poolB_c.c")
|
||||
if(NOT EXISTS "${unitybuild_c1}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c1} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(tgt_project "${RunCMake_TEST_BINARY_DIR}/tgt.vcxproj")
|
||||
if (NOT EXISTS "${tgt_project}")
|
||||
set(RunCMake_TEST_FAILED "Generated project file ${tgt_project} doesn't exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
file(STRINGS ${tgt_project} tgt_projects_strings)
|
||||
|
||||
foreach(line IN LISTS tgt_projects_strings)
|
||||
if (line MATCHES "<EnableUnitySupport>true</EnableUnitySupport>")
|
||||
set(have_unity_support ON)
|
||||
endif()
|
||||
|
||||
if (line MATCHES "<ClCompile Include=.*IncludeInUnityFile=\"false\" CustomUnityFile=\"true\"")
|
||||
list(APPEND unity_source_lines ${line})
|
||||
endif()
|
||||
|
||||
if (line MATCHES "<ClCompile Include=.*IncludeInUnityFile=\"true\" CustomUnityFile=\"true\"")
|
||||
list(APPEND sources_list ${line})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if (NOT have_unity_support)
|
||||
set(RunCMake_TEST_FAILED "Generated project should include the <EnableUnitySupport> block.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
string(REPLACE "\\" "/" unity_source_lines "${unity_source_lines}")
|
||||
string(FIND "${unity_source_lines}" "CMakeFiles/tgt.dir/Unity/unity_poolA_c.c" unity_source_file_position)
|
||||
if (unity_source_file_position EQUAL "-1")
|
||||
set(RunCMake_TEST_FAILED "Generated project should include the generated unity source file 'poolA'.")
|
||||
return()
|
||||
endif()
|
||||
string(FIND "${unity_source_lines}" "CMakeFiles/tgt.dir/Unity/unity_poolB_c.c" unity_source_file_position)
|
||||
if (unity_source_file_position EQUAL "-1")
|
||||
set(RunCMake_TEST_FAILED "Generated project should include the generated unity source file 'poolB'.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
list(LENGTH sources_list number_of_sources)
|
||||
if(NOT number_of_sources EQUAL 7)
|
||||
set(RunCMake_TEST_FAILED "Generated project doesn't include the expect number of files.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,20 @@
|
||||
project(unitybuild_c C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON UNITY_BUILD_MODE GROUP)
|
||||
|
||||
set_source_files_properties(s1.c s2.c s3.c s4.c
|
||||
PROPERTIES UNITY_GROUP "poolA"
|
||||
)
|
||||
|
||||
set_source_files_properties(s5.c s6.c s7.c
|
||||
PROPERTIES UNITY_GROUP "poolB"
|
||||
)
|
||||
@@ -0,0 +1,59 @@
|
||||
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_poolA.c")
|
||||
if(NOT EXISTS "${unitybuild_c0}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c0} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(unitybuild_c1 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_poolB.c")
|
||||
if(NOT EXISTS "${unitybuild_c1}")
|
||||
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c1} does not exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(tgt_project "${RunCMake_TEST_BINARY_DIR}/tgt.vcxproj")
|
||||
if (NOT EXISTS "${tgt_project}")
|
||||
set(RunCMake_TEST_FAILED "Generated project file ${tgt_project} doesn't exist.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
file(STRINGS ${tgt_project} tgt_projects_strings)
|
||||
|
||||
foreach(line IN LISTS tgt_projects_strings)
|
||||
if (line MATCHES "<ClCompile Include=.*/>")
|
||||
set(unity_source_line ${line})
|
||||
endif()
|
||||
|
||||
if (line MATCHES "<ClCompile Include=\"[^\"]*\">")
|
||||
string(REGEX MATCH "<ClCompile Include=\"([^\"]*)\">" source_file ${line})
|
||||
list(APPEND sources_list ${source_file})
|
||||
endif()
|
||||
|
||||
if (line MATCHES "<ExcludedFromBuild.*</ExcludedFromBuild>")
|
||||
list(APPEND excluded_sources_list ${source_file})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
string(REPLACE "\\" "/" unity_source_line ${unity_source_line})
|
||||
string(FIND "${unity_source_line}" "CMakeFiles/tgt.dir/Unity/unity_poolA.c" unity_source_file_position)
|
||||
if (unity_source_file_position EQUAL "-1")
|
||||
set(RunCMake_TEST_FAILED "Generated project should include the generated unity source file.")
|
||||
return()
|
||||
endif()
|
||||
string(FIND "${unity_source_line}" "CMakeFiles/tgt.dir/Unity/unity_poolB.c" unity_source_file_position)
|
||||
if (unity_source_file_position EQUAL "-1")
|
||||
set(RunCMake_TEST_FAILED "Generated project should include the generated unity source file.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
list(LENGTH sources_list number_of_sources)
|
||||
if(NOT number_of_sources EQUAL 7)
|
||||
set(RunCMake_TEST_FAILED "Generated project doesn't include the expect number of files.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Exclusions for Debug|Release|MinSizeRel|RelWithDebInfo
|
||||
list(LENGTH excluded_sources_list number_of_excluded_sources)
|
||||
if(NOT number_of_excluded_sources EQUAL 28)
|
||||
set(RunCMake_TEST_FAILED "Generated project doesn't exclude the source files for all configurations.")
|
||||
return()
|
||||
endif()
|
||||
@@ -0,0 +1,20 @@
|
||||
project(unitybuild_c C)
|
||||
|
||||
set(srcs "")
|
||||
foreach(s RANGE 1 8)
|
||||
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
|
||||
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
|
||||
list(APPEND srcs "${src}")
|
||||
endforeach()
|
||||
|
||||
add_library(tgt SHARED ${srcs})
|
||||
|
||||
set_target_properties(tgt PROPERTIES UNITY_BUILD ON UNITY_BUILD_MODE GROUP)
|
||||
|
||||
set_source_files_properties(s1.c s2.c s3.c s4.c
|
||||
PROPERTIES UNITY_GROUP "poolA"
|
||||
)
|
||||
|
||||
set_source_files_properties(s5.c s6.c s7.c
|
||||
PROPERTIES UNITY_GROUP "poolB"
|
||||
)
|
||||
Reference in New Issue
Block a user