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