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