Skip to content

Commit cb1dcca

Browse files
authored
Add Code Coverage support using CMake (#531)
1 parent 8a4ff71 commit cb1dcca

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ option(DISABLE_MSVC_ITERATOR_DEBUGGING "Disable iterator debugging in Debug conf
3939

4040
option(ENABLE_CODE_ANALYSIS "Use Static Code Analysis on build" OFF)
4141

42+
option(ENABLE_CODE_COVERAGE "Build with code-coverage" OFF)
43+
4244
option(USE_PREBUILT_SHADERS "Use externally built HLSL shaders" OFF)
4345

4446
option(NO_WCHAR_T "Use legacy wide-character as unsigned short" OFF)
@@ -568,7 +570,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
568570

569571
if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.37)
570572
AND (NOT (${DIRECTX_ARCH} MATCHES "^arm"))
571-
AND ((${DIRECTX_ARCH} MATCHES "x64") OR (CMAKE_SIZEOF_VOID_P EQUAL 8)))
573+
AND ((${DIRECTX_ARCH} STREQUAL "x64") OR (CMAKE_SIZEOF_VOID_P EQUAL 8)))
572574
# Enable since DirectXTex library has a lot of large switch statements
573575
target_compile_options(${PROJECT_NAME} PRIVATE /jumptablerdata)
574576
endif()
@@ -659,6 +661,10 @@ if(WIN32 AND (NOT WINDOWS_STORE) AND (NOT (DEFINED XBOX_CONSOLE_TARGET)))
659661
if(BUILD_TESTING AND (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Tests/CMakeLists.txt"))
660662
enable_testing()
661663
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/Tests)
664+
665+
if(ENABLE_CODE_COVERAGE AND (DEFINED COV_COMPILER_SWITCHES))
666+
target_compile_options(${PROJECT_NAME} PRIVATE ${COV_COMPILER_SWITCHES})
667+
endif()
662668
elseif(BUILD_FUZZING AND (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Tests/fuzzloaders/CMakeLists.txt"))
663669
message(STATUS "Building for fuzzing")
664670
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/Tests/fuzzloaders)

CMakePresets.json

+22-2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@
231231
"strategy": "external"
232232
}
233233
},
234+
235+
{
236+
"name": "Analyze",
237+
"cacheVariables":
238+
{
239+
"ENABLE_CODE_ANALYSIS": true
240+
},
241+
"hidden": true
242+
},
243+
{
244+
"name": "Coverage",
245+
"cacheVariables":
246+
{
247+
"BUILD_TOOLS": false,
248+
"ENABLE_CODE_COVERAGE": true
249+
},
250+
"hidden": true
251+
},
234252
{
235253
"name": "Fuzzing",
236254
"cacheVariables": {
@@ -353,8 +371,10 @@
353371
{ "name": "x64-Debug-ICX" , "description": "Intel oneAPI Compiler (Debug) with DX12", "inherits": [ "base", "x64", "Debug", "IntelLLVM" ] },
354372
{ "name": "x64-Release-ICX" , "description": "Intel oneAPI Compiler (Release) with DX12", "inherits": [ "base", "x64", "Release", "IntelLLVM" ] },
355373

356-
{ "name": "x64-Fuzzing" , "description": "MSVC for x64 (Release) with ASan", "inherits": [ "base", "x64", "Release", "MSVC", "Fuzzing" ] },
357-
{ "name": "x64-Analyze" , "description": "MSVC for x64 (Debug) with DX12 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC" ], "cacheVariables": { "ENABLE_CODE_ANALYSIS": true } }
374+
{ "name": "x64-Analyze" , "description": "MSVC for x64 (Debug) with DX12 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC", "Analyze" ] },
375+
{ "name": "x64-Coverage" , "description": "MSVC for x64 (Debug) with DX12 w/ Code Coverage", "inherits": [ "base", "x64", "Debug", "MSVC", "Coverage" ] },
376+
{ "name": "x64-Coverage-Clang", "description": "Clang/LLVM for x64 (Debug) with DX12 w/ Code Coverage", "inherits": [ "base", "x64", "Debug", "Clang", "Coverage" ] },
377+
{ "name": "x64-Fuzzing" , "description": "MSVC for x64 (Release) with ASan", "inherits": [ "base", "x64", "Release", "MSVC", "Fuzzing" ] }
358378
],
359379
"testPresets": [
360380
{ "name": "x64-Debug" , "configurePreset": "x64-Debug" },

build/CompilerAndLinker.cmake

+17-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if(MSVC)
4949
list(APPEND COMPILER_SWITCHES "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>")
5050
list(APPEND LINKER_SWITCHES /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO)
5151

52-
if((${DIRECTX_ARCH} MATCHES "x86")
52+
if((${DIRECTX_ARCH} STREQUAL "x86")
5353
OR ((CMAKE_SIZEOF_VOID_P EQUAL 4) AND (NOT (${DIRECTX_ARCH} MATCHES "^arm"))))
5454
list(APPEND LINKER_SWITCHES /SAFESEH)
5555
endif()
@@ -71,7 +71,7 @@ if(XBOX_CONSOLE_TARGET STREQUAL "scarlett")
7171
elseif(XBOX_CONSOLE_TARGET MATCHES "xboxone|durango")
7272
list(APPEND COMPILER_SWITCHES $<IF:$<CXX_COMPILER_ID:MSVC>,/favor:AMD64 /arch:AVX,-march=btver2>)
7373
elseif(NOT (${DIRECTX_ARCH} MATCHES "^arm"))
74-
if((${DIRECTX_ARCH} MATCHES "x86") OR (CMAKE_SIZEOF_VOID_P EQUAL 4))
74+
if((${DIRECTX_ARCH} STREQUAL "x86") OR (CMAKE_SIZEOF_VOID_P EQUAL 4))
7575
set(ARCH_SSE2 $<$<CXX_COMPILER_ID:MSVC,Intel>:/arch:SSE2> $<$<NOT:$<CXX_COMPILER_ID:MSVC,Intel>>:-msse2>)
7676
else()
7777
set(ARCH_SSE2 $<$<NOT:$<CXX_COMPILER_ID:MSVC,Intel>>:-msse2>)
@@ -207,3 +207,18 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
207207
set(ASAN_SWITCHES /fsanitize=address /fsanitize-coverage=inline-8bit-counters /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div)
208208
set(ASAN_LIBS sancov.lib)
209209
endif()
210+
211+
#--- Code coverage
212+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
213+
set(COV_COMPILER_SWITCHES --coverage -g)
214+
215+
if(${DIRECTX_ARCH} STREQUAL "x64")
216+
set(COV_LIBS clang_rt.profile-x86_64.lib)
217+
elseif(${DIRECTX_ARCH} STREQUAL "x86")
218+
set(COV_LIBS clang_rt.profile-i386.lib)
219+
elseif(${DIRECTX_ARCH} STREQUAL "arm64")
220+
set(COV_LIBS clang_rt.profile-aarch64.lib)
221+
endif()
222+
elseif((CMAKE_CXX_COMPILER_ID MATCHES "MSVC") AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.33))
223+
set(COV_LINKER_SWITCHES /PROFILE)
224+
endif()

0 commit comments

Comments
 (0)