1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-20 04:24:36 +08:00

Unity build: Add unit tests

This commit is contained in:
Cristian Adam
2019-07-29 19:33:55 +02:00
parent 8dfeb5d278
commit 1353802af3
30 changed files with 388 additions and 0 deletions

View File

@@ -576,3 +576,4 @@ endif()
add_RunCMake_test("CTestCommandExpandLists")
add_RunCMake_test(PrecompileHeaders)
add_RunCMake_test("UnityBuild")

View File

@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.15)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@@ -0,0 +1,23 @@
include(RunCMake)
run_cmake(unitybuild_c)
run_cmake(unitybuild_cxx)
run_cmake(unitybuild_c_and_cxx)
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_order)
function(run_test name)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${name}-build)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake(${name})
run_cmake_command(${name}-build ${CMAKE_COMMAND} --build . --config Debug)
run_cmake_command(${name}-test ${CMAKE_CTEST_COMMAND} -C Debug)
unset(RunCMake_TEST_BINARY_DIR)
unset(RunCMake_TEST_NO_CLEAN)
endfunction()
run_test(unitybuild_runtest)

View File

@@ -0,0 +1,6 @@
#include "func.h"
int func(void)
{
return 0;
}

View File

@@ -0,0 +1,6 @@
#ifndef func_h
#define func_h
extern int func(void);
#endif

View File

@@ -0,0 +1,6 @@
#include "func.h"
int main(void)
{
return func();
}

View File

@@ -0,0 +1,11 @@
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
set(unitybuild_c1 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_1.c")
if(NOT EXISTS "${unitybuild_c0}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c0} does not exist.")
return()
endif()
if(NOT EXISTS "${unitybuild_c1}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c1} does not exist.")
return()
endif()

View File

@@ -0,0 +1,16 @@
project(unitybuild_batchsize 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_BATCH_SIZE 4
)

View File

@@ -0,0 +1,5 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.")
return()
endif()

View File

@@ -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)

View File

@@ -0,0 +1,11 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.")
return()
endif()
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_cxx} does not exist.")
return()
endif()

View File

@@ -0,0 +1,17 @@
project(unitybuild_c_and_cxx C CXX)
set(srcs "")
foreach(s RANGE 1 8)
set(src_c "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src_c}" "int s${s}(void) { return 0; }\n")
set(src_cxx "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
file(WRITE "${src_cxx}" "int s${s}(void) { return 0; }\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)

View File

@@ -0,0 +1,5 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
if(EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} should not exist.")
return()
endif()

View File

@@ -0,0 +1,10 @@
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})

View File

@@ -0,0 +1,7 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
string(REGEX MATCH "#define NOMINMAX.*#include.*s1.c.*#undef NOMINMAX" matched_code ${unitybuild_c_strings})
if(NOT matched_code)
set(RunCMake_TEST_FAILED "Generated unity file doesn't include expected code before and after include")
return()
endif()

View File

@@ -0,0 +1,13 @@
project(unitybuild_code_before_and_after_include C)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s1.c")
file(WRITE "${src}" "int s1(void) { return 0; }\n")
add_library(tgt SHARED ${src})
set_target_properties(tgt
PROPERTIES
UNITY_BUILD ON
UNITY_BUILD_CODE_BEFORE_INCLUDE "#define NOMINMAX"
UNITY_BUILD_CODE_AFTER_INCLUDE "#undef NOMINMAX"
)

View File

@@ -0,0 +1,5 @@
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_cxx} does not exist.")
return()
endif()

View File

@@ -0,0 +1,12 @@
project(unitybuild_cxx CXX)
set(srcs "")
foreach(s RANGE 1 8)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
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)

View File

@@ -0,0 +1,7 @@
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
file(STRINGS ${unitybuild_c0} unitybuild_c_strings REGEX "/s[0-9]+.c\"$" )
list(LENGTH unitybuild_c_strings number_of_includes)
if(NOT number_of_includes EQUAL 8)
set(RunCMake_TEST_FAILED "Generated unity doesn't include the expect number of files")
return()
endif()

View File

@@ -0,0 +1,15 @@
project(unitybuild_default_batchsize C)
set(srcs "")
foreach(s RANGE 1 10)
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
)

View File

@@ -0,0 +1,7 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
string(REGEX MATCH ".*#include.*s3.c.*#include.*s1.c.*#include.*s2.c.*" matched_code ${unitybuild_c_strings})
if(NOT matched_code)
set(RunCMake_TEST_FAILED "Generated unity file doesn't include expected oder of source files")
return()
endif()

View File

@@ -0,0 +1,12 @@
project(unitybuild_order C)
set(srcs "")
foreach(s 3 1 2)
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)

View File

@@ -0,0 +1,8 @@
project(unitybuild_runtest C)
set(CMAKE_UNITY_BUILD ON) # This tests that the variable works in addition to the property
add_executable(main main.c func.c)
enable_testing()
add_test(NAME main COMMAND main)

View File

@@ -0,0 +1,14 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
string(REGEX MATCH "\\/s[1-6].c" matched_files_1_6 ${unitybuild_c_strings})
if(matched_files_1_6)
set(RunCMake_TEST_FAILED "Generated unity contains s1.c -> s6.c which should have been skipped")
return()
endif()
string(REGEX MATCH "\\/s[7-8].c" matched_files_7_8 ${unitybuild_c_strings})
if(NOT matched_files_7_8)
set(RunCMake_TEST_FAILED "Generated unity should have contained s7.c, s8.c!")
return()
endif()

View File

@@ -0,0 +1,30 @@
project(unitybuild_skip 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)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s1.c
PROPERTIES GENERATED ON)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s2.c
PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s3.c
PROPERTIES COMPILE_OPTIONS "val")
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s4.c
PROPERTIES COMPILE_DEFINITIONS "val")
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s5.c
PROPERTIES COMPILE_FLAGS "val")
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/s6.c
PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}")

View File

@@ -34,3 +34,12 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.20)
run_cmake(VsSpectreMitigation)
endif()
# Visual Studio 2017 has toolset version 141
string(REPLACE "v" "" generator_toolset "${RunCMake_GENERATOR_TOOLSET}")
if (RunCMake_GENERATOR MATCHES "Visual Studio 1[0-4] 201[0-5]" OR
(RunCMake_GENERATOR_TOOLSET AND generator_toolset VERSION_LESS "141"))
run_cmake(UnityBuildPre2017)
else()
run_cmake(UnityBuildNative)
endif()

View File

@@ -0,0 +1,45 @@
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
if(NOT EXISTS "${unitybuild_c0}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c0} 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\"")
set(unity_source_line ${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_line "${unity_source_line}")
string(FIND "${unity_source_line}" "CMakeFiles/tgt.dir/Unity/unity_0.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 8)
set(RunCMake_TEST_FAILED "Generated project doesn't include the expect number of files.")
return()
endif()

View File

@@ -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)

View File

@@ -0,0 +1,48 @@
set(unitybuild_c0 "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0.c")
if(NOT EXISTS "${unitybuild_c0}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c0} 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_0.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 8)
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 32)
set(RunCMake_TEST_FAILED "Generated project doesn't exclude the source files for all configurations.")
return()
endif()

View File

@@ -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)