varmod-loop.mk revision 1.25 1 1.25 rillig # $NetBSD: varmod-loop.mk,v 1.25 2024/06/01 05:08:48 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.25 rillig # Var_Subst, which replaces each '$$' with a single '$'.
86 1.25 rillig # See ApplyModifier_Loop and ParseModifierPart for examples.
87 1.7 rillig #
88 1.7 rillig .MAKEFLAGS: -dcp
89 1.7 rillig USE_8_DOLLARS= ${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
90 1.7 rillig .if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
91 1.7 rillig . error
92 1.7 rillig .endif
93 1.7 rillig #
94 1.7 rillig SUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
95 1.11 rillig # The ':=' assignment operator evaluates the variable value using the mode
96 1.11 rillig # VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
97 1.11 rillig # but not all. The dollar signs in the top-level expression and in the
98 1.11 rillig # indirect ${8_DOLLARS} are preserved.
99 1.7 rillig #
100 1.7 rillig # The variable modifier :@var@ does not preserve the dollar signs though, no
101 1.7 rillig # matter in which context it is evaluated. What happens in detail is:
102 1.7 rillig # First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
103 1.7 rillig # Next, each word of the value is expanded on its own, and at this moment
104 1.11 rillig # in ApplyModifier_Loop, the flag keepDollar is not passed down to
105 1.7 rillig # ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
106 1.7 rillig #
107 1.7 rillig # The remaining words of USE_8_DOLLARS are not affected by any variable
108 1.11 rillig # modifier and are thus expanded with the flag keepDollar in action.
109 1.7 rillig # The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
110 1.7 rillig # "$$$$ $$$$$$$$ $$$$$$$$".
111 1.7 rillig #
112 1.24 rillig # The expression in the condition then expands this raw stored value
113 1.7 rillig # once, resulting in "$$ $$$$ $$$$". The effects from VARE_KEEP_DOLLAR no
114 1.7 rillig # longer take place since they had only been active during the evaluation of
115 1.7 rillig # the variable assignment.
116 1.7 rillig .if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
117 1.7 rillig . error
118 1.7 rillig .endif
119 1.7 rillig .MAKEFLAGS: -d0
120 1.10 rillig
121 1.10 rillig # After looping over the words of the expression, the loop variable gets
122 1.10 rillig # undefined. The modifier ':@' uses an ordinary global variable for this,
123 1.10 rillig # which is different from the '.for' loop, which replaces ${var} with
124 1.10 rillig # ${:Uvalue} in the body of the loop. This choice of implementation detail
125 1.10 rillig # can be used for a nasty side effect. The expression ${:U:@VAR@@} evaluates
126 1.10 rillig # to an empty string, plus it undefines the variable 'VAR'. This is the only
127 1.10 rillig # possibility to undefine a global variable during evaluation.
128 1.10 rillig GLOBAL= before-global
129 1.10 rillig RESULT:= ${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
130 1.10 rillig .if ${RESULT} != "before-global undefined"
131 1.10 rillig . error
132 1.10 rillig .endif
133 1.10 rillig
134 1.10 rillig # The above side effect of undefining a variable from a certain scope can be
135 1.10 rillig # further combined with the otherwise undocumented implementation detail that
136 1.10 rillig # the argument of an '.if' directive is evaluated in cmdline scope. Putting
137 1.10 rillig # these together makes it possible to undefine variables from the cmdline
138 1.10 rillig # scope, something that is not possible in a straight-forward way.
139 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline
140 1.10 rillig .if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
141 1.10 rillig . error
142 1.10 rillig .endif
143 1.10 rillig # Now the cmdline variable got undefined.
144 1.10 rillig .if ${CMDLINE} != "cmdline"
145 1.10 rillig . error
146 1.10 rillig .endif
147 1.10 rillig # At this point, it still looks as if the cmdline variable were defined,
148 1.10 rillig # since the value of CMDLINE is still "cmdline". That impression is only
149 1.10 rillig # superficial though, the cmdline variable is actually deleted. To
150 1.10 rillig # demonstrate this, it is now possible to override its value using a global
151 1.10 rillig # variable, something that was not possible before:
152 1.10 rillig CMDLINE= global
153 1.10 rillig .if ${CMDLINE} != "global"
154 1.10 rillig . error
155 1.10 rillig .endif
156 1.10 rillig # Now undefine that global variable again, to get back to the original value.
157 1.10 rillig .undef CMDLINE
158 1.10 rillig .if ${CMDLINE} != "cmdline"
159 1.10 rillig . error
160 1.10 rillig .endif
161 1.10 rillig # What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
162 1.10 rillig # target in the cmdline scope, that same variable was exported to the
163 1.10 rillig # environment, see Var_SetWithFlags.
164 1.10 rillig .unexport CMDLINE
165 1.10 rillig .if ${CMDLINE} != "cmdline"
166 1.10 rillig . error
167 1.10 rillig .endif
168 1.10 rillig # The above '.unexport' has no effect since UnexportVar requires a global
169 1.10 rillig # variable of the same name to be defined, otherwise nothing is unexported.
170 1.10 rillig CMDLINE= global
171 1.10 rillig .unexport CMDLINE
172 1.10 rillig .undef CMDLINE
173 1.10 rillig .if ${CMDLINE} != "cmdline"
174 1.10 rillig . error
175 1.10 rillig .endif
176 1.10 rillig # This still didn't work since there must not only be a global variable, the
177 1.10 rillig # variable must be marked as exported as well, which it wasn't before.
178 1.10 rillig CMDLINE= global
179 1.10 rillig .export CMDLINE
180 1.10 rillig .unexport CMDLINE
181 1.10 rillig .undef CMDLINE
182 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
183 1.10 rillig . error
184 1.10 rillig .endif
185 1.10 rillig # Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
186 1.10 rillig # traces from the environment are gone as well. To do that, a global variable
187 1.10 rillig # had to be defined and exported, something that is far from obvious. To
188 1.10 rillig # recap, here is the essence of the above story:
189 1.10 rillig .MAKEFLAGS: CMDLINE=cmdline # have a cmdline + environment variable
190 1.10 rillig .if ${:U:@CMDLINE@@}} # undefine cmdline, keep environment
191 1.10 rillig .endif
192 1.10 rillig CMDLINE= global # needed for deleting the environment
193 1.10 rillig .export CMDLINE # needed for deleting the environment
194 1.10 rillig .unexport CMDLINE # delete the environment
195 1.10 rillig .undef CMDLINE # delete the global helper variable
196 1.10 rillig .if ${CMDLINE:Uundefined} != "undefined"
197 1.10 rillig . error # 'CMDLINE' is gone now from all scopes
198 1.10 rillig .endif
199 1.10 rillig
200 1.19 rillig
201 1.19 rillig # In the loop body text of the ':@' modifier, a literal '$' is written as '$$',
202 1.19 rillig # not '\$'. In the following example, each '$$' turns into a single '$',
203 1.19 rillig # except for '$i', which is replaced with the then-current value '1' of the
204 1.19 rillig # iteration variable.
205 1.19 rillig #
206 1.22 rillig # See parse-var.mk, keyword 'BRACE_GROUP'.
207 1.19 rillig all: varmod-loop-literal-dollar
208 1.19 rillig varmod-loop-literal-dollar: .PHONY
209 1.19 rillig : ${:U1:@i@ t=$$(( $${t:-0} + $i ))@}
210 1.19 rillig
211 1.19 rillig
212 1.21 rillig # When parsing the loop body, each '\$', '\@' and '\\' is unescaped to '$',
213 1.22 rillig # '@' and '\', respectively; all other backslashes are retained.
214 1.21 rillig #
215 1.21 rillig # In practice, the '$' is not escaped as '\$', as there is a second round of
216 1.21 rillig # unescaping '$$' to '$' later when the loop body is expanded after setting the
217 1.21 rillig # iteration variable.
218 1.21 rillig #
219 1.21 rillig # After the iteration variable has been set, the loop body is expanded with
220 1.21 rillig # this unescaping, regardless of whether .MAKE.SAVE_DOLLARS is set or not:
221 1.21 rillig # $$ a literal '$'
222 1.21 rillig # $x, ${var}, $(var) a nested expression
223 1.21 rillig # any other character itself
224 1.21 rillig all: escape-modifier
225 1.21 rillig escape-modifier: .PHONY
226 1.21 rillig # In the first round, '\$ ' is unescaped to '$ ', and since the
227 1.21 rillig # variable named ' ' is not defined, the expression '$ ' expands to an
228 1.21 rillig # empty string.
229 1.21 rillig # expect: : dollar=end
230 1.21 rillig : ${:U1:@i@ dollar=\$ end@}
231 1.21 rillig
232 1.21 rillig # Like in other modifiers, '\ ' is preserved, since ' ' is not one of
233 1.21 rillig # the characters that _must_ be escaped.
234 1.21 rillig # expect: : backslash=\ end
235 1.21 rillig : ${:U1:@i@ backslash=\ end@}
236 1.21 rillig
237 1.21 rillig # expect: : dollar=$ at=@ backslash=\ end
238 1.21 rillig : ${:U1:@i@ dollar=\$\$ at=\@ backslash=\\ end@}
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
244 1.17 rillig all: .PHONY
245