1*ac7615f6Srillig# $NetBSD: varmod-loop.mk,v 1.23 2023/02/18 11:55:20 rillig Exp $
21fb97e4bSrillig#
3*ac7615f6Srillig# Tests for the expression modifier ':@var@body@', which replaces each word of
4*ac7615f6Srillig# the expression with the expanded body, which may contain references to the
5*ac7615f6Srillig# variable 'var'.  For example, '${1 2 3:L:@word@<${word}>@}' encloses each
6*ac7615f6Srillig# word in angle quotes, resulting in '<1> <2> <3>'.
7*ac7615f6Srillig#
8*ac7615f6Srillig# The variable name can be chosen freely, except that it must not contain a
9*ac7615f6Srillig# '$'.  For simplicity and readability, variable names should only use the
10*ac7615f6Srillig# characters 'A-Za-z0-9'.
11*ac7615f6Srillig#
12*ac7615f6Srillig# The body may contain subexpressions in the form '${...}' or '$(...)'.  These
13*ac7615f6Srillig# subexpressions differ from everywhere else in makefiles in that the parser
14*ac7615f6Srillig# only scans '${...}' for balanced '{' and '}', likewise for '$(...)'.  Any
15*ac7615f6Srillig# other '$' is left as-is during parsing.  Later, when the body is expanded
16*ac7615f6Srillig# for each word, each '$$' is interpreted as a single '$', and the remaining
17*ac7615f6Srillig# '$' are interpreted as expressions, like when evaluating a regular variable.
181fb97e4bSrillig
19a4748faaSrillig# Force the test results to be independent of the default value of this
20a4748faaSrillig# setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
21a4748faaSrillig# distribution and pkgsrc/devel/bmake.
221d4d4ae2Srillig.MAKE.SAVE_DOLLARS=	yes
231d4d4ae2Srillig
24454079b0Srilligall: varname-overwriting-target
2591e13788Srilligall: mod-loop-dollar
261fb97e4bSrillig
27454079b0Srilligvarname-overwriting-target:
2891e13788Srillig	# Even "@" works as a variable name since the variable is installed
2991e13788Srillig	# in the "current" scope, which in this case is the one from the
30454079b0Srillig	# target.  Because of this, after the loop has finished, '$@' is
31454079b0Srillig	# undefined.  This is something that make doesn't expect, this may
32454079b0Srillig	# even trigger an assertion failure somewhere.
3391e13788Srillig	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
34f03f9dfbSrillig
3591e13788Srillig
363415e83bSrillig# Demonstrate that it is possible to generate dollar signs using the
3791e13788Srillig# :@ modifier.
3891e13788Srillig#
3991e13788Srillig# These are edge cases that could have resulted in a parse error as well
4091e13788Srillig# since the $@ at the end could have been interpreted as a variable, which
4191e13788Srillig# would mean a missing closing @ delimiter.
4291e13788Srilligmod-loop-dollar:
4391e13788Srillig	@echo $@:${:U1:@word@${word}$@:Q}:
4491e13788Srillig	@echo $@:${:U2:@word@$${word}$$@:Q}:
4591e13788Srillig	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
4691e13788Srillig	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
4791e13788Srillig	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
4891e13788Srillig	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
49990a4803Srillig
50990a4803Srillig# It may happen that there are nested :@ modifiers that use the same name for
51990a4803Srillig# for the loop variable.  These modifiers influence each other.
52990a4803Srillig#
53f03f9dfbSrillig# As of 2020-10-18, the :@ modifier is implemented by actually setting a
544f7273d6Srillig# variable in the scope of the expression and deleting it again after the
55990a4803Srillig# loop.  This is different from the .for loops, which substitute the variable
56990a4803Srillig# expression with ${:Uvalue}, leading to different unwanted side effects.
57990a4803Srillig#
58990a4803Srillig# To make the behavior more predictable, the :@ modifier should restore the
59990a4803Srillig# loop variable to the value it had before the loop.  This would result in
60990a4803Srillig# the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
61990a4803Srillig.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
62990a4803Srillig.  error
63990a4803Srillig.endif
64f03f9dfbSrillig
65f03f9dfbSrillig# During the loop, the variable is actually defined and nonempty.
66f03f9dfbSrillig# If the loop were implemented in the same way as the .for loop, the variable
67f03f9dfbSrillig# would be neither defined nor nonempty since all expressions of the form
68f03f9dfbSrillig# ${var} would have been replaced with ${:Uword} before evaluating them.
69f03f9dfbSrillig.if defined(var)
70f03f9dfbSrillig.  error
71f03f9dfbSrillig.endif
72f03f9dfbSrillig.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
73f03f9dfbSrillig    != "def nonempty"
74f03f9dfbSrillig.  error
75f03f9dfbSrillig.endif
76f03f9dfbSrillig.if defined(var)
77f03f9dfbSrillig.  error
78f03f9dfbSrillig.endif
79b925b94aSrillig
80b925b94aSrillig# Assignment using the ':=' operator, combined with the :@var@ modifier
81b925b94aSrillig#
82b925b94aSrillig8_DOLLARS=	$$$$$$$$
83b925b94aSrillig# This string literal is written with 8 dollars, and this is saved as the
84b925b94aSrillig# variable value.  But as soon as this value is evaluated, it goes through
85b925b94aSrillig# Var_Subst, which replaces each '$$' with a single '$'.  This could be
8612201810Srillig# prevented by VARE_EVAL_KEEP_DOLLAR, but that flag is usually removed
87413b6626Srillig# before expanding subexpressions.  See ApplyModifier_Loop and
88413b6626Srillig# ParseModifierPart for examples.
89b925b94aSrillig#
90b925b94aSrillig.MAKEFLAGS: -dcp
91b925b94aSrilligUSE_8_DOLLARS=	${:U1:@var@${8_DOLLARS}@} ${8_DOLLARS} $$$$$$$$
92b925b94aSrillig.if ${USE_8_DOLLARS} != "\$\$\$\$ \$\$\$\$ \$\$\$\$"
93b925b94aSrillig.  error
94b925b94aSrillig.endif
95b925b94aSrillig#
96b925b94aSrilligSUBST_CONTAINING_LOOP:= ${USE_8_DOLLARS}
97413b6626Srillig# The ':=' assignment operator evaluates the variable value using the mode
98413b6626Srillig# VARE_KEEP_DOLLAR_UNDEF, which means that some dollar signs are preserved,
99413b6626Srillig# but not all.  The dollar signs in the top-level expression and in the
100413b6626Srillig# indirect ${8_DOLLARS} are preserved.
101b925b94aSrillig#
102b925b94aSrillig# The variable modifier :@var@ does not preserve the dollar signs though, no
103b925b94aSrillig# matter in which context it is evaluated.  What happens in detail is:
104b925b94aSrillig# First, the modifier part "${8_DOLLARS}" is parsed without expanding it.
105b925b94aSrillig# Next, each word of the value is expanded on its own, and at this moment
106413b6626Srillig# in ApplyModifier_Loop, the flag keepDollar is not passed down to
107b925b94aSrillig# ModifyWords, resulting in "$$$$" for the first word of USE_8_DOLLARS.
108b925b94aSrillig#
109b925b94aSrillig# The remaining words of USE_8_DOLLARS are not affected by any variable
110413b6626Srillig# modifier and are thus expanded with the flag keepDollar in action.
111b925b94aSrillig# The variable SUBST_CONTAINING_LOOP therefore gets assigned the raw value
112b925b94aSrillig# "$$$$ $$$$$$$$ $$$$$$$$".
113b925b94aSrillig#
114b925b94aSrillig# The variable expression in the condition then expands this raw stored value
115b925b94aSrillig# once, resulting in "$$ $$$$ $$$$".  The effects from VARE_KEEP_DOLLAR no
116b925b94aSrillig# longer take place since they had only been active during the evaluation of
117b925b94aSrillig# the variable assignment.
118b925b94aSrillig.if ${SUBST_CONTAINING_LOOP} != "\$\$ \$\$\$\$ \$\$\$\$"
119b925b94aSrillig.  error
120b925b94aSrillig.endif
121b925b94aSrillig.MAKEFLAGS: -d0
12288cc89a5Srillig
12388cc89a5Srillig# After looping over the words of the expression, the loop variable gets
12488cc89a5Srillig# undefined.  The modifier ':@' uses an ordinary global variable for this,
12588cc89a5Srillig# which is different from the '.for' loop, which replaces ${var} with
12688cc89a5Srillig# ${:Uvalue} in the body of the loop.  This choice of implementation detail
12788cc89a5Srillig# can be used for a nasty side effect.  The expression ${:U:@VAR@@} evaluates
12888cc89a5Srillig# to an empty string, plus it undefines the variable 'VAR'.  This is the only
12988cc89a5Srillig# possibility to undefine a global variable during evaluation.
13088cc89a5SrilligGLOBAL=		before-global
13188cc89a5SrilligRESULT:=	${:U${GLOBAL} ${:U:@GLOBAL@@} ${GLOBAL:Uundefined}}
13288cc89a5Srillig.if ${RESULT} != "before-global  undefined"
13388cc89a5Srillig.  error
13488cc89a5Srillig.endif
13588cc89a5Srillig
13688cc89a5Srillig# The above side effect of undefining a variable from a certain scope can be
13788cc89a5Srillig# further combined with the otherwise undocumented implementation detail that
13888cc89a5Srillig# the argument of an '.if' directive is evaluated in cmdline scope.  Putting
13988cc89a5Srillig# these together makes it possible to undefine variables from the cmdline
14088cc89a5Srillig# scope, something that is not possible in a straight-forward way.
14188cc89a5Srillig.MAKEFLAGS: CMDLINE=cmdline
14288cc89a5Srillig.if ${:U${CMDLINE}${:U:@CMDLINE@@}} != "cmdline"
14388cc89a5Srillig.  error
14488cc89a5Srillig.endif
14588cc89a5Srillig# Now the cmdline variable got undefined.
14688cc89a5Srillig.if ${CMDLINE} != "cmdline"
14788cc89a5Srillig.  error
14888cc89a5Srillig.endif
14988cc89a5Srillig# At this point, it still looks as if the cmdline variable were defined,
15088cc89a5Srillig# since the value of CMDLINE is still "cmdline".  That impression is only
15188cc89a5Srillig# superficial though, the cmdline variable is actually deleted.  To
15288cc89a5Srillig# demonstrate this, it is now possible to override its value using a global
15388cc89a5Srillig# variable, something that was not possible before:
15488cc89a5SrilligCMDLINE=	global
15588cc89a5Srillig.if ${CMDLINE} != "global"
15688cc89a5Srillig.  error
15788cc89a5Srillig.endif
15888cc89a5Srillig# Now undefine that global variable again, to get back to the original value.
15988cc89a5Srillig.undef CMDLINE
16088cc89a5Srillig.if ${CMDLINE} != "cmdline"
16188cc89a5Srillig.  error
16288cc89a5Srillig.endif
16388cc89a5Srillig# What actually happened is that when CMDLINE was set by the '.MAKEFLAGS'
16488cc89a5Srillig# target in the cmdline scope, that same variable was exported to the
16588cc89a5Srillig# environment, see Var_SetWithFlags.
16688cc89a5Srillig.unexport CMDLINE
16788cc89a5Srillig.if ${CMDLINE} != "cmdline"
16888cc89a5Srillig.  error
16988cc89a5Srillig.endif
17088cc89a5Srillig# The above '.unexport' has no effect since UnexportVar requires a global
17188cc89a5Srillig# variable of the same name to be defined, otherwise nothing is unexported.
17288cc89a5SrilligCMDLINE=	global
17388cc89a5Srillig.unexport CMDLINE
17488cc89a5Srillig.undef CMDLINE
17588cc89a5Srillig.if ${CMDLINE} != "cmdline"
17688cc89a5Srillig.  error
17788cc89a5Srillig.endif
17888cc89a5Srillig# This still didn't work since there must not only be a global variable, the
17988cc89a5Srillig# variable must be marked as exported as well, which it wasn't before.
18088cc89a5SrilligCMDLINE=	global
18188cc89a5Srillig.export CMDLINE
18288cc89a5Srillig.unexport CMDLINE
18388cc89a5Srillig.undef CMDLINE
18488cc89a5Srillig.if ${CMDLINE:Uundefined} != "undefined"
18588cc89a5Srillig.  error
18688cc89a5Srillig.endif
18788cc89a5Srillig# Finally the variable 'CMDLINE' from the cmdline scope is gone, and all its
18888cc89a5Srillig# traces from the environment are gone as well.  To do that, a global variable
18988cc89a5Srillig# had to be defined and exported, something that is far from obvious.  To
19088cc89a5Srillig# recap, here is the essence of the above story:
19188cc89a5Srillig.MAKEFLAGS: CMDLINE=cmdline	# have a cmdline + environment variable
19288cc89a5Srillig.if ${:U:@CMDLINE@@}}		# undefine cmdline, keep environment
19388cc89a5Srillig.endif
19488cc89a5SrilligCMDLINE=	global		# needed for deleting the environment
19588cc89a5Srillig.export CMDLINE			# needed for deleting the environment
19688cc89a5Srillig.unexport CMDLINE		# delete the environment
19788cc89a5Srillig.undef CMDLINE			# delete the global helper variable
19888cc89a5Srillig.if ${CMDLINE:Uundefined} != "undefined"
19988cc89a5Srillig.  error			# 'CMDLINE' is gone now from all scopes
20088cc89a5Srillig.endif
20188cc89a5Srillig
20207908e3aSrillig
20307908e3aSrillig# In the loop body text of the ':@' modifier, a literal '$' is written as '$$',
20407908e3aSrillig# not '\$'.  In the following example, each '$$' turns into a single '$',
20507908e3aSrillig# except for '$i', which is replaced with the then-current value '1' of the
20607908e3aSrillig# iteration variable.
20707908e3aSrillig#
208d357be07Srillig# See parse-var.mk, keyword 'BRACE_GROUP'.
20907908e3aSrilligall: varmod-loop-literal-dollar
21007908e3aSrilligvarmod-loop-literal-dollar: .PHONY
21107908e3aSrillig	: ${:U1:@i@ t=$$(( $${t:-0} + $i ))@}
21207908e3aSrillig
21307908e3aSrillig
214bdd34607Srillig# When parsing the loop body, each '\$', '\@' and '\\' is unescaped to '$',
215d357be07Srillig# '@' and '\', respectively; all other backslashes are retained.
216bdd34607Srillig#
217bdd34607Srillig# In practice, the '$' is not escaped as '\$', as there is a second round of
218bdd34607Srillig# unescaping '$$' to '$' later when the loop body is expanded after setting the
219bdd34607Srillig# iteration variable.
220bdd34607Srillig#
221bdd34607Srillig# After the iteration variable has been set, the loop body is expanded with
222bdd34607Srillig# this unescaping, regardless of whether .MAKE.SAVE_DOLLARS is set or not:
223bdd34607Srillig#	$$			a literal '$'
224bdd34607Srillig#	$x, ${var}, $(var)	a nested expression
225bdd34607Srillig#	any other character	itself
226bdd34607Srilligall: escape-modifier
227bdd34607Srilligescape-modifier: .PHONY
228bdd34607Srillig	# In the first round, '\$ ' is unescaped to '$ ', and since the
229bdd34607Srillig	# variable named ' ' is not defined, the expression '$ ' expands to an
230bdd34607Srillig	# empty string.
231bdd34607Srillig	# expect: :  dollar=end
232bdd34607Srillig	: ${:U1:@i@ dollar=\$ end@}
233bdd34607Srillig
234bdd34607Srillig	# Like in other modifiers, '\ ' is preserved, since ' ' is not one of
235bdd34607Srillig	# the characters that _must_ be escaped.
236bdd34607Srillig	# expect: :  backslash=\ end
237bdd34607Srillig	: ${:U1:@i@ backslash=\ end@}
238bdd34607Srillig
239bdd34607Srillig	# expect: :  dollar=$ at=@ backslash=\ end
240bdd34607Srillig	: ${:U1:@i@ dollar=\$\$ at=\@ backslash=\\ end@}
241bdd34607Srillig	# expect: :  dollar=$$ at=@@ backslash=\\ end
242bdd34607Srillig	: ${:U1:@i@ dollar=\$\$\$\$ at=\@\@ backslash=\\\\ end@}
243bdd34607Srillig	# expect: :  dollar=$$ at=@@ backslash=\\ end
244bdd34607Srillig	: ${:U1:@i@ dollar=$$$$ at=\@\@ backslash=\\\\ end@}
245bdd34607Srillig
24641515451Srilligall: .PHONY
247