Skip to content

Commit e3a6760

Browse files
committed
Move LLVM_VERSION env var detection to configure
The LLVM_VERSION env variable is passed at configure time to the Tools/jit/build.py script as --llvm-version parameter.
1 parent 6a04388 commit e3a6760

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

Tools/jit/_llvm.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
import _targets
1212

13-
_LLVM_VERSION_DEFAULT = "19"
14-
_EXTERNALS_LLVM_TAG_DEFAULT = "llvm-19.1.7.0"
1513

16-
_LLVM_VERSION = os.getenv("LLVM_VERSION", _LLVM_VERSION_DEFAULT)
17-
_LLVM_VERSION_PATTERN = re.compile(rf"version\s+{_LLVM_VERSION}\.\d+\.\d+\S*\s+")
18-
_EXTERNALS_LLVM_TAG = os.getenv("EXTERNALS_LLVM_TAG", _EXTERNALS_LLVM_TAG_DEFAULT)
14+
_LLVM_VERSION = "19"
15+
_EXTERNALS_LLVM_TAG = "llvm-19.1.7.0"
1916

2017
_P = typing.ParamSpec("_P")
2118
_R = typing.TypeVar("_R")
@@ -59,53 +56,64 @@ async def _run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str
5956

6057

6158
@_async_cache
62-
async def _check_tool_version(name: str, *, echo: bool = False) -> bool:
59+
async def _check_tool_version(name: str, llvm_version: str, *, echo: bool = False) -> bool:
6360
output = await _run(name, ["--version"], echo=echo)
64-
return bool(output and _LLVM_VERSION_PATTERN.search(output))
61+
_llvm_version_pattern = re.compile(rf"version\s+{llvm_version}\.\d+\.\d+\S*\s+")
62+
return bool(output and _llvm_version_pattern.search(output))
6563

6664

6765
@_async_cache
68-
async def _get_brew_llvm_prefix(*, echo: bool = False) -> str | None:
69-
output = await _run("brew", ["--prefix", f"llvm@{_LLVM_VERSION}"], echo=echo)
66+
async def _get_brew_llvm_prefix(llvm_version: str, *, echo: bool = False) -> str | None:
67+
output = await _run("brew", ["--prefix", f"llvm@{llvm_version}"], echo=echo)
7068
return output and output.removesuffix("\n")
7169

7270

7371
@_async_cache
74-
async def _find_tool(tool: str, *, echo: bool = False) -> str | None:
72+
async def _find_tool(tool: str, llvm_version: str, *, echo: bool = False) -> str | None:
7573
# Unversioned executables:
7674
path = tool
77-
if await _check_tool_version(path, echo=echo):
75+
if await _check_tool_version(path, llvm_version, echo=echo):
7876
return path
7977
# Versioned executables:
80-
path = f"{tool}-{_LLVM_VERSION}"
81-
if await _check_tool_version(path, echo=echo):
78+
path = f"{tool}-{llvm_version}"
79+
if await _check_tool_version(path, llvm_version, echo=echo):
8280
return path
8381
# PCbuild externals:
8482
externals = os.environ.get("EXTERNALS_DIR", _targets.EXTERNALS)
8583
path = os.path.join(externals, _EXTERNALS_LLVM_TAG, "bin", tool)
86-
if await _check_tool_version(path, echo=echo):
84+
if await _check_tool_version(path, llvm_version, echo=echo):
8785
return path
8886
# Homebrew-installed executables:
89-
prefix = await _get_brew_llvm_prefix(echo=echo)
87+
prefix = await _get_brew_llvm_prefix(llvm_version, echo=echo)
9088
if prefix is not None:
9189
path = os.path.join(prefix, "bin", tool)
92-
if await _check_tool_version(path, echo=echo):
90+
if await _check_tool_version(path, llvm_version, echo=echo):
9391
return path
9492
# Nothing found:
9593
return None
9694

9795

9896
async def maybe_run(
99-
tool: str, args: typing.Iterable[str], echo: bool = False
97+
tool: str, args: typing.Iterable[str], echo: bool = False, llvm_version: str = ""
10098
) -> str | None:
10199
"""Run an LLVM tool if it can be found. Otherwise, return None."""
102-
path = await _find_tool(tool, echo=echo)
100+
101+
if not llvm_version:
102+
llvm_version = _LLVM_VERSION
103+
104+
path = await _find_tool(tool, llvm_version, echo=echo)
103105
return path and await _run(path, args, echo=echo)
104106

105107

106-
async def run(tool: str, args: typing.Iterable[str], echo: bool = False) -> str:
108+
async def run(
109+
tool: str, args: typing.Iterable[str], echo: bool = False, llvm_version: str = ""
110+
) -> str:
107111
"""Run an LLVM tool if it can be found. Otherwise, raise RuntimeError."""
108-
output = await maybe_run(tool, args, echo=echo)
112+
113+
if not llvm_version:
114+
llvm_version = _LLVM_VERSION
115+
116+
output = await maybe_run(tool, args, echo=echo, llvm_version=llvm_version)
109117
if output is None:
110-
raise RuntimeError(f"Can't find {tool}-{_LLVM_VERSION}!")
118+
raise RuntimeError(f"Can't find {tool}-{llvm_version}!")
111119
return output

Tools/jit/_targets.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class _Target(typing.Generic[_S, _R]):
5050
debug: bool = False
5151
verbose: bool = False
5252
cflags: str = ""
53+
llvm_version: str = ""
5354
known_symbols: dict[str, int] = dataclasses.field(default_factory=dict)
5455
pyconfig_dir: pathlib.Path = pathlib.Path.cwd().resolve()
5556

@@ -81,7 +82,9 @@ def _compute_digest(self) -> str:
8182
async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
8283
group = _stencils.StencilGroup()
8384
args = ["--disassemble", "--reloc", f"{path}"]
84-
output = await _llvm.maybe_run("llvm-objdump", args, echo=self.verbose)
85+
output = await _llvm.maybe_run(
86+
"llvm-objdump", args, echo=self.verbose, llvm_version=self.llvm_version
87+
)
8588
if output is not None:
8689
# Make sure that full paths don't leak out (for reproducibility):
8790
long, short = str(path), str(path.name)
@@ -99,7 +102,9 @@ async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
99102
"--sections",
100103
f"{path}",
101104
]
102-
output = await _llvm.run("llvm-readobj", args, echo=self.verbose)
105+
output = await _llvm.run(
106+
"llvm-readobj", args, echo=self.verbose, llvm_version=self.llvm_version
107+
)
103108
# --elf-output-style=JSON is only *slightly* broken on Mach-O...
104109
output = output.replace("PrivateExtern\n", "\n")
105110
output = output.replace("Extern\n", "\n")
@@ -175,12 +180,12 @@ async def _compile(
175180
# Allow user-provided CFLAGS to override any defaults
176181
*shlex.split(self.cflags),
177182
]
178-
await _llvm.run("clang", args_s, echo=self.verbose)
183+
await _llvm.run("clang", args_s, echo=self.verbose, llvm_version=self.llvm_version)
179184
self.optimizer(
180185
s, label_prefix=self.label_prefix, symbol_prefix=self.symbol_prefix
181186
).run()
182187
args_o = [f"--target={self.triple}", "-c", "-o", f"{o}", f"{s}"]
183-
await _llvm.run("clang", args_o, echo=self.verbose)
188+
await _llvm.run("clang", args_o, echo=self.verbose, llvm_version=self.llvm_version)
184189
return await self._parse(o)
185190

186191
async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:

Tools/jit/build.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242
parser.add_argument(
4343
"--cflags", help="additional flags to pass to the compiler", default=""
4444
)
45+
parser.add_argument("--llvm-version", help="LLVM version to use")
4546
args = parser.parse_args()
4647
for target in args.target:
4748
target.debug = args.debug
4849
target.force = args.force
4950
target.verbose = args.verbose
5051
target.cflags = args.cflags
52+
target.llvm_version = args.llvm_version
5153
target.pyconfig_dir = args.pyconfig_dir
5254
target.build(
5355
comment=comment,

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,7 @@ AS_VAR_IF([jit_flags],
27872787
[],
27882788
[AS_VAR_APPEND([CFLAGS_NODIST], [" $jit_flags"])
27892789
AS_VAR_SET([REGEN_JIT_COMMAND],
2790-
["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py ${ARCH_TRIPLES:-$host} --output-dir . --pyconfig-dir . --cflags=\"$CFLAGS_JIT\""])
2790+
["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py ${ARCH_TRIPLES:-$host} --output-dir . --pyconfig-dir . --cflags=\"$CFLAGS_JIT\" --llvm-version=\"$LLVM_VERSION\""])
27912791
AS_VAR_SET([JIT_STENCILS_H], ["jit_stencils.h"])
27922792
AS_VAR_IF([Py_DEBUG],
27932793
[true],

0 commit comments

Comments
 (0)