blob: 01ef422e323bd1295e8b16ef3509e4e8f72f417b [file] [log] [blame]
Dave Rodgman20964782024-02-29 14:06:19 +00001# -*-mode: sh; sh-shell: bash -*-
2#
Dave Rodgman98a79cd2024-02-26 12:37:44 +00003# 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 Rodgmand0e38272024-02-26 17:28:56 +000012#
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
Dave Rodgman5f7862a2024-02-29 14:14:37 +000021# be silenced, e.g. " --version | test ". In this example, "make lib test" will
22# not be silent, but "make lib" will be.
Dave Rodgman98a79cd2024-02-26 12:37:44 +000023
24# Locate original tool
Dave Rodgmane03088b2024-02-26 12:48:49 +000025TOOL_WITH_PATH=$(dirname "$0")/$TOOL
26ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$TOOL_WITH_PATH" | head -n1)
Dave Rodgman98a79cd2024-02-26 12:37:44 +000027
28print_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 Rodgmandbc2e8d2024-02-26 17:29:31 +000032 # Get bash to quote the string
Dave Rodgmana3e694c2024-02-29 14:06:49 +000033 printf -v q '%q' "$a"
Dave Rodgmandbc2e8d2024-02-26 17:29:31 +000034 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 Rodgman98a79cd2024-02-26 12:37:44 +000041 fi
Dave Rodgman67126bb2024-02-26 17:30:37 +000042 printf " %s" "$q"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000043 done
44}
45
46if [[ ! " $* " =~ " --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 Rodgman2f947662024-02-26 17:30:56 +000050 printf %s "${TOOL}" 1>&2
51 print_quoted_args "$@" 1>&2
52 echo 1>&2
Dave Rodgman98a79cd2024-02-26 12:37:44 +000053fi
54
55if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
56 # Run original command with no output supression
57 exec "${ORIGINAL_TOOL}" "$@"
58else
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
74fi