upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/mc_command.py
diff options
context:
space:
mode:
authorbhimio rice <bhimio@bhimio.xyz>2026-11-23 20:35:58 -0500
committerbhimio rice <bhimio@bhimio.xyz>2026-11-23 20:51:04 -0500
commitfca3be703c20767ea3e9f837eaab17ef6199f8e0 (patch)
tree187fd888cfbd83479cf112bb8754c9f156b422db /tools/mc_command.py
parent9ec34cdff41b19d7671bc260e5f40dc69790d905 (diff)
fix(linux): find a versioned llvm-mc and give Linux install hints
Debian and Ubuntu's llvm-<N> packages install llvm-mc only as llvm-mc-<N> and /usr/lib/llvm-<N>/bin/llvm-mc. With LLVM_MC unset, the build looked for the plain name alone and stopped with Windows advice (scripts\build_llvm_mc.ps1, WSL), while tests/conftest.py already found the versioned install, so the pytest suites passed where make_game.py failed. mc_command() now keeps the plain llvm-mc when it is on PATH (the name a work folder's disassembly record holds) and otherwise takes the newest versioned install, by version number. install_hint() gives each OS its own advice; the preflight, shader_disasm.py and not_found() use it, and the SPIRV-Tools hint names the distribution package off Windows and macOS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/mc_command.py')
-rw-r--r--tools/mc_command.py64
1 files changed, 58 insertions, 6 deletions
diff --git a/tools/mc_command.py b/tools/mc_command.py
index 3e470d319..762008d7a 100644
--- a/tools/mc_command.py
+++ b/tools/mc_command.py
@@ -9,14 +9,21 @@ target:
9On Windows, arguments follow the MSVC C runtime's quoting rules. Double a trailing backslash in a quoted 9On Windows, arguments follow the MSVC C runtime's quoting rules. Double a trailing backslash in a quoted
10argument before the closing quote. 10argument before the closing quote.
11 11
12With LLVM_MC unset, the plain name llvm-mc is used when it is on PATH. Debian and Ubuntu install `llvm-<N>` under
13versioned names only (/usr/bin/llvm-mc-<N> and /usr/lib/llvm-<N>/bin/llvm-mc; the plain name comes with the
14unversioned `llvm` package), so off Windows the newest of those is used when the plain name is missing.
15
12run() starts such a command and turns a missing llvm-mc into one line instead of a FileNotFoundError traceback. 16run() starts such a command and turns a missing llvm-mc into one line instead of a FileNotFoundError traceback.
13""" 17"""
14from __future__ import annotations 18from __future__ import annotations
15 19
20import glob
16import os 21import os
22import re
17import shlex 23import shlex
18import shutil 24import shutil
19import subprocess 25import subprocess
26import sys
20 27
21 28
22def split_windows_command(raw: str) -> list[str]: 29def split_windows_command(raw: str) -> list[str]:
@@ -76,20 +83,65 @@ def split_command(raw: str) -> list[str]:
76 raise SystemExit(f"LLVM_MC: invalid command line {raw!r}: {e}") from None 83 raise SystemExit(f"LLVM_MC: invalid command line {raw!r}: {e}") from None
77 84
78 85
86DEBIAN_LLVM_MC = "/usr/lib/llvm-*/bin/llvm-mc" # Debian and Ubuntu: one folder per llvm-<N> package
87
88
89def versioned_llvm_mc() -> str | None:
90 """Off Windows: the newest llvm-mc installed under a versioned name, llvm-mc-<N> on PATH or
91 /usr/lib/llvm-<N>/bin/llvm-mc, or None. Only names are looked at; nothing is started, because
92 shader_disasm.py resolves the command at import time. Empty PATH entries (the current folder) are skipped."""
93 if os.name == "nt":
94 return None
95 found = {}
96 for folder in os.environ.get("PATH", "").split(os.pathsep):
97 if not folder:
98 continue
99 for path in glob.glob(os.path.join(glob.escape(folder), "llvm-mc-[0-9]*")):
100 m = re.fullmatch(r"llvm-mc-(\d+)", os.path.basename(path))
101 if m and os.path.isfile(path) and os.access(path, os.X_OK):
102 found.setdefault(int(m.group(1)), path)
103 for path in glob.glob(DEBIAN_LLVM_MC):
104 m = re.fullmatch(r"llvm-(\d+)", os.path.basename(os.path.dirname(os.path.dirname(path))))
105 if m and os.path.isfile(path) and os.access(path, os.X_OK):
106 found.setdefault(int(m.group(1)), path)
107 return found[max(found)] if found else None
108
109
110def default_command() -> str:
111 """llvm-mc when the plain name is on PATH (the name a work folder's disassembly record already holds), else
112 versioned_llvm_mc(), else the plain name, which then fails with not_found()'s message."""
113 if shutil.which("llvm-mc"):
114 return "llvm-mc"
115 return versioned_llvm_mc() or "llvm-mc"
116
117
79def mc_command(raw: str | None = None) -> list: 118def mc_command(raw: str | None = None) -> list:
80 """LLVM_MC is a path, or a command line: 'wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc'.""" 119 """LLVM_MC is a path, or a command line: 'wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc'. Unset or empty,
120 default_command()."""
81 if raw is None: 121 if raw is None:
82 raw = os.environ.get("LLVM_MC", "llvm-mc") 122 raw = os.environ.get("LLVM_MC", "")
83 return split_command(raw) or ["llvm-mc"] 123 return split_command(raw) or [default_command()]
124
125
126def install_hint() -> str:
127 """Where to get an llvm-mc with the AMDGPU target on this OS, for the messages that report a missing one."""
128 if os.name == "nt":
129 return ("The official Windows LLVM build has no AMDGPU target: build a native one with "
130 "scripts\\build_llvm_mc.ps1 (it sets LLVM_MC), or install llvm-21 in WSL and set LLVM_MC to the "
131 "whole command, e.g.\n LLVM_MC=wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc")
132 if sys.platform == "darwin":
133 return ("Homebrew's llvm has the AMDGPU target but is keg-only (not on PATH): brew install llvm, then\n"
134 " LLVM_MC=\"$(brew --prefix llvm)/bin/llvm-mc\"")
135 return ("Distribution LLVM packages include the AMDGPU target: on Debian or Ubuntu, sudo apt install llvm-21\n"
136 "(found as /usr/lib/llvm-21/bin/llvm-mc without further setup); elsewhere install your distribution's\n"
137 "LLVM 21+ package and put llvm-mc on PATH, or set LLVM_MC=/path/to/llvm-mc")
84 138
85 139
86def not_found(exe: str, error: OSError, flag: str | None = None) -> str: 140def not_found(exe: str, error: OSError, flag: str | None = None) -> str:
87 """The message for an llvm-mc that can't be started (shader_disasm.py's wording). `flag` is the tool's own 141 """The message for an llvm-mc that can't be started (shader_disasm.py's wording). `flag` is the tool's own
88 option for the command, if it has one (run_gpu_tests.py's --llvm-mc); the others read LLVM_MC only.""" 142 option for the command, if it has one (run_gpu_tests.py's --llvm-mc); the others read LLVM_MC only."""
89 where = f"set LLVM_MC (or pass {flag})" if flag else "set LLVM_MC" 143 where = f"set LLVM_MC (or pass {flag})" if flag else "set LLVM_MC"
90 return (f"'{exe}' could not be started: {error}. Put llvm-mc on PATH or {where} to it\n" 144 return f"'{exe}' could not be started: {error}. Put llvm-mc on PATH or {where} to it.\n{install_hint()}"
91 f"(on Windows, scripts\\build_llvm_mc.ps1 builds one and sets it), e.g.\n"
92 f" LLVM_MC=wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc")
93 145
94 146
95def run(cmd: list, flag: str | None = None, **kwargs) -> subprocess.CompletedProcess: 147def run(cmd: list, flag: str | None = None, **kwargs) -> subprocess.CompletedProcess: