|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o nounset |
| 4 | +set -o errexit |
| 5 | +trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR |
| 6 | +set -o errtrace |
| 7 | +set -o pipefail |
| 8 | +IFS=$'\n\t' |
| 9 | + |
| 10 | +############################################################################### |
| 11 | +# Environment |
| 12 | +############################################################################### |
| 13 | + |
| 14 | +# $_ME |
| 15 | +# |
| 16 | +# This program's basename. |
| 17 | +_ME="$(basename "${0}")" |
| 18 | + |
| 19 | +############################################################################### |
| 20 | +# Help |
| 21 | +############################################################################### |
| 22 | + |
| 23 | +# _print_help() |
| 24 | +# |
| 25 | +# Usage: |
| 26 | +# _print_help |
| 27 | +# |
| 28 | +# Print the program help information. |
| 29 | +_print_help() { |
| 30 | + cat <<HEREDOC |
| 31 | +
|
| 32 | +Callback script to process CMake Instrumentation data |
| 33 | +https://cmake.org/cmake/help/latest/command/cmake_instrumentation.html |
| 34 | +
|
| 35 | +Usage: |
| 36 | + ${_ME} [<arguments>] |
| 37 | + ${_ME} -h | --help |
| 38 | +
|
| 39 | +Options: |
| 40 | + -h --help Show this screen. |
| 41 | +HEREDOC |
| 42 | +} |
| 43 | + |
| 44 | +############################################################################### |
| 45 | +# Program Functions |
| 46 | +############################################################################### |
| 47 | +_debug_print() { |
| 48 | + if [[ -n "${DEBUG:-}" ]]; then |
| 49 | + printf "[DEBUG] $(date +'%H:%M:%S'): %s \n" "$1" >&2 |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +_check_file_exists() { |
| 54 | + local file="$1" |
| 55 | + if [[ ! -f "${file}" ]]; then |
| 56 | + echo "Error: File not found: ${file}" >&2 |
| 57 | + exit 1 # Exit the entire script with a non-zero status |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +_process_index() { |
| 62 | + indexFile=${1:-} |
| 63 | + _check_file_exists "${indexFile}" |
| 64 | + _debug_print "$(cat "${indexFile}")" |
| 65 | + |
| 66 | + local buildDir |
| 67 | + buildDir=$(jq -r '.buildDir' "${1:-}") |
| 68 | + _debug_print "$(printf "buildDir is |%q|" "${buildDir}")" |
| 69 | + |
| 70 | + local dataDir |
| 71 | + dataDir=$(jq -r '.dataDir' "${1:-}") |
| 72 | + _debug_print "$(printf "dataDir is |%q|" "${dataDir}")" |
| 73 | + |
| 74 | + local hook |
| 75 | + hook=$(jq -r '.hook' "${1:-}") |
| 76 | + _debug_print "$(printf "hook is |%q|" "${hook}")" |
| 77 | + |
| 78 | + local trace |
| 79 | + trace=$(jq -r '.trace' "${1:-}") |
| 80 | + _debug_print "$(printf "trace is |%q|" "${trace}")" |
| 81 | + |
| 82 | + local outputDir |
| 83 | + outputDir="${buildDir}/.trace" |
| 84 | + _debug_print "$(printf "Copy trace to |%q|" "${outputDir}")" |
| 85 | + mkdir -p "${outputDir}" |
| 86 | + |
| 87 | + local traceDestFile |
| 88 | + traceDestFile="${outputDir}/${hook}-$(basename "${trace}")" |
| 89 | + _debug_print "$(printf "traceDestFile: |%q|" "${traceDestFile}")" |
| 90 | + cp "${dataDir}/${trace}" "${outputDir}/${hook}-$(basename "${trace}")" |
| 91 | +} |
| 92 | + |
| 93 | +############################################################################### |
| 94 | +# Main |
| 95 | +############################################################################### |
| 96 | + |
| 97 | +# _main() |
| 98 | +# |
| 99 | +# Usage: |
| 100 | +# _main [<options>] [<arguments>] |
| 101 | +# |
| 102 | +# Description: |
| 103 | +# Entry point for the program, handling basic option parsing and dispatching. |
| 104 | +_main() { |
| 105 | + # Avoid complex option parsing when only one program option is expected. |
| 106 | + if [[ "${1:-}" =~ ^-h|--help$ ]] |
| 107 | + then |
| 108 | + _print_help |
| 109 | + else |
| 110 | + _process_index "$@" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +# Call `_main` after everything has been defined. |
| 115 | +_main "$@" |
0 commit comments