Skip to content

Commit

Permalink
Use bazel to manage clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
npaun committed Sep 25, 2024
1 parent bc61fe6 commit c904c11
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
31 changes: 31 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,34 @@ http_archive(
integrity = "sha256-H2yX4kuLyNdBrkRPhTr61FQqJRyiKeLq4TnMmKE0t2A=",
url = "https://github.com/astral-sh/ruff/releases/download/0.6.7/ruff-x86_64-pc-windows-msvc.zip",
)

# clang-format static binary builds via GH Actions: https://github.com/npaun/bins/blob/master/.github/workflows/llvm.yml
# TODO(soon): Move this workflow to a repo in the cloudflare GH organization

http_file(
name = "clang-format-darwin-arm64",
executable = True,
integrity = "sha256-1hG7AcfgGL+IBrSCEhD9ed6pvIpZMdXMdhCDGkqzhpA=",
url = "https://github.com/npaun/bins/releases/download/llvm-18.1.8/llvm-18.1.8-darwin-arm64-clang-format",
)

http_file(
name = "clang-format-linux-arm64",
executable = True,
integrity = "sha256-iCbaPg60x60eA9ZIWmSdFva/RD9xOBcJLUwSRK8Gxzk=",
url = "https://github.com/npaun/bins/releases/download/llvm-18.1.8/llvm-18.1.8-linux-arm64-clang-format",
)

http_file(
name = "clang-format-linux-amd64",
executable = True,
integrity = "sha256-iCbaPg60x60eA9ZIWmSdFva/RD9xOBcJLUwSRK8Gxzk=",
url = "https://github.com/npaun/bins/releases/download/llvm-18.1.8/llvm-18.1.8-linux-amd64-clang-format",
)

http_file(
name = "clang-format-windows-amd64",
executable = True,
integrity = "sha256-4V2KXVoX5Ny1J7ABfVRx0nAHpAGegykhzac7zW3nK0k=",
url = "https://github.com/npaun/bins/releases/download/llvm-18.1.8/llvm-18.1.8-windows-amd64-clang-format.exe",
)
35 changes: 6 additions & 29 deletions tools/cross/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import platform
import re
import subprocess
import sys
from argparse import ArgumentParser, Namespace
Expand All @@ -13,7 +12,6 @@
from sys import exit
from typing import Callable, Optional

CLANG_FORMAT = os.environ.get("CLANG_FORMAT", "clang-format")
PRETTIER = os.environ.get(
"PRETTIER", "bazel-bin/node_modules/prettier/bin/prettier.cjs"
)
Expand Down Expand Up @@ -68,25 +66,6 @@ def parse_args() -> Namespace:
return options


def check_clang_format() -> None:
try:
# Run clang-format with --version to check its version
output = subprocess.check_output([CLANG_FORMAT, "--version"], encoding="utf-8")
match = re.search(r"version\s*(\d+)\.(\d+)\.(\d+)", output)
if not match:
logging.error("unable to read clang version")
exit(1)

major, minor, patch = match.groups()
if int(major) != 18 or int(minor) != 1 or int(patch) != 8:
logging.error("clang-format version must be 18.1.8")
exit(1)
except FileNotFoundError:
# Clang-format is not in the PATH
logging.exception("clang-format not found in the PATH")
exit(1)


def filter_files_by_globs(
files: list[Path], dir_path: Path, globs: tuple[str, ...]
) -> list[Path]:
Expand Down Expand Up @@ -150,13 +129,11 @@ def run_bazel_tool(


def clang_format(files: list[Path], check: bool = False) -> bool:
check_clang_format()
cmd = [CLANG_FORMAT]
if check:
cmd += ["--dry-run", "--Werror"]
cmd = ["--dry-run", "--Werror"]
else:
cmd.append("-i")
result = subprocess.run(cmd + files)
cmd = ["-i"]
result = run_bazel_tool("clang-format", cmd + files)
return result.returncode == 0


Expand Down Expand Up @@ -230,9 +207,9 @@ class FormatConfig:


FORMATTERS = [
# FormatConfig(
# directory="src/workerd", globs=("*.c++", "*.h"), formatter=clang_format
# ),
FormatConfig(
directory="src/workerd", globs=("*.c++", "*.h"), formatter=clang_format
),
FormatConfig(
directory="src",
globs=("*.js", "*.ts", "*.cjs", "*.ejs", "*.mjs"),
Expand Down

0 comments on commit c904c11

Please sign in to comment.