modmisc.mk revision 1.36 1 1.36 rillig # $Id: modmisc.mk,v 1.36 2020/08/08 13:09: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.23 rillig all: mod-subst
20 1.30 rillig all: mod-subst-chain
21 1.23 rillig all: mod-regex
22 1.23 rillig all: mod-loop-varname mod-loop-resolve mod-loop-varname-dollar
23 1.12 rillig all: mod-subst-dollar mod-loop-dollar
24 1.23 rillig all: mod-regex-limits
25 1.23 rillig all: mod-regex-errors
26 1.16 rillig all: mod-assign
27 1.18 rillig all: mod-assign-nested
28 1.17 rillig all: mod-tu-space
29 1.23 rillig all: mod-quote
30 1.22 rillig all: mod-break-many-words
31 1.27 rillig all: mod-remember
32 1.28 rillig all: mod-gmtime
33 1.36 rillig all: mod-gmtime-indirect
34 1.28 rillig all: mod-localtime
35 1.28 rillig all: mod-hash
36 1.28 rillig all: mod-range
37 1.1 apb
38 1.23 rillig # See also sysv.mk.
39 1.1 apb modsysv:
40 1.1 apb @echo "The answer is ${libfoo.a:L:libfoo.a=42}"
41 1.1 apb
42 1.23 rillig # Demonstrates modifiers that are given indirectly from a variable.
43 1.1 apb modvar:
44 1.1 apb @echo "path='${path}'"
45 1.1 apb @echo "path='${path:${MOD_NODOT}}'"
46 1.1 apb @echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
47 1.1 apb @echo "path=${path:${MOD_NODOTX}:ts:}"
48 1.1 apb @echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
49 1.1 apb
50 1.1 apb .for d in ${path:${MOD_SEP}:N.} /usr/xbin
51 1.1 apb path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
52 1.1 apb paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
53 1.1 apb .endfor
54 1.1 apb
55 1.1 apb modvarloop:
56 1.1 apb @echo "path_/usr/xbin=${path_/usr/xbin}"
57 1.1 apb @echo "paths=${paths}"
58 1.1 apb @echo "PATHS=${paths:tu}"
59 1.2 rillig
60 1.6 rillig PATHNAMES= a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
61 1.4 rillig mod-HTE:
62 1.6 rillig @echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
63 1.6 rillig @echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
64 1.6 rillig @echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
65 1.6 rillig @echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
66 1.7 rillig
67 1.7 rillig # When a modifier is applied to the "" variable, the result is discarded.
68 1.7 rillig emptyvar:
69 1.7 rillig @echo S:${:S,^$,empty,}
70 1.7 rillig @echo C:${:C,^$,empty,}
71 1.7 rillig @echo @:${:@var@${var}@}
72 1.7 rillig
73 1.7 rillig # The :U modifier turns even the "" variable into something that has a value.
74 1.7 rillig # The resulting variable is empty, but is still considered to contain a
75 1.7 rillig # single empty word. This word can be accessed by the :S and :C modifiers,
76 1.7 rillig # but not by the :@ modifier since it explicitly skips empty words.
77 1.7 rillig undefvar:
78 1.7 rillig @echo S:${:U:S,^$,empty,}
79 1.7 rillig @echo C:${:U:C,^$,empty,}
80 1.7 rillig @echo @:${:U:@var@empty@}
81 1.8 rillig
82 1.24 rillig WORDS= sequences of letters
83 1.26 rillig .if ${WORDS:S,,,} != ${WORDS}
84 1.26 rillig .warning The empty pattern matches something.
85 1.26 rillig .endif
86 1.25 rillig .if ${WORDS:S,e,*,1} != "s*quences of letters"
87 1.26 rillig .warning The :S modifier flag '1' is not applied exactly once.
88 1.24 rillig .endif
89 1.24 rillig .if ${WORDS:S,e,*,} != "s*quences of l*tters"
90 1.26 rillig .warning The :S modifier does not replace every first match per word.
91 1.24 rillig .endif
92 1.24 rillig .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
93 1.26 rillig .warning The :S modifier flag 'g' does not replace every occurrence.
94 1.24 rillig .endif
95 1.24 rillig .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
96 1.26 rillig .warning The :S modifier fails for a short match anchored at the start.
97 1.24 rillig .endif
98 1.24 rillig .if ${WORDS:S,^of,with,} != "sequences with letters"
99 1.26 rillig .warning The :S modifier fails for an exact match anchored at the start.
100 1.24 rillig .endif
101 1.24 rillig .if ${WORDS:S,^office,does not match,} != ${WORDS}
102 1.26 rillig .warning The :S modifier matches a too long pattern anchored at the start.
103 1.26 rillig .endif
104 1.26 rillig .if ${WORDS:S,f$,r,} != "sequences or letters"
105 1.26 rillig .warning The :S modifier fails for a short match anchored at the end.
106 1.26 rillig .endif
107 1.26 rillig .if ${WORDS:S,s$,,} != "sequence of letter"
108 1.26 rillig .warning The :S modifier fails to replace one occurrence per word.
109 1.26 rillig .endif
110 1.26 rillig .if ${WORDS:S,of$,,} != "sequences letters"
111 1.26 rillig .warning The :S modifier fails for an exact match anchored at the end.
112 1.26 rillig .endif
113 1.26 rillig .if ${WORDS:S,eof$,,} != ${WORDS}
114 1.26 rillig .warning The :S modifier matches a too long pattern anchored at the end.
115 1.26 rillig .endif
116 1.26 rillig .if ${WORDS:S,^of$,,} != "sequences letters"
117 1.26 rillig .warning The :S modifier does not match a word anchored at both ends.
118 1.26 rillig .endif
119 1.26 rillig .if ${WORDS:S,^o$,,} != ${WORDS}
120 1.26 rillig .warning The :S modifier matches a prefix anchored at both ends.
121 1.26 rillig .endif
122 1.26 rillig .if ${WORDS:S,^f$,,} != ${WORDS}
123 1.26 rillig .warning The :S modifier matches a suffix anchored at both ends.
124 1.26 rillig .endif
125 1.26 rillig .if ${WORDS:S,^eof$,,} != ${WORDS}
126 1.26 rillig .warning The :S modifier matches a too long prefix anchored at both ends.
127 1.26 rillig .endif
128 1.26 rillig .if ${WORDS:S,^office$,,} != ${WORDS}
129 1.26 rillig .warning The :S modifier matches a too long suffix anchored at both ends.
130 1.24 rillig .endif
131 1.24 rillig
132 1.23 rillig mod-subst:
133 1.20 rillig @echo $@:
134 1.8 rillig @echo :${:Ua b b c:S,a b,,:Q}:
135 1.8 rillig @echo :${:Ua b b c:S,a b,,1:Q}:
136 1.8 rillig @echo :${:Ua b b c:S,a b,,W:Q}:
137 1.8 rillig @echo :${:Ua b b c:S,b,,g:Q}:
138 1.8 rillig @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
139 1.20 rillig @echo ${:U12345:S,,sep,g:Q}
140 1.8 rillig
141 1.30 rillig # The :S and :C modifiers can be chained without a separating ':'.
142 1.30 rillig # This is not documented in the manual page.
143 1.30 rillig # It works because ApplyModifier_Subst scans for the known modifiers g1W
144 1.30 rillig # and then just returns to ApplyModifiers. There, the colon is optionally
145 1.30 rillig # skipped (see the *st.next == ':' at the end of the loop).
146 1.30 rillig #
147 1.30 rillig # Most other modifiers cannot be chained since their parsers skip until
148 1.30 rillig # the next ':' or '}' or ')'.
149 1.30 rillig mod-subst-chain:
150 1.30 rillig @echo $@:
151 1.30 rillig @echo ${:Ua b c:S,a,A,S,b,B,}.
152 1.35 rillig # There is no 'i' modifier for the :S or :C modifiers.
153 1.35 rillig # The error message is "make: Unknown modifier 'i'", which is
154 1.35 rillig # kind of correct, although it is mixing the terms for variable
155 1.35 rillig # modifiers with the matching modifiers.
156 1.35 rillig @echo ${:Uvalue:S,a,x,i}.
157 1.30 rillig
158 1.23 rillig mod-regex:
159 1.20 rillig @echo $@:
160 1.8 rillig @echo :${:Ua b b c:C,a b,,:Q}:
161 1.8 rillig @echo :${:Ua b b c:C,a b,,1:Q}:
162 1.8 rillig @echo :${:Ua b b c:C,a b,,W:Q}:
163 1.8 rillig @echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
164 1.8 rillig @echo :${:Ua b b c:C,b,,g:Q}:
165 1.8 rillig @echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
166 1.9 rillig
167 1.9 rillig # In the :@ modifier, the name of the loop variable can even be generated
168 1.9 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
169 1.9 rillig # will ever depend on this, but technically it's possible.
170 1.33 rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
171 1.23 rillig mod-loop-varname:
172 1.9 rillig @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
173 1.34 rillig # ":::" is a very creative variable name, unlikely in practice
174 1.34 rillig # The expression ${\:\:\:} would not work since backslashes can only
175 1.34 rillig # be escaped in the modifiers, but not in the variable name.
176 1.34 rillig @echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
177 1.34 rillig # "@@" is another creative variable name.
178 1.34 rillig @echo :${:U1 2 3:@\@\@@x${@@}y@}:
179 1.34 rillig # Even "@" works as a variable name since the variable is installed
180 1.34 rillig # in the "current" scope, which in this case is the one from the
181 1.34 rillig # target.
182 1.34 rillig @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
183 1.34 rillig # In extreme cases, even the backslash can be used as variable name.
184 1.34 rillig # It needs to be doubled though.
185 1.34 rillig @echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
186 1.10 rillig
187 1.10 rillig # The :@ modifier resolves the variables a little more often than expected.
188 1.10 rillig # In particular, it resolves _all_ variables from the context, and not only
189 1.10 rillig # the loop variable (in this case v).
190 1.11 rillig #
191 1.11 rillig # The d means direct reference, the i means indirect reference.
192 1.10 rillig RESOLVE= ${RES1} $${RES1}
193 1.11 rillig RES1= 1d${RES2} 1i$${RES2}
194 1.11 rillig RES2= 2d${RES3} 2i$${RES3}
195 1.11 rillig RES3= 3
196 1.10 rillig
197 1.23 rillig mod-loop-resolve:
198 1.10 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
199 1.12 rillig
200 1.23 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
201 1.23 rillig # or two dollar signs, which were silently ignored.
202 1.23 rillig # There's no point in allowing a dollar sign in that position.
203 1.23 rillig mod-loop-varname-dollar:
204 1.13 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
205 1.13 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
206 1.13 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
207 1.13 rillig
208 1.12 rillig # No matter how many dollar characters there are, they all get merged
209 1.12 rillig # into a single dollar by the :S modifier.
210 1.12 rillig mod-subst-dollar:
211 1.12 rillig @echo $@:${:U1:S,^,$,:Q}:
212 1.12 rillig @echo $@:${:U2:S,^,$$,:Q}:
213 1.12 rillig @echo $@:${:U3:S,^,$$$,:Q}:
214 1.12 rillig @echo $@:${:U4:S,^,$$$$,:Q}:
215 1.12 rillig @echo $@:${:U5:S,^,$$$$$,:Q}:
216 1.12 rillig @echo $@:${:U6:S,^,$$$$$$,:Q}:
217 1.12 rillig @echo $@:${:U7:S,^,$$$$$$$,:Q}:
218 1.12 rillig @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
219 1.12 rillig # This generates no dollar at all:
220 1.12 rillig @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
221 1.12 rillig # Here is an alternative way to generate dollar characters.
222 1.12 rillig # It's unexpectedly complicated though.
223 1.12 rillig @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
224 1.12 rillig
225 1.12 rillig # Demonstrate that it is possible to generate dollar characters using the
226 1.12 rillig # :@ modifier.
227 1.12 rillig #
228 1.12 rillig # These are edge cases that could have resulted in a parse error as well
229 1.12 rillig # since the $@ at the end could have been interpreted as a variable, which
230 1.12 rillig # would mean a missing closing @ delimiter.
231 1.12 rillig mod-loop-dollar:
232 1.12 rillig @echo $@:${:U1:@word@${word}$@:Q}:
233 1.12 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
234 1.12 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
235 1.12 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
236 1.12 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
237 1.12 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
238 1.14 rillig
239 1.23 rillig mod-regex-limits:
240 1.14 rillig @echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
241 1.14 rillig @echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
242 1.14 rillig @echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
243 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
244 1.14 rillig @echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
245 1.14 rillig @echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
246 1.15 rillig # The :C modifier only handles single-digit capturing groups,
247 1.15 rillig # which is more than enough for daily use.
248 1.15 rillig @echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
249 1.16 rillig
250 1.23 rillig mod-regex-errors:
251 1.21 rillig @echo $@: ${UNDEF:Uvalue:C,[,,}
252 1.21 rillig
253 1.16 rillig # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
254 1.16 rillig mod-assign:
255 1.16 rillig @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
256 1.16 rillig @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
257 1.16 rillig @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
258 1.16 rillig @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
259 1.16 rillig # The assignments happen in the global scope and thus are
260 1.16 rillig # preserved even after the shell command has been run.
261 1.16 rillig @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
262 1.17 rillig
263 1.18 rillig mod-assign-nested:
264 1.18 rillig @echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
265 1.18 rillig @echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
266 1.18 rillig @echo $@: ${SINK3:Q}
267 1.18 rillig @echo $@: ${SINK4:Q}
268 1.18 rillig SINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
269 1.18 rillig SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
270 1.18 rillig
271 1.17 rillig mod-tu-space:
272 1.17 rillig # The :tu and :tl modifiers operate on the variable value
273 1.17 rillig # as a single string, not as a list of words. Therefore,
274 1.17 rillig # the adjacent spaces are preserved.
275 1.17 rillig @echo $@: ${a b:L:tu:Q}
276 1.19 rillig
277 1.23 rillig mod-quote:
278 1.19 rillig @echo $@: new${.newline:Q}${.newline:Q}line
279 1.22 rillig
280 1.22 rillig # Cover the bmake_realloc in brk_string.
281 1.22 rillig mod-break-many-words:
282 1.22 rillig @echo $@: ${UNDEF:U:range=500:[#]}
283 1.27 rillig
284 1.27 rillig # Demonstrate the :_ modifier.
285 1.27 rillig # In the parameterized form, having the variable name on the right side
286 1.27 rillig # of the = assignment operator is confusing. Luckily this modifier is
287 1.27 rillig # only rarely needed.
288 1.27 rillig mod-remember:
289 1.27 rillig @echo $@: ${1 2 3:L:_:@var@${_}@}
290 1.27 rillig @echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
291 1.28 rillig
292 1.28 rillig mod-gmtime:
293 1.28 rillig @echo $@:
294 1.28 rillig @echo ${%Y:L:gmtim=1593536400} # modifier name too short
295 1.28 rillig @echo ${%Y:L:gmtime=1593536400} # 2020-07-01T00:00:00Z
296 1.28 rillig @echo ${%Y:L:gmtimer=1593536400} # modifier name too long
297 1.29 rillig @echo ${%Y:L:gm=gm:M*}
298 1.28 rillig
299 1.36 rillig mod-gmtime-indirect:
300 1.36 rillig @echo $@:
301 1.36 rillig # It's not possible to pass the seconds via a variable expression.
302 1.36 rillig # Parsing of the :gmtime modifier stops at the '$' and returns to
303 1.36 rillig # ApplyModifiers. There, a colon would be skipped but not a dollar.
304 1.36 rillig # Parsing continues by looking at the next modifier. Now the ${:U}
305 1.36 rillig # is expanded and interpreted as a variable modifier, which results
306 1.36 rillig # in the error message "Unknown modifier '1'".
307 1.36 rillig @echo ${%Y:L:gmtime=${:U1593536400}}
308 1.36 rillig
309 1.28 rillig mod-localtime:
310 1.28 rillig @echo $@:
311 1.28 rillig @echo ${%Y:L:localtim=1593536400} # modifier name too short
312 1.28 rillig @echo ${%Y:L:localtime=1593536400} # 2020-07-01T00:00:00Z
313 1.28 rillig @echo ${%Y:L:localtimer=1593536400} # modifier name too long
314 1.28 rillig
315 1.28 rillig mod-hash:
316 1.28 rillig @echo $@:
317 1.28 rillig @echo ${12345:L:has} # modifier name too short
318 1.28 rillig @echo ${12345:L:hash} # ok
319 1.28 rillig @echo ${12345:L:hash=SHA-256} # :hash does not accept '='
320 1.28 rillig @echo ${12345:L:hasX} # misspelled
321 1.28 rillig @echo ${12345:L:hashed} # modifier name too long
322 1.28 rillig
323 1.28 rillig mod-range:
324 1.28 rillig @echo $@:
325 1.28 rillig @echo ${a b c:L:rang} # modifier name too short
326 1.28 rillig @echo ${a b c:L:range} # ok
327 1.28 rillig @echo ${a b c:L:rango} # misspelled
328 1.28 rillig @echo ${a b c:L:ranger} # modifier name too long
329 1.31 rillig
330 1.31 rillig # To apply a modifier indirectly via another variable, the whole
331 1.31 rillig # modifier must be put into a single variable.
332 1.31 rillig .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
333 1.31 rillig .warning unexpected
334 1.31 rillig .endif
335 1.31 rillig
336 1.31 rillig # Adding another level of indirection (the 2 nested :U expressions) helps.
337 1.31 rillig .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
338 1.31 rillig .warning unexpected
339 1.31 rillig .endif
340 1.31 rillig
341 1.31 rillig # Multiple indirect modifiers can be applied one after another as long as
342 1.31 rillig # they are separated with colons.
343 1.31 rillig .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
344 1.31 rillig .warning unexpected
345 1.31 rillig .endif
346 1.32 rillig
347 1.32 rillig # An indirect variable that evaluates to the empty string is allowed though.
348 1.32 rillig # This makes it possible to define conditional modifiers, like this:
349 1.32 rillig #
350 1.32 rillig # M.little-endian= S,1234,4321,
351 1.32 rillig # M.big-endian= # none
352 1.32 rillig .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
353 1.32 rillig .warning unexpected
354 1.32 rillig .endif
355