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