• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4ROOT_DIR="$( cd "${SRC_DIR}/.." >/dev/null 2>&1 && pwd )"
5TESTS_DIR="$( cd "${ROOT_DIR}/tests" >/dev/null 2>&1 && pwd )"
6
7CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
8${CLANG_FORMAT} --version
9
10show_help()
11{
12# Tells cat to stop reading file when EOF is detected
13cat << EOF
14Usage: ./clang-format-all.sh [-ah]
15Format files in the SwiftShader repository
16-h, --help      Display this message and exit
17-a, --all       Format all files (default is to format only files active in a git CL)
18EOF
19# cat finishes printing
20}
21
22while [[ $# -gt 0 ]]
23do
24    case $1 in
25        -a|--all)
26            all=1
27            shift
28            ;;
29        -h|--help)
30            show_help
31            exit 0
32            ;;
33    esac
34done
35
36if [[ $all -eq 1 ]]
37then
38    for DIR in "${SRC_DIR}/Device" "${SRC_DIR}/Pipeline" "${SRC_DIR}/Reactor" "${SRC_DIR}/System" "${SRC_DIR}/Vulkan" "${SRC_DIR}/WSI" "${TESTS_DIR}"
39    do
40        # Double clang-format, as it seems that one pass isn't always enough
41        find ${DIR} -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs ${CLANG_FORMAT} -i -style=file
42        find ${DIR} -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.inl" | xargs ${CLANG_FORMAT} -i -style=file
43    done
44else
45    BASEDIR=$(git rev-parse --show-toplevel)
46    FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cpp$\|\.hpp\|\.c$\|\.h$')
47    for FILE in $FILES
48    do
49        ${CLANG_FORMAT} -i -style=file "$BASEDIR/$FILE"
50    done
51fi
52