varmod-loop-varname.mk revision 1.3 1 1.3 rillig # $NetBSD: varmod-loop-varname.mk,v 1.3 2021/11/30 23:52:19 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for the first part of the variable modifier ':@var (a] ...@', which
4 1.1 rillig # contains the variable name to use during the loop.
5 1.1 rillig
6 1.3 rillig # Force the test results to be independent of the default value of this
7 1.3 rillig # setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
8 1.3 rillig # distribution and pkgsrc/devel/bmake.
9 1.1 rillig .MAKE.SAVE_DOLLARS= yes
10 1.1 rillig
11 1.1 rillig
12 1.2 rillig # Before 2021-04-04, the name of the loop variable could be generated
13 1.2 rillig # dynamically. There was no practical use-case for this.
14 1.2 rillig # Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
15 1.2 rillig # variable name.
16 1.1 rillig .if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
17 1.1 rillig . error
18 1.1 rillig .endif
19 1.1 rillig
20 1.1 rillig
21 1.1 rillig # ":::" is a very creative variable name, unlikely to occur in practice.
22 1.1 rillig # The expression ${\:\:\:} would not work since backslashes can only
23 1.1 rillig # be escaped in the modifiers, but not in the variable name, therefore
24 1.1 rillig # the extra indirection via the modifier ':U'.
25 1.1 rillig .if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
26 1.1 rillig . error
27 1.1 rillig .endif
28 1.1 rillig
29 1.1 rillig
30 1.1 rillig # "@@" is another creative variable name.
31 1.1 rillig .if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
32 1.1 rillig . error
33 1.1 rillig .endif
34 1.1 rillig
35 1.1 rillig
36 1.1 rillig # In extreme cases, even the backslash can be used as variable name.
37 1.1 rillig # It needs to be doubled though.
38 1.1 rillig .if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
39 1.1 rillig . error
40 1.1 rillig .endif
41 1.1 rillig
42 1.1 rillig
43 1.1 rillig # The variable name can technically be empty, and in this situation
44 1.1 rillig # the variable value cannot be accessed since the empty "variable"
45 1.1 rillig # is protected to always return an empty string.
46 1.1 rillig .if ${:U1 2 3:@@x${}y@} != "xy xy xy"
47 1.1 rillig . error
48 1.1 rillig .endif
49 1.1 rillig
50 1.1 rillig
51 1.1 rillig # The :@ modifier resolves the variables from the replacement text once more
52 1.1 rillig # than expected. In particular, it resolves _all_ variables from the scope,
53 1.1 rillig # and not only the loop variable (in this case v).
54 1.1 rillig SRCS= source
55 1.1 rillig CFLAGS.source= before
56 1.1 rillig ALL_CFLAGS:= ${SRCS:@src@${CFLAGS.${src}}@} # note the ':='
57 1.1 rillig CFLAGS.source+= after
58 1.1 rillig .if ${ALL_CFLAGS} != "before"
59 1.1 rillig . error
60 1.1 rillig .endif
61 1.1 rillig
62 1.1 rillig
63 1.1 rillig # In the following example, the modifier ':@' expands the '$$' to '$'. This
64 1.1 rillig # means that when the resulting expression is evaluated, these resulting '$'
65 1.1 rillig # will be interpreted as starting a subexpression.
66 1.1 rillig #
67 1.1 rillig # The d means direct reference, the i means indirect reference.
68 1.1 rillig RESOLVE= ${RES1} $${RES1}
69 1.1 rillig RES1= 1d${RES2} 1i$${RES2}
70 1.1 rillig RES2= 2d${RES3} 2i$${RES3}
71 1.1 rillig RES3= 3
72 1.1 rillig
73 1.1 rillig .if ${RESOLVE:@v@w${v}w@} != "w1d2d3w w2i3w w1i2d3 2i\${RES3}w w1d2d3 2i\${RES3} 1i\${RES2}w"
74 1.1 rillig . error
75 1.1 rillig .endif
76 1.1 rillig
77 1.1 rillig
78 1.1 rillig # Until 2020-07-20, the variable name of the :@ modifier could end with one
79 1.1 rillig # or two dollar signs, which were silently ignored.
80 1.1 rillig # There's no point in allowing a dollar sign in that position.
81 1.2 rillig # Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
82 1.2 rillig # variable name.
83 1.1 rillig .if ${1 2 3:L:@v$@($v)@} != "(1) (2) (3)"
84 1.1 rillig . error
85 1.2 rillig .else
86 1.2 rillig . error
87 1.1 rillig .endif
88 1.1 rillig .if ${1 2 3:L:@v$$@($v)@} != "() () ()"
89 1.1 rillig . error
90 1.2 rillig .else
91 1.2 rillig . error
92 1.1 rillig .endif
93 1.1 rillig .if ${1 2 3:L:@v$$$@($v)@} != "() () ()"
94 1.1 rillig . error
95 1.2 rillig .else
96 1.2 rillig . error
97 1.1 rillig .endif
98 1.1 rillig
99 1.1 rillig
100 1.1 rillig # It may happen that there are nested :@ modifiers that use the same name for
101 1.1 rillig # for the loop variable. These modifiers influence each other.
102 1.1 rillig #
103 1.1 rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
104 1.1 rillig # variable in the scope of the expression and deleting it again after the
105 1.1 rillig # loop. This is different from the .for loops, which substitute the variable
106 1.1 rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
107 1.1 rillig #
108 1.1 rillig # To make the behavior more predictable, the :@ modifier should restore the
109 1.1 rillig # loop variable to the value it had before the loop. This would result in
110 1.1 rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
111 1.1 rillig .if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
112 1.1 rillig . error
113 1.1 rillig .endif
114 1.1 rillig
115 1.1 rillig # During the loop, the variable is actually defined and nonempty.
116 1.1 rillig # If the loop were implemented in the same way as the .for loop, the variable
117 1.1 rillig # would be neither defined nor nonempty since all expressions of the form
118 1.1 rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
119 1.1 rillig .if defined(var)
120 1.1 rillig . error
121 1.1 rillig .endif
122 1.1 rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
123 1.1 rillig != "def nonempty"
124 1.1 rillig . error
125 1.1 rillig .endif
126 1.1 rillig .if defined(var)
127 1.1 rillig . error
128 1.1 rillig .endif
129 1.1 rillig
130 1.1 rillig all: .PHONY
131