| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2024 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
from collections import defaultdict |
| 6 |
import os |
| 7 |
import tempfile |
| 8 |
import typing as T |
| 9 |
import subprocess |
| 10 |
|
| 11 |
from .run_tool import run_tool_on_targets, run_with_buffered_output |
| 12 |
from .. import build, mlog |
| 13 |
from ..mesonlib import MachineChoice, PerMachine |
| 14 |
from ..tooldetect import detect_ninja |
| 15 |
|
| 16 |
if T.TYPE_CHECKING: |
| 17 |
from ..compilers.rust import RustCompiler |
| 18 |
|
| 19 |
class ClippyDriver: |
| 20 |
def __init__(self, build: build.Build, tempdir: str, args: list[str]): |
| 21 |
self.tools: PerMachine[T.List[str]] = PerMachine([], []) |
| 22 |
self.warned: T.DefaultDict[str, bool] = defaultdict(lambda: False) |
| 23 |
self.tempdir = tempdir |
| 24 |
for machine in MachineChoice: |
| 25 |
compilers = build.environment.coredata.compilers[machine] |
| 26 |
if 'rust' in compilers: |
| 27 |
compiler = T.cast('RustCompiler', compilers['rust']) |
| 28 |
self.tools[machine] = compiler.get_rust_tool('clippy-driver') |
| 29 |
self.args = args |
| 30 |
|
| 31 |
def warn_missing_clippy(self, machine: str) -> None: |
| 32 |
if self.warned[machine]: |
| 33 |
return |
| 34 |
mlog.warning(f'clippy-driver not found for {machine} machine') |
| 35 |
self.warned[machine] = True |
| 36 |
|
| 37 |
def __call__(self, target: T.Dict[str, T.Any]) -> T.Iterable[T.Coroutine[None, None, int]]: |
| 38 |
for src_block in target['target_sources']: |
| 39 |
if 'compiler' in src_block and src_block['language'] == 'rust': |
| 40 |
clippy = getattr(self.tools, src_block['machine']) |
| 41 |
if not clippy: |
| 42 |
self.warn_missing_clippy(src_block['machine']) |
| 43 |
continue |
| 44 |
|
| 45 |
cmdlist = list(clippy) |
| 46 |
prev = None |
| 47 |
lints_cap = None |
| 48 |
for arg in src_block['parameters']: |
| 49 |
if prev == '--cap-lints': |
| 50 |
cmdlist.append(prev) |
| 51 |
lints_cap = arg |
| 52 |
prev = None |
| 53 |
elif prev: |
| 54 |
prev = None |
| 55 |
continue |
| 56 |
if arg in {'--emit', '--out-dir', '--cap-lints'}: |
| 57 |
prev = arg |
| 58 |
else: |
| 59 |
cmdlist.append(arg) |
| 60 |
|
| 61 |
# no use in running clippy if it wouldn't print anything anyway |
| 62 |
if lints_cap == 'allow': |
| 63 |
break |
| 64 |
|
| 65 |
cmdlist.extend(src_block['sources']) |
| 66 |
# the default for --emit is to go all the way to linking, |
| 67 |
# and --emit dep-info= is not enough for clippy to do |
| 68 |
# enough analysis, so use --emit metadata. |
| 69 |
cmdlist.append('--emit') |
| 70 |
cmdlist.append('metadata') |
| 71 |
cmdlist.append('--out-dir') |
| 72 |
cmdlist.append(self.tempdir) |
| 73 |
cmdlist += self.args |
| 74 |
yield run_with_buffered_output(cmdlist) |
| 75 |
|
| 76 |
def run(args: T.List[str]) -> int: |
| 77 |
os.chdir(args[0]) |
| 78 |
build_data = build.load(os.getcwd()) |
| 79 |
|
| 80 |
# Build as much of the project as possible, or else |
| 81 |
# we get errors about missing libraries in the build directory and |
| 82 |
# other related errors. |
| 83 |
subprocess.run(detect_ninja() + ['clippy-json-prereq', '-k0']) |
| 84 |
|
| 85 |
with tempfile.TemporaryDirectory() as d: |
| 86 |
return run_tool_on_targets(ClippyDriver(build_data, d, args[1:])) |