Skip to content

Commit

Permalink
fix in-place formatting with older clang-format versions
Browse files Browse the repository at this point in the history
Change-Id: I0a98b8625c05b962fa1bcc7d8a00f2291ee8efbc
  • Loading branch information
oliverlee committed Jun 28, 2024
1 parent 0930167 commit 55302d6
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- example: format-binary
errors: 2
- example: format-config
errors: 0
errors: 1
- example: format-ignore
errors: 0
steps:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ 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

<details><summary></summary>
Expand Down Expand Up @@ -98,7 +97,7 @@ filegroup(
# //:.bazelrc

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

Expand Down Expand Up @@ -126,7 +125,7 @@ filegroup(
# //:.bazelrc

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

Expand Down
24 changes: 15 additions & 9 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ def _do_format(ctx, f, format_options, execution_reqs):
set -euo pipefail
test -e .clang-format || ln -s -f {config} .clang-format
{binary} {format_options} {infile}
# https://github.com/llvm/llvm-project/issues/46336
# although newer versions of clang-format (e.g. 18.1.4) *do* appear to work
# with symlinks
#
{binary} {format_options} $(readlink --canonicalize {infile})
touch {outfile}
""".format(
config = ctx.file._config.path,
binary = binary.path if binary else "clang-format",
format_options = " ".join(format_options),
infile = f.path,
outfile = out.path,
),
config = ctx.file._config.path,
binary = binary.path if binary else "clang-format",
format_options = " ".join(format_options),
infile = f.path,
outfile = out.path,
),
mnemonic = "ClangFormat",
progress_message = "Formatting {}".format(f.short_path),
execution_requirements = execution_reqs,
Expand All @@ -57,8 +63,8 @@ def _clang_format_aspect_impl(format_options, execution_requirements):
execution_requirements,
)
for f in (
_source_files_in(ctx, "srcs") +
_source_files_in(ctx, "hdrs")
_source_files_in(ctx, "srcs") +
_source_files_in(ctx, "hdrs")
)
]

Expand Down
10 changes: 8 additions & 2 deletions example/default/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --output_groups=report
common --enable_bzlmod=false

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
15 changes: 12 additions & 3 deletions example/format-binary/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --@bazel_clang_format//:binary=@llvm14//:clang-format
build:clang-format --output_groups=report
common --enable_bzlmod=false

build:clang-format-base --@bazel_clang_format//:binary=@llvm14//:clang-format
build:clang-format-base --output_groups=report
build:clang-format-base --keep_going

build:clang-format --config=clang-format-base
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect

build:clang-format-fix --config=clang-format-base
build:clang-format-fix --aspects @bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --use_action_cache=false
5 changes: 0 additions & 5 deletions example/format-binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ cc_binary(
name = "main",
srcs = ["main.cpp"],
)

filegroup(
name = "clang_format_config",
srcs = [".clang-format"],
)
15 changes: 12 additions & 3 deletions example/format-config/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --@bazel_clang_format//:config=//:clang_format_config
build:clang-format --output_groups=report
common --enable_bzlmod=false

build:clang-format-base --@bazel_clang_format//:config=//:clang-format-config
build:clang-format-base --output_groups=report
build:clang-format-base --keep_going

build:clang-format --config=clang-format-base
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect

build:clang-format-fix --config=clang-format-base
build:clang-format-fix --aspects @bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --use_action_cache=false
2 changes: 1 addition & 1 deletion example/format-config/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ DerivePointerAlignment: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'true'
IndentWidth: '4'
IndentWidth: '2'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: None
Expand Down
2 changes: 1 addition & 1 deletion example/format-config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ cc_binary(
)

filegroup(
name = "clang_format_config",
name = "clang-format-config",
srcs = [".clang-format"],
)
17 changes: 13 additions & 4 deletions example/format-ignore/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect
build:clang-format --@bazel_clang_format//:binary=@llvm14//:clang-format
build:clang-format --@bazel_clang_format//:ignore=//:clang_format_ignore
build:clang-format --output_groups=report
common --enable_bzlmod=false

build:clang-format-base --@bazel_clang_format//:binary=@llvm14//:clang-format
build:clang-format-base --@bazel_clang_format//:ignore=//:clang-format-ignore
build:clang-format-base --output_groups=report
build:clang-format-base --keep_going

build:clang-format --config=clang-format-base
build:clang-format --aspects @bazel_clang_format//:defs.bzl%check_aspect

build:clang-format-fix --config=clang-format-base
build:clang-format-fix --aspects @bazel_clang_format//:defs.bzl%fix_aspect
build:clang-format-fix --use_action_cache=false
7 changes: 1 addition & 6 deletions example/format-ignore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ cc_binary(
)

filegroup(
name = "clang_format_config",
srcs = [".clang-format"],
)

filegroup(
name = "clang_format_ignore",
name = "clang-format-ignore",
srcs = [
":main",
],
Expand Down

0 comments on commit 55302d6

Please sign in to comment.