1#!/usr/bin/env bash
  2
  3PROG=${0##*/}
  4build_dir="build-ci-debug"
  5
  6# Print Color Commands
  7red=$(tput setaf 1)
  8green=$(tput setaf 2)
  9yellow=$(tput setaf 3)
 10blue=$(tput setaf 4)
 11magenta=$(tput setaf 5)
 12cyan=$(tput setaf 6)
 13normal=$(tput sgr0)
 14
 15
 16# Print Help Message
 17####################
 18
 19print_full_help() {
 20  cat << EOF
 21Usage: $PROG [OPTION]... <test_regex> (test_number)
 22Debug specific ctest program.
 23
 24Options:
 25  -h, --help            display this help and exit
 26  -g                    run in gdb mode
 27
 28Arguments:
 29  <test_regex>     (Mandatory) Supply one regex to the script to filter tests
 30  (test_number)    (Optional) Test number to run a specific test
 31
 32Example:
 33  $PROG test-tokenizer
 34  $PROG test-tokenizer 3
 35EOF
 36}
 37
 38abort() {
 39  echo "Error: $1" >&2
 40  cat << EOF >&2
 41Usage: $PROG [OPTION]... <test_regex> (test_number)
 42Debug specific ctest program.
 43Refer to --help for full instructions.
 44EOF
 45  exit 1
 46}
 47
 48
 49# Dependency Sanity Check
 50#########################
 51
 52check_dependency() {
 53  command -v "$1" >/dev/null 2>&1 || {
 54    abort "$1 is required but not found. Please install it and try again."
 55  }
 56}
 57
 58check_dependency ctest
 59check_dependency cmake
 60
 61
 62# Step 0: Check the args
 63########################
 64
 65if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then
 66  print_full_help >&2
 67  exit 0
 68fi
 69
 70# Parse command-line options
 71gdb_mode=false
 72while getopts "g" opt; do
 73    case $opt in
 74        g)
 75            gdb_mode=true
 76            echo "gdb_mode Mode Enabled"
 77            ;;
 78    esac
 79done
 80
 81# Shift the option parameters
 82shift $((OPTIND - 1))
 83
 84# Positionial Argument Processing : <test_regex>
 85if [ -z "${1}" ]; then
 86    abort "Test regex is required"
 87else
 88    test_suite=${1:-}
 89fi
 90
 91# Positionial Argument Processing : (test_number)
 92test_number=${2:-}
 93
 94
 95# Step 1: Reset and Setup folder context
 96########################################
 97
 98## Sanity check that we are actually in a git repo
 99repo_root=$(git rev-parse --show-toplevel)
100if [ ! -d "$repo_root" ]; then
101    abort "Not in a Git repository."
102fi
103
104## Reset folder to root context of git repo and Create and enter build directory
105pushd "$repo_root"
106rm -rf "$build_dir" && mkdir "$build_dir" || abort "Failed to make $build_dir"
107
108
109# Step 2: Setup Build Environment and Compile Test Binaries
110###########################################################
111
112cmake -B "./$build_dir" -DCMAKE_BUILD_TYPE=Debug -DGGML_CUDA=1 || abort "Failed to build environment"
113pushd "$build_dir"
114make -j || abort "Failed to compile"
115popd > /dev/null || exit 1
116
117
118# Step 3: Find all tests available that matches REGEX
119####################################################
120
121# Ctest Gather Tests
122# `-R test-tokenizer` : looks for all the test files named `test-tokenizer*` (R=Regex)
123# `-N` : "show-only" disables test execution & shows test commands that you can feed to GDB.
124# `-V` : Verbose Mode
125printf "\n\nGathering tests that fit REGEX: ${test_suite} ...\n"
126pushd "$build_dir"
127tests=($(ctest -R ${test_suite} -V -N | grep -E " +Test +#[0-9]+*" | cut -d':' -f2 | awk '{$1=$1};1'))
128if [ ${#tests[@]} -eq 0 ]; then
129    abort "No tests available... check your compilation process..."
130fi
131popd > /dev/null || exit 1
132
133
134# Step 4: Identify Test Command for Debugging
135#############################################
136
137# Select test number
138if [ -z $test_number ]; then
139    # List out available tests
140    printf "Which test would you like to debug?\n"
141    id=0
142    for s in "${tests[@]}"
143    do
144        echo "Test# ${id}"
145        echo "  $s"
146        ((id++))
147    done
148
149    # Prompt user which test they wanted to run
150    printf "\nRun test#? "
151    read test_number
152
153else
154    printf "\nUser Already Requested #${test_number}\n"
155
156fi
157
158# Grab all tests commands
159pushd "$build_dir"
160sIFS=$IFS # Save Initial IFS (Internal Field Separator)
161IFS=$'\n' # Change IFS (Internal Field Separator) (So we split ctest output by newline rather than by spaces)
162test_args=($(ctest -R ${test_suite} -V -N | grep "Test command" | cut -d':' -f3 | awk '{$1=$1};1' )) # Get test args
163IFS=$sIFS # Reset IFS (Internal Field Separator)
164popd > /dev/null || exit 1
165
166# Grab specific test command
167single_test_name="${tests[test_number]}"
168single_test_command="${test_args[test_number]}"
169
170
171# Step 5: Execute or GDB Debug
172##############################
173
174printf "${magenta}Running Test #${test_number}: ${single_test_name}${normal}\n"
175printf "${cyan}single_test_command: ${single_test_command}${normal}\n"
176
177if [ "$gdb_mode" = "true" ]; then
178    # Execute debugger
179    pushd "$repo_root" || exit 1
180    eval "gdb --args ${single_test_command}"
181    popd > /dev/null || exit 1
182
183else
184    # Execute Test
185    pushd "$repo_root" || exit 1
186    eval "${single_test_command}"
187    exit_code=$?
188    popd > /dev/null || exit 1
189
190    # Print Result
191    printf "${blue}Ran Test #${test_number}: ${single_test_name}${normal}\n"
192    printf "${yellow}Command: ${single_test_command}${normal}\n"
193    if [ $exit_code -eq 0 ]; then
194        printf "${green}TEST PASS${normal}\n"
195    else
196        printf "${red}TEST FAIL${normal}\n"
197    fi
198
199fi
200
201# Return to the directory from which the user ran the command.
202popd > /dev/null || exit 1