|
1 | | -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | + |
2 | 3 | set -e |
3 | 4 |
|
4 | 5 | # Script to run clang-tidy on pg_deeplake source files |
5 | 6 | # Usage: ./scripts/run_clang_tidy.sh [build_dir] |
6 | 7 |
|
7 | | -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
8 | | -PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" |
9 | | -BUILD_DIR="${1:-${PROJECT_ROOT}/builds/deeplake-pg-dev}" |
10 | | - |
11 | | -# Convert BUILD_DIR to absolute path if it's relative |
12 | | -if [[ "$BUILD_DIR" != /* ]]; then |
13 | | - BUILD_DIR="${PROJECT_ROOT}/${BUILD_DIR}" |
14 | | -fi |
| 8 | +trap "rm -rf ${TEMP_DIR}" EXIT |
15 | 9 |
|
16 | | -echo "Running clang-tidy on cpp/deeplake_pg..." |
17 | | -echo "Build directory: ${BUILD_DIR}" |
| 10 | +function run_tidy() { |
| 11 | + local output file_warnings file_errors |
| 12 | + output=$(clang-tidy --header-filter='.*deeplake_pg.*' -p "$BUILD_DIR" "$1" 2>&1 || true) |
| 13 | + file_warnings=$(echo "$output" | grep -c "deeplake_pg/.*warning:" || true) |
| 14 | + file_errors=$(echo "$output" | grep -c "deeplake_pg/.*error:" || true) |
| 15 | + echo "$file|$file_warnings|$file_errors" >"${TEMP_DIR}/${1}.count" |
| 16 | + if [ "$file_errors" -gt 0 ]; then |
| 17 | + echo "$output" | grep "deeplake_pg/.*error:" >"${TEMP_DIR}/${1}.output" |
| 18 | + elif [ "$file_warnings" -gt 0 ]; then |
| 19 | + echo "$output" | grep "deeplake_pg/.*warning:" >"${TEMP_DIR}/${1}.output" |
| 20 | + fi |
| 21 | +} |
| 22 | + |
| 23 | +function log() { |
| 24 | + local level ts |
| 25 | + level="$1" |
| 26 | + ts="$(date --utc -Iseconds)" |
| 27 | + shift |
| 28 | + printf "[%s] - [%s] - \"%s\"\n" "${level^^}" "${ts}" "$*" |
| 29 | +} |
| 30 | + |
| 31 | +SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" |
| 32 | +PROJECT_ROOT="$(cd $SCRIPT_DIR && realpath ..)" |
| 33 | +BUILD_DIR="$(realpath "${1:-${PROJECT_ROOT}/builds/deeplake-pg-dev}")" |
| 34 | + |
| 35 | +log info "running clang-tidy on cpp/deeplake_pg..." |
| 36 | +log info "build directory: ${BUILD_DIR}" |
18 | 37 |
|
19 | 38 | if [ ! -f "${BUILD_DIR}/compile_commands.json" ]; then |
20 | | - echo "Error: compile_commands.json not found in ${BUILD_DIR}" |
21 | | - echo "Please build the project first to generate compile_commands.json" |
22 | | - exit 1 |
| 39 | + log error "compile_commands.json not found in ${BUILD_DIR}, please build the project first to generate compile_commands.json" |
| 40 | + exit 1 |
23 | 41 | fi |
24 | 42 |
|
25 | 43 | cd "${PROJECT_ROOT}/cpp/deeplake_pg" |
26 | | - |
27 | | -# Create temp directory for parallel output |
28 | 44 | TEMP_DIR=$(mktemp -d) |
29 | | -trap "rm -rf ${TEMP_DIR}" EXIT |
30 | 45 |
|
31 | | -echo "Running clang-tidy in parallel..." |
| 46 | +log info "running clang-tidy in parallel..." |
32 | 47 |
|
33 | | -# Run clang-tidy for each file in parallel |
| 48 | +worker_count="$(nproc)" |
34 | 49 | for file in *.cpp; do |
35 | | - ( |
36 | | - OUTPUT=$(clang-tidy --header-filter='.*deeplake_pg.*' -p "$BUILD_DIR" "$file" 2>&1 || true) |
37 | | - |
38 | | - # Count warnings in this file (only in deeplake_pg directory) |
39 | | - FILE_WARNINGS=$(echo "$OUTPUT" | grep -c "deeplake_pg/.*warning:" || true) |
40 | | - # Count errors in this file |
41 | | - FILE_ERRORS=$(echo "$OUTPUT" | grep -c "deeplake_pg/.*error:" || true) |
42 | | - |
43 | | - # Save results to temp file |
44 | | - echo "$file|$FILE_WARNINGS|$FILE_ERRORS" > "${TEMP_DIR}/${file}.count" |
45 | | - |
46 | | - if [ "$FILE_ERRORS" -gt 0 ]; then |
47 | | - echo "$OUTPUT" | grep "deeplake_pg/.*error:" > "${TEMP_DIR}/${file}.output" |
48 | | - elif [ "$FILE_WARNINGS" -gt 0 ]; then |
49 | | - echo "$OUTPUT" | grep "deeplake_pg/.*warning:" > "${TEMP_DIR}/${file}.output" |
50 | | - fi |
51 | | - ) & |
| 50 | + run_tidy $file & |
| 51 | + while [ "$(jobs | wc -l)" -ge "$worker_count" ]; do |
| 52 | + sleep 0.1 |
| 53 | + done |
52 | 54 | done |
53 | | - |
54 | | -# Wait for all parallel jobs to complete |
55 | 55 | wait |
56 | 56 |
|
57 | | -echo "" |
58 | | -echo "Processing results..." |
| 57 | +log info "processing results..." |
59 | 58 |
|
60 | 59 | WARNINGS=0 |
61 | 60 | ERRORS=0 |
62 | 61 |
|
63 | | -# Process results in order |
64 | 62 | for file in *.cpp; do |
65 | | - if [ -f "${TEMP_DIR}/${file}.count" ]; then |
66 | | - IFS='|' read -r fname FILE_WARNINGS FILE_ERRORS < "${TEMP_DIR}/${file}.count" |
67 | | - |
68 | | - if [ "$FILE_ERRORS" -gt 0 ]; then |
69 | | - echo "❌ $file - has $FILE_ERRORS errors" |
70 | | - cat "${TEMP_DIR}/${file}.output" |
71 | | - ERRORS=$((ERRORS + FILE_ERRORS)) |
72 | | - elif [ "$FILE_WARNINGS" -gt 0 ]; then |
73 | | - echo "⚠ $file - has $FILE_WARNINGS warnings" |
74 | | - cat "${TEMP_DIR}/${file}.output" |
75 | | - WARNINGS=$((WARNINGS + FILE_WARNINGS)) |
76 | | - else |
77 | | - echo "✓ $file - no issues" |
78 | | - fi |
| 63 | + if [ -f "${TEMP_DIR}/${file}.count" ]; then |
| 64 | + IFS='|' read -r fname FILE_WARNINGS FILE_ERRORS <"${TEMP_DIR}/${file}.count" |
| 65 | + |
| 66 | + if [ "$FILE_ERRORS" -gt 0 ]; then |
| 67 | + log error "❌ $file - has $FILE_ERRORS errors" |
| 68 | + cat "${TEMP_DIR}/${file}.output" |
| 69 | + ERRORS=$((ERRORS + FILE_ERRORS)) |
| 70 | + elif [ "$FILE_WARNINGS" -gt 0 ]; then |
| 71 | + log warn "⚠ $file - has $FILE_WARNINGS warnings" |
| 72 | + cat "${TEMP_DIR}/${file}.output" |
| 73 | + WARNINGS=$((WARNINGS + FILE_WARNINGS)) |
| 74 | + else |
| 75 | + log info "✓ $file - no issues" |
79 | 76 | fi |
| 77 | + fi |
80 | 78 | done |
81 | 79 |
|
82 | | -echo "" |
83 | | -echo "====================" |
84 | | -echo "Clang-Tidy Summary" |
85 | | -echo "====================" |
86 | | -echo "Total warnings: $WARNINGS" |
87 | | -echo "Total errors: $ERRORS" |
| 80 | +log info "clang-tidy summary: warnings=$WARNINGS errors=$ERRORS" |
88 | 81 |
|
89 | 82 | if [ $ERRORS -gt 0 ]; then |
90 | | - echo "❌ Clang-tidy found $ERRORS errors!" |
91 | | - exit 1 |
| 83 | + log error "❌ Clang-tidy found $ERRORS errors!" |
| 84 | + exit 1 |
92 | 85 | elif [ $WARNINGS -gt 0 ]; then |
93 | | - echo "⚠ Clang-tidy found $WARNINGS warnings (non-blocking)" |
94 | | - exit 0 |
| 86 | + log warn "⚠ Clang-tidy found $WARNINGS warnings (non-blocking)" |
| 87 | + exit 0 |
95 | 88 | else |
96 | | - echo "✅ No issues found!" |
97 | | - exit 0 |
| 89 | + log info "✅ No issues found!" |
| 90 | + exit 0 |
98 | 91 | fi |
| 92 | + |
0 commit comments