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