modmisc.mk revision 1.43 1 1.43 rillig # $Id: modmisc.mk,v 1.43 2020/08/16 12:48:55 rillig Exp $
2 1.1 apb #
3 1.1 apb # miscellaneous modifier tests
4 1.1 apb
5 1.1 apb # do not put any dirs in this list which exist on some
6 1.1 apb # but not all target systems - an exists() check is below.
7 1.1 apb path=:/bin:/tmp::/:.:/no/such/dir:.
8 1.1 apb # strip cwd from path.
9 1.1 apb MOD_NODOT=S/:/ /g:N.:ts:
10 1.1 apb # and decorate, note that $'s need to be doubled. Also note that
11 1.1 apb # the modifier_variable can be used with other modifiers.
12 1.1 apb MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
13 1.1 apb # another mod - pretend it is more interesting
14 1.1 apb MOD_HOMES=S,/home/,/homes/,
15 1.1 apb MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
16 1.1 apb MOD_SEP=S,:, ,g
17 1.1 apb
18 1.7 rillig all: modvar modvarloop modsysv mod-HTE emptyvar undefvar
19 1.17 rillig all: mod-tu-space
20 1.23 rillig all: mod-quote
21 1.22 rillig all: mod-break-many-words
22 1.27 rillig all: mod-remember
23 1.28 rillig all: mod-localtime
24 1.28 rillig all: mod-hash
25 1.28 rillig all: mod-range
26 1.1 apb
27 1.23 rillig # See also sysv.mk.
28 1.1 apb modsysv:
29 1.1 apb @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
30 1.1 apb
31 1.23 rillig # Demonstrates modifiers that are given indirectly from a variable.
32 1.1 apb modvar:
33 1.1 apb @echo "path='${path}'"
34 1.1 apb @echo "path='${path:${MOD_NODOT}}'"
35 1.1 apb @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
36 1.1 apb @echo "path=${path:${MOD_NODOTX}:ts:}"
37 1.1 apb @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
38 1.1 apb
39 1.1 apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
40 1.1 apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
41 1.1 apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
42 1.1 apb .endfor
43 1.1 apb
44 1.1 apb modvarloop:
45 1.1 apb @echo "path_/usr/xbin=${path_/usr/xbin}"
46 1.1 apb @echo "paths=${paths}"
47 1.1 apb @echo "PATHS=${paths:tu}"
48 1.2 rillig
49 1.6 rillig PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
50 1.4 rillig mod-HTE:
51 1.6 rillig @echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
52 1.6 rillig @echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
53 1.6 rillig @echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
54 1.6 rillig @echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
55 1.7 rillig
56 1.7 rillig # When a modifier is applied to the "" variable, the result is discarded.
57 1.7 rillig emptyvar:
58 1.7 rillig @echo S:${:S,^$,empty,}
59 1.7 rillig @echo C:${:C,^$,empty,}
60 1.7 rillig @echo @:${:@var@${var}@}
61 1.7 rillig
62 1.7 rillig # The :U modifier turns even the "" variable into something that has a value.
63 1.7 rillig # The resulting variable is empty, but is still considered to contain a
64 1.7 rillig # single empty word. This word can be accessed by the :S and :C modifiers,
65 1.7 rillig # but not by the :@ modifier since it explicitly skips empty words.
66 1.7 rillig undefvar:
67 1.7 rillig @echo S:${:U:S,^$,empty,}
68 1.7 rillig @echo C:${:U:C,^$,empty,}
69 1.7 rillig @echo @:${:U:@var@empty@}
70 1.8 rillig
71 1.17 rillig mod-tu-space:
72 1.17 rillig # The :tu and :tl modifiers operate on the variable value
73 1.17 rillig # as a single string, not as a list of words. Therefore,
74 1.17 rillig # the adjacent spaces are preserved.
75 1.17 rillig @echo $@: ${a b:L:tu:Q}
76 1.19 rillig
77 1.23 rillig mod-quote:
78 1.19 rillig @echo $@: new${.newline:Q}${.newline:Q}line
79 1.22 rillig
80 1.22 rillig # Cover the bmake_realloc in brk_string.
81 1.22 rillig mod-break-many-words:
82 1.22 rillig @echo $@: ${UNDEF:U:range=500:[#]}
83 1.27 rillig
84 1.27 rillig # Demonstrate the :_ modifier.
85 1.27 rillig # In the parameterized form, having the variable name on the right side
86 1.27 rillig # of the = assignment operator is confusing. Luckily this modifier is
87 1.27 rillig # only rarely needed.
88 1.27 rillig mod-remember:
89 1.27 rillig @echo $@: ${1 2 3:L:_:@var@${_}@}
90 1.27 rillig @echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
91 1.28 rillig
92 1.28 rillig mod-localtime:
93 1.28 rillig @echo $@:
94 1.28 rillig @echo ${%Y:L:localtim=1593536400} # modifier name too short
95 1.28 rillig @echo ${%Y:L:localtime=1593536400} # 2020-07-01T00:00:00Z
96 1.28 rillig @echo ${%Y:L:localtimer=1593536400} # modifier name too long
97 1.28 rillig
98 1.28 rillig mod-hash:
99 1.28 rillig @echo $@:
100 1.28 rillig @echo ${12345:L:has} # modifier name too short
101 1.28 rillig @echo ${12345:L:hash} # ok
102 1.28 rillig @echo ${12345:L:hash=SHA-256} # :hash does not accept '='
103 1.28 rillig @echo ${12345:L:hasX} # misspelled
104 1.28 rillig @echo ${12345:L:hashed} # modifier name too long
105 1.28 rillig
106 1.28 rillig mod-range:
107 1.28 rillig @echo $@:
108 1.28 rillig @echo ${a b c:L:rang} # modifier name too short
109 1.28 rillig @echo ${a b c:L:range} # ok
110 1.28 rillig @echo ${a b c:L:rango} # misspelled
111 1.28 rillig @echo ${a b c:L:ranger} # modifier name too long
112 1.31 rillig
113 1.31 rillig # To apply a modifier indirectly via another variable, the whole
114 1.31 rillig # modifier must be put into a single variable.
115 1.31 rillig .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
116 1.31 rillig .warning unexpected
117 1.31 rillig .endif
118 1.31 rillig
119 1.31 rillig # Adding another level of indirection (the 2 nested :U expressions) helps.
120 1.31 rillig .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
121 1.31 rillig .warning unexpected
122 1.31 rillig .endif
123 1.31 rillig
124 1.31 rillig # Multiple indirect modifiers can be applied one after another as long as
125 1.31 rillig # they are separated with colons.
126 1.31 rillig .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
127 1.31 rillig .warning unexpected
128 1.31 rillig .endif
129 1.32 rillig
130 1.32 rillig # An indirect variable that evaluates to the empty string is allowed though.
131 1.32 rillig # This makes it possible to define conditional modifiers, like this:
132 1.32 rillig #
133 1.32 rillig # M.little-endian= S,1234,4321,
134 1.32 rillig # M.big-endian= # none
135 1.32 rillig .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
136 1.32 rillig .warning unexpected
137 1.32 rillig .endif
138 1.40 rillig
139