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