Merge pull request #24943 from foresto/prev-subtitle
[xbmc.git] / cmake / scripts / linux / CodeCoverage.cmake
blobf0a36fcdd3a5ec0ae50047d073676272f353d0e5
1 # - CodeCoverage
2 # Generate code coverage reports with LCOV and GCovr.
4 # Configuration:
5 #  COVERAGE_SOURCE_DIR      - Source root directory (default ${CMAKE_SOURCE_DIR}).
6 #  COVERAGE_BINARY_DIR      - Directory where the coverage reports (and intermediate files)
7 #                             are generated to.
8 #  COVERAGE_EXCLUDES        - List of exclude patterns (for example '*/tests/*').
10 # The following targets will be generated:
11 #  coverage                 - Builds an html report. Requires LCOV.
12 #  coverage_xml             - Builds an xml report (in Cobertura format for Jenkins).
13 #                             Requires Gcovr.
15 # Inspired by https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake
17 # Compiler and linker setup
18 set(CMAKE_C_FLAGS_COVERAGE "-g -O0 --coverage" CACHE STRING
19   "Flags used by the C compiler during coverage builds." FORCE)
20 set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage" CACHE STRING
21   "Flags used by the C++ compiler during coverage builds." FORCE)
22 set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage" CACHE STRING
23   "Flags used for linking binaries during coverage builds." FORCE)
24 set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "--coverage" CACHE STRING
25   "Flags used by the shared libraries linker during coverage builds." FORCE)
26 mark_as_advanced(
27   CMAKE_C_FLAGS_COVERAGE CMAKE_CXX_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE
28   CMAKE_SHARED_LINKER_FLAGS_COVERAGE CMAKE_STATIC_LINKER_FLAGS_COVERAGE
31 find_program(LCOV_EXECUTABLE lcov)
32 find_program(GENINFO_EXECUTABLE geninfo)
33 find_program(GENHTML_EXECUTABLE genhtml)
34 find_program(GCOVR_EXECUTABLE gcovr)
35 mark_as_advanced(LCOV_EXECUTABLE GENINFO_EXECUTABLE GENHTML_EXECUTABLE GCOVR_EXECUTABLE)
37 # Default options
38 if(NOT COVERAGE_SOURCE_DIR)
39   set(COVERAGE_SOURCE_DIR ${CMAKE_SOURCE_DIR})
40 endif()
41 if(NOT COVERAGE_BINARY_DIR)
42   set(COVERAGE_BINARY_DIR ${CMAKE_BINARY_DIR}/coverage)
43 endif()
44 if(NOT COVERAGE_EXCLUDES)
45   set(COVERAGE_EXCLUDES)
46 endif()
48 # Allow variables in COVERAGE_DEPENDS that are not evaluated before this file is included.
49 string(CONFIGURE "${COVERAGE_DEPENDS}" COVERAGE_DEPENDS)
51 # Add coverage target that generates an HTML report using LCOV
52 if(LCOV_EXECUTABLE AND GENINFO_EXECUTABLE AND GENHTML_EXECUTABLE)
53   file(MAKE_DIRECTORY ${COVERAGE_BINARY_DIR})
54   add_custom_target(coverage
55     COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_BINARY_DIR}
56     COMMAND ${LCOV_EXECUTABLE} -z -q -d ${CMAKE_BINARY_DIR}
57     COMMAND ${LCOV_EXECUTABLE} -c -q -i -d ${CMAKE_BINARY_DIR} -b ${COVERAGE_SOURCE_DIR}
58                                -o ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage_base.info
59     COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target test || true
60     COMMAND ${LCOV_EXECUTABLE} -c -q -d ${CMAKE_BINARY_DIR} -b ${COVERAGE_SOURCE_DIR}
61                                -o ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage_test.info
62     COMMAND ${LCOV_EXECUTABLE} -a ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage_base.info
63                                -a ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage_test.info
64                                -o ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage.info -q
65     COMMAND ${LCOV_EXECUTABLE} -q -r ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage.info
66                                /usr/include/* ${CMAKE_BINARY_DIR}/* ${COVERAGE_EXCLUDES}
67                                -o ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage.info
68     COMMAND ${GENHTML_EXECUTABLE} ${COVERAGE_BINARY_DIR}/${PROJECT_NAME}.coverage.info
69                                -o ${COVERAGE_BINARY_DIR}/html -s --legend --highlight --demangle-cpp
70     COMMAND ${CMAKE_COMMAND} -E echo "Coverage report: file://${COVERAGE_BINARY_DIR}/html/index.html"
71     WORKING_DIRECTORY ${COVERAGE_BINARY_DIR}
72     VERBATIM
73     DEPENDS ${COVERAGE_DEPENDS}
74     COMMENT "Generate code coverage html report"
75   )
76 else()
77   message(WARNING "Target coverage not available (lcov, geninfo and genhtml needed).")
78 endif()
80 # Add coverage target that generates an XML report using Gcovr
81 if(GCOVR_EXECUTABLE)
82   file(MAKE_DIRECTORY ${COVERAGE_BINARY_DIR})
83   string(REGEX REPLACE "([^;]+)" "--exclude=\"\\1\"" _gcovr_excludes "${COVERAGE_EXCLUDES}")
84   string(REPLACE "*" ".*" _gcovr_excludes "${_gcovr_excludes}")
85   add_custom_target(coverage_xml
86     COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target test || true
87     COMMAND ${GCOVR_EXECUTABLE} -x -r ${COVERAGE_SOURCE_DIR} -o ${COVERAGE_BINARY_DIR}/coverage.xml
88                                 --object-directory ${CMAKE_BINARY_DIR} ${_gcovr_excludes} ${CMAKE_BINARY_DIR}
89     COMMAND ${CMAKE_COMMAND} -E echo "Coverage report: file://${COVERAGE_BINARY_DIR}/coverage.xml"
90     WORKING_DIRECTORY ${COVERAGE_BINARY_DIR}
91     DEPENDS ${COVERAGE_DEPENDS}
92     COMMENT "Generate code coverage xml report"
93   )
94   unset(_gcovr_excludes)
95 else()
96   message(WARNING "Target coverage_xml not available (gcovr needed).")
97 endif()