1# -*-mode: sh; sh-shell: bash -*- 2# 3# 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# 12# VERBOSE_LOGS=1 13# 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 22# be silenced, e.g. " --version | test ". In this example, "make lib test" will 23# not be silent, but "make lib" will be. 24 25# Identify path to original tool. There is an edge-case here where the quiet wrapper is on the path via 26# a symlink or relative path, but "type -ap" yields the wrapper with it's normalised path. We use 27# the -ef operator to compare paths, to avoid picking the wrapper in this case (to avoid infinitely 28# recursing). 29while IFS= read -r ORIGINAL_TOOL; do 30 if ! [[ $ORIGINAL_TOOL -ef "$0" ]]; then break; fi 31done < <(type -ap -- "$TOOL") 32 33print_quoted_args() { 34 # similar to printf '%q' "$@" 35 # but produce more human-readable results for common/simple cases like "a b" 36 for a in "$@"; do 37 # Get bash to quote the string 38 printf -v q '%q' "$a" 39 simple_pattern="^([-[:alnum:]_+./:@]+=)?([^']*)$" 40 if [[ "$a" != "$q" && $a =~ $simple_pattern ]]; then 41 # a requires some quoting (a != q), but has no single quotes, so we can 42 # simplify the quoted form - e.g.: 43 # a b -> 'a b' 44 # CFLAGS=a b -> CFLAGS='a b' 45 q="${BASH_REMATCH[1]}'${BASH_REMATCH[2]}'" 46 fi 47 printf " %s" "$q" 48 done 49} 50 51if [[ ! " $* " =~ " --version " ]]; then 52 # Display the command being invoked - if it succeeds, this is all that will 53 # be displayed. Don't do this for invocations with --version, because 54 # this output is often parsed by scripts, so we don't want to modify it. 55 printf %s "${TOOL}" 1>&2 56 print_quoted_args "$@" 1>&2 57 echo 1>&2 58fi 59 60if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then 61 # Run original command with no output supression 62 exec "${ORIGINAL_TOOL}" "$@" 63else 64 # Run original command and capture output & exit status 65 TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX") 66 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1 67 EXIT_STATUS=$? 68 69 if [[ $EXIT_STATUS -ne 0 ]]; then 70 # On error, display the full output 71 cat "${TMPFILE}" 72 fi 73 74 # Remove tmpfile 75 rm "${TMPFILE}" 76 77 # Propagate the exit status 78 exit $EXIT_STATUS 79fi 80