blob: b1432301dea92b9b9154510edc8356d4d8285769 [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
Dave Rodgman4c4f1f12024-03-07 16:50:33 +000025# Function to normalise paths
26get_real_filename() {
27 leafname="$(basename "$1")"
28 ( cd $(dirname "$1"); echo "$(pwd)/$leafname" )
29}
30
31# Normalise path to wrapper script
32WRAPPER_WITH_PATH=$(get_real_filename "$0")
33# Identify original tool (compare normalised path to WRAPPER_WITH_PATH to avoid recursively calling the same wrapper script)
34ORIGINAL_TOOL="$(for p in $(type -ap "$TOOL"); do get_real_filename "$p" ; done | grep -v -Fx "$WRAPPER_WITH_PATH" | head -n1)"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000035
36print_quoted_args() {
37 # similar to printf '%q' "$@"
38 # but produce more human-readable results for common/simple cases like "a b"
39 for a in "$@"; do
Dave Rodgmandbc2e8d2024-02-26 17:29:31 +000040 # Get bash to quote the string
Dave Rodgmana3e694c2024-02-29 14:06:49 +000041 printf -v q '%q' "$a"
Dave Rodgmandbc2e8d2024-02-26 17:29:31 +000042 simple_pattern="^([-[:alnum:]_+./:@]+=)?([^']*)$"
43 if [[ "$a" != "$q" && $a =~ $simple_pattern ]]; then
44 # a requires some quoting (a != q), but has no single quotes, so we can
45 # simplify the quoted form - e.g.:
46 # a b -> 'a b'
47 # CFLAGS=a b -> CFLAGS='a b'
48 q="${BASH_REMATCH[1]}'${BASH_REMATCH[2]}'"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000049 fi
Dave Rodgman67126bb2024-02-26 17:30:37 +000050 printf " %s" "$q"
Dave Rodgman98a79cd2024-02-26 12:37:44 +000051 done
52}
53
54if [[ ! " $* " =~ " --version " ]]; then
55 # Display the command being invoked - if it succeeds, this is all that will
56 # be displayed. Don't do this for invocations with --version, because
57 # this output is often parsed by scripts, so we don't want to modify it.
Dave Rodgman2f947662024-02-26 17:30:56 +000058 printf %s "${TOOL}" 1>&2
59 print_quoted_args "$@" 1>&2
60 echo 1>&2
Dave Rodgman98a79cd2024-02-26 12:37:44 +000061fi
62
63if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
64 # Run original command with no output supression
65 exec "${ORIGINAL_TOOL}" "$@"
66else
67 # Run original command and capture output & exit status
68 TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX")
69 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1
70 EXIT_STATUS=$?
71
72 if [[ $EXIT_STATUS -ne 0 ]]; then
73 # On error, display the full output
74 cat "${TMPFILE}"
75 fi
76
77 # Remove tmpfile
78 rm "${TMPFILE}"
79
80 # Propagate the exit status
81 exit $EXIT_STATUS
82fi