Skip to content

Commit 55be211

Browse files
authored
[CMake] Simplify code coverage CMake configuration (#4793)
* [CMake] Simplify code coverage CMake configuration This just addes an option processed by the CMake cache file to configure code coverage for DXC. With this patch you can generate a coverage report locally if you build with Clang using the following commnads: cmake -G Ninja -DDXC_COVERAGE=On <other options> -C <path to dxc>/cmake/caches/PredefinedParams.cmake <path to dxc> ninja ctest ninja generate-coverage-report * Adding code comment explaining caches CMake cache scripts aren't super common, but are very useful. Adding a comment to explain how they work is probably generally valuable.
1 parent 4670813 commit 55be211

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

cmake/caches/PredefinedParams.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# This file contains the basic options required for building DXC using CMake on
2+
# *nix platforms. It is passed to CMake using the `-C` flag and gets processed
3+
# before the root CMakeLists.txt file. Only cached variables persist after this
4+
# file executes, so all state must be saved into the cache. These variables also
5+
# will not override explicit command line parameters, and can only read
6+
# parameters that are specified before the `-C` flag.
7+
8+
if (DXC_COVERAGE)
9+
set(LLVM_BUILD_INSTRUMENTED_COVERAGE ON CACHE BOOL "")
10+
set(LLVM_PROFILE_DATA_DIR "${CMAKE_BINARY_DIR}/profile" CACHE STRING "")
11+
set(LLVM_CODE_COVERAGE_TARGETS "dxc;dxcompiler" CACHE STRING "")
12+
endif()
13+
114
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "")
215
set(LLVM_APPEND_VC_REV ON CACHE BOOL "")
316
set(LLVM_DEFAULT_TARGET_TRIPLE "dxil-ms-dx" CACHE STRING "")

cmake/modules/CoverageReport.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ file(TO_NATIVE_PATH
1010
# llvm-cov and llvm-profdata need to match the host compiler. They can either be
1111
# explicitly provided by the user, or we will look them up based on the install
1212
# location of the C++ compiler.
13+
14+
# HLSL Change Begin - This is probably worth upstreaming. Some Linux packages
15+
# install versions of the LLVM tools that are versioned. This handles that case.
16+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
17+
string(REGEX REPLACE "(^[0-9]+)(\\.[0-9\\.]+)" "-\\1" HOST_LLVM_VERSION_SUFFIX ${CMAKE_CXX_COMPILER_VERSION})
18+
endif()
19+
1320
get_filename_component(COMPILER_DIRECTORY ${CMAKE_CXX_COMPILER} DIRECTORY)
14-
find_program(LLVM_COV "llvm-cov" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
15-
find_program(LLVM_PROFDATA "llvm-profdata" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
21+
find_program(LLVM_COV NAMES llvm-cov llvm-cov${HOST_LLVM_VERSION_SUFFIX} PATHS ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
22+
find_program(LLVM_PROFDATA NAMES llvm-profdata llvm-profdata${HOST_LLVM_VERSION_SUFFIX} PATHS ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
23+
# HLSL Change End - Detect versioned tools.
1624

1725
if(NOT LLVM_COV OR NOT LLVM_PROFDATA)
1826
message(WARNING "Could not find code coverage tools, skipping generating targets. You may explicitly specify LLVM_COV and LLVM_PROFDATA to work around this warning.")

0 commit comments

Comments
 (0)