Bump to ccache 4.4
[platform/upstream/ccache.git] / misc / clang-format
1 #!/bin/sh
2 #
3 # This script executes clang-format in the following order:
4 #
5 # 1. If environment variable CLANG_FORMAT is set, execute $CLANG_FORMAT.
6 # 2. Otherwise, if <ccache-top-dir>/misc/.clang-format-exe exists, execute that
7 #    program.
8 # 3. Otherwise, download a statically linked clang-format executable, verify its
9 #    integrity, place it in <ccache-top-dir>/misc/.clang-format-exe and execute
10 #    it.
11
12 set -eu
13
14 if [ -n "${CLANG_FORMAT:-}" ]; then
15     exec "$CLANG_FORMAT" "$@"
16 fi
17
18 top_dir="$(dirname "$0")"
19 clang_format_exe="$top_dir/.clang-format-exe"
20 clang_format_version=10
21 url_prefix="https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-22538c65/clang-format-${clang_format_version}_"
22
23 if [ ! -x "$clang_format_exe" ]; then
24     case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
25         *mingw*|*cygwin*|*msys*)
26             url_suffix=windows-amd64.exe
27             checksum=0b21dfb9041437eebcc44032096e4dfba9ee7b668ee6867ada71ea3541f7b09657ea592a5b1cf659bc9f8072bdc477dfc9da07b3edacb7834b70e68d7a3fc887
28             ;;
29         *darwin*)
30             url_suffix=macosx-amd64
31             checksum=8458753e13d3cbb7949a302beab812bed6d9dd9001c6e9618e5ba2e438233633ae04704a4e150aa2abfbaf103f1df4bc4a77b306012f44b37e543964bd527951
32             ;;
33         *linux*)
34             url_suffix=linux-amd64
35             checksum=3c4aaa90ad83313a1d7b350b30f9ad62578be73241f00f7d6e92838289e0b734fab80dee9dfcbd1546135bdb4e3601cfb2cf6b47360fba0bfc961e5a7cab2015
36             ;;
37         *)
38             echo "Error: Please set CLANG_FORMAT to clang-format version $clang_format_version" >&2
39             exit 1
40             ;;
41     esac
42
43     url="$url_prefix$url_suffix"
44
45     if command -v wget >/dev/null; then
46         wget -qO "$clang_format_exe.tmp" "$url"
47     elif command -v curl >/dev/null; then
48         curl -so "$clang_format_exe.tmp" -L --retry 20 "$url"
49     else
50         echo "Error: Neither wget nor curl found" >&2
51         exit 1
52     fi
53
54     if ! command -v sha512sum >/dev/null; then
55         echo "Warning: sha512sum not found, not verifying clang-format integrity" >&2
56     elif ! echo "$checksum $clang_format_exe.tmp" | sha512sum --status -c; then
57         echo "Error: Bad checksum of downloaded clang-format" >&2
58         exit 1
59     fi
60
61     chmod +x "$clang_format_exe.tmp"
62     mv "$clang_format_exe.tmp" "$clang_format_exe"
63 fi
64
65 exec "$clang_format_exe" "$@"