Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Format all source files in the project. |
| 4 | # |
| 5 | # Set CLANG_FORMAT_CMD environment variable to specify executable used (default: `clang-format`). |
| 6 | |
| 7 | set -e |
| 8 | |
| 9 | ################################################################################ |
| 10 | # Configuration |
| 11 | |
| 12 | # folders to search |
| 13 | FOLDERS=" |
| 14 | include |
| 15 | internal |
| 16 | examples |
| 17 | " |
| 18 | |
| 19 | # paths to ignore |
| 20 | EXCLUDE_PATHS=" |
| 21 | internal/ceres/gtest/* |
| 22 | internal/ceres/gmock/* |
| 23 | internal/ceres/gmock_gtest_all.cc |
| 24 | internal/ceres/gmock_main.cc |
| 25 | internal/ceres/generated/* |
| 26 | internal/ceres/generated_bundle_adjustment_tests/* |
| 27 | internal/ceres/schur_eliminator.cc |
| 28 | internal/ceres/partitioned_matrix_view.cc |
| 29 | internal/ceres/schur_templates.cc |
| 30 | " |
| 31 | |
| 32 | ################################################################################ |
| 33 | # Implementation |
| 34 | |
| 35 | # directory of this script and the repository root |
| 36 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 37 | REPO_DIR="$SCRIPT_DIR/.." |
| 38 | |
| 39 | # set default for CLANG_FORMAT_CMD |
| 40 | CLANG_FORMAT_CMD=${CLANG_FORMAT_CMD:-clang-format} |
| 41 | echo "Formatting with $CLANG_FORMAT_CMD (`$CLANG_FORMAT_CMD --version`)" |
| 42 | |
| 43 | # prepare arguments to exclude ignored paths |
| 44 | EXCLUDE_ARGS="" |
| 45 | for p in $EXCLUDE_PATHS; do |
| 46 | EXCLUDE_ARGS="-not -path */$p $EXCLUDE_ARGS" |
| 47 | done |
| 48 | |
| 49 | # for each folder, format header and source dirs |
| 50 | for d in $FOLDERS; do |
| 51 | d="$REPO_DIR/$d" |
| 52 | find "$d" \( -name "*.h" -or -name "*.cc" \) $EXCLUDE_ARGS | xargs $CLANG_FORMAT_CMD -verbose -i |
| 53 | done |