File indexing completed on 2025-01-18 09:10:44
0001
0002 import sys
0003 import os
0004 import subprocess
0005 import argparse
0006 import multiprocessing as mp
0007 import re
0008
0009
0010 if not os.path.exists("CMakeCache.txt"):
0011 print("Not in CMake build dir. Not executing")
0012 sys.exit(1)
0013
0014
0015 def check_output(*args, **kwargs):
0016 p = subprocess.Popen(
0017 *args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs
0018 )
0019 p.wait()
0020 stdout, stderr = p.communicate()
0021 stdout = stdout.decode("utf-8")
0022 return (p.returncode, stdout.strip())
0023
0024
0025
0026 def call(cmd):
0027 print(" ".join(cmd))
0028 try:
0029 subprocess.check_call(cmd)
0030 except subprocess.CalledProcessError as e:
0031 print("Failed, output: ", e.output)
0032 raise e
0033
0034
0035 p = argparse.ArgumentParser()
0036 p.add_argument("--gcov", default=check_output(["which", "gcov"])[1])
0037 args = p.parse_args()
0038
0039 ret, gcovr_exe = check_output(["which", "gcovr"])
0040 assert ret == 0, "gcovr not installed. Use 'pip install gcovr'."
0041
0042 ret, gcovr_version_text = check_output(["gcovr", "--version"])
0043 gcovr_version = tuple(
0044 map(int, re.match(r"gcovr (\d+\.\d+)", gcovr_version_text).group(1).split("."))
0045 )
0046
0047 extra_flags = []
0048
0049 print(f"Found gcovr version {gcovr_version[0]}.{gcovr_version[1]}")
0050 if gcovr_version < (5,):
0051 print("Consider upgrading to a newer gcovr version.")
0052 elif gcovr_version == (5, 1):
0053 assert False and "Version 5.1 does not support parallel processing of gcov data"
0054 elif gcovr_version >= (6,):
0055 extra_flags += ["--exclude-noncode-lines"]
0056
0057 gcovr = [gcovr_exe]
0058
0059 script_dir = os.path.dirname(__file__)
0060 source_dir = os.path.abspath(os.path.join(script_dir, ".."))
0061 coverage_dir = os.path.abspath("coverage")
0062
0063 if not os.path.exists(coverage_dir):
0064 os.makedirs(coverage_dir)
0065
0066 excludes = ["-e", "../Tests/", "-e", r".*json\.hpp"]
0067
0068
0069 call(
0070 gcovr
0071 + ["-r", source_dir]
0072 + ["--gcov-executable", args.gcov]
0073 + ["-j", str(mp.cpu_count())]
0074 + excludes
0075 + extra_flags
0076 + ["--sonarqube", "coverage/cov.xml"]
0077 )
0078
0079 call(
0080 gcovr
0081 + ["-r", source_dir]
0082 + ["-j", str(mp.cpu_count())]
0083 + ["--gcov-executable", args.gcov]
0084 + excludes
0085 + extra_flags
0086 )