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