-
Notifications
You must be signed in to change notification settings - Fork 0
/
rksh
executable file
·59 lines (52 loc) · 2.15 KB
/
rksh
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
#!/bin/bash
set -Eeo pipefail
# Description:
# Wraps passed make targets to print timing traces to stderr (if enabled).
#
# Usage:
# MAKE_TRACE_TIME=true make <target>
#
# Requirements:
# This script must act as shell for make, thus must be configured via Makefile args (bottom of our Makefile):
# SHELL = /app/rksh
# .SHELLFLAGS = $@
#
# Naming:
# This script was named "rksh" to convince "make" that we are still running a
# POSIX compliant shell in .ONESHELL mode. This is a hack to work around the fact
# that .ONESHELL mode only applies its "special escape handling" when it assumes
# POSIX shell mode, thus properly excapes "@-+" from the targets, which will not
# be the case when naming this file differently.
#
# See:
# https://www.gnu.org/software/make/manual/make.html#One-Shell
# https://git.savannah.gnu.org/cgit/make.git/tree/src/job.c?h=4.4.1#n433 (is_bourne_compatible_shell)
#
# Passed args:
# >&2 echo $1 # name of the make target
# >&2 echo $2 # body of the make target
# >&2 echo "make $1: /bin/bash -cEeuo pipefail $2" # debug: combined
# colors:
RED='\033[0;31m' # errors
GREY='\033[0;90m' # trace start
GREEN='\033[0;32m' # trace end
NC='\033[0m' # No Color
function color_reset {
>&2 printf "${NC}" # reset color
}
trap color_reset EXIT
if [ "$MAKE_TRACE_TIME" = true ] ; then
# Ensure to write additional information to stderr as make targets stdout may be piped
>&2 echo -e "${GREY}$(date -u +"%Y-%m-%dT%H:%M:%SZ") l${MAKELEVEL:-"*"}s${SHLVL:-"*"} RUN 'make $1' ${NC}"
TIMEFORMAT="%Rsec"
time {
# TIMEFORMAT gets appended and printed to stderr
(/bin/bash -cEeuo pipefail "$2" \
&& (ret=$?; >&2 printf "${GREEN}$(date -u +"%Y-%m-%dT%H:%M:%SZ") l${MAKELEVEL:-"*"}s${SHLVL:-"*"} END 'make $1' exit ${ret} in ")) \
|| (ret=$?; >&2 printf "${RED}$(date -u +"%Y-%m-%dT%H:%M:%SZ") l${MAKELEVEL:-"*"}s${SHLVL:-"*"} ERR 'make $1' exit ${ret} in " && exit "${ret}")
}
else
# Just run the make target as usual while highlighting errors
(/bin/bash -cEeuo pipefail "$2") \
|| (ret=$?; >&2 echo -e "${RED}l${MAKELEVEL:-"*"}s${SHLVL:-"*"} ERR 'make $1' exit ${ret}${NC}" && exit "${ret}")
fi