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