|
10 | 10 |
|
11 | 11 | import _targets
|
12 | 12 |
|
13 |
| -_LLVM_VERSION = 19 |
14 |
| -_LLVM_VERSION_PATTERN = re.compile(rf"version\s+{_LLVM_VERSION}\.\d+\.\d+\S*\s+") |
| 13 | + |
| 14 | +_LLVM_VERSION = "19" |
15 | 15 | _EXTERNALS_LLVM_TAG = "llvm-19.1.7.0"
|
16 | 16 |
|
17 | 17 | _P = typing.ParamSpec("_P")
|
@@ -56,53 +56,66 @@ async def _run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str
|
56 | 56 |
|
57 | 57 |
|
58 | 58 | @_async_cache
|
59 |
| -async def _check_tool_version(name: str, *, echo: bool = False) -> bool: |
| 59 | +async def _check_tool_version( |
| 60 | + name: str, llvm_version: str, *, echo: bool = False |
| 61 | +) -> bool: |
60 | 62 | output = await _run(name, ["--version"], echo=echo)
|
61 |
| - return bool(output and _LLVM_VERSION_PATTERN.search(output)) |
| 63 | + _llvm_version_pattern = re.compile(rf"version\s+{llvm_version}\.\d+\.\d+\S*\s+") |
| 64 | + return bool(output and _llvm_version_pattern.search(output)) |
62 | 65 |
|
63 | 66 |
|
64 | 67 | @_async_cache
|
65 |
| -async def _get_brew_llvm_prefix(*, echo: bool = False) -> str | None: |
66 |
| - output = await _run("brew", ["--prefix", f"llvm@{_LLVM_VERSION}"], echo=echo) |
| 68 | +async def _get_brew_llvm_prefix(llvm_version: str, *, echo: bool = False) -> str | None: |
| 69 | + output = await _run("brew", ["--prefix", f"llvm@{llvm_version}"], echo=echo) |
67 | 70 | return output and output.removesuffix("\n")
|
68 | 71 |
|
69 | 72 |
|
70 | 73 | @_async_cache
|
71 |
| -async def _find_tool(tool: str, *, echo: bool = False) -> str | None: |
| 74 | +async def _find_tool(tool: str, llvm_version: str, *, echo: bool = False) -> str | None: |
72 | 75 | # Unversioned executables:
|
73 | 76 | path = tool
|
74 |
| - if await _check_tool_version(path, echo=echo): |
| 77 | + if await _check_tool_version(path, llvm_version, echo=echo): |
75 | 78 | return path
|
76 | 79 | # Versioned executables:
|
77 |
| - path = f"{tool}-{_LLVM_VERSION}" |
78 |
| - if await _check_tool_version(path, echo=echo): |
| 80 | + path = f"{tool}-{llvm_version}" |
| 81 | + if await _check_tool_version(path, llvm_version, echo=echo): |
79 | 82 | return path
|
80 | 83 | # PCbuild externals:
|
81 | 84 | externals = os.environ.get("EXTERNALS_DIR", _targets.EXTERNALS)
|
82 | 85 | path = os.path.join(externals, _EXTERNALS_LLVM_TAG, "bin", tool)
|
83 |
| - if await _check_tool_version(path, echo=echo): |
| 86 | + if await _check_tool_version(path, llvm_version, echo=echo): |
84 | 87 | return path
|
85 | 88 | # Homebrew-installed executables:
|
86 |
| - prefix = await _get_brew_llvm_prefix(echo=echo) |
| 89 | + prefix = await _get_brew_llvm_prefix(llvm_version, echo=echo) |
87 | 90 | if prefix is not None:
|
88 | 91 | path = os.path.join(prefix, "bin", tool)
|
89 |
| - if await _check_tool_version(path, echo=echo): |
| 92 | + if await _check_tool_version(path, llvm_version, echo=echo): |
90 | 93 | return path
|
91 | 94 | # Nothing found:
|
92 | 95 | return None
|
93 | 96 |
|
94 | 97 |
|
95 | 98 | async def maybe_run(
|
96 |
| - tool: str, args: typing.Iterable[str], echo: bool = False |
| 99 | + tool: str, |
| 100 | + args: typing.Iterable[str], |
| 101 | + echo: bool = False, |
| 102 | + llvm_version: str = _LLVM_VERSION, |
97 | 103 | ) -> str | None:
|
98 | 104 | """Run an LLVM tool if it can be found. Otherwise, return None."""
|
99 |
| - path = await _find_tool(tool, echo=echo) |
| 105 | + |
| 106 | + path = await _find_tool(tool, llvm_version, echo=echo) |
100 | 107 | return path and await _run(path, args, echo=echo)
|
101 | 108 |
|
102 | 109 |
|
103 |
| -async def run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str: |
| 110 | +async def run( |
| 111 | + tool: str, |
| 112 | + args: typing.Iterable[str], |
| 113 | + echo: bool = False, |
| 114 | + llvm_version: str = _LLVM_VERSION, |
| 115 | +) -> str: |
104 | 116 | """Run an LLVM tool if it can be found. Otherwise, raise RuntimeError."""
|
105 |
| - output = await maybe_run(tool, args, echo=echo) |
| 117 | + |
| 118 | + output = await maybe_run(tool, args, echo=echo, llvm_version=llvm_version) |
106 | 119 | if output is None:
|
107 |
| - raise RuntimeError(f"Can't find {tool}-{_LLVM_VERSION}!") |
| 120 | + raise RuntimeError(f"Can't find {tool}-{llvm_version}!") |
108 | 121 | return output
|
0 commit comments