summaryrefslogtreecommitdiffstats
path: root/test/tools/gcov_coveralls.py
blob: dcbd93f5b275121d26c11ab7d9e8ea0bf02c6746 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
"""
Script to save coverage info for C source files in JSON for coveralls.io

When C code compiled with --coverage flag, for each object files *.gcno is
generated, it contains information to reconstruct the basic block graphs and
assign source line numbers to blocks

When binary executed *.gcda file is written on exit, with same base name as
corresponding *.gcno file. It contains some summary information, counters, e.t.c.

gcov(1) utility can be used to get information from *.gcda file and write text
reports to *.gocov file (one file for each source file from which object was compiled).

The script finds *.gcno files, uses gcov to generate *.gcov files, parses them
and accumulates statistics for all source files.

This script was written with quite a few assumptions:

    * Code was build using absolute path to source directory (and absolute path
      stored in object file debug symbols).

    * Current directory is writable and there is no useful *.gcov files in it
      (because they will be deleted).

    * Object file has same base name as *.gcno file (e. g. foo.c.gcno and foo.c.o).
      This is the case for cmake builds, but probably not for other build systems

    * Source file names contain only ASCII characters.
"""

import argparse
from collections import defaultdict
from glob import glob
import hashlib
import json
import os
from os.path import isabs, join, normpath, relpath
import os.path
import subprocess
import sys


def warn(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def parse_gcov_file(gcov_file):
    """Parses the content of .gcov file written by gcov -i

    Returns:
      str: Source file name
      dict: coverage info { line_number: hits }
    """
    count = {}
    with open(gcov_file) as fh:
        for line in fh:
            tag, value = line.split(':')
            if tag == 'file':
                src_file = value.rstrip()
            elif tag == 'lcount':
                line_num, exec_count = value.split(',')
                count[int(line_num)] = int(exec_count)

    return src_file, count


def run_gcov(filename, coverage, args):
    """ * run gcov on given file
        * parse generated .gcov files and update coverage structure
        * store source file md5 (if not yet stored)
        * delete .gcov files
    """
    if args.verbose:
        warn("calling:", 'gcov', '-i', filename)
        stdout = None
    else:
        # gcov is noisy and don't have quit flag so redirect stdout to /dev/null
        stdout = subprocess.DEVNULL

    subprocess.check_call(['gcov', '-i', filename], stdout=stdout)

    for gcov_file in glob('*.gcov'):
        if args.verbose:
            warn('parsing', gcov_file)
        src_file, count = parse_gcov_file(gcov_file)
        os.remove(gcov_file)

        if src_file not in coverage:
            coverage[src_file] = defaultdict(int, count)
        else:
            # sum execution counts
            for line, exe_cnt in count.items():
                coverage[src_file][line] += exe_cnt


def main():
    parser = argparse.ArgumentParser(
        description='Save gcov coverage results in JSON file for coveralls.io.')
    parser.add_argument(
        '-v',
        '--verbose',
        action="store_true",
        help='Display additional information and gcov command output.')
    parser.add_argument(
        '-e',
        '--exclude',
        action='append',
        metavar='DIR',
        help=
        ("Don't look for .gcno/.gcda files in this directories (repeat option to skip several directories). "
         "Path is relative to the directory where script was started, e. g. '.git'"))
    parser.add_argument(
        '-p',
        '--prefix',
        action='append',
        help=
        ("Strip this prefix from absolute path to source file. "
         "If this option is provided, then only files with given prefixes in absolute path "
         "will be added to coverage (option can be repeated)."))
    parser.add_argument(
        '--out',
        type=argparse.FileType('w'),
        required=True,
        metavar='FILE',
        help='Save JSON payload to this file')
    args = parser.parse_args()

    # ensure that there is no unrelated .gcov files in current directory
    for gcov_file in glob('*.gcov'):
        os.remove(gcov_file)
        warn("Warning: {} deleted".format(gcov_file))

    # dict { src_file_name: {line1: exec_count1, line2: exec_count2, ...} }
    coverage = {}

    # find . -name '*.gcno' (respecting args.exclude)
    for root, dirs, files in os.walk('.'):
        for f in files:
            # Usually gcov called with a source file as an argument, but this
            # name used only to find .gcno and .gcda files.  To find source
            # file information from debug symbols is used.  So we can call gcov
            # on .gcno file.
            if f.endswith('.gcno'):
                run_gcov(join(root, f), coverage, args)

        # don't look into excluded dirs
        for subdir in dirs:
            # path relative to start dir
            path = normpath(join(root, subdir))
            if path in args.exclude:
                if args.verbose:
                    warn('directory "{}" excluded'.format(path))
                dirs.remove(subdir)

    # prepare JSON pyload for coveralls.io API
    # https://docs.coveralls.io/api-introduction
    coveralls_data = {'source_files': []}

    for src_file in coverage:
        # filter by prefix and save path with stripped prefix
        src_file_rel = src_file
        if args.prefix and isabs(src_file):
            for prefix in args.prefix:
                if src_file.startswith(prefix):
                    src_file_rel = relpath(src_file, start=prefix)
                    break
            else:
                # skip file outside given prefixes
                # it can be e. g. library include file
                if args.verbose:
                    warn('file "{}" is not matched by prefix, skipping'.format(src_file))
                continue

        try:
            with open(src_file, mode='rb') as fh:
                line_count = sum(1 for _ in fh)
                fh.seek(0)
                md5 = hashlib.md5(fh.read()).hexdigest()
        except OSError as err:
            # skip files for which source file is not available
            warn(err, 'not adding to coverage')
            continue

        coverage_array = [None] * line_count

        for line_num, exe_cnt in coverage[src_file].items():
            # item at index 0 representing the coverage for line 1 of the source code
            assert 1 <= line_num <= line_count
            coverage_array[line_num - 1] = exe_cnt

        coveralls_data['source_files'].append({
            'name': src_file_rel,
            'coverage': coverage_array,
            'source_digest': md5
        })

    args.out.write(json.dumps(coveralls_data))

    if args.verbose:
        warn('Coverage for {} source files was written'.format(
            len(coveralls_data['source_files'])))


if __name__ == '__main__':
    main()