[docs] Fix build-docs.sh
[llvm-project.git] / llvm / cmake / modules / CoverageReport.cmake
blob60ea8934359419c08a0571cf2620a8e45d127934
1 # if coverage reports are not enabled, skip all of this
2 if(NOT LLVM_BUILD_INSTRUMENTED_COVERAGE)
3   return()
4 endif()
6 file(TO_NATIVE_PATH
7      "${LLVM_SOURCE_DIR}/utils/prepare-code-coverage-artifact.py"
8      PREPARE_CODE_COV_ARTIFACT)
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.
13 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)
17 if(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()
20 endif()
22 set(LLVM_CODE_COVERAGE_TARGETS "" CACHE STRING "Targets to run code coverage on (defaults to all exported targets if empty)")
23 mark_as_advanced(LLVM_CODE_COVERAGE_TARGETS)
25 if(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)
28 endif()
30 file(TO_NATIVE_PATH
31      "${CMAKE_BINARY_DIR}/report/"
32      REPORT_DIR)
34 foreach(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()
39 endforeach()
41 set(LLVM_COVERAGE_SOURCE_DIRS "" CACHE STRING "Source directories to restrict coverage reports to.")
42 mark_as_advanced(LLVM_COVERAGE_SOURCE_DIRS)
44 foreach(dir ${LLVM_COVERAGE_SOURCE_DIRS})
45   list(APPEND restrict_flags -restrict ${dir})
46 endforeach()
48 # Utility target to clear out profile data.
49 # This isn't connected to any dependencies because it is a bit finacky to get
50 # working exactly how a user might want.
51 add_custom_target(clear-profile-data
52                   COMMAND ${CMAKE_COMMAND} -E
53                           remove_directory ${LLVM_PROFILE_DATA_DIR})
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.
58 add_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