xref: /openbmc/libpldm/scripts/apply-renames (revision 28b649bc)
1#!/usr/bin/bash
2
3set -eou pipefail
4
5# Path to the meson build directory
6: "${BUILD:=build}"
7
8ABSPATH="$(command -v "$0" | xargs realpath)"
9
10# Deal with relative paths
11: "${LIBPLDM_ROOT:="${ABSPATH%scripts/apply-renames}"}"
12
13# Deal with clang including its version in all the tool names
14: "${CLANG_VERSION:=18}"
15: "${CLANG_RENAME:="$(command -v clang-rename-"${CLANG_VERSION}")"}"
16: "${CLANG_APPLY_REPLACEMENTS:="$(command -v clang-apply-replacements-"${CLANG_VERSION}")"}"
17
18# Make it parallel
19: "${JOBS:="$(nproc)"}"
20
21# Allow clang-rename to pick up the per-file flags from the compile command
22# database
23: "${CRFLAGS:="-p=."}"
24
25CRINPUT=$(realpath --relative-to "$BUILD" "$1")
26
27# Deal with clang-rename segfaulting when file paths from the compile command
28# database don't resolve properly
29cd "$BUILD"
30
31# We export the fixes to yaml files so we can apply them all in one hit later
32trap "rm -f fixes.*.yaml" EXIT
33
34# See `man 7 gitglossary` for some explanation of the pathspec provided to `git
35# ls-files`, which, separately, is also subject to bash's brace expansion. See
36# "Brace Expansion" in `man bash`.
37# shellcheck disable=SC2016
38git ls-files -- ':/:*.[ch]'{,pp} |
39xargs -I '{}' -n 1 -P "$JOBS" -- \
40    bash -c '"$0" --force --input="$1" --export-fixes="$(mktemp fixes.XXXXX.yaml)" "$2" "$3"' \
41    "$CLANG_RENAME" "$CRINPUT" "$CRFLAGS" '{}'
42
43# Now apply the generated fixes
44"$CLANG_APPLY_REPLACEMENTS" ..
45
46# Deal with subsequent runs of clang-rename bailing out because it's modified
47# the symbol declaration in the headers, and then is surprised by the lack of a
48# declaration for the as-yet un-renamed symbol in the implementation
49git -C "$LIBPLDM_ROOT" restore -- include
50