Skip to content

Commit

Permalink
fix formatting of files in place (#11)
Browse files Browse the repository at this point in the history
This commit renames and redefines the `clang_format_aspect` to take
`clang-format` options and execution requirements. It also defines a fix
aspect which modifies files in place with `clang-format`, removing the
need for an update script and subsequent need to determine the `bazel`
process.

resolves #5

Change-Id: I90d6c95e921d6ab9565bfe5318635901d3de8ad1
  • Loading branch information
oliverlee committed Jun 28, 2024
1 parent 200e71b commit 7e319a3
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 374 deletions.
21 changes: 0 additions & 21 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
load("//:defs.bzl", "bool_flag", "clang_format_update")

package(default_visibility = ["//visibility:public"])

bool_flag(
name = "dry_run",
build_setting_default = True,
)

# Your binary should be a `*_binary`, *NOT* filegroup
filegroup(
name = "default_binary",
Expand Down Expand Up @@ -37,17 +30,3 @@ label_flag(
name = "ignore",
build_setting_default = ":default_ignore",
)

filegroup(
name = "wrapper",
srcs = ["wrapper.sh"],
)

filegroup(
name = "template",
srcs = ["update.template.sh"],
)

clang_format_update(
name = "update",
)
176 changes: 69 additions & 107 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# bazel_clang_format

Run clang-format on Bazel C++ targets directly. It's like
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.
`clang-format`.

## usage

```py
Update your project with

```Starlark
# //:WORKSPACE.bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_repository")

BAZEL_CLANG_FORMAT_COMMIT = ...
BAZEL_CLANG_FORMAT_SHA = ...

http_repository(
name = "bazel_clang_format",
sha256 = BAZEL_CLANG_FORMAT_SHA,
integrity = ...,
strip_prefix = "bazel_clang_format-{commit}".format(
commit = BAZEL_CLANG_FORMAT_COMMIT,
),
Expand All @@ -25,155 +26,116 @@ http_repository(
)
```

You can now compile using the default clang format configuration provided using
the following command:

```
bazel build //... \
--aspects @bazel_clang_format//:defs.bzl%clang_format_aspect \
--output_groups=report
```
```Starlark
# //:.bazelrc

By default, `.clang-format` from this repo is applied. If you wish to override
the config, define a filegroup:
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --output_groups=report

```py
# //:BUILD.bazel
filegroup(
name = "clang_format_config",
srcs = [".clang-format"],
visibility = ["//visibility:public"],
)
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
```

and override the default config file using the config setting:
Check formatting with

```sh
bazel build //... \
--aspects @bazel_clang_format//:defs.bzl%clang_format_aspect \
--@bazel_clang_format//:config=//:clang_format_config \ # <-----------
--output_groups=report
bazel build //... --config=clang-format
```

To specify a specific binary (e.g. `clang-format` is specified by a hermetic
toolchain like [this](https://github.com/grailbio/bazel-toolchain), with the
binary setting:
Fix formatting with

```sh
bazel build //... \
--aspects @bazel_clang_format//:defs.bzl%clang_format_aspect \
--@bazel_clang_format//:binary=@llvm15//:clang-format \ # <-----------
--output_groups=report
bazel build //... --config=clang-format-fix
```

Now if you don't want to type this out every time, it is recommended that you
add a config in your .bazelrc that matches this command line;
This will use `clang-format` in your `PATH` and `.clang-format` defined in this
repo.

Config shorthand:

```
# //.bazelrc
build:clang-format --aspects @bazel_clang_format//:defs.bzl%clang_format_aspect
build:clang-format --output_groups=report
```
or with configuration:
### using a hermetic toolchain

```
# //.bazelrc
build:clang-format --aspects @bazel_clang_format//:defs.bzl%clang_format_aspect
build:clang-format --@bazel_clang_format//:binary=@llvm15//:clang-format
build:clang-format --@bazel_clang_format//:config=//:clang_format_config
build:clang-format --output_groups=report
```
<details><summary></summary>

then run:
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`.

```sh
bazel build //... --config clang-format
```
```Starlark
# //:.bazelrc

To format all source files:
build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:binary=@llvm18//:clang-format

```sh
bazel run @bazel_clang_format//:update \
--@bazel_clang_format//:binary=@llvm15//:clang_format \
--@bazel_clang_format//:config=//:clang_format_config
```
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect

with a specific binary/config if desired.
build:clang-format-fix --aspects @bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --use_action_cache=false
```

Or to format specific targets:
</details>

```sh
bazel run @bazel_clang_format//:update -- //src/...
```
### specifying `.clang-format`

## ignoring formatting of specific targets
<details><summary></summary>

Formatting can be skipped for specific targets by specifying a filegroup
To override the default `.clang-format`, define a `filegroup` containing the
replacement config and update build setting in `.bazelrc`.

```python
```Starlark
# //:BUILD.bazel

load("@bazel_clang_format//:defs.bzl")

filegroup(
name = "clang_format_ignore",
srcs = [
"//third_party/lib1",
"//third_party/lib2",
],
name = "clang-format-config",
srcs = [".clang-format"],
visibility = ["//visibility:public"],
)
```

```sh
# //.bazelrc
...
build:clang-format --@bazel_clang_format//:ignore=//:clang_format_ignore
```Starlark
# //:.bazelrc

build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:config=//:clang-format-config # <-----
...
```

## defaults without .bazelrc
</details>

Both the aspect and update rule can be defined locally to bake in a default
binary or config.
### ignoring certain targets

```python
# //:BUILD.bazel
load("@bazel_clang_format//:defs.bzl", "clang_format_update")
<details><summary></summary>

alias(
name = "default_clang_format_binary",
actual = "@llvm_toolchain//:clang-format",
)
Formatting can be skipped for certain targets by specifying a filegroup

filegroup(
name = "default_clang_format_config",
srcs = [".clang-format"]
visibility = ["//visibility:public"],
)
```Starlark
# //:BUILD.bazel

clang_format_update(
name = "clang_format",
binary = ":default_clang_format_binary",
config = ":default_clang_format_config",
filegroup(
name = "clang-format-ignore",
srcs = [
"//third_party/lib1",
"//third_party/lib2",
],
)
```

```python
# //:aspects.bzl
load("@bazel_clang_format//:defs.bzl", "make_clang_format_aspect")
```Starlark
# //:.bazelrc

clang_format = make_clang_format_aspect(
binary = "//:default_clang_format_binary",
config = "//:default_clang_format_config",
)
build:clang-format-base --output_groups=report
build:clang-format-base --@bazel_clang_format//:ignore=//:clang-format-ignore# <-----
...
```

```sh
bazel run //:clang_format
bazel build //... --aspects //:aspects.bzl%clang_format --output_groups=report
```
</details>

## Requirements

- Bazel ???
- clang-format ???

I'm not sure what the minimum versions are but please let me know if you are
using a version that doesn't work.
Loading

0 comments on commit 7e319a3

Please sign in to comment.