Skip to content

Commit

Permalink
Add Code Coverage support using CMake (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn authored Oct 9, 2024
1 parent 29c2067 commit 270d2e7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ option(DISABLE_MSVC_ITERATOR_DEBUGGING "Disable iterator debugging in Debug conf

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

option(ENABLE_CODE_COVERAGE "Build with code-coverage" OFF)

option(USE_PREBUILT_SHADERS "Use externally built HLSL shaders" OFF)

option(NO_WCHAR_T "Use legacy wide-character as unsigned short" OFF)
Expand Down Expand Up @@ -519,6 +521,10 @@ if(WIN32 AND (NOT WINDOWS_STORE) AND (NOT (DEFINED XBOX_CONSOLE_TARGET)))
if(BUILD_TESTING AND (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Tests/CMakeLists.txt"))
enable_testing()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/Tests)

if(ENABLE_CODE_COVERAGE AND (DEFINED COV_COMPILER_SWITCHES))
target_compile_options(${PROJECT_NAME} PRIVATE ${COV_COMPILER_SWITCHES})
endif()
elseif(BUILD_FUZZING AND (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Tests/fuzzloaders/CMakeLists.txt"))
message(STATUS "Building for fuzzing")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/Tests/fuzzloaders)
Expand Down
26 changes: 23 additions & 3 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@
"strategy": "external"
}
},

{
"name": "Analyze",
"cacheVariables":
{
"ENABLE_CODE_ANALYSIS": true
},
"hidden": true
},
{
"name": "Coverage",
"cacheVariables":
{
"BUILD_TOOLS": false,
"ENABLE_CODE_COVERAGE": true
},
"hidden": true
},
{
"name": "Fuzzing",
"cacheVariables": {
Expand Down Expand Up @@ -294,9 +312,11 @@
{ "name": "x64-Debug-ICX" , "description": "Intel oneAPI Compiler (Debug) for Windows 8", "inherits": [ "base", "x64", "Debug", "IntelLLVM" ] },
{ "name": "x64-Release-ICX" , "description": "Intel oneAPI Compiler (Release) for Windows 8", "inherits": [ "base", "x64", "Release", "IntelLLVM" ] },

{ "name": "x64-Fuzzing" , "description": "MSVC for x64 (Release) with ASan", "inherits": [ "base", "x64", "Release", "MSVC", "Win10", "Fuzzing" ] },
{ "name": "x64-Analyze" , "description": "MSVC for x64 (Debug) for Windows 8 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC" ], "cacheVariables": { "ENABLE_CODE_ANALYSIS": true } },
{ "name": "x64-Analyze-Win10" , "description": "MSVC for x64 (Debug) for Windows 10 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC", "Win10" ], "cacheVariables": { "ENABLE_CODE_ANALYSIS": true } }
{ "name": "x64-Analyze" , "description": "MSVC for x64 (Debug) for Windows 8 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC", "Analyze" ] },
{ "name": "x64-Analyze-Win10" , "description": "MSVC for x64 (Debug) for Windows 10 using /analyze", "inherits": [ "base", "x64", "Debug", "MSVC", "Win10", "Analyze" ] },
{ "name": "x64-Coverage" , "description": "MSVC for x64 (Debug) w/ Code Coverage", "inherits": [ "base", "x64", "Debug", "MSVC", "Coverage" ] },
{ "name": "x64-Coverage-Clang", "description": "Clang/LLVM for x64 (Debug) w/ Code Coverage", "inherits": [ "base", "x64", "Debug", "Clang", "Coverage" ] },
{ "name": "x64-Fuzzing" , "description": "MSVC for x64 (Release) with ASan", "inherits": [ "base", "x64", "Release", "MSVC", "Win10", "Fuzzing" ] }
],
"testPresets": [
{ "name": "x64-Debug" , "configurePreset": "x64-Debug" },
Expand Down
19 changes: 17 additions & 2 deletions build/CompilerAndLinker.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if(MSVC)
list(APPEND COMPILER_SWITCHES "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>")
list(APPEND LINKER_SWITCHES /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO)

if((${DIRECTX_ARCH} MATCHES "x86")
if((${DIRECTX_ARCH} STREQUAL "x86")
OR ((CMAKE_SIZEOF_VOID_P EQUAL 4) AND (NOT (${DIRECTX_ARCH} MATCHES "^arm"))))
list(APPEND LINKER_SWITCHES /SAFESEH)
endif()
Expand All @@ -71,7 +71,7 @@ if(XBOX_CONSOLE_TARGET STREQUAL "scarlett")
elseif(XBOX_CONSOLE_TARGET MATCHES "xboxone|durango")
list(APPEND COMPILER_SWITCHES $<IF:$<CXX_COMPILER_ID:MSVC>,/favor:AMD64 /arch:AVX,-march=btver2>)
elseif(NOT (${DIRECTX_ARCH} MATCHES "^arm"))
if((${DIRECTX_ARCH} MATCHES "x86") OR (CMAKE_SIZEOF_VOID_P EQUAL 4))
if((${DIRECTX_ARCH} STREQUAL "x86") OR (CMAKE_SIZEOF_VOID_P EQUAL 4))
set(ARCH_SSE2 $<$<CXX_COMPILER_ID:MSVC,Intel>:/arch:SSE2> $<$<NOT:$<CXX_COMPILER_ID:MSVC,Intel>>:-msse2>)
else()
set(ARCH_SSE2 $<$<NOT:$<CXX_COMPILER_ID:MSVC,Intel>>:-msse2>)
Expand Down Expand Up @@ -207,3 +207,18 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(ASAN_SWITCHES /fsanitize=address /fsanitize-coverage=inline-8bit-counters /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div)
set(ASAN_LIBS sancov.lib)
endif()

#--- Code coverage
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(COV_COMPILER_SWITCHES --coverage -g)

if(${DIRECTX_ARCH} STREQUAL "x64")
set(COV_LIBS clang_rt.profile-x86_64.lib)
elseif(${DIRECTX_ARCH} STREQUAL "x86")
set(COV_LIBS clang_rt.profile-i386.lib)
elseif(${DIRECTX_ARCH} STREQUAL "arm64")
set(COV_LIBS clang_rt.profile-aarch64.lib)
endif()
elseif((CMAKE_CXX_COMPILER_ID MATCHES "MSVC") AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.33))
set(COV_LINKER_SWITCHES /PROFILE)
endif()

0 comments on commit 270d2e7

Please sign in to comment.