blob: 9fb5a522678c3785f98843f274cf548f8f47b426 [file] [log] [blame]
Nicolas Capens04c1eb32021-11-19 13:43:37 -05001#!/bin/bash
Sean Risser08762e32021-05-05 14:28:24 -04002
Ben Claytond4e64472019-02-01 08:18:19 +00003SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
Ben Clayton0b4bc582020-01-06 13:22:14 +00004ROOT_DIR="$( cd "${SRC_DIR}/.." >/dev/null 2>&1 && pwd )"
5TESTS_DIR="$( cd "${ROOT_DIR}/tests" >/dev/null 2>&1 && pwd )"
Ben Clayton750660e2019-12-18 15:09:46 +00006
7CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
Nicolas Capensdac99e82020-11-19 04:18:58 +00008${CLANG_FORMAT} --version
Ben Clayton750660e2019-12-18 15:09:46 +00009
Sean Risser08762e32021-05-05 14:28:24 -040010show_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 ]]
Ben Claytond4e64472019-02-01 08:18:19 +000023do
Sean Risser08762e32021-05-05 14:28:24 -040024 case $1 in
25 -a|--all)
26 all=1
27 shift
28 ;;
29 -h|--help)
30 show_help
31 exit 0
32 ;;
33 esac
Ben Claytond4e64472019-02-01 08:18:19 +000034done
Ben Claytond4e64472019-02-01 08:18:19 +000035
Sean Risser08762e32021-05-05 14:28:24 -040036if [[ $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)
Alexis Hetu78cfdcd2022-09-01 10:55:48 -040046 FILES=$(git diff --name-only --diff-filter=ACM | grep '\.cpp$\|\.hpp\|\.c$\|\.h$')
Sean Risser08762e32021-05-05 14:28:24 -040047 for FILE in $FILES
48 do
49 ${CLANG_FORMAT} -i -style=file "$BASEDIR/$FILE"
50 done
51fi