Nicolas Capens | 04c1eb3 | 2021-11-19 13:43:37 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Sean Risser | 08762e3 | 2021-05-05 14:28:24 -0400 | [diff] [blame] | 2 | |
Ben Clayton | d4e6447 | 2019-02-01 08:18:19 +0000 | [diff] [blame] | 3 | SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
Ben Clayton | 0b4bc58 | 2020-01-06 13:22:14 +0000 | [diff] [blame] | 4 | ROOT_DIR="$( cd "${SRC_DIR}/.." >/dev/null 2>&1 && pwd )" |
| 5 | TESTS_DIR="$( cd "${ROOT_DIR}/tests" >/dev/null 2>&1 && pwd )" |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 6 | |
| 7 | CLANG_FORMAT=${CLANG_FORMAT:-clang-format} |
Nicolas Capens | dac99e8 | 2020-11-19 04:18:58 +0000 | [diff] [blame] | 8 | ${CLANG_FORMAT} --version |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 9 | |
Sean Risser | 08762e3 | 2021-05-05 14:28:24 -0400 | [diff] [blame] | 10 | show_help() |
| 11 | { |
| 12 | # Tells cat to stop reading file when EOF is detected |
| 13 | cat << EOF |
| 14 | Usage: ./clang-format-all.sh [-ah] |
| 15 | Format 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) |
| 18 | EOF |
| 19 | # cat finishes printing |
| 20 | } |
| 21 | |
| 22 | while [[ $# -gt 0 ]] |
Ben Clayton | d4e6447 | 2019-02-01 08:18:19 +0000 | [diff] [blame] | 23 | do |
Sean Risser | 08762e3 | 2021-05-05 14:28:24 -0400 | [diff] [blame] | 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 |
Ben Clayton | d4e6447 | 2019-02-01 08:18:19 +0000 | [diff] [blame] | 34 | done |
Ben Clayton | d4e6447 | 2019-02-01 08:18:19 +0000 | [diff] [blame] | 35 | |
Sean Risser | 08762e3 | 2021-05-05 14:28:24 -0400 | [diff] [blame] | 36 | if [[ $all -eq 1 ]] |
| 37 | then |
| 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 |
| 44 | else |
| 45 | BASEDIR=$(git rev-parse --show-toplevel) |
Alexis Hetu | 78cfdcd | 2022-09-01 10:55:48 -0400 | [diff] [blame] | 46 | FILES=$(git diff --name-only --diff-filter=ACM | grep '\.cpp$\|\.hpp\|\.c$\|\.h$') |
Sean Risser | 08762e3 | 2021-05-05 14:28:24 -0400 | [diff] [blame] | 47 | for FILE in $FILES |
| 48 | do |
| 49 | ${CLANG_FORMAT} -i -style=file "$BASEDIR/$FILE" |
| 50 | done |
| 51 | fi |