Skip to content

Commit

Permalink
pw_presubmit: Add cpp formatting support for Bazel
Browse files Browse the repository at this point in the history
Adds CPP formatting support for the Bazel-focused pigweed_format.py.

Bug: b/326309165
Change-Id: I68b4642332144f3712c82304aecfa3914dde5f85
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/266992
Docs-Not-Needed: Armando Montanez <amontanez@google.com>
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Pigweed-Auto-Submit: Armando Montanez <amontanez@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Feb 10, 2025
1 parent 18479f6 commit 08bb4c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pw_presubmit/py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations under
# the License.

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
load("//pw_build:python.bzl", "pw_py_binary", "pw_py_test")
Expand Down Expand Up @@ -109,12 +110,24 @@ py_console_script_binary(
visibility = ["//visibility:private"],
)

copy_file(
name = "clang-format",
src = select({
"@platforms//os:macos": "@llvm_toolchain_macos//:bin/clang-format",
"//conditions:default": "@llvm_toolchain//:bin/clang-format",
}),
out = "clang-format",
allow_symlink = True,
is_executable = True,
)

pw_py_binary(
name = "format",
srcs = ["pw_presubmit/format/pigweed_format.py"],
data = [
"//:.black.toml",
"//pw_presubmit/py:black",
"//pw_presubmit/py:clang-format",
],
main = "pw_presubmit/format/pigweed_format.py",
deps = [
Expand Down
9 changes: 9 additions & 0 deletions pw_presubmit/py/pw_presubmit/format/pigweed_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pw_cli.tool_runner import ToolRunner, BasicSubprocessRunner
from pw_presubmit.format.private.cli import FormattingSuite
from pw_presubmit.format.python import BlackFormatter
from pw_presubmit.format.cpp import ClangFormatFormatter

try:
from python.runfiles import runfiles # type: ignore
Expand Down Expand Up @@ -84,6 +85,11 @@ def _pigweed_formatting_suite() -> FormattingSuite:
default_runner.add_tool( # type: ignore[attr-defined]
'black', 'pigweed/pw_presubmit/py/black', r.CurrentRepository()
)
default_runner.add_tool( # type: ignore[attr-defined]
'clang-format',
'pigweed/pw_presubmit/py/clang-format',
r.CurrentRepository(),
)

# This is deliberately not public since there's some work to do to make
# things more extensible/maintainable before downstream projects try to
Expand All @@ -93,6 +99,9 @@ def _pigweed_formatting_suite() -> FormattingSuite:
config_file=tool_runfiles['.black.toml'],
tool_runner=default_runner,
),
ClangFormatFormatter(
tool_runner=default_runner,
),
]
return FormattingSuite(pigweed_formatters)

Expand Down

0 comments on commit 08bb4c4

Please sign in to comment.