varmod-loop.mk revision 1.3 1 1.3 rillig # $NetBSD: varmod-loop.mk,v 1.3 2020/09/13 07:32:32 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :@var (a] ...${var}...@ variable modifier.
4 1.1 rillig
5 1.2 rillig all: mod-loop-varname
6 1.2 rillig all: mod-loop-resolve
7 1.2 rillig all: mod-loop-varname-dollar
8 1.2 rillig all: mod-loop-dollar
9 1.1 rillig
10 1.2 rillig # In the :@ modifier, the name of the loop variable can even be generated
11 1.2 rillig # dynamically. There's no practical use-case for this, and hopefully nobody
12 1.2 rillig # will ever depend on this, but technically it's possible.
13 1.2 rillig # Therefore, in -dL mode, this is forbidden, see lint.mk.
14 1.2 rillig mod-loop-varname:
15 1.2 rillig @echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
16 1.2 rillig # ":::" is a very creative variable name, unlikely in practice
17 1.2 rillig # The expression ${\:\:\:} would not work since backslashes can only
18 1.2 rillig # be escaped in the modifiers, but not in the variable name.
19 1.2 rillig @echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
20 1.2 rillig # "@@" is another creative variable name.
21 1.2 rillig @echo :${:U1 2 3:@\@\@@x${@@}y@}:
22 1.2 rillig # Even "@" works as a variable name since the variable is installed
23 1.2 rillig # in the "current" scope, which in this case is the one from the
24 1.2 rillig # target.
25 1.2 rillig @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
26 1.2 rillig # In extreme cases, even the backslash can be used as variable name.
27 1.2 rillig # It needs to be doubled though.
28 1.2 rillig @echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
29 1.2 rillig
30 1.3 rillig # The variable name can technically be empty, and in this situation
31 1.3 rillig # the variable value cannot be accessed since the empty variable is
32 1.3 rillig # protected to always return an empty string.
33 1.3 rillig @echo empty: :${:U1 2 3:@@x${}y@}:
34 1.3 rillig
35 1.2 rillig # The :@ modifier resolves the variables a little more often than expected.
36 1.2 rillig # In particular, it resolves _all_ variables from the context, and not only
37 1.2 rillig # the loop variable (in this case v).
38 1.2 rillig #
39 1.2 rillig # The d means direct reference, the i means indirect reference.
40 1.2 rillig RESOLVE= ${RES1} $${RES1}
41 1.2 rillig RES1= 1d${RES2} 1i$${RES2}
42 1.2 rillig RES2= 2d${RES3} 2i$${RES3}
43 1.2 rillig RES3= 3
44 1.2 rillig
45 1.2 rillig mod-loop-resolve:
46 1.2 rillig @echo $@:${RESOLVE:@v@w${v}w@:Q}:
47 1.2 rillig
48 1.2 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
49 1.2 rillig # or two dollar signs, which were silently ignored.
50 1.2 rillig # There's no point in allowing a dollar sign in that position.
51 1.2 rillig mod-loop-varname-dollar:
52 1.2 rillig @echo $@:${1 2 3:L:@v$@($v)@:Q}.
53 1.2 rillig @echo $@:${1 2 3:L:@v$$@($v)@:Q}.
54 1.2 rillig @echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
55 1.2 rillig
56 1.2 rillig # Demonstrate that it is possible to generate dollar characters using the
57 1.2 rillig # :@ modifier.
58 1.2 rillig #
59 1.2 rillig # These are edge cases that could have resulted in a parse error as well
60 1.2 rillig # since the $@ at the end could have been interpreted as a variable, which
61 1.2 rillig # would mean a missing closing @ delimiter.
62 1.2 rillig mod-loop-dollar:
63 1.2 rillig @echo $@:${:U1:@word@${word}$@:Q}:
64 1.2 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
65 1.2 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
66 1.2 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
67 1.2 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
68 1.2 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
69