Dave Rodgman | 2096478 | 2024-02-29 14:06:19 +0000 | [diff] [blame] | 1 | # -*-mode: sh; sh-shell: bash -*- |
| 2 | # |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 5 | # |
| 6 | # This swallows the output of the wrapped tool, unless there is an error. |
| 7 | # This helps reduce excess logging in the CI. |
| 8 | |
| 9 | # If you are debugging a build / CI issue, you can get complete unsilenced logs |
| 10 | # by un-commenting the following line (or setting VERBOSE_LOGS in your environment): |
| 11 | # VERBOSE_LOGS=1 |
Dave Rodgman | d0e3827 | 2024-02-26 17:28:56 +0000 | [diff] [blame] | 12 | # |
| 13 | # This script provides most of the functionality for the adjacent make and cmake |
| 14 | # wrappers. |
| 15 | # |
| 16 | # It requires two variables to be set: |
| 17 | # |
| 18 | # TOOL - the name of the tool that is being wrapped (with no path), e.g. "make" |
| 19 | # |
| 20 | # NO_SILENCE - a regex that describes the commandline arguments for which output will not |
| 21 | # be silenced, e.g. " --version | test ". In this example, "make test" will |
Dave Rodgman | 63c94a3 | 2024-02-29 14:06:36 +0000 | [diff] [blame] | 22 | # not be silent, but "make lib test" will be. |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 23 | |
| 24 | # Locate original tool |
Dave Rodgman | e03088b | 2024-02-26 12:48:49 +0000 | [diff] [blame] | 25 | TOOL_WITH_PATH=$(dirname "$0")/$TOOL |
| 26 | ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$TOOL_WITH_PATH" | head -n1) |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 27 | |
| 28 | print_quoted_args() { |
| 29 | # similar to printf '%q' "$@" |
| 30 | # but produce more human-readable results for common/simple cases like "a b" |
| 31 | for a in "$@"; do |
Dave Rodgman | dbc2e8d | 2024-02-26 17:29:31 +0000 | [diff] [blame] | 32 | # Get bash to quote the string |
Dave Rodgman | a3e694c | 2024-02-29 14:06:49 +0000 | [diff] [blame^] | 33 | printf -v q '%q' "$a" |
Dave Rodgman | dbc2e8d | 2024-02-26 17:29:31 +0000 | [diff] [blame] | 34 | simple_pattern="^([-[:alnum:]_+./:@]+=)?([^']*)$" |
| 35 | if [[ "$a" != "$q" && $a =~ $simple_pattern ]]; then |
| 36 | # a requires some quoting (a != q), but has no single quotes, so we can |
| 37 | # simplify the quoted form - e.g.: |
| 38 | # a b -> 'a b' |
| 39 | # CFLAGS=a b -> CFLAGS='a b' |
| 40 | q="${BASH_REMATCH[1]}'${BASH_REMATCH[2]}'" |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 41 | fi |
Dave Rodgman | 67126bb | 2024-02-26 17:30:37 +0000 | [diff] [blame] | 42 | printf " %s" "$q" |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 43 | done |
| 44 | } |
| 45 | |
| 46 | if [[ ! " $* " =~ " --version " ]]; then |
| 47 | # Display the command being invoked - if it succeeds, this is all that will |
| 48 | # be displayed. Don't do this for invocations with --version, because |
| 49 | # this output is often parsed by scripts, so we don't want to modify it. |
Dave Rodgman | 2f94766 | 2024-02-26 17:30:56 +0000 | [diff] [blame] | 50 | printf %s "${TOOL}" 1>&2 |
| 51 | print_quoted_args "$@" 1>&2 |
| 52 | echo 1>&2 |
Dave Rodgman | 98a79cd | 2024-02-26 12:37:44 +0000 | [diff] [blame] | 53 | fi |
| 54 | |
| 55 | if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then |
| 56 | # Run original command with no output supression |
| 57 | exec "${ORIGINAL_TOOL}" "$@" |
| 58 | else |
| 59 | # Run original command and capture output & exit status |
| 60 | TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX") |
| 61 | "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1 |
| 62 | EXIT_STATUS=$? |
| 63 | |
| 64 | if [[ $EXIT_STATUS -ne 0 ]]; then |
| 65 | # On error, display the full output |
| 66 | cat "${TMPFILE}" |
| 67 | fi |
| 68 | |
| 69 | # Remove tmpfile |
| 70 | rm "${TMPFILE}" |
| 71 | |
| 72 | # Propagate the exit status |
| 73 | exit $EXIT_STATUS |
| 74 | fi |