Skip to content

Commit 0f00407

Browse files
committed
fix(scripts): pip-install clang-format if missing at codegen time
CI containers running with `--no-build-isolation` (metax, moore, cambricon) skip `[build-system].requires` and never install `clang-format` from PyPI; system packages do not provide it either, so cmake-time codegen fails with `FileNotFoundError`. Probe `PATH` for `clang-format` at codegen entry; if missing, `pip install clang-format` into the running interpreter and reuse the installed binary. Adds at most a couple of seconds to a first-time configure on hosts without the binary.
1 parent dc2991c commit 0f00407

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

scripts/generate_torch_ops.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,14 +824,42 @@ class Operator<{pascal}, kDev, {slot}> : public {pascal} {{
824824
"""
825825

826826

827+
def _ensure_clang_format() -> str:
828+
"""Return the path to a `clang-format` binary, installing the
829+
`clang-format` PyPI wheel into the running interpreter if the system
830+
does not provide one (CI containers running with
831+
`--no-build-isolation` skip `[build-system].requires`)."""
832+
833+
found = shutil.which("clang-format")
834+
835+
if found:
836+
return found
837+
838+
print(
839+
"`clang-format` not found on PATH; installing `clang-format` from PyPI...",
840+
file=sys.stderr,
841+
)
842+
subprocess.run(
843+
[sys.executable, "-m", "pip", "install", "--quiet", "clang-format"],
844+
check=True,
845+
)
846+
847+
found = shutil.which("clang-format")
848+
849+
if not found:
850+
raise RuntimeError("`clang-format` still not available after `pip install`.")
851+
852+
return found
853+
854+
827855
def _clang_format(text: str, path: pathlib.Path) -> str:
828856
"""Pipe `text` through `clang-format` so generated headers / sources
829857
satisfy the same style check (`clang-format` v21) that CI runs.
830858
`path` informs include sorting (the file's own header should come
831859
first in a `.cc`)."""
832860

833861
return subprocess.run(
834-
["clang-format", f"--assume-filename={path}"],
862+
[_CLANG_FORMAT, f"--assume-filename={path}"],
835863
input=text,
836864
capture_output=True,
837865
text=True,
@@ -868,6 +896,9 @@ def main() -> int:
868896
)
869897
args = parser.parse_args()
870898

899+
global _CLANG_FORMAT
900+
_CLANG_FORMAT = _ensure_clang_format()
901+
871902
op_names = args.ops or yaml.safe_load(_OPS_YAML_PATH.read_text())
872903
aten_entries = yaml.safe_load(_load_aten_yaml())
873904

0 commit comments

Comments
 (0)