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