5

How to detect if the makefile --silent / --quiet command line options was set?

Related questions:

  1. how to detect if --quiet option is specified with rake
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

3 Answers3

6

I think you need:

$(findstring s,$(word 1, $(MAKEFLAGS)))

Because MAKEFLAGS has long options, too, eg:

MAKEFLAGS=s -j80 --jobserver-auth=4,6

So, IOW:

 # Set SILENT to 's' if --quiet/-s set, otherwise ''.
 SILENT := $(findstring s,$(word 1, $(MAKEFLAGS)))
Rusty Russell
  • 76
  • 1
  • 4
2

If you call either make --quiet or --silent, the variable {MAKEFLAGS} is set only to s. And if you add other options like --ignore-errors and --keep-going, the variable {MAKEFLAGS} is set to iks. Then, you can capture it with this:

ECHOCMD:=/bin/echo -e
SHELL := /bin/bash

all:
    printf 'Calling with "%s" %s\n' "${MAKECMDGOALS}" "${MAKEFLAGS}";

    if [[ "ws" == "w$(findstring s,${MAKEFLAGS})" ]]; then \
        printf '--silent option was set\n'; \
    fi

References:

  1. Recursive make: correct way to insert `$(MAKEFLAGS)`
  2. How to set MAKEFLAGS from Makefile, in order to remove default implicit-rules
  3. Remove target from MAKECMDGOALS?
  4. https://www.gnu.org/software/autoconf/manual/autoconf-2.61/html_node/The-Make-Macro-MAKEFLAGS.html
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
1
make            gives  MAKEFLAGS = ""
make -j8        gives  MAKEFLAGS = " -j8 --jobserver-auth=3,4"
make -j8 -s     gives  MAKEFLAGS = "s -j8 --jobserver-auth=3,4"
make -j8 -s -B  gives  MAKEFLAGS = "Bs -j8 --jobserver-auth=3,4"

So this should do the trick:

$(findstring s,$(filter-out -%,$(MAKEFLAGS)))

It returns "s" or an empty string, which is suitable as the condition for an $(if ...).