~ci-train-bot/ubuntu-settings-components/ubuntu-settings-components-ubuntu-yakkety-landing-092

« back to all changes in this revision

Viewing changes to cmake/modules/EnableCoverageReport.cmake

  • Committer: Bileto Bot
  • Date: 2016-08-18 10:53:52 UTC
  • mfrom: (103.1.43 add-autopkgtests)
  • Revision ID: ci-train-bot@canonical.com-20160818105352-eg3v2lkxv3w7w9n5
rename qtdeclarative5-ubuntu-settings-components to new naming scheme,
qml-module-ubuntu-settings-components

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# - Creates a special coverage build type and target on GCC.
2
 
#
3
 
# Defines a function ENABLE_COVERAGE_REPORT which generates the coverage target
4
 
# for selected targets. Optional arguments to this function are used to filter
5
 
# unwanted results using globbing expressions. Moreover targets with tests for
6
 
# the source code can be specified to trigger regenerating the report if the
7
 
# test has changed
8
 
#
9
 
# ENABLE_COVERAGE_REPORT(TARGETS target... [FILTER filter...] [TESTS test targets...])
10
 
#
11
 
# To generate a coverage report first build the project with
12
 
# CMAKE_BUILD_TYPE=coverage, then call make test and afterwards make coverage.
13
 
#
14
 
# The coverage report is based on gcov. Depending on the availability of lcov
15
 
# a HTML report will be generated and/or an XML report of gcovr is found.
16
 
# The generated coverage target executes all found solutions. Special targets
17
 
# exist to create e.g. only the xml report: coverage-xml. 
18
 
#
19
 
# Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
20
 
#
21
 
# This program is free software; you can redistribute it
22
 
# and/or modify it under the terms of the GNU General
23
 
# Public License as published by the Free Software Foundation;
24
 
# either version 2, or (at your option)
25
 
# any later version.
26
 
#
27
 
# This program is distributed in the hope that it will be useful,
28
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 
# GNU General Public License for more details.
31
 
#
32
 
 
33
 
INCLUDE(ParseArguments)
34
 
 
35
 
FIND_PACKAGE(Lcov)
36
 
FIND_PACKAGE(gcovr)
37
 
 
38
 
FUNCTION(ENABLE_COVERAGE_REPORT)
39
 
    
40
 
    # argument parsing
41
 
    PARSE_ARGUMENTS(ARG "FILTER;TARGETS;TESTS" "" ${ARGN})
42
 
    
43
 
    SET(COVERAGE_RAW_FILE "${CMAKE_BINARY_DIR}/coverage.raw.info")
44
 
    SET(COVERAGE_FILTERED_FILE "${CMAKE_BINARY_DIR}/coverage.info")
45
 
    SET(COVERAGE_REPORT_DIR "${CMAKE_BINARY_DIR}/coveragereport")
46
 
    SET(COVERAGE_XML_FILE "${CMAKE_BINARY_DIR}/coverage.xml")
47
 
    SET(COVERAGE_XML_COMMAND_FILE "${CMAKE_BINARY_DIR}/coverage-xml.cmake")
48
 
    
49
 
    # decide if there is any tool to create coverage data
50
 
    SET(TOOL_FOUND FALSE)
51
 
    IF(LCOV_FOUND OR GCOVR_FOUND)
52
 
        SET(TOOL_FOUND TRUE)
53
 
    ENDIF()
54
 
    IF(NOT TOOL_FOUND)
55
 
        MESSAGE(STATUS "Cannot enable coverage targets because neither lcov nor gcovr are found.")
56
 
    ENDIF()
57
 
    
58
 
    STRING(TOLOWER "${CMAKE_BUILD_TYPE}" COVERAGE_BUILD_TYPE)
59
 
    IF(CMAKE_COMPILER_IS_GNUCXX AND TOOL_FOUND AND "${COVERAGE_BUILD_TYPE}" MATCHES "coverage")
60
 
    
61
 
        MESSAGE(STATUS "Coverage support enabled for targets: ${ARG_TARGETS}")
62
 
    
63
 
        # create coverage build type
64
 
        SET(CMAKE_CXX_FLAGS_COVERAGE ${CMAKE_CXX_FLAGS_DEBUG} PARENT_SCOPE)
65
 
        SET(CMAKE_C_FLAGS_COVERAGE ${CMAKE_C_FLAGS_DEBUG} PARENT_SCOPE)
66
 
        SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES} coverage PARENT_SCOPE)
67
 
    
68
 
        # instrument targets
69
 
        SET_TARGET_PROPERTIES(${ARG_TARGETS} PROPERTIES COMPILE_FLAGS --coverage
70
 
                                                        LINK_FLAGS --coverage)
71
 
    
72
 
        # html report
73
 
        IF (LCOV_FOUND)
74
 
        
75
 
            MESSAGE(STATUS "Enabling HTML coverage report")
76
 
    
77
 
            # set up coverage target
78
 
            
79
 
            ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_RAW_FILE}
80
 
                               COMMAND ${LCOV_EXECUTABLE} -c -d ${CMAKE_BINARY_DIR} -o ${COVERAGE_RAW_FILE}
81
 
                               WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
82
 
                               COMMENT "Collecting coverage data"
83
 
                               DEPENDS ${ARG_TARGETS} ${ARG_TESTS}
84
 
                               VERBATIM)
85
 
            
86
 
            # filter unwanted stuff
87
 
            LIST(LENGTH ARG_FILTER FILTER_LENGTH)
88
 
            IF(${FILTER_LENGTH} GREATER 0)
89
 
                SET(FILTER COMMAND ${LCOV_EXECUTABLE})
90
 
                FOREACH(F ${ARG_FILTER})
91
 
                    SET(FILTER ${FILTER} -r ${COVERAGE_FILTERED_FILE} ${F})
92
 
                ENDFOREACH()
93
 
                SET(FILTER ${FILTER} -o ${COVERAGE_FILTERED_FILE})
94
 
            ELSE()
95
 
                SET(FILTER "")
96
 
            ENDIF()
97
 
            
98
 
            ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_FILTERED_FILE}
99
 
                               COMMAND ${LCOV_EXECUTABLE} -e ${COVERAGE_RAW_FILE} "${CMAKE_SOURCE_DIR}*"  -o ${COVERAGE_FILTERED_FILE}
100
 
                               ${FILTER}
101
 
                               DEPENDS ${COVERAGE_RAW_FILE}
102
 
                               COMMENT "Filtering recorded coverage data for project-relevant entries"
103
 
                               VERBATIM)
104
 
            ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_REPORT_DIR}
105
 
                               COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_REPORT_DIR}
106
 
                               COMMAND ${GENHTML_EXECUTABLE} --legend --show-details -t "${PROJECT_NAME} test coverage" -o ${COVERAGE_REPORT_DIR} ${COVERAGE_FILTERED_FILE}
107
 
                               DEPENDS ${COVERAGE_FILTERED_FILE}
108
 
                               COMMENT "Generating HTML coverage report in ${COVERAGE_REPORT_DIR}"
109
 
                               VERBATIM)
110
 
                               
111
 
            ADD_CUSTOM_TARGET(coverage-html
112
 
                              DEPENDS ${COVERAGE_REPORT_DIR})
113
 
                              
114
 
        ENDIF()
115
 
        
116
 
        # xml coverage report
117
 
        IF(GCOVR_FOUND)
118
 
        
119
 
            MESSAGE(STATUS "Enabling XML coverage report")
120
 
 
121
 
            # filter unwanted stuff
122
 
            SET(GCOV_FILTER "")
123
 
            LIST(LENGTH ARG_FILTER FILTER_LENGTH)
124
 
            IF(${FILTER_LENGTH} GREATER 0)
125
 
                FOREACH(F ${ARG_FILTER})
126
 
                    SET(GCOV_FILTER "${GCOV_FILTER} -e \"${F}\"")
127
 
                ENDFOREACH()
128
 
            ENDIF()
129
 
 
130
 
            # gcovr cannot write directly to a file so the execution needs to
131
 
            # be wrapped in a cmake file that generates the file output
132
 
            FILE(WRITE ${COVERAGE_XML_COMMAND_FILE}
133
 
                 "SET(ENV{LANG} en)\n")
134
 
            FILE(APPEND ${COVERAGE_XML_COMMAND_FILE}
135
 
                 "EXECUTE_PROCESS(COMMAND \"${GCOVR_EXECUTABLE}\" -x -r \"${CMAKE_SOURCE_DIR}\" ${GCOV_FILTER} OUTPUT_FILE \"${COVERAGE_XML_FILE}\" WORKING_DIRECTORY \"${CMAKE_BINARY_DIR}\")\n")
136
 
        
137
 
            ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_XML_FILE}
138
 
                               COMMAND ${CMAKE_COMMAND} ARGS -P ${COVERAGE_XML_COMMAND_FILE}
139
 
                               COMMENT "Generating coverage XML report"
140
 
                               VERBATIM)
141
 
                               
142
 
            ADD_CUSTOM_TARGET(coverage-xml
143
 
                              DEPENDS ${COVERAGE_XML_FILE})
144
 
        
145
 
        ENDIF()
146
 
        
147
 
        # provide a global coverage target executing both steps if available
148
 
        SET(GLOBAL_DEPENDS "")
149
 
        IF(LCOV_FOUND)
150
 
            LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_REPORT_DIR})
151
 
        ENDIF()
152
 
        IF(GCOVR_FOUND)
153
 
            LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_XML_FILE})
154
 
        ENDIF()
155
 
        IF(LCOV_FOUND OR GCOVR_FOUND)
156
 
            ADD_CUSTOM_TARGET(coverage
157
 
                              DEPENDS ${GLOBAL_DEPENDS})
158
 
        ENDIF()
159
 
                          
160
 
    ENDIF()
161
 
 
162
 
    # This gets rid of any stale .gcda files. Run this if a running a binary causes lots of messages about
163
 
    # about a "merge mismatch for summaries".
164
 
    ADD_CUSTOM_TARGET(clean-coverage COMMAND find ${CMAKE_BINARY_DIR} -name '*.gcda' | xargs rm -f)
165
 
 
166
 
ENDFUNCTION()