1# Copyright 2025 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""A CLI utility that checks and fixes formatting for source files.""" 15 16import sys 17 18from pw_build.runfiles_manager import RunfilesManager 19from pw_presubmit.format.bazel import BuildifierFormatter 20from pw_presubmit.format.cpp import ClangFormatFormatter 21from pw_presubmit.format.private.cli import FormattingSuite 22from pw_presubmit.format.python import BlackFormatter 23 24 25def _pigweed_formatting_suite() -> FormattingSuite: 26 runfiles = RunfilesManager() 27 # GN 28 runfiles.add_bootstrapped_tool( 29 'clang-format', 'clang-format', from_shell_path=True 30 ) 31 runfiles.add_bootstrapped_tool('black', 'black', from_shell_path=True) 32 runfiles.add_bootstrapped_tool( 33 'buildifier', 'buildifier', from_shell_path=True 34 ) 35 36 # Bazel 37 runfiles.add_bazel_tool('clang-format', 'llvm_toolchain.clang_format') 38 runfiles.add_bazel_tool('black', 'pw_presubmit.py.black_runfiles') 39 runfiles.add_bazel_tool('buildifier', 'pw_presubmit.py.buildifier_runfiles') 40 41 # This list can be broken out and library-ified as the default set of 42 # formatters once config file loading is smarter (i.e. loads from the 43 # path of the file that is being formatted rather than as a runfile 44 # dependency). 45 pigweed_formatters = [ 46 BlackFormatter( 47 tool_runner=runfiles, 48 ), 49 BuildifierFormatter( 50 tool_runner=runfiles, 51 ), 52 ClangFormatFormatter( 53 tool_runner=runfiles, 54 ), 55 ] 56 return FormattingSuite(pigweed_formatters) 57 58 59if __name__ == '__main__': 60 sys.exit(_pigweed_formatting_suite().main()) 61