https://github.com/oliverlee/bazel_clang_format
Run clang-format on Bazel C++ targets
https://github.com/oliverlee/bazel_clang_format
bazel clang-format cpp
Last synced: 2 months ago
JSON representation
Run clang-format on Bazel C++ targets
- Host: GitHub
- URL: https://github.com/oliverlee/bazel_clang_format
- Owner: oliverlee
- License: apache-2.0
- Created: 2022-12-22T07:40:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-31T21:57:00.000Z (7 months ago)
- Last Synced: 2025-08-31T23:40:45.092Z (7 months ago)
- Topics: bazel, clang-format, cpp
- Language: Starlark
- Homepage:
- Size: 34.2 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# bazel_clang_format
Run `clang-format` on Bazel C++ targets directly. It's like
[bazel_clang_tidy](https://github.com/erenon/bazel_clang_tidy) but for
`clang-format`.
## usage
MODULE
```starlark
# //:.bazelrc
common --registry=https://raw.githubusercontent.com/digiboys/bazel-registry/main
common --registry=https://bcr.bazel.build
```
```starlark
# //:MODULE.bazel
bazel_dep(
name = "bazel_clang_format",
version = "0.0.0",
dev_dependency = True
)
```
WORKSPACE
```starlark
# //:WORKSPACE.bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_repository")
BAZEL_CLANG_FORMAT_COMMIT = ...
http_repository(
name = "bazel_clang_format",
integrity = ...,
strip_prefix = "bazel_clang_format-{commit}".format(
commit = BAZEL_CLANG_FORMAT_COMMIT,
),
url = "https://github.com/oliverlee/bazel_clang_format/archive/{commit}.tar.gz".format(
commit = BAZEL_CLANG_FORMAT_COMMIT,
),
)
```
```starlark
# //:.bazelrc
build:clang-format --aspects=@bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --output_groups=report
build:clang-format-fix --aspects=@bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --output_groups=report
build:clang-format-fix --use_action_cache=false
```
Check formatting with
```sh
bazel build //... --config=clang-format
```
Fix formatting with
```sh
bazel build //... --config=clang-format-fix
```
This will use `clang-format` in your `PATH` and `.clang-format` defined in this
repo.
### using a hermetic toolchain
To specify a specific binary (e.g. `clang-format` is specified by a hermetic
toolchain like [this](https://github.com/grailbio/bazel-toolchain)), update the
build setting in `.bazelrc`.
```Starlark
# //:.bazelrc
build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:binary=@llvm18//:clang-format
build:clang-format --aspects=@bazel_clang_format//:defs.bzl%check_aspect
build:clang-format-fix --aspects=@bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --use_action_cache=false
```
### specifying `.clang-format`
To override the default `.clang-format`, define a `filegroup` containing the
replacement config and update build setting in `.bazelrc`.
```Starlark
# //:BUILD.bazel
load("@bazel_clang_format//:defs.bzl")
filegroup(
name = "clang-format-config",
srcs = [".clang-format"],
visibility = ["//visibility:public"],
)
```
```Starlark
# //:.bazelrc
build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:config=//:clang-format-config
...
```
### ignoring certain targets
Formatting can be skipped for certain targets by specifying a filegroup
```Starlark
# //:BUILD.bazel
filegroup(
name = "clang-format-ignore",
srcs = [
"//third_party/lib1",
"//third_party/lib2",
],
)
```
```Starlark
# //:.bazelrc
build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:ignore=//:clang-format-ignore
...
```
## Requirements
- Bazel 5.x
- ClangFormat 14