xref: /llvm-project/llvm/cmake/modules/CoverageReport.cmake (revision 6111125d734404aa9a11c567e4bd3aea3e596ac4)
1# if coverage reports are not enabled, skip all of this
2if(NOT LLVM_BUILD_INSTRUMENTED_COVERAGE)
3  return()
4endif()
5
6file(TO_NATIVE_PATH
7     "${LLVM_SOURCE_DIR}/utils/prepare-code-coverage-artifact.py"
8     PREPARE_CODE_COV_ARTIFACT)
9
10# llvm-cov and llvm-profdata need to match the host compiler. They can either be
11# explicitly provided by the user, or we will look them up based on the install
12# location of the C++ compiler.
13get_filename_component(COMPILER_DIRECTORY ${CMAKE_CXX_COMPILER} DIRECTORY)
14find_program(LLVM_COV "llvm-cov" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
15find_program(LLVM_PROFDATA "llvm-profdata" ${COMPILER_DIRECTORY} NO_DEFAULT_PATH)
16
17if(NOT LLVM_COV OR NOT LLVM_PROFDATA)
18  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.")
19  return()
20endif()
21
22set(LLVM_CODE_COVERAGE_TARGETS "" CACHE STRING "Targets to run code coverage on (defaults to all exported targets if empty)")
23mark_as_advanced(LLVM_CODE_COVERAGE_TARGETS)
24
25if(NOT LLVM_CODE_COVERAGE_TARGETS)
26  # by default run the coverage report across all the exports provided
27  get_property(COV_TARGETS GLOBAL PROPERTY LLVM_EXPORTS)
28endif()
29
30file(TO_NATIVE_PATH
31     "${CMAKE_BINARY_DIR}/report/"
32     REPORT_DIR)
33
34foreach(target ${LLVM_CODE_COVERAGE_TARGETS} ${COV_TARGETS})
35  get_target_property(target_type ${target} TYPE)
36  if("${target_type}" STREQUAL "SHARED_LIBRARY" OR "${target_type}" STREQUAL "EXECUTABLE")
37    list(APPEND coverage_binaries $<TARGET_FILE:${target}>)
38  endif()
39endforeach()
40
41set(LLVM_COVERAGE_SOURCE_DIRS "" CACHE STRING "Source directories to restrict coverage reports to.")
42mark_as_advanced(LLVM_COVERAGE_SOURCE_DIRS)
43
44foreach(dir ${LLVM_COVERAGE_SOURCE_DIRS})
45  list(APPEND restrict_flags -restrict ${dir})
46endforeach()
47
48# Utility target to clear out profile data.
49# This isn't connected to any dependencies because it is a bit finicky to get
50# working exactly how a user might want.
51add_custom_target(clear-profile-data
52                  COMMAND ${CMAKE_COMMAND} -E
53                          remove_directory ${LLVM_PROFILE_DATA_DIR})
54
55# This currently only works for LLVM, but could be expanded to work for all
56# sub-projects. The current limitation is based on not having a good way to
57# automaticall plumb through the targets that we want to run coverage against.
58add_custom_target(generate-coverage-report
59                  COMMAND ${Python3_EXECUTABLE} ${PREPARE_CODE_COV_ARTIFACT}
60                          ${LLVM_PROFDATA} ${LLVM_COV} ${LLVM_PROFILE_DATA_DIR}
61                          ${REPORT_DIR} ${coverage_binaries}
62                          --unified-report ${restrict_flags}
63                  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
64                  DEPENDS check-llvm) # Run tests
65