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