xref: /isa-l_crypto/tools/format.sh (revision f36388d4b3c8a76e2ad93762aed12ef3b442b26f)
1#!/usr/bin/env bash
2
3verbose=0
4clang_format_min_version=18
5
6function clang_format_version() {
7    version_str=$($clang_format --version)
8    regex="[0-9]+"
9    if [[ $version_str =~ $regex ]]; then
10        major_version="${BASH_REMATCH[0]}"
11        echo $major_version
12    fi
13}
14
15# set clang-format binary if not set externally
16if [[ -z $CLANGFORMAT ]]; then
17    clang_format="clang-format"
18else
19    clang_format=$CLANGFORMAT
20fi
21
22while [ -n "$*" ]; do
23    case "$1" in
24        -v )
25            verbose=1
26            shift
27            ;;
28        -h )
29            echo format.sh [-h -v]
30            exit 0
31            ;;
32    esac
33done
34
35if [ $(clang_format_version) -ge $clang_format_min_version ]; then
36    echo "Formatting files using clang-format v$(clang_format_version)..."
37    for f in `git ls-files '*.[c|h]'`; do
38        [ "$verbose" -gt 0 ] && echo "formatting $f"
39        $clang_format -style=file -i "$f" &
40    done
41else
42    echo "clang-format version ${clang_format_min_version}+ is required!"
43fi
44
45# wait for background processes to finish
46wait
47