| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2020 The Meson development team |
| 3 |
# Copyright © 2023-2025 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import typing as T |
| 10 |
|
| 11 |
from . import coredata |
| 12 |
from . import mesonlib |
| 13 |
from . import mlog |
| 14 |
from .mesonlib import MachineChoice, Popen_safe, search_version, quote_arg, split_args |
| 15 |
from .programs import ExternalProgram |
| 16 |
|
| 17 |
|
| 18 |
def detect_gcovr(gcovr_exe: str = 'gcovr', min_version: str = '3.3', log: bool = False) \ |
| 19 |
-> T.Union[T.Tuple[None, None], T.Tuple[str, str]]: |
| 20 |
try: |
| 21 |
p, found = Popen_safe([gcovr_exe, '--version'])[0:2] |
| 22 |
except (FileNotFoundError, PermissionError): |
| 23 |
# Doesn't exist in PATH or isn't executable |
| 24 |
return None, None |
| 25 |
found = search_version(found) |
| 26 |
if p.returncode == 0 and mesonlib.version_compare(found, '>=' + min_version): |
| 27 |
if log: |
| 28 |
mlog.log('Found gcovr-{} at {}'.format(found, quote_arg(shutil.which(gcovr_exe)))) |
| 29 |
return gcovr_exe, found |
| 30 |
return None, None |
| 31 |
|
| 32 |
def detect_lcov(lcov_exe: str = 'lcov', log: bool = False) \ |
| 33 |
-> T.Union[T.Tuple[None, None], T.Tuple[str, str]]: |
| 34 |
try: |
| 35 |
p, found = Popen_safe([lcov_exe, '--version'])[0:2] |
| 36 |
except (FileNotFoundError, PermissionError): |
| 37 |
# Doesn't exist in PATH or isn't executable |
| 38 |
return None, None |
| 39 |
found = search_version(found) |
| 40 |
if p.returncode == 0 and found: |
| 41 |
if log: |
| 42 |
mlog.log('Found lcov-{} at {}'.format(found, quote_arg(shutil.which(lcov_exe)))) |
| 43 |
return lcov_exe, found |
| 44 |
return None, None |
| 45 |
|
| 46 |
def detect_llvm_cov(suffix: T.Optional[str] = None) -> T.Optional[str]: |
| 47 |
# If there's a known suffix or forced lack of suffix, use that |
| 48 |
if suffix is not None: |
| 49 |
if suffix == '': |
| 50 |
tool = 'llvm-cov' |
| 51 |
else: |
| 52 |
tool = f'llvm-cov-{suffix}' |
| 53 |
if shutil.which(tool) is not None: |
| 54 |
return tool |
| 55 |
else: |
| 56 |
# Otherwise guess in the dark |
| 57 |
tools = get_llvm_tool_names('llvm-cov') |
| 58 |
for tool in tools: |
| 59 |
if shutil.which(tool): |
| 60 |
return tool |
| 61 |
return None |
| 62 |
|
| 63 |
def compute_llvm_suffix(coredata: coredata.CoreData) -> T.Optional[str]: |
| 64 |
# Check to see if the user is trying to do coverage for either a C or C++ project |
| 65 |
compilers = coredata.compilers[MachineChoice.BUILD] |
| 66 |
cpp_compiler_is_clang = 'cpp' in compilers and compilers['cpp'].id == 'clang' |
| 67 |
c_compiler_is_clang = 'c' in compilers and compilers['c'].id == 'clang' |
| 68 |
# Extract first the C++ compiler if available. If it's a Clang of some kind, compute the suffix if possible |
| 69 |
if cpp_compiler_is_clang: |
| 70 |
suffix = compilers['cpp'].version.split('.')[0] |
| 71 |
return suffix |
| 72 |
|
| 73 |
# Then the C compiler, again checking if it's some kind of Clang and computing the suffix |
| 74 |
if c_compiler_is_clang: |
| 75 |
suffix = compilers['c'].version.split('.')[0] |
| 76 |
return suffix |
| 77 |
|
| 78 |
# Neither compiler is a Clang, or no compilers are for C or C++ |
| 79 |
return None |
| 80 |
|
| 81 |
def detect_lcov_genhtml(lcov_exe_: str = 'lcov', genhtml_exe_: str = 'genhtml') \ |
| 82 |
-> tuple[str | None, str | None, str | None]: |
| 83 |
lcov_exe, lcov_version = detect_lcov(lcov_exe_) |
| 84 |
if shutil.which(genhtml_exe_) is None: |
| 85 |
genhtml_exe = None |
| 86 |
else: |
| 87 |
genhtml_exe = genhtml_exe_ |
| 88 |
|
| 89 |
return lcov_exe, lcov_version, genhtml_exe |
| 90 |
|
| 91 |
def find_coverage_tools(coredata: coredata.CoreData) -> T.Tuple[T.Optional[str], T.Optional[str], T.Optional[str], T.Optional[str], T.Optional[str], T.Optional[str]]: |
| 92 |
gcovr_exe, gcovr_version = detect_gcovr() |
| 93 |
|
| 94 |
llvm_cov_exe = detect_llvm_cov(compute_llvm_suffix(coredata)) |
| 95 |
# Some platforms may provide versioned clang but only non-versioned llvm utils |
| 96 |
if llvm_cov_exe is None: |
| 97 |
llvm_cov_exe = detect_llvm_cov('') |
| 98 |
|
| 99 |
lcov_exe, lcov_version, genhtml_exe = detect_lcov_genhtml() |
| 100 |
|
| 101 |
return gcovr_exe, gcovr_version, lcov_exe, lcov_version, genhtml_exe, llvm_cov_exe |
| 102 |
|
| 103 |
def detect_ninja(version: str = '1.8.2', log: bool = False) -> T.Optional[T.List[str]]: |
| 104 |
r = detect_ninja_command_and_version(version, log) |
| 105 |
return r[0] if r else None |
| 106 |
|
| 107 |
def detect_ninja_command_and_version(version: str = '1.8.2', log: bool = False) -> T.Optional[T.Tuple[T.List[str], str]]: |
| 108 |
env_ninja = os.environ.get('NINJA', None) |
| 109 |
for n in [env_ninja] if env_ninja else ['ninja', 'ninja-build', 'samu']: |
| 110 |
prog = ExternalProgram(n, silent=True) |
| 111 |
if not prog.found(): |
| 112 |
continue |
| 113 |
try: |
| 114 |
p, found = Popen_safe(prog.command + ['--version'])[0:2] |
| 115 |
except (FileNotFoundError, PermissionError): |
| 116 |
# Doesn't exist in PATH or isn't executable |
| 117 |
continue |
| 118 |
found = found.strip() |
| 119 |
# Perhaps we should add a way for the caller to know the failure mode |
| 120 |
# (not found or too old) |
| 121 |
if p.returncode == 0 and mesonlib.version_compare(found, '>=' + version): |
| 122 |
if log: |
| 123 |
name = os.path.basename(n) |
| 124 |
if name.endswith('-' + found): |
| 125 |
name = name[0:-1 - len(found)] |
| 126 |
if name == 'ninja-build': |
| 127 |
name = 'ninja' |
| 128 |
if name == 'samu': |
| 129 |
name = 'samurai' |
| 130 |
mlog.log('Found {}-{} at {}'.format(name, found, |
| 131 |
' '.join([quote_arg(x) for x in prog.command]))) |
| 132 |
return (prog.command, found) |
| 133 |
return None |
| 134 |
|
| 135 |
def get_llvm_tool_names(tool: str) -> T.List[str]: |
| 136 |
# Ordered list of possible suffixes of LLVM executables to try. Start with |
| 137 |
# base, then try newest back to oldest (3.5 is arbitrary), and finally the |
| 138 |
# devel version. Please note that the development snapshot in Debian does |
| 139 |
# not have a distinct name. Do not move it to the beginning of the list |
| 140 |
# unless it becomes a stable release. |
| 141 |
suffixes = [ |
| 142 |
'', # base (no suffix) |
| 143 |
'-22.1', '22.1', |
| 144 |
'-22', '22', |
| 145 |
'-21.1', '21.1', |
| 146 |
'-21', '21', |
| 147 |
'-20.1', '20.1', |
| 148 |
'-20', '20', |
| 149 |
'-19.1', '19.1', |
| 150 |
'-19', '19', |
| 151 |
'-18.1', '18.1', |
| 152 |
'-18', '18', |
| 153 |
'-17', '17', |
| 154 |
'-16', '16', |
| 155 |
'-15', '15', |
| 156 |
'-14', '14', |
| 157 |
'-13', '13', |
| 158 |
'-12', '12', |
| 159 |
'-11', '11', |
| 160 |
'-10', '10', |
| 161 |
'-9', '90', |
| 162 |
'-8', '80', |
| 163 |
'-7', '70', |
| 164 |
'-6.0', '60', |
| 165 |
'-5.0', '50', |
| 166 |
'-4.0', '40', |
| 167 |
'-3.9', '39', |
| 168 |
'-3.8', '38', |
| 169 |
'-3.7', '37', |
| 170 |
'-3.6', '36', |
| 171 |
'-3.5', '35', |
| 172 |
#'-20', # Debian development snapshot |
| 173 |
'-devel', # FreeBSD development snapshot |
| 174 |
] |
| 175 |
names: T.List[str] = [] |
| 176 |
for suffix in suffixes: |
| 177 |
names.append(tool + suffix) |
| 178 |
return names |
| 179 |
|
| 180 |
def detect_scanbuild() -> T.List[str]: |
| 181 |
""" Look for scan-build binary on build platform |
| 182 |
|
| 183 |
First, if a SCANBUILD env variable has been provided, give it precedence |
| 184 |
on all platforms. |
| 185 |
|
| 186 |
For most platforms, scan-build is found is the PATH contains a binary |
| 187 |
named "scan-build". However, some distribution's package manager (FreeBSD) |
| 188 |
don't. For those, loop through a list of candidates to see if one is |
| 189 |
available. |
| 190 |
|
| 191 |
Return: a single-element list of the found scan-build binary ready to be |
| 192 |
passed to Popen() |
| 193 |
""" |
| 194 |
exelist: T.List[str] = [] |
| 195 |
if 'SCANBUILD' in os.environ: |
| 196 |
exelist = split_args(os.environ['SCANBUILD']) |
| 197 |
|
| 198 |
else: |
| 199 |
tools = get_llvm_tool_names('scan-build') |
| 200 |
for tool in tools: |
| 201 |
which = shutil.which(tool) |
| 202 |
if which is not None: |
| 203 |
exelist = [which] |
| 204 |
break |
| 205 |
|
| 206 |
if exelist: |
| 207 |
tool = shutil.which(exelist[0]) |
| 208 |
if tool is not None: |
| 209 |
return [tool] + exelist[1:] |
| 210 |
return [] |
| 211 |
|
| 212 |
def detect_clangformat() -> T.List[str]: |
| 213 |
""" Look for clang-format binary on build platform |
| 214 |
|
| 215 |
Do the same thing as detect_scanbuild to find clang-format except it |
| 216 |
currently does not check the environment variable. |
| 217 |
|
| 218 |
Return: a single-element list of the found clang-format binary ready to be |
| 219 |
passed to Popen() |
| 220 |
""" |
| 221 |
tools = get_llvm_tool_names('clang-format') |
| 222 |
for tool in tools: |
| 223 |
path = shutil.which(tool) |
| 224 |
if path is not None: |
| 225 |
return [path] |
| 226 |
return [] |
| 227 |
|
| 228 |
def detect_clangtidy() -> T.List[str]: |
| 229 |
""" Look for clang-tidy binary on build platform |
| 230 |
|
| 231 |
Return: a single-element list of the found clang-tidy binary ready to be |
| 232 |
passed to Popen() |
| 233 |
""" |
| 234 |
tools = get_llvm_tool_names('clang-tidy') |
| 235 |
for tool in tools: |
| 236 |
path = shutil.which(tool) |
| 237 |
if path is not None: |
| 238 |
return [path] |
| 239 |
return [] |
| 240 |
|
| 241 |
def detect_clangapply() -> T.List[str]: |
| 242 |
""" Look for clang-apply-replacements binary on build platform |
| 243 |
|
| 244 |
Return: a single-element list of the found clang-apply-replacements binary |
| 245 |
ready to be passed to Popen() |
| 246 |
""" |
| 247 |
tools = get_llvm_tool_names('clang-apply-replacements') |
| 248 |
for tool in tools: |
| 249 |
path = shutil.which(tool) |
| 250 |
if path is not None: |
| 251 |
return [path] |
| 252 |
return [] |