Debian Codegraph @

Tools/jit/_llvm.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """Utilities for invoking LLVM tools."""
2
3 import asyncio
4 import functools
5 import os
6 import re
7 import shlex
8 import subprocess
9 import typing
10
11 import _targets
12
13 _LLVM_VERSION = 19
14 _LLVM_VERSION_PATTERN = re.compile(
15 rf"(?<!Apple )(LLVM|clang) version\s+{_LLVM_VERSION}\.\d+\.\d+\S*\s+"
16 )
17 _EXTERNALS_LLVM_TAG = "llvm-19.1.7.0"
18
19 _P = typing.ParamSpec("_P")
20 _R = typing.TypeVar("_R")
21 _C = typing.Callable[_P, typing.Awaitable[_R]]
22
23
24 def _async_cache(f: _C[_P, _R]) -> _C[_P, _R]:
25 cache = {}
26 lock = asyncio.Lock()
27
28 @functools.wraps(f)
29 async def wrapper(
30 *args: _P.args, **kwargs: _P.kwargs # pylint: disable = no-member
31 ) -> _R:
32 async with lock:
33 if args not in cache:
34 cache[args] = await f(*args, **kwargs)
35 return cache[args]
36
37 return wrapper
38
39
40 _CORES = asyncio.BoundedSemaphore(os.cpu_count() or 1)
41
42
43 async def _run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str | None:
44 command = [tool, *args]
45 async with _CORES:
46 if echo:
47 print(shlex.join(command))
48 try:
49 process = await asyncio.create_subprocess_exec(
50 *command, stdout=subprocess.PIPE
51 )
52 except FileNotFoundError:
53 return None
54 out, _ = await process.communicate()
55 if process.returncode:
56 raise RuntimeError(f"{tool} exited with return code {process.returncode}")
57 return out.decode()
58
59
60 @_async_cache
61 async def _check_tool_version(name: str, *, echo: bool = False) -> bool:
62 output = await _run(name, ["--version"], echo=echo)
63 return bool(output and _LLVM_VERSION_PATTERN.search(output))
64
65
66 @_async_cache
67 async def _get_brew_llvm_prefix(*, echo: bool = False) -> str | None:
68 output = await _run("brew", ["--prefix", f"llvm@{_LLVM_VERSION}"], echo=echo)
69 return output and output.removesuffix("\n")
70
71
72 @_async_cache
73 async def _find_tool(tool: str, *, echo: bool = False) -> str | None:
74 # Unversioned executables:
75 path = tool
76 if await _check_tool_version(path, echo=echo):
77 return path
78 # Versioned executables:
79 path = f"{tool}-{_LLVM_VERSION}"
80 if await _check_tool_version(path, echo=echo):
81 return path
82 # PCbuild externals:
83 externals = os.environ.get("EXTERNALS_DIR", _targets.EXTERNALS)
84 path = os.path.join(externals, _EXTERNALS_LLVM_TAG, "bin", tool)
85 if await _check_tool_version(path, echo=echo):
86 return path
87 # Homebrew-installed executables:
88 prefix = await _get_brew_llvm_prefix(echo=echo)
89 if prefix is not None:
90 path = os.path.join(prefix, "bin", tool)
91 if await _check_tool_version(path, echo=echo):
92 return path
93 # Nothing found:
94 return None
95
96
97 async def maybe_run(
98 tool: str, args: typing.Iterable[str], echo: bool = False
99 ) -> str | None:
100 """Run an LLVM tool if it can be found. Otherwise, return None."""
101 path = await _find_tool(tool, echo=echo)
102 return path and await _run(path, args, echo=echo)
103
104
105 async def run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str:
106 """Run an LLVM tool if it can be found. Otherwise, raise RuntimeError."""
107 output = await maybe_run(tool, args, echo=echo)
108 if output is None:
109 raise RuntimeError(f"Can't find {tool}-{_LLVM_VERSION}!")
110 return output