|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +set -o pipefail |
| 4 | + |
| 5 | +# Function to display help message |
| 6 | +show_help() { |
| 7 | + echo "Usage: ./mfc.sh precheck [OPTIONS]" |
| 8 | + echo "Run the same fast checks that CI runs before expensive tests start." |
| 9 | + echo "Use this locally before pushing to catch issues early." |
| 10 | + echo "" |
| 11 | + echo "Options:" |
| 12 | + echo " -h, --help Display this help message and exit." |
| 13 | + echo " -j, --jobs JOBS Runs JOBS number of parallel jobs for formatting." |
| 14 | + echo "" |
| 15 | + exit 0 |
| 16 | +} |
| 17 | + |
| 18 | +# Cross-platform hash function (macOS uses md5, Linux uses md5sum) |
| 19 | +compute_hash() { |
| 20 | + if command -v md5sum > /dev/null 2>&1; then |
| 21 | + md5sum | cut -d' ' -f1 |
| 22 | + elif command -v md5 > /dev/null 2>&1; then |
| 23 | + md5 -q |
| 24 | + else |
| 25 | + # Fallback: use cksum if neither available |
| 26 | + cksum | cut -d' ' -f1 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +JOBS=1 |
| 31 | + |
| 32 | +while [[ $# -gt 0 ]]; do |
| 33 | + case "$1" in |
| 34 | + -j|--jobs) |
| 35 | + if [[ -z "$2" || "$2" == -* ]]; then |
| 36 | + echo "Precheck: -j/--jobs requires a value." |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + if ! [[ "$2" =~ ^[0-9]+$ ]]; then |
| 40 | + echo "Precheck: jobs value '$2' is not a valid number." |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + JOBS="$2" |
| 44 | + shift |
| 45 | + ;; |
| 46 | + -h | --help) |
| 47 | + show_help |
| 48 | + ;; |
| 49 | + *) |
| 50 | + echo "Precheck: unknown argument: $1." |
| 51 | + exit 1 |
| 52 | + ;; |
| 53 | + esac |
| 54 | + |
| 55 | + shift |
| 56 | +done |
| 57 | + |
| 58 | +FAILED=0 |
| 59 | + |
| 60 | +log "Running$MAGENTA precheck$COLOR_RESET (same checks as CI lint-gate)..." |
| 61 | +echo "" |
| 62 | + |
| 63 | +# 1. Check formatting |
| 64 | +log "[$CYAN 1/4$COLOR_RESET] Checking$MAGENTA formatting$COLOR_RESET..." |
| 65 | +# Capture state before formatting |
| 66 | +BEFORE_HASH=$(git diff -- '*.f90' '*.fpp' '*.py' 2>/dev/null | compute_hash) |
| 67 | +if ! ./mfc.sh format -j "$JOBS" > /dev/null 2>&1; then |
| 68 | + error "Formatting check failed to run." |
| 69 | + FAILED=1 |
| 70 | +else |
| 71 | + # Check if formatting changed any Fortran/Python files |
| 72 | + AFTER_HASH=$(git diff -- '*.f90' '*.fpp' '*.py' 2>/dev/null | compute_hash) |
| 73 | + if [ "$BEFORE_HASH" != "$AFTER_HASH" ]; then |
| 74 | + error "Code was not formatted. Files have been auto-formatted; review and stage the changes." |
| 75 | + echo "" |
| 76 | + git diff --stat -- '*.f90' '*.fpp' '*.py' 2>/dev/null || true |
| 77 | + echo "" |
| 78 | + FAILED=1 |
| 79 | + else |
| 80 | + ok "Formatting check passed." |
| 81 | + fi |
| 82 | +fi |
| 83 | + |
| 84 | +# 2. Spell check |
| 85 | +log "[$CYAN 2/4$COLOR_RESET] Running$MAGENTA spell check$COLOR_RESET..." |
| 86 | +if ./mfc.sh spelling > /dev/null 2>&1; then |
| 87 | + ok "Spell check passed." |
| 88 | +else |
| 89 | + error "Spell check failed. Run$MAGENTA ./mfc.sh spelling$COLOR_RESET for details." |
| 90 | + FAILED=1 |
| 91 | +fi |
| 92 | + |
| 93 | +# 3. Lint toolchain (Python) |
| 94 | +log "[$CYAN 3/4$COLOR_RESET] Running$MAGENTA toolchain lint$COLOR_RESET..." |
| 95 | +if ./mfc.sh lint > /dev/null 2>&1; then |
| 96 | + ok "Toolchain lint passed." |
| 97 | +else |
| 98 | + error "Toolchain lint failed. Run$MAGENTA ./mfc.sh lint$COLOR_RESET for details." |
| 99 | + FAILED=1 |
| 100 | +fi |
| 101 | + |
| 102 | +# 4. Source code lint checks |
| 103 | +log "[$CYAN 4/4$COLOR_RESET] Running$MAGENTA source lint$COLOR_RESET checks..." |
| 104 | +SOURCE_FAILED=0 |
| 105 | + |
| 106 | +# Check for raw OpenACC/OpenMP directives |
| 107 | +if grep -qiR '!\$acc\|!\$omp' --exclude="parallel_macros.fpp" --exclude="acc_macros.fpp" --exclude="omp_macros.fpp" --exclude="shared_parallel_macros.fpp" --exclude="syscheck.fpp" ./src/* 2>/dev/null; then |
| 108 | + error "Found raw OpenACC/OpenMP directives. Use macros instead." |
| 109 | + SOURCE_FAILED=1 |
| 110 | +fi |
| 111 | + |
| 112 | +# Check for double precision intrinsics |
| 113 | +if grep -qiR 'double_precision\|dsqrt\|dexp\|dlog\|dble\|dabs\|double\ precision\|real(8)\|real(4)\|dprod\|dmin\|dmax\|dfloat\|dreal\|dcos\|dsin\|dtan\|dsign\|dtanh\|dsinh\|dcosh\|d0' --exclude-dir=syscheck --exclude="*nvtx*" --exclude="*precision_select*" ./src/* 2>/dev/null; then |
| 114 | + error "Found double precision intrinsics. Use generic intrinsics." |
| 115 | + SOURCE_FAILED=1 |
| 116 | +fi |
| 117 | + |
| 118 | +# Check for junk code patterns |
| 119 | +if grep -qiR -e '\.\.\.' -e '\-\-\-' -e '===' ./src/* 2>/dev/null; then |
| 120 | + error "Found junk code patterns (..., ---, ===) in source." |
| 121 | + SOURCE_FAILED=1 |
| 122 | +fi |
| 123 | + |
| 124 | +if [ $SOURCE_FAILED -eq 0 ]; then |
| 125 | + ok "Source lint passed." |
| 126 | +else |
| 127 | + FAILED=1 |
| 128 | +fi |
| 129 | + |
| 130 | +echo "" |
| 131 | + |
| 132 | +if [ $FAILED -eq 0 ]; then |
| 133 | + ok "All precheck tests passed! Safe to push." |
| 134 | + exit 0 |
| 135 | +else |
| 136 | + error "Some precheck tests failed. Fix issues before pushing." |
| 137 | + exit 1 |
| 138 | +fi |
0 commit comments