verbose.mk: fallback to standard file descriptors
[openwrt/openwrt.git] / include / verbose.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 ifndef OPENWRT_VERBOSE
6 OPENWRT_VERBOSE:=
7 endif
8 ifeq ("$(origin V)", "command line")
9 OPENWRT_VERBOSE:=$(V)
10 endif
11
12 ifeq ($(OPENWRT_VERBOSE),1)
13 OPENWRT_VERBOSE:=w
14 endif
15 ifeq ($(OPENWRT_VERBOSE),99)
16 OPENWRT_VERBOSE:=s
17 endif
18
19 ifeq ($(NO_TRACE_MAKE),)
20 NO_TRACE_MAKE := $(MAKE) V=s$(OPENWRT_VERBOSE)
21 export NO_TRACE_MAKE
22 endif
23
24 ifeq ($(IS_TTY),1)
25 ifneq ($(strip $(NO_COLOR)),1)
26 _Y:=\\033[33m
27 _R:=\\033[31m
28 _N:=\\033[m
29 endif
30 endif
31
32 define ERROR_MESSAGE
33 { \
34 printf "$(_R)%s$(_N)\n" "$(1)" >&9 || \
35 printf "$(_R)%s$(_N)\n" "$(1)"; \
36 } >&2 2>/dev/null
37 endef
38
39 ifeq ($(findstring s,$(OPENWRT_VERBOSE)),)
40 define MESSAGE
41 { \
42 printf "$(_Y)%s$(_N)\n" "$(1)" >&8 || \
43 printf "$(_Y)%s$(_N)\n" "$(1)"; \
44 } 2>/dev/null
45 endef
46
47 ifeq ($(QUIET),1)
48 ifneq ($(CURDIR),$(TOPDIR))
49 _DIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
50 else
51 _DIR:=
52 endif
53 _MESSAGE:=$(if $(MAKECMDGOALS),$(shell \
54 $(call MESSAGE, make[$(MAKELEVEL)]$(if $(_DIR), -C $(_DIR)) $(MAKECMDGOALS)); \
55 ))
56 ifneq ($(strip $(_MESSAGE)),)
57 $(info $(_MESSAGE))
58 endif
59 SUBMAKE=$(MAKE)
60 else
61 SILENT:=>/dev/null $(if $(findstring w,$(OPENWRT_VERBOSE)),,2>&1)
62 export QUIET:=1
63 SUBMAKE=cmd() { $(SILENT) $(MAKE) -s "$$@" < /dev/null || { echo "make $$*: build failed. Please re-run make with -j1 V=s or V=sc for a higher verbosity level to see what's going on"; false; } } 8>&1 9>&2; cmd
64 endif
65
66 .SILENT: $(MAKECMDGOALS)
67 else
68 SUBMAKE=$(MAKE) -w
69 define MESSAGE
70 printf "%s\n" "$(1)"
71 endef
72 endif