Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bazel to manage clang-format #2791

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to a repository inside Cloudflare organization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon hopefully

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be merged and we can move it to a CF org repo later, it'll involve some tickets and stuff and shouldn't block enabling formatting again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment in here explaining that would be helpful

)

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",
)
4 changes: 4 additions & 0 deletions build/deps/update-deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Usage: update-deps.py [dep_name]
"""

import datetime
import hashlib
import io
Expand Down Expand Up @@ -132,12 +133,15 @@ def macro_name(repo):
class RateLimitedException(Exception):
pass


class AssetsException(Exception):
pass


class UnsupportedException(Exception):
pass


def github_urlopen(url):
"""
A wrapper around urllib.request.urlopen() which parses GitHub rate limit errors and
Expand Down
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