xref: /aosp_15_r20/external/libnl/tools/clang-format-container.sh (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1#!/bin/bash
2
3set -e
4
5die() {
6    echo "$@" >&2
7    exit 1
8}
9
10DIR="$(realpath "$(dirname "$0")/../")"
11cd "$DIR"
12
13# The correct clang-format version is the one from the Fedora version used in our
14# github action pipeline. Parse it from ".github/workflows/ci.yml".
15FEDORA_VERSION="$(sed -n 's/^      image: fedora:\([0-9]\+\)$/\1/p' .github/workflows/ci.yml)"
16
17test -n "$FEDORA_VERSION" || die "Could not detect the Fedora version in .github/workflows/ci.yml"
18
19IMAGENAME="libnl-code-format-f$FEDORA_VERSION"
20
21ARGS=( "$@" )
22
23if ! podman image exists "$IMAGENAME" ; then
24    echo "Building image \"$IMAGENAME\"..."
25    podman build \
26        --squash-all \
27        --tag "$IMAGENAME" \
28        -f <(cat <<EOF
29FROM fedora:$FEDORA_VERSION
30RUN dnf upgrade -y
31RUN dnf install -y git /usr/bin/clang-format
32EOF
33)
34fi
35
36CMD=( ./tools/clang-format.sh "${ARGS[@]}" )
37
38podman run \
39    --rm \
40    --name "libnm-code-format-f$FEDORA_VERSION" \
41    -v "$DIR:/tmp/NetworkManager:Z" \
42    -w /tmp/NetworkManager \
43    -e "_LIBNL_CODE_FORMAT_CONTAINER=$IMAGENAME" \
44    -ti \
45    "$IMAGENAME" \
46    "${CMD[@]}"
47