summaryrefslogtreecommitdiffstats
path: root/build/msg.sh
blob: a7da31b398ca4f07e8312b71c011e00a96bc3353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh

# Copyright © Tavian Barnes <tavianator@tavianator.com>
# SPDX-License-Identifier: 0BSD

# Print a message from a makefile:
#
#     $ make -s
#     $ make
#     [ CC ] src/main.c
#     $ make V=1
#     cc -Isrc -Igen -D...

set -eu

# Get the $MAKEFLAGS from the top-level make invocation
MFLAGS="${XMAKEFLAGS-${MAKEFLAGS-}}"

# Check if make should be quiet (make -s)
is_quiet() {
    # GNU make puts single-letter flags in the first word of $MAKEFLAGS,
    # without a leading dash
    case "${MFLAGS%% *}" in
        -*) : ;;
        *s*) return 0 ;;
    esac

    # BSD make puts each flag separately like -r -s -j 48
    for flag in $MFLAGS; do
        case "$flag" in
            # Ignore things like --jobserver-auth
            --*) continue ;;
            # Skip variable assignments
            *=*) break ;;
            -*s*) return 0 ;;
        esac
    done

    return 1
}

# Check if make should be loud (make V=1)
is_loud() {
    test "$XV"
}

MSG="$1"
shift

if ! is_quiet && ! is_loud; then
    printf '%s\n' "$MSG"
fi

if [ $# -eq 0 ]; then
    exit
fi

if is_loud; then
    printf '%s\n' "$*"
fi

"$@"