blob: 30ee569a225f14a16306b3c1128e11ad9cecef07 [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):
Dave Rodgman79aaaa42024-02-29 18:41:36 +000011#
Dave Rodgman98a79cd2024-02-26 12:37:44 +000012# VERBOSE_LOGS=1
Dave Rodgmand0e38272024-02-26 17:28:56 +000013#
14# This script provides most of the functionality for the adjacent make and cmake
15# wrappers.
16#
17# It requires two variables to be set:
18#
19# TOOL - the name of the tool that is being wrapped (with no path), e.g. "make"
20#
21# NO_SILENCE - a regex that describes the commandline arguments for which output will not
Dave Rodgman5f7862a2024-02-29 14:14:37 +000022# be silenced, e.g. " --version | test ". In this example, "make lib test" will
23# not be silent, but "make lib" will be.
Dave Rodgman98a79cd2024-02-26 12:37:44 +000024
25# Locate original tool
Dave Rodgmane03088b2024-02-26 12:48:49 +000026TOOL_WITH_PATH=$(dirname "$0")/$TOOL
27ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$TOOL_WITH_PATH" | head -n1)
Dave Rodgman98a79cd2024-02-26 12:37:44 +000028
29print_quoted_args() {
30 # similar to printf '%q' "$@"
31 # but produce more human-readable results for common/simple cases like "a b"
32 for a in "$@"; do
Dave Rodgmandbc2e8d2024-02-26 17:29:31 +000033 # Get bash to quote the string
Dave Rodgmana3e694c2024-02-29 14:06:49 +000034 printf -v q '%q' "$a"
Dave Rodgmandbc2e8d2024-02-26 17:29:31 +000035 simple_pattern="^([-[:alnum:]_+./:@]+=)?([^']*)$"
36 if [[ "$a" != "$q" && $a =~ $simple_pattern ]]; then
37 # a requires some quoting (a != q), but has no single quotes, so we can
38 # simplify the quoted form - e.g.:
39 # a b -> 'a b'
40 # CFLAGS=a b -> CFLAGS='a b'
41 q="${BASH_REMATCH[1]}'${BASH_REMATCH[2]}'"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000042 fi
Dave Rodgman67126bb2024-02-26 17:30:37 +000043 printf " %s" "$q"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000044 done
45}
46
47if [[ ! " $* " =~ " --version " ]]; then
48 # Display the command being invoked - if it succeeds, this is all that will
49 # be displayed. Don't do this for invocations with --version, because
50 # this output is often parsed by scripts, so we don't want to modify it.
Dave Rodgman2f947662024-02-26 17:30:56 +000051 printf %s "${TOOL}" 1>&2
52 print_quoted_args "$@" 1>&2
53 echo 1>&2
Dave Rodgman98a79cd2024-02-26 12:37:44 +000054fi
55
56if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
57 # Run original command with no output supression
58 exec "${ORIGINAL_TOOL}" "$@"
59else
60 # Run original command and capture output & exit status
61 TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX")
62 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1
63 EXIT_STATUS=$?
64
65 if [[ $EXIT_STATUS -ne 0 ]]; then
66 # On error, display the full output
67 cat "${TMPFILE}"
68 fi
69
70 # Remove tmpfile
71 rm "${TMPFILE}"
72
73 # Propagate the exit status
74 exit $EXIT_STATUS
75fi