Skip to content

Commit c739b19

Browse files
committed
fix(scripts): make clang-format optional at codegen time
Some CI containers (metax, cambricon) run offline and cannot reach PyPI; `pip install clang-format` fails with name-resolution errors and the codegen aborts before any output is written. Generated files live under `generated/` (gitignored), so they do not need to satisfy the repo-level `clang-format` check — they only need to compile. Fall through to writing unformatted output when no `clang-format` binary is reachable. When a binary is available (local dev, online CI), formatting still happens and the output that gets pushed to `src/base/<op>.h` for hand-written-base PRs stays clang-format-clean.
1 parent 0f00407 commit c739b19

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

scripts/generate_torch_ops.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -824,39 +824,51 @@ 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`)."""
827+
def _find_clang_format() -> str | None:
828+
"""Return the path to a `clang-format` binary, or `None` if none is
829+
available. When the system does not provide one, try installing the
830+
`clang-format` PyPI wheel; offline CI containers (no PyPI mirror) end
831+
up returning `None` and the codegen falls through to writing
832+
unformatted output — generated files live under `generated/` (which
833+
is gitignored) so they do not need to satisfy the repo-level
834+
clang-format check, only compile cleanly."""
832835

833836
found = shutil.which("clang-format")
834837

835838
if found:
836839
return found
837840

838841
print(
839-
"`clang-format` not found on PATH; installing `clang-format` from PyPI...",
842+
"`clang-format` not found on PATH; trying `pip install clang-format`...",
840843
file=sys.stderr,
841844
)
842-
subprocess.run(
843-
[sys.executable, "-m", "pip", "install", "--quiet", "clang-format"],
844-
check=True,
845-
)
846845

847-
found = shutil.which("clang-format")
846+
try:
847+
subprocess.run(
848+
[sys.executable, "-m", "pip", "install", "--quiet", "clang-format"],
849+
check=True,
850+
)
851+
except subprocess.CalledProcessError:
852+
print(
853+
"`pip install clang-format` failed (likely offline CI); generated "
854+
"files will be emitted without formatting.",
855+
file=sys.stderr,
856+
)
848857

849-
if not found:
850-
raise RuntimeError("`clang-format` still not available after `pip install`.")
858+
return None
851859

852-
return found
860+
return shutil.which("clang-format")
853861

854862

855863
def _clang_format(text: str, path: pathlib.Path) -> str:
856864
"""Pipe `text` through `clang-format` so generated headers / sources
857865
satisfy the same style check (`clang-format` v21) that CI runs.
858866
`path` informs include sorting (the file's own header should come
859-
first in a `.cc`)."""
867+
first in a `.cc`). If no `clang-format` binary is available, return
868+
the input unchanged."""
869+
870+
if _CLANG_FORMAT is None:
871+
return text
860872

861873
return subprocess.run(
862874
[_CLANG_FORMAT, f"--assume-filename={path}"],
@@ -897,7 +909,7 @@ def main() -> int:
897909
args = parser.parse_args()
898910

899911
global _CLANG_FORMAT
900-
_CLANG_FORMAT = _ensure_clang_format()
912+
_CLANG_FORMAT = _find_clang_format()
901913

902914
op_names = args.ops or yaml.safe_load(_OPS_YAML_PATH.read_text())
903915
aten_entries = yaml.safe_load(_load_aten_yaml())

0 commit comments

Comments
 (0)