-
Notifications
You must be signed in to change notification settings - Fork 22
/
Makefile.help
146 lines (131 loc) · 3.97 KB
/
Makefile.help
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Makefile.help
#
# This file defines several functions that make the help output from make
# a lot more useable for our end-users.
#
# Usage:
#
# include Makefile.help
#
# .DEFAULT_GOAL := help
# .PHONY: help
# .SILENT: help
# help::
# $(call print_help_usage)
# echo
# echo "This is some text"
# echo
# $(call print_help_section, "Available targets")
# $(call print_help_target, help, "to show this help text", "")
#
# Usage: $(call assert_numeric_bool, VARIABLE-NAME)
define assert_numeric_bool
$(if $(filter-out 0 1,$($1)), $(error Unexpected value for parameter $1, expect either 0 or 1))
endef
# Usage: $(call uniq, $(LIST-OF-WORDS))
define uniq
$(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
endef
define unquote
$(patsubst "%,%,$(patsubst %",%,$(1)))
endef
# Black 0;30 Dark Gray 1;30
# Red 0;31 Light Red 1;31
# Green 0;32 Light Green 1;32
# Brown/Orange 0;33 Yellow 1;33
# Blue 0;34 Light Blue 1;34
# Purple 0;35 Light Purple 1;35
# Cyan 0;36 Light Cyan 1;36
# Light Gray 0;37 White 1;37
_blk := \e[30m
_red := \e[31m
_grn := \e[32m
_yel := \e[33m
_blu := \e[34m
_pur := \e[35m
_cyn := \e[36m
_whi := \e[37m
_rst := \e[0m
_bld := \e[1m
_dim := \e[2m
# Usage: $(call print_header, SOME-TEXT)
define print_header
@printf "________________________________________"
@printf "________________________________________\n"
@printf ":: %s\n" $(1)
endef
# Usage: $(call print_help_section, SECTION-TITLE)
define print_help_section
@printf "%s:\n" $(1)
endef
# Usage: $(call print_help_subsection, SUBSECTION-TITLE)
define print_help_subsection
@printf " %s:\n" $(1)
endef
# Usage: $(call print_help_target, TARGET-NAME, HELP-DESCRIPTION, HELP-CATEGORY)
define print_help_target
@printf " $(_blu)% -21s$(_rst) % -45b $(_dim)%s$(_rst)\n" $(1) $(2) $(3)
endef
# Usage: $(call print_help_option, VAR-NAME, VAR-DEFAULT, DESCRIPTION)
define print_help_option
@printf " % -36s %b\n" $(shell printf "$(_grn)%s$(_rst)=$(_dim)%s$(_rst)" $(1) $(2)) $(3)
endef
# Usage: $(call print_help_define, VAR-NAME, $(VAR-NAME))
define print_help_define
@printf " $(_grn)%s$(_rst)=$(_dim)%s$(_rst)\n" $(1) $(2)
endef
define print_help_define_align
@printf " $(_grn)%-16s$(_rst)=$(_dim) %s$(_rst)\n" $(1) $(2)
endef
# Usage: $(call print_help_define_lines, VAR-NAME, $(VAR-NAME))
#
# For example, given a call with the following:
#
# $(call print_help_define_lines, FROM_VERSIONS, 18.04 20.04 22.04)
#
# we will get the following output:
#
# FROM_VERSIONS=\
# 18.04
# 20.04
# 22.04
#
# If the contents of $(VAR-NAME) is empty or only contains whitespace,
# then no extra newline is printed, and it behaves as print_help_define.
define print_help_define_lines
@printf " $(_grn)%s$(_rst)=$(_dim)" $(1)
@if [ -n "$(strip $(2))" ]; then printf "\\ \n"; fi
@printf "%s" "$$(echo " $(2)" | fmt -5)"
@printf "$(_rst)\n"
endef
# Usage: $(call print_help_args_lines, VAR-NAME, $(VAR-NAME))
#
# This function behaves as print_help_define_lines, except that
# it doesn't split on words, but on arguments. Each argument (leading with -)
# is prepended with a newline.
#
# For example, given a call with the following:
#
# $(call print_help_define_lines, DOCKER_ARGS, -it --rm --network host)
#
# we will get the following output:
#
# DOCKER_ARGS=\
# -it
# --rm
# --network host
#
# If the contents of $(VAR-NAME) is empty, same behavior as print_help_define.
define print_help_args_lines
@printf " $(_grn)%s$(_rst)=$(_dim)" $(1)
@if [ -n "$(strip $(2))" ]; then printf "\\ \n"; fi
@printf "%s" "$$(echo " $(2)" | sed -r -e 's/\W--?[^-]/\n \0/g' -e 's/^\s*\n//')"
@printf "$(_rst)\n"
endef
# Usage: $(call print_help_usage)
#
# This should be called only once, before all other print_help_* calls,
# so that the help message starts with a usage statement.
define print_help_usage
@printf "Usage: make $(_blu)target$(_rst)\n"
endef