Skip to content

Commit 4189a3e

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 4189a3e

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
LLVM version can be modified during build with environment variables
2-
"LLVM_VERSION" and "EXTERNALS_LLVM_TAG".
1+
LLVM version can be modified during build with environment variable
2+
"LLVM_VERSION".

Tools/jit/_llvm.py

Lines changed: 31 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,66 @@ 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(
60+
name: str, llvm_version: str, *, echo: bool = False
61+
) -> bool:
6362
output = await _run(name, ["--version"], echo=echo)
64-
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))
6565

6666

6767
@_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)
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)
7070
return output and output.removesuffix("\n")
7171

7272

7373
@_async_cache
74-
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:
7575
# Unversioned executables:
7676
path = tool
77-
if await _check_tool_version(path, echo=echo):
77+
if await _check_tool_version(path, llvm_version, echo=echo):
7878
return path
7979
# Versioned executables:
80-
path = f"{tool}-{_LLVM_VERSION}"
81-
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):
8282
return path
8383
# PCbuild externals:
8484
externals = os.environ.get("EXTERNALS_DIR", _targets.EXTERNALS)
8585
path = os.path.join(externals, _EXTERNALS_LLVM_TAG, "bin", tool)
86-
if await _check_tool_version(path, echo=echo):
86+
if await _check_tool_version(path, llvm_version, echo=echo):
8787
return path
8888
# Homebrew-installed executables:
89-
prefix = await _get_brew_llvm_prefix(echo=echo)
89+
prefix = await _get_brew_llvm_prefix(llvm_version, echo=echo)
9090
if prefix is not None:
9191
path = os.path.join(prefix, "bin", tool)
92-
if await _check_tool_version(path, echo=echo):
92+
if await _check_tool_version(path, llvm_version, echo=echo):
9393
return path
9494
# Nothing found:
9595
return None
9696

9797

9898
async def maybe_run(
99-
tool: str, args: typing.Iterable[str], echo: bool = False
99+
tool: str, args: typing.Iterable[str], echo: bool = False, llvm_version: str = ""
100100
) -> str | None:
101101
"""Run an LLVM tool if it can be found. Otherwise, return None."""
102-
path = await _find_tool(tool, echo=echo)
102+
103+
if not llvm_version:
104+
llvm_version = _LLVM_VERSION
105+
106+
path = await _find_tool(tool, llvm_version, echo=echo)
103107
return path and await _run(path, args, echo=echo)
104108

105109

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

Tools/jit/_targets.py

Lines changed: 13 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,16 @@ 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(
184+
"clang", args_s, echo=self.verbose, llvm_version=self.llvm_version
185+
)
179186
self.optimizer(
180187
s, label_prefix=self.label_prefix, symbol_prefix=self.symbol_prefix
181188
).run()
182189
args_o = [f"--target={self.triple}", "-c", "-o", f"{o}", f"{s}"]
183-
await _llvm.run("clang", args_o, echo=self.verbose)
190+
await _llvm.run(
191+
"clang", args_o, echo=self.verbose, llvm_version=self.llvm_version
192+
)
184193
return await self._parse(o)
185194

186195
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)