3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (C) 2020-2023 Philippe Proulx <pproulx@efficios.com>
7 expected_formatter_major_version
=15
9 # Runs the formatter, returning 1 if it's not the expected version.
14 # Remaining arguments:
21 local formatter
=("$@")
24 if ! version
=$
("${formatter[@]}" --version); then
25 echo "Cannot execute \`${formatter[*]} --version\`." >&2
29 if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
31 echo "Expecting clang-format $expected_formatter_major_version."
41 # Using xargs(1) to fail as soon as the formatter fails (`-exec`
42 # won't stop if its subprocess fails).
44 # We want an absolute starting directory because find(1) excludes
45 # files in specific subdirectories.
46 find "$(realpath "$root_dir")" \
( -name '*.cpp' -o -name '*.hpp' \
) \
47 ! -path '*/src/cpp-common/vendor/*' \
48 ! -path '*/src/cpp-common/nlohmann/json.hpp' \
49 ! -path '*/src/plugins/ctf/common/metadata/parser.*' \
50 ! -path '*/src/plugins/ctf/common/metadata/lexer.*' \
51 -print0 |
xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}"
55 if [[ -n "$FORMATTER" ]]; then
56 # Try using environment-provided formatter
57 read -ra formatter
<<< "$FORMATTER"
58 elif command -v clang-format-
$expected_formatter_major_version &> /dev
/null
; then
59 # Try using the expected version of clang-format
60 formatter
=("clang-format-$expected_formatter_major_version" -i)
62 # Try using `clang-format` as is
63 formatter
=(clang-format
-i)
66 # Choose root directory
70 if [[ ! -d "$root_dir" ]]; then
71 echo "\`$root_dir\`: expecting an existing directory." >& 2
75 # Default: root of the project, processing all C++ files
76 root_dir
="$(dirname "${BASH_SOURCE[0]}")/.."
80 format_cpp
"$root_dir" "${formatter[@]}"