2
0
Эх сурвалжийг харах

[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.
Chris B 2 жил өмнө
parent
commit
55be2117a6

+ 13 - 0
cmake/caches/PredefinedParams.cmake

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

+ 10 - 2
cmake/modules/CoverageReport.cmake

@@ -10,9 +10,17 @@ file(TO_NATIVE_PATH
 # llvm-cov and llvm-profdata need to match the host compiler. They can either be
 # explicitly provided by the user, or we will look them up based on the install
 # location of the C++ compiler.
+
+# HLSL Change Begin - This is probably worth upstreaming. Some Linux packages
+# install versions of the LLVM tools that are versioned. This handles that case.
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  string(REGEX REPLACE "(^[0-9]+)(\\.[0-9\\.]+)" "-\\1" HOST_LLVM_VERSION_SUFFIX ${CMAKE_CXX_COMPILER_VERSION})
+endif()
+
 get_filename_component(COMPILER_DIRECTORY ${CMAKE_CXX_COMPILER} DIRECTORY)
-find_program(LLVM_COV "llvm-cov" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
-find_program(LLVM_PROFDATA "llvm-profdata" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
+find_program(LLVM_COV NAMES llvm-cov llvm-cov${HOST_LLVM_VERSION_SUFFIX} PATHS ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
+find_program(LLVM_PROFDATA NAMES llvm-profdata llvm-profdata${HOST_LLVM_VERSION_SUFFIX} PATHS ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
+# HLSL Change End - Detect versioned tools.
 
 if(NOT LLVM_COV OR NOT LLVM_PROFDATA)
   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.")