|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +PROJECT_ROOT="$(realpath "$(dirname "$0")/../")" |
| 4 | + |
| 5 | +if [ -n "$(which clang-tidy-13)" ]; then |
| 6 | + # NOTE: Using clang-tidy-13 in CI system, too |
| 7 | + LINTER=clang-tidy-13 |
| 8 | +elif [ -n "$(which clang-tidy)" ]; then |
| 9 | + echo "Did not find clang-tidy-13. Trying clang-tidy. Results may not" |
| 10 | + echo "match formatting in GitHub CI process." |
| 11 | + LINTER=clang-tidy |
| 12 | +else |
| 13 | + echo "Could not find clang-tidy. Please make sure it is installed" 1>&2 |
| 14 | + exit 2 |
| 15 | +fi |
| 16 | + |
| 17 | +usage() { |
| 18 | + echo "$(basename "$0") path/to/compile_commands.json [source_to_lint]" 1>&2 |
| 19 | + echo 1>&2 |
| 20 | + echo " compile_commands.json" 1>&2 |
| 21 | + echo " Produced during a cmake build when configured with the" 1>&2 |
| 22 | + echo " -DCMAKE_EXPORT_COMPILE_COMMANDS=yes flag" 1>&2 |
| 23 | + echo 1>&2 |
| 24 | + echo " source_to_lint (optional)" 1>&2 |
| 25 | + echo " Source file to run clang-tidy against. If not specified," 1>&2 |
| 26 | + echo " all source files in the repo will be scanned." 1>&2 |
| 27 | +} |
| 28 | + |
| 29 | +if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "/h" ] || [ "$1" == "/?" ]; then |
| 30 | + usage |
| 31 | + exit |
| 32 | +fi |
| 33 | + |
| 34 | +compile_database="$1" |
| 35 | + |
| 36 | +if [ -z "$compile_database" ]; then |
| 37 | + echo "No compile database specified. Make sure cmake was configured" 1>&2 |
| 38 | + echo "with '-DCMAKE_EXPORT_COMPILE_COMMANDS=yes' and re-run the build" 1>&2 |
| 39 | + echo 1>&2 |
| 40 | + echo "Usage:" 1>&2 |
| 41 | + usage |
| 42 | + exit 1 |
| 43 | +elif [ ! -f "$compile_database" ]; then |
| 44 | + echo "Compile database file not found. Make sure cmake was configured" 1>&2 |
| 45 | + echo "with '-DCMAKE_EXPORT_COMPILE_COMMANDS=yes' and re-run the build" 1>&2 |
| 46 | + echo 1>&2 |
| 47 | + echo "Usage:" 1>&2 |
| 48 | + usage |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +compile_database="$(realpath "$compile_database")" |
| 53 | + |
| 54 | +target_source="$2" |
| 55 | + |
| 56 | +if [ -z "$target_source" ]; then |
| 57 | + echo "Running $LINTER on all files in '$PROJECT_ROOT'" |
| 58 | + shopt -s globstar |
| 59 | + |
| 60 | + pushd "$PROJECT_ROOT" > /dev/null |
| 61 | + for f in pubsub/include/**/*.h pubsub/src/**/*.cpp rpc/include/**/*.h rpc/src/**/*.cpp; do |
| 62 | + if [[ ! ("$f" =~ "build/") ]]; then |
| 63 | + echo |
| 64 | + echo "Checking file '$f'" |
| 65 | + $LINTER -p "$(dirname "$compile_database")" "$f" |
| 66 | + fi |
| 67 | + done |
| 68 | + popd > /dev/null |
| 69 | + exit |
| 70 | +fi |
| 71 | + |
| 72 | +if [ ! -f "$target_source" ]; then |
| 73 | + echo "Target source file '$target_source' not found." 1>&2 |
| 74 | + echo 1>&2 |
| 75 | + echo "Usage:" 1>&2 |
| 76 | + usage |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +echo "Running $LINTER on '$target_source'" |
| 81 | +$LINTER -p "$(dirname "$compile_database")" "$target_source" |
0 commit comments