| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import argparse |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import os |
| 7 |
import shutil |
| 8 |
from typing import List |
| 9 |
|
| 10 |
ACCEPTABLE_COMBINATIONS = ( |
| 11 |
("gcov", "genhtml", "html"), |
| 12 |
("clang", "llvm-cov", "html"), |
| 13 |
("gcov", "gcovr", "summary"), |
| 14 |
("gcov", "gcovr", "html"), |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def parse_args(): |
| 19 |
parser = argparse.ArgumentParser( |
| 20 |
description="Create a coverage report for Dungeon Crawl Stone Soup.", |
| 21 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 22 |
epilog="Acceptable combinations for coverage/generator/format:\n{combos}.".format( |
| 23 |
combos=acceptable_combinations_description() |
| 24 |
), |
| 25 |
) |
| 26 |
parser.add_argument( |
| 27 |
"-c", |
| 28 |
"--coverage", |
| 29 |
choices=("gcov", "clang"), |
| 30 |
default=default_coverage(), |
| 31 |
help="Coverage type. Use the same value you passed to the Makefile as COVERAGE. (auto-detected default)", |
| 32 |
) |
| 33 |
parser.add_argument( |
| 34 |
"-g", |
| 35 |
"--generator", |
| 36 |
choices=("genhtml", "llvm-cov", "gcovr"), |
| 37 |
default=default_generator(), |
| 38 |
help="Report generator (auto-detected default)", |
| 39 |
) |
| 40 |
parser.add_argument( |
| 41 |
"-f", |
| 42 |
"--format", |
| 43 |
choices=("html", "summary"), |
| 44 |
default="html", |
| 45 |
help="Report format", |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
"-b", |
| 49 |
"--binary", |
| 50 |
choices=("crawl", "catch2-tests-executable"), |
| 51 |
help="Binary to report on (auto-detected default)", |
| 52 |
default=default_binary(), |
| 53 |
) |
| 54 |
|
| 55 |
return parser.parse_args() |
| 56 |
|
| 57 |
|
| 58 |
def acceptable_combinations_description(): |
| 59 |
return ", ".join("/".join(combo) for combo in sorted(ACCEPTABLE_COMBINATIONS)) |
| 60 |
|
| 61 |
|
| 62 |
def sanity_check_args(args): |
| 63 |
combo = (args.coverage, args.generator, args.format) |
| 64 |
if combo not in ACCEPTABLE_COMBINATIONS: |
| 65 |
die("Unsupported combination of coverage, generator & format.") |
| 66 |
|
| 67 |
if not shutil.which(args.generator): |
| 68 |
die( |
| 69 |
"Report generator %s doesn't exist. Do you need to install it?" |
| 70 |
% args.generator |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
def clean(name: str): |
| 75 |
if os.path.exists(name): |
| 76 |
print("Deleting existing %s" % name) |
| 77 |
else: |
| 78 |
return |
| 79 |
if os.path.isfile(name): |
| 80 |
os.unlink(name) |
| 81 |
elif os.path.isdir(name): |
| 82 |
subprocess.run(["rm", "-r", name]) |
| 83 |
else: |
| 84 |
die("Not sure how to do that actually...") |
| 85 |
|
| 86 |
|
| 87 |
def gcovr_common_args(): |
| 88 |
return ( |
| 89 |
"gcovr", |
| 90 |
"--exclude=catch2-tests/catch.hpp", |
| 91 |
"--exclude=conftest", |
| 92 |
"--exclude=levcomp", |
| 93 |
"--exclude=catch2-tests/catch.hpp", |
| 94 |
"--exclude=conftest", |
| 95 |
"--exclude=levcomp", |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
def generate_report(generator, report_format, binary): |
| 100 |
if report_format == "html": |
| 101 |
clean("coverage-html-report") |
| 102 |
os.makedirs("coverage-html-report") |
| 103 |
|
| 104 |
if generator == "llvm-cov" and report_format == "html": |
| 105 |
run( |
| 106 |
[ |
| 107 |
"llvm-cov", |
| 108 |
"show", |
| 109 |
binary, |
| 110 |
"-instr-profile=coverage.profdata", |
| 111 |
"-format=html", |
| 112 |
"-output-dir=coverage-html-report", |
| 113 |
] |
| 114 |
) |
| 115 |
elif generator == "genhtml": |
| 116 |
run( |
| 117 |
[ |
| 118 |
"genhtml", |
| 119 |
"coverage.info", |
| 120 |
"--output-directory", |
| 121 |
"coverage-html-report", |
| 122 |
"--ignore-errors", |
| 123 |
"source", |
| 124 |
] |
| 125 |
) |
| 126 |
elif generator == "gcovr" and report_format == "summary": |
| 127 |
run([*gcovr_common_args(), "--print-summary"]) |
| 128 |
elif generator == "gcovr" and report_format == "html": |
| 129 |
run( |
| 130 |
[ |
| 131 |
*gcovr_common_args(), |
| 132 |
"--html=coverage-html-report/index.html", |
| 133 |
"--html-details", |
| 134 |
] |
| 135 |
) |
| 136 |
else: |
| 137 |
die("Unhandled generator/format combo") |
| 138 |
|
| 139 |
if report_format == "html" and shutil.which("open"): |
| 140 |
run(["open", "coverage-html-report/index.html"]) |
| 141 |
|
| 142 |
|
| 143 |
def prepare_intermediate_profiling_data(coverage: str, generator: str): |
| 144 |
if coverage == "gcov" and generator != "gcovr": |
| 145 |
clean("coverage.info") |
| 146 |
run( |
| 147 |
[ |
| 148 |
"lcov", |
| 149 |
"--capture", |
| 150 |
"--directory", |
| 151 |
".", |
| 152 |
"--output-file", |
| 153 |
"coverage.info", |
| 154 |
"--ignore-errors", |
| 155 |
"source", |
| 156 |
] |
| 157 |
) |
| 158 |
elif coverage == "gcov" and generator == "gcovr": |
| 159 |
pass |
| 160 |
elif coverage == "clang": |
| 161 |
clean("coverage.profdata") |
| 162 |
run( |
| 163 |
[ |
| 164 |
"llvm-profdata", |
| 165 |
"merge", |
| 166 |
"-sparse", |
| 167 |
"default.profraw", |
| 168 |
"-o", |
| 169 |
"coverage.profdata", |
| 170 |
] |
| 171 |
) |
| 172 |
else: |
| 173 |
die("Can't prepare profiling data of unknown type (%s)." % coverage) |
| 174 |
|
| 175 |
|
| 176 |
def profiling_data_exists(coverage: str) -> bool: |
| 177 |
if coverage == "gcov": |
| 178 |
return os.path.isfile("english.gcda") # file picked arbitrarily |
| 179 |
elif coverage == "clang": |
| 180 |
return os.path.isfile("default.profraw") |
| 181 |
else: |
| 182 |
die("Can't check for unknown type of profiling data (%s)." % coverage) |
| 183 |
|
| 184 |
|
| 185 |
def run(cmd: List[str]) -> int: |
| 186 |
print("Running: %s" % " ".join(cmd)) |
| 187 |
ret = subprocess.call(cmd) |
| 188 |
if ret != 0: |
| 189 |
die("Command failed, exiting %s" % sys.argv[0]) |
| 190 |
return ret |
| 191 |
|
| 192 |
|
| 193 |
def die(msg: str): |
| 194 |
print(msg, file=sys.stderr) |
| 195 |
sys.exit(1) |
| 196 |
|
| 197 |
|
| 198 |
def default_coverage() -> str: |
| 199 |
# capture_output=True requires Python 3.7+ :( |
| 200 |
p = subprocess.run(["cc", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 201 |
if b"clang" in p.stderr.split(b"\n")[0]: |
| 202 |
return "clang" |
| 203 |
return "gcov" |
| 204 |
|
| 205 |
|
| 206 |
def default_generator() -> str: |
| 207 |
if default_coverage() == "gcov": |
| 208 |
if shutil.which("gcovr"): |
| 209 |
return "gcovr" |
| 210 |
return "genhtml" |
| 211 |
return "llvm-cov" |
| 212 |
|
| 213 |
|
| 214 |
def default_binary() -> str: |
| 215 |
if os.path.isfile("catch2-tests-executable"): |
| 216 |
return "catch2-tests-executable" |
| 217 |
else: |
| 218 |
return "crawl" |
| 219 |
|
| 220 |
|
| 221 |
def main(): |
| 222 |
if sys.version_info < (3, 5): |
| 223 |
# hint for subprocess.run |
| 224 |
raise ImportError("This script requires Python 3.5+") |
| 225 |
|
| 226 |
args = parse_args() |
| 227 |
|
| 228 |
print( |
| 229 |
"Generating {format} report with {generator} for '{binary}' (with {coverage} coverage data)".format( |
| 230 |
format=args.format, |
| 231 |
generator=args.generator, |
| 232 |
coverage=args.coverage, |
| 233 |
binary=args.binary, |
| 234 |
) |
| 235 |
) |
| 236 |
|
| 237 |
sanity_check_args(args) |
| 238 |
|
| 239 |
if not profiling_data_exists(args.coverage): |
| 240 |
die( |
| 241 |
"There is no profiling data (hint: did you make {bin} with COVERAGE={cov} set and then run the binary?)".format( |
| 242 |
bin=args.binary, cov=args.coverage |
| 243 |
) |
| 244 |
) |
| 245 |
|
| 246 |
prepare_intermediate_profiling_data(args.coverage, args.generator) |
| 247 |
|
| 248 |
generate_report(args.generator, args.format, args.binary) |
| 249 |
|
| 250 |
|
| 251 |
if __name__ == "__main__": |
| 252 |
main() |