varmod-loop.mk revision 1.21 1 1.21 rillig # $NetBSD: varmod-loop.mk,v 1.21 2022/08/23 21:13:46 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :@var (a] ...${var}...@ variable modifier.
4 1.1 rillig
5 1.16 rillig # Force the test results to be independent of the default value of this
6 1.16 rillig # setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
7 1.16 rillig # distribution and pkgsrc/devel/bmake.
8 1.8 rillig .MAKE.SAVE_DOLLARS= yes
9 1.8 rillig
10 1.13 rillig all: varname-overwriting-target
11 1.2 rillig all: mod-loop-dollar
12 1.1 rillig
13 1.13 rillig varname-overwriting-target:
14 1.2 rillig # Even "@" works as a variable name since the variable is installed
15 1.2 rillig # in the "current" scope, which in this case is the one from the
16 1.13 rillig # target. Because of this, after the loop has finished, '$@' is
17 1.13 rillig # undefined. This is something that make doesn't expect, this may
18 1.13 rillig # even trigger an assertion failure somewhere.
19 1.2 rillig @echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
20 1.5 rillig
21 1.10 rillig
22 1.2 rillig
23 1.6 rillig # Demonstrate that it is possible to generate dollar signs using the
24 1.2 rillig # :@ modifier.
25 1.2 rillig #
26 1.2 rillig # These are edge cases that could have resulted in a parse error as well
27 1.2 rillig # since the $@ at the end could have been interpreted as a variable, which
28 1.2 rillig # would mean a missing closing @ delimiter.
29 1.2 rillig mod-loop-dollar:
30 1.2 rillig @echo $@:${:U1:@word@${word}$@:Q}:
31 1.2 rillig @echo $@:${:U2:@word@$${word}$$@:Q}:
32 1.2 rillig @echo $@:${:U3:@word@$$${word}$$$@:Q}:
33 1.2 rillig @echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
34 1.2 rillig @echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
35 1.2 rillig @echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
36 1.4 rillig
37 1.4 rillig # It may happen that there are nested :@ modifiers that use the same name for
38 1.4 rillig # for the loop variable. These modifiers influence each other.
39 1.4 rillig #
40 1.5 rillig # As of 2020-10-18, the :@ modifier is implemented by actually setting a
41 1.9 rillig # variable in the scope of the expression and deleting it again after the
42 1.4 rillig # loop. This is different from the .for loops, which substitute the variable
43 1.4 rillig # expression with ${:Uvalue}, leading to different unwanted side effects.
44 1.4 rillig #
45 1.4 rillig # To make the behavior more predictable, the :@ modifier should restore the
46 1.4 rillig # loop variable to the value it had before the loop. This would result in
47 1.4 rillig # the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
48 1.4 rillig .if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
49 1.4 rillig . error
50 1.4 rillig .endif
51 1.5 rillig
52 1.5 rillig # During the loop, the variable is actually defined and nonempty.
53 1.5 rillig # If the loop were implemented in the same way as the .for loop, the variable
54 1.5 rillig # would be neither defined nor nonempty since all expressions of the form
55 1.5 rillig # ${var} would have been replaced with ${:Uword} before evaluating them.
56 1.5 rillig .if defined(var)
57 1.5 rillig . error
58 1.5 rillig .endif
59 1.5 rillig .if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
60 1.5 rillig != "def nonempty"
61 1.5 rillig . error
62 1.5 rillig .endif
63 1.5 rillig .if defined(var)
64 1.5 rillig . error
65 1.5 rillig .endif
66 1.7 rillig
67 1.7 rillig # Assignment using the ':=' operator, combined with the :@var@ modifier
68 1.7 rillig #
69 1.7 rillig 8_DOLLARS= $$$$$$$$
70 1.7 rillig # This string literal is written with 8 dollars, and this is saved as the
71 1.7 rillig # variable value. But as soon as this value is evaluated, it goes through
72 1.7 rillig # Var_Subst, which replaces each '$$' with a single '$'. This could be
73 1.15 rillig # prevented by VARE_EVAL_KEEP_DOLLAR, but that flag is usually removed
74 1.11 rillig # before expanding subexpressions. See ApplyModifier_Loop and
75 1.11 rillig # ParseModifierPart for examples.
76 1.7 rillig #
77 1.7 rillig .MAKEFLAGS: -dcp
78 1.7 rillig USE_8_DOLLARS= ${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
79 1.7 rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
80 1.7 rillig . error
81 1.7 rillig .endif
82 1.7 rillig #
83 1.7 rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
84 1.11 rillig # The ':=' assignment operator evaluates the variable value using the mode
85 1.11 rillig # VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
86 1.11 rillig # but not all. The dollar signs in the top-level expression and in the
87 1.11 rillig # indirect ${8_DOLLARS} are preserved.
88 1.7 rillig #
89 1.7 rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
90 1.7 rillig # matter in which context it is evaluated. What happens in detail is:
91 1.7 rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
92 1.7 rillig # Next, each word of the value is expanded on its own, and at this moment
93 1.11 rillig # in ApplyModifier_Loop, the flag keepDollar is not passed down to
94 1.7 rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
95 1.7 rillig #
96 1.7 rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
97 1.11 rillig # modifier and are thus expanded with the flag keepDollar in action.
98 1.7 rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
99 1.7 rillig # "$$$$ $$$$$$$$ $$$$$$$$".
100 1.7 rillig #
101 1.7 rillig # The variable expression in the condition then expands this raw stored value
102 1.7 rillig # once, resulting in "$$ $$$$ $$$$". The effects from VARE_KEEP_DOLLAR no
103 1.7 rillig # longer take place since they had only been active during the evaluation of
104 1.7 rillig # the variable assignment.
105 1.7 rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
106 1.7 rillig . error
107 1.7 rillig .endif
108 1.7 rillig .MAKEFLAGS: -d0
109 1.10 rillig
110 1.10 rillig # After looping over the words of the expression, the loop variable gets
111 1.10 rillig # undefined. The modifier ':@' uses an ordinary global variable for this,
112 1.10 rillig # which is different from the '.for' loop, which replaces ${var} with
113 1.10 rillig # ${:Uvalue} in the body of the loop. This choice of implementation detail
114 1.10 rillig # can be used for a nasty side effect. The expression ${:U:@VAR@@} evaluates
115 1.10 rillig # to an empty string, plus it undefines the variable 'VAR'. This is the only
116 1.10 rillig # possibility to undefine a global variable during evaluation.
117 1.10 rillig GLOBAL= before-global
118 1.10 rillig RESULT:= ${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
119 1.10 rillig .if ${RESULT} != "before-global undefined"
120 1.10 rillig . error
121 1.10 rillig .endif
122 1.10 rillig
123 1.10 rillig # The above side effect of undefining a variable from a certain scope can be
124 1.10 rillig # further combined with the otherwise undocumented implementation detail that
125 1.10 rillig # the argument of an '.if' directive is evaluated in cmdline scope. Putting
126 1.10 rillig # these together makes it possible to undefine variables from the cmdline
127 1.10 rillig # scope, something that is not possible in a straight-forward way.
128 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline
129 1.10 rillig .if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
130 1.10 rillig . error
131 1.10 rillig .endif
132 1.10 rillig # Now the cmdline variable got undefined.
133 1.10 rillig .if ${CMDLINE} != "cmdline"
134 1.10 rillig . error
135 1.10 rillig .endif
136 1.10 rillig # At this point, it still looks as if the cmdline variable were defined,
137 1.10 rillig # since the value of CMDLINE is still "cmdline". That impression is only
138 1.10 rillig # superficial though, the cmdline variable is actually deleted. To
139 1.10 rillig # demonstrate this, it is now possible to override its value using a global
140 1.10 rillig # variable, something that was not possible before:
141 1.10 rillig CMDLINE= global
142 1.10 rillig .if ${CMDLINE} != "global"
143 1.10 rillig . error
144 1.10 rillig .endif
145 1.10 rillig # Now undefine that global variable again, to get back to the original value.
146 1.10 rillig .undef CMDLINE
147 1.10 rillig .if ${CMDLINE} != "cmdline"
148 1.10 rillig . error
149 1.10 rillig .endif
150 1.10 rillig # What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
151 1.10 rillig # target in the cmdline scope, that same variable was exported to the
152 1.10 rillig # environment, see Var_SetWithFlags.
153 1.10 rillig .unexport CMDLINE
154 1.10 rillig .if ${CMDLINE} != "cmdline"
155 1.10 rillig . error
156 1.10 rillig .endif
157 1.10 rillig # The above '.unexport' has no effect since UnexportVar requires a global
158 1.10 rillig # variable of the same name to be defined, otherwise nothing is unexported.
159 1.10 rillig CMDLINE= global
160 1.10 rillig .unexport CMDLINE
161 1.10 rillig .undef CMDLINE
162 1.10 rillig .if ${CMDLINE} != "cmdline"
163 1.10 rillig . error
164 1.10 rillig .endif
165 1.10 rillig # This still didn't work since there must not only be a global variable, the
166 1.10 rillig # variable must be marked as exported as well, which it wasn't before.
167 1.10 rillig CMDLINE= global
168 1.10 rillig .export CMDLINE
169 1.10 rillig .unexport CMDLINE
170 1.10 rillig .undef CMDLINE
171 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
172 1.10 rillig . error
173 1.10 rillig .endif
174 1.10 rillig # Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
175 1.10 rillig # traces from the environment are gone as well. To do that, a global variable
176 1.10 rillig # had to be defined and exported, something that is far from obvious. To
177 1.10 rillig # recap, here is the essence of the above story:
178 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline # have a cmdline + environment variable
179 1.10 rillig .if ${:U:@CMDLINE@@}} # undefine cmdline, keep environment
180 1.10 rillig .endif
181 1.10 rillig CMDLINE= global # needed for deleting the environment
182 1.10 rillig .export CMDLINE # needed for deleting the environment
183 1.10 rillig .unexport CMDLINE # delete the environment
184 1.10 rillig .undef CMDLINE # delete the global helper variable
185 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
186 1.10 rillig . error # 'CMDLINE' is gone now from all scopes
187 1.10 rillig .endif
188 1.10 rillig
189 1.19 rillig
190 1.19 rillig # In the loop body text of the ':@' modifier, a literal '$' is written as '$$',
191 1.19 rillig # not '\$'. In the following example, each '$$' turns into a single '$',
192 1.19 rillig # except for '$i', which is replaced with the then-current value '1' of the
193 1.19 rillig # iteration variable.
194 1.19 rillig #
195 1.20 rillig # XXX: was broken in var.c 1.1028 from 2022-08-08, reverted in var.c 1.1029
196 1.20 rillig # from 2022-08-23; see parse-var.mk, keyword 'BRACE_GROUP'.
197 1.19 rillig all: varmod-loop-literal-dollar
198 1.19 rillig varmod-loop-literal-dollar: .PHONY
199 1.19 rillig : ${:U1:@i@ t=$$(( $${t:-0} + $i ))@}
200 1.19 rillig
201 1.19 rillig
202 1.21 rillig # When parsing the loop body, each '\$', '\@' and '\\' is unescaped to '$',
203 1.21 rillig # '@' and '\'; all other backslashes are retained.
204 1.21 rillig #
205 1.21 rillig # In practice, the '$' is not escaped as '\$', as there is a second round of
206 1.21 rillig # unescaping '$$' to '$' later when the loop body is expanded after setting the
207 1.21 rillig # iteration variable.
208 1.21 rillig #
209 1.21 rillig # After the iteration variable has been set, the loop body is expanded with
210 1.21 rillig # this unescaping, regardless of whether .MAKE.SAVE_DOLLARS is set or not:
211 1.21 rillig # $$ a literal '$'
212 1.21 rillig # $x, ${var}, $(var) a nested expression
213 1.21 rillig # any other character itself
214 1.21 rillig all: escape-modifier
215 1.21 rillig escape-modifier: .PHONY
216 1.21 rillig # In the first round, '\$ ' is unescaped to '$ ', and since the
217 1.21 rillig # variable named ' ' is not defined, the expression '$ ' expands to an
218 1.21 rillig # empty string.
219 1.21 rillig # expect: : dollar=end
220 1.21 rillig : ${:U1:@i@ dollar=\$ end@}
221 1.21 rillig
222 1.21 rillig # Like in other modifiers, '\ ' is preserved, since ' ' is not one of
223 1.21 rillig # the characters that _must_ be escaped.
224 1.21 rillig # expect: : backslash=\ end
225 1.21 rillig : ${:U1:@i@ backslash=\ end@}
226 1.21 rillig
227 1.21 rillig # expect: : dollar=$ at=@ backslash=\ end
228 1.21 rillig : ${:U1:@i@ dollar=\$\$ at=\@ backslash=\\ end@}
229 1.21 rillig # expect: : dollar=$$ at=@@ backslash=\\ end
230 1.21 rillig : ${:U1:@i@ dollar=\$\$\$\$ at=\@\@ backslash=\\\\ end@}
231 1.21 rillig # expect: : dollar=$$ at=@@ backslash=\\ end
232 1.21 rillig : ${:U1:@i@ dollar=$$$$ at=\@\@ backslash=\\\\ end@}
233 1.21 rillig
234 1.17 rillig all: .PHONY
235