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

fix in-place formatting with older clang-format versions #15

Merged
merged 1 commit into from
Jun 28, 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
51 changes: 44 additions & 7 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,61 @@ jobs:
check-format:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- example: default
errors: 3
- example: format-binary
errors: 2
- example: format-config
errors: 0
errors: 1
- example: format-ignore
errors: 0
steps:
- uses: actions/checkout@v4
- run: |
- shell: bash
run: |
set -x

cd example/${{ matrix.example }}

bazel build \
--config=clang-format \
--color=yes \
//... 2>&1 | tee log
if bazel build \
--config=clang-format \
--color=yes \
//... 2>&1 | tee log; then
[[ ${{ matrix.errors }} -eq 0 ]]
else
[[ ${{ matrix.errors }} -ne 0 ]]
fi

[[ ${{ matrix.errors }} -eq $(cat log | grep -c "Wclang-format-violations") ]]

test ${{ matrix.errors }} -eq $(cat log | grep -c "Wclang-format-violations")
fix-format:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- example: default
- example: format-binary
- example: format-config
- example: format-ignore
steps:
- uses: actions/checkout@v4
- shell: bash
run: |
cd example/${{ matrix.example }}

bazel build --config=clang-format-fix

bazel build --config=clang-format

all:
runs-on: ubuntu-latest
if: ${{ github.base_ref == 'main' }}
needs:
- check-format
- fix-format
steps:
- run: true
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"],
)
4 changes: 1 addition & 3 deletions example/format-binary/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

BAZEL_TOOLCHAIN_COMMIT = "66f938c956d247d01997ecfc84725f165ceaf1ed"

BAZEL_TOOLCHAIN_SHA = None

http_archive(
name = "com_grail_bazel_toolchain",
sha256 = BAZEL_TOOLCHAIN_SHA,
integrity = "sha256-Gf4ow1hoJKbokzIAiGXv9PskwCR63o9ryKN+CLsaemw=",
strip_prefix = "bazel-toolchain-{commit}".format(
commit = BAZEL_TOOLCHAIN_COMMIT,
),
Expand Down
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
4 changes: 1 addition & 3 deletions example/format-ignore/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

BAZEL_TOOLCHAIN_COMMIT = "66f938c956d247d01997ecfc84725f165ceaf1ed"

BAZEL_TOOLCHAIN_SHA = None

http_archive(
name = "com_grail_bazel_toolchain",
sha256 = BAZEL_TOOLCHAIN_SHA,
integrity = "sha256-Gf4ow1hoJKbokzIAiGXv9PskwCR63o9ryKN+CLsaemw=",
strip_prefix = "bazel-toolchain-{commit}".format(
commit = BAZEL_TOOLCHAIN_COMMIT,
),
Expand Down