~sil2100/location-service/gps-arm64

« back to all changes in this revision

Viewing changes to cmake/EnableCoverageReport.cmake

  • Committer: Thomas Voß
  • Date: 2013-05-28 14:20:45 UTC
  • Revision ID: thomas.voss@canonical.com-20130528142045-kq5umqdmm4o53vwk
Initial push.

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
            # gcovr cannot write directly to a file so the execution needs to
 
122
            # be wrapped in a cmake file that generates the file output
 
123
            FILE(WRITE ${COVERAGE_XML_COMMAND_FILE}
 
124
                 "SET(ENV{LANG} en)\n")
 
125
            FILE(APPEND ${COVERAGE_XML_COMMAND_FILE}
 
126
                "EXECUTE_PROCESS(COMMAND \"${GCOVR_EXECUTABLE}\" --exclude=3rd_party.* --exclude=tests.* --exclude=obj-.* --exclude=cmake.* --exclude=include.mir_test.* --exclude=include.mir_test_doubles.* --exclude=include.mir_test_framework.* -c \"${CMAKE_GCOV}\" -x -r \"${CMAKE_SOURCE_DIR}\" OUTPUT_FILE \"${COVERAGE_XML_FILE}\" WORKING_DIRECTORY \"${CMAKE_BINARY_DIR}\")\n")
 
127
        
 
128
            ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_XML_FILE}
 
129
                               COMMAND ${CMAKE_COMMAND} ARGS -P ${COVERAGE_XML_COMMAND_FILE}
 
130
                               COMMENT "Generating coverage XML report"
 
131
                               VERBATIM)
 
132
                               
 
133
            ADD_CUSTOM_TARGET(coverage-xml
 
134
                              DEPENDS ${COVERAGE_XML_FILE})
 
135
        
 
136
        ENDIF()
 
137
        
 
138
        # provide a global coverage target executing both steps if available
 
139
        SET(GLOBAL_DEPENDS "")
 
140
        IF(LCOV_FOUND)
 
141
            LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_REPORT_DIR})
 
142
        ENDIF()
 
143
        IF(GCOVR_FOUND)
 
144
            LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_XML_FILE})
 
145
        ENDIF()
 
146
        IF(LCOV_FOUND OR GCOVR_FOUND)
 
147
            ADD_CUSTOM_TARGET(coverage
 
148
                              DEPENDS ${GLOBAL_DEPENDS})
 
149
        ENDIF()
 
150
                          
 
151
    ENDIF()
 
152
 
 
153
ENDFUNCTION()