Debian Codegraph @

mesonbuild/scripts/scanbuild.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2016 The Meson development team
3
4 from __future__ import annotations
5
6 import subprocess
7 import shutil
8 import tempfile
9 from ..cmdline import get_cmd_line_file, CmdLineFileParser
10 from ..tooldetect import detect_ninja, detect_scanbuild
11 from ..mesonlib import windows_proof_rmtree, determine_worker_count
12 from pathlib import Path
13 import typing as T
14 from ast import literal_eval
15 import os
16
17 def scanbuild(exelist: T.List[str], srcdir: Path, blddir: Path, privdir: Path, logdir: Path, subprojdir: Path, args: T.List[str]) -> int:
18 # In case of problems leave the temp directory around
19 # so it can be debugged.
20 scandir = tempfile.mkdtemp(dir=str(privdir))
21 meson_cmd = exelist + args
22 build_cmd = exelist + ['--exclude', str(subprojdir), '-o', str(logdir)] + detect_ninja() + ['-C', scandir, f'-j{determine_worker_count()}']
23 rc = subprocess.call(meson_cmd + [str(srcdir), scandir])
24 if rc != 0:
25 return rc
26 rc = subprocess.call(build_cmd)
27 if rc == 0:
28 windows_proof_rmtree(scandir)
29 return rc
30
31 def run(args: T.List[str]) -> int:
32 srcdir = Path(args[0])
33 bldpath = Path(args[1])
34 subprojdir = srcdir / Path(args[2])
35 blddir = args[1]
36 meson_cmd = args[3:]
37 privdir = bldpath / 'meson-private'
38 logdir = bldpath / 'meson-logs' / 'scanbuild'
39 shutil.rmtree(str(logdir), ignore_errors=True)
40
41 # if any cross or native files are specified we should use them
42 cmd = get_cmd_line_file(blddir)
43 data = CmdLineFileParser()
44 data.read(cmd)
45
46 if 'cross_file' in data['properties']:
47 meson_cmd.extend([f'--cross-file={os.path.abspath(f)}' for f in literal_eval(data['properties']['cross_file'])])
48
49 if 'native_file' in data['properties']:
50 meson_cmd.extend([f'--native-file={os.path.abspath(f)}' for f in literal_eval(data['properties']['native_file'])])
51
52 exelist = detect_scanbuild()
53 if not exelist:
54 print('Could not execute scan-build "%s"' % ' '.join(exelist))
55 return 1
56
57 return scanbuild(exelist, srcdir, bldpath, privdir, logdir, subprojdir, meson_cmd)