Commit | Line | Data |
---|---|---|
b61d5465 PP |
1 | #!/bin/bash |
2 | # | |
3 | # SPDX-License-Identifier: GPL-2.0-only | |
4 | # | |
3e674024 | 5 | # Copyright (C) 2020-2023 Philippe Proulx <pproulx@efficios.com> |
9021ae77 | 6 | |
1d323849 | 7 | expected_formatter_major_version=15 |
9021ae77 | 8 | |
3e674024 PP |
9 | # Runs the formatter, returning 1 if it's not the expected version. |
10 | # | |
11 | # Argument 1: | |
12 | # Starting directory. | |
13 | # | |
14 | # Remaining arguments: | |
15 | # Formatter command. | |
9021ae77 | 16 | format_cpp() { |
3e674024 PP |
17 | local root_dir=$1 |
18 | ||
19 | shift | |
20 | ||
b11f8a1d | 21 | local formatter=("$@") |
9021ae77 PP |
22 | local version |
23 | ||
b11f8a1d PP |
24 | if ! version=$("${formatter[@]}" --version); then |
25 | echo "Cannot execute \`${formatter[*]} --version\`." >&2 | |
9021ae77 PP |
26 | return 1 |
27 | fi | |
28 | ||
29 | if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then | |
ed6ecded PP |
30 | { |
31 | echo "Expecting clang-format $expected_formatter_major_version." | |
32 | echo | |
33 | echo Got: | |
34 | echo | |
35 | echo "$version" | |
36 | } >& 2 | |
37 | ||
9021ae77 PP |
38 | return 1 |
39 | fi | |
40 | ||
3e674024 | 41 | # Using xargs(1) to fail as soon as the formatter fails (`-exec` |
9021ae77 | 42 | # won't stop if its subprocess fails). |
3e674024 PP |
43 | # |
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/optional.hpp' \ | |
48 | ! -path '*/src/cpp-common/string_view.hpp' \ | |
49 | ! -path '*/src/cpp-common/nlohmann/json.hpp' \ | |
6bb5283a PP |
50 | ! -path '*/src/plugins/ctf/common/metadata/parser.*' \ |
51 | ! -path '*/src/plugins/ctf/common/metadata/lexer.*' \ | |
0ea5233c | 52 | -print0 | xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}" |
9021ae77 PP |
53 | } |
54 | ||
3e674024 | 55 | # Choose formatter |
9021ae77 PP |
56 | if [[ -n "$FORMATTER" ]]; then |
57 | # Try using environment-provided formatter | |
b11f8a1d | 58 | read -ra formatter <<< "$FORMATTER" |
9021ae77 PP |
59 | elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then |
60 | # Try using the expected version of clang-format | |
b11f8a1d | 61 | formatter=("clang-format-$expected_formatter_major_version" -i) |
9021ae77 PP |
62 | else |
63 | # Try using `clang-format` as is | |
b11f8a1d | 64 | formatter=(clang-format -i) |
9021ae77 PP |
65 | fi |
66 | ||
3e674024 PP |
67 | # Choose root directory |
68 | if (($# == 1)); then | |
69 | root_dir=$1 | |
70 | ||
71 | if [[ ! -d "$root_dir" ]]; then | |
72 | echo "\`$root_dir\`: expecting an existing directory." >& 2 | |
73 | exit 1 | |
74 | fi | |
75 | else | |
76 | # Default: root of the project, processing all C++ files | |
77 | root_dir="$(dirname "${BASH_SOURCE[0]}")/.." | |
78 | fi | |
79 | ||
9021ae77 | 80 | # Try to format files |
3e674024 | 81 | format_cpp "$root_dir" "${formatter[@]}" |