xref: /freebsd/contrib/bmake/unit-tests/varmod-loop-varname.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1*6a7405f5SSimon J. Gerraty# $NetBSD: varmod-loop-varname.mk,v 1.12 2025/01/11 20:54:46 rillig Exp $
2b0c40a00SSimon J. Gerraty#
3b0c40a00SSimon J. Gerraty# Tests for the first part of the variable modifier ':@var@...@', which
4b0c40a00SSimon J. Gerraty# contains the variable name to use during the loop.
5b0c40a00SSimon J. Gerraty
612904384SSimon J. Gerraty# Force the test results to be independent of the default value of this
712904384SSimon J. Gerraty# setting, which is 'yes' for NetBSD's usr.bin/make but 'no' for the bmake
812904384SSimon J. Gerraty# distribution and pkgsrc/devel/bmake.
9b0c40a00SSimon J. Gerraty.MAKE.SAVE_DOLLARS=	yes
10b0c40a00SSimon J. Gerraty
11b0c40a00SSimon J. Gerraty
12b0c40a00SSimon J. Gerraty# Before 2021-04-04, the name of the loop variable could be generated
13b0c40a00SSimon J. Gerraty# dynamically.  There was no practical use-case for this.
14b0c40a00SSimon J. Gerraty# Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
15b0c40a00SSimon J. Gerraty# variable name.
16*6a7405f5SSimon J. Gerraty# expect+1: In the :@ modifier, the variable name "${:Ubar:S,b,v,}" must not contain a dollar
17b0c40a00SSimon J. Gerraty.if ${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@} != "+one+ +two+ +three+"
18b0c40a00SSimon J. Gerraty.  error
1912904384SSimon J. Gerraty.else
2012904384SSimon J. Gerraty.  error
21b0c40a00SSimon J. Gerraty.endif
22b0c40a00SSimon J. Gerraty
23b0c40a00SSimon J. Gerraty
24b0c40a00SSimon J. Gerraty# ":::" is a very creative variable name, unlikely to occur in practice.
25b0c40a00SSimon J. Gerraty# The expression ${\:\:\:} would not work since backslashes can only
26b0c40a00SSimon J. Gerraty# be escaped in the modifiers, but not in the variable name, therefore
27b0c40a00SSimon J. Gerraty# the extra indirection via the modifier ':U'.
28b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@:::@x${${:U\:\:\:}}y@} != "x1y x2y x3y"
29b0c40a00SSimon J. Gerraty.  error
30b0c40a00SSimon J. Gerraty.endif
31b0c40a00SSimon J. Gerraty
32b0c40a00SSimon J. Gerraty
33b0c40a00SSimon J. Gerraty# "@@" is another creative variable name.
34b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@\@\@@x${@@}y@} != "x1y x2y x3y"
35b0c40a00SSimon J. Gerraty.  error
36b0c40a00SSimon J. Gerraty.endif
37b0c40a00SSimon J. Gerraty
38b0c40a00SSimon J. Gerraty
39b0c40a00SSimon J. Gerraty# In extreme cases, even the backslash can be used as variable name.
40b0c40a00SSimon J. Gerraty# It needs to be doubled though.
41b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@} != "x1y x2y x3y"
42b0c40a00SSimon J. Gerraty.  error
43b0c40a00SSimon J. Gerraty.endif
44b0c40a00SSimon J. Gerraty
45b0c40a00SSimon J. Gerraty
46b0c40a00SSimon J. Gerraty# The variable name can technically be empty, and in this situation
47b0c40a00SSimon J. Gerraty# the variable value cannot be accessed since the empty "variable"
48b0c40a00SSimon J. Gerraty# is protected to always return an empty string.
49b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@@x${}y@} != "xy xy xy"
50b0c40a00SSimon J. Gerraty.  error
51b0c40a00SSimon J. Gerraty.endif
52b0c40a00SSimon J. Gerraty
53b0c40a00SSimon J. Gerraty
54b0c40a00SSimon J. Gerraty# The :@ modifier resolves the variables from the replacement text once more
55b0c40a00SSimon J. Gerraty# than expected.  In particular, it resolves _all_ variables from the scope,
56b0c40a00SSimon J. Gerraty# and not only the loop variable (in this case v).
57b0c40a00SSimon J. GerratySRCS=		source
58b0c40a00SSimon J. GerratyCFLAGS.source=	before
59b0c40a00SSimon J. GerratyALL_CFLAGS:=	${SRCS:@src@${CFLAGS.${src}}@}	# note the ':='
60b0c40a00SSimon J. GerratyCFLAGS.source+=	after
61b0c40a00SSimon J. Gerraty.if ${ALL_CFLAGS} != "before"
62b0c40a00SSimon J. Gerraty.  error
63b0c40a00SSimon J. Gerraty.endif
64b0c40a00SSimon J. Gerraty
65b0c40a00SSimon J. Gerraty
66b0c40a00SSimon J. Gerraty# In the following example, the modifier ':@' expands the '$$' to '$'.  This
67b0c40a00SSimon J. Gerraty# means that when the resulting expression is evaluated, these resulting '$'
68b0c40a00SSimon J. Gerraty# will be interpreted as starting a subexpression.
69b0c40a00SSimon J. Gerraty#
70b0c40a00SSimon J. Gerraty# The d means direct reference, the i means indirect reference.
71b0c40a00SSimon J. GerratyRESOLVE=	${RES1} $${RES1}
72b0c40a00SSimon J. GerratyRES1=		1d${RES2} 1i$${RES2}
73b0c40a00SSimon J. GerratyRES2=		2d${RES3} 2i$${RES3}
74b0c40a00SSimon J. GerratyRES3=		3
75b0c40a00SSimon J. Gerraty
76b0c40a00SSimon J. Gerraty.if ${RESOLVE:@v@w${v}w@} != "w1d2d3w w2i3w w1i2d3 2i\${RES3}w w1d2d3 2i\${RES3} 1i\${RES2}w"
77b0c40a00SSimon J. Gerraty.  error
78b0c40a00SSimon J. Gerraty.endif
79b0c40a00SSimon J. Gerraty
80b0c40a00SSimon J. Gerraty
81b0c40a00SSimon J. Gerraty# Until 2020-07-20, the variable name of the :@ modifier could end with one
82b0c40a00SSimon J. Gerraty# or two dollar signs, which were silently ignored.
83b0c40a00SSimon J. Gerraty# There's no point in allowing a dollar sign in that position.
84b0c40a00SSimon J. Gerraty# Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
85b0c40a00SSimon J. Gerraty# variable name.
86*6a7405f5SSimon J. Gerraty# expect+1: In the :@ modifier, the variable name "v$" must not contain a dollar
87b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$@($v)@} != "(1) (2) (3)"
88b0c40a00SSimon J. Gerraty.  error
89b0c40a00SSimon J. Gerraty.else
90b0c40a00SSimon J. Gerraty.  error
91b0c40a00SSimon J. Gerraty.endif
92*6a7405f5SSimon J. Gerraty# expect+1: In the :@ modifier, the variable name "v$$" must not contain a dollar
93b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$$@($v)@} != "() () ()"
94b0c40a00SSimon J. Gerraty.  error
95b0c40a00SSimon J. Gerraty.else
96b0c40a00SSimon J. Gerraty.  error
97b0c40a00SSimon J. Gerraty.endif
98*6a7405f5SSimon J. Gerraty# expect+1: In the :@ modifier, the variable name "v$$$" must not contain a dollar
99b0c40a00SSimon J. Gerraty.if ${1 2 3:L:@v$$$@($v)@} != "() () ()"
100b0c40a00SSimon J. Gerraty.  error
101b0c40a00SSimon J. Gerraty.else
102b0c40a00SSimon J. Gerraty.  error
103b0c40a00SSimon J. Gerraty.endif
104b0c40a00SSimon J. Gerraty
105b0c40a00SSimon J. Gerraty
106b0c40a00SSimon J. Gerraty# It may happen that there are nested :@ modifiers that use the same name for
107b0c40a00SSimon J. Gerraty# for the loop variable.  These modifiers influence each other.
108b0c40a00SSimon J. Gerraty#
109b0c40a00SSimon J. Gerraty# As of 2020-10-18, the :@ modifier is implemented by actually setting a
110b0c40a00SSimon J. Gerraty# variable in the scope of the expression and deleting it again after the
111d5e0a182SSimon J. Gerraty# loop.  This is different from the .for loops, which substitute the
112b0c40a00SSimon J. Gerraty# expression with ${:Uvalue}, leading to different unwanted side effects.
113b0c40a00SSimon J. Gerraty#
114b0c40a00SSimon J. Gerraty# To make the behavior more predictable, the :@ modifier should restore the
115b0c40a00SSimon J. Gerraty# loop variable to the value it had before the loop.  This would result in
116b0c40a00SSimon J. Gerraty# the string "1a b c1 2a b c2 3a b c3", making the two loops independent.
117b0c40a00SSimon J. Gerraty.if ${:U1 2 3:@i@$i${:Ua b c:@i@$i@}${i:Uu}@} != "1a b cu 2a b cu 3a b cu"
118b0c40a00SSimon J. Gerraty.  error
119b0c40a00SSimon J. Gerraty.endif
120b0c40a00SSimon J. Gerraty
121b0c40a00SSimon J. Gerraty# During the loop, the variable is actually defined and nonempty.
122b0c40a00SSimon J. Gerraty# If the loop were implemented in the same way as the .for loop, the variable
123b0c40a00SSimon J. Gerraty# would be neither defined nor nonempty since all expressions of the form
124b0c40a00SSimon J. Gerraty# ${var} would have been replaced with ${:Uword} before evaluating them.
125b0c40a00SSimon J. Gerraty.if defined(var)
126b0c40a00SSimon J. Gerraty.  error
127b0c40a00SSimon J. Gerraty.endif
128b0c40a00SSimon J. Gerraty.if ${:Uword:@var@${defined(var):?def:undef} ${empty(var):?empty:nonempty}@} \
129b0c40a00SSimon J. Gerraty    != "def nonempty"
130b0c40a00SSimon J. Gerraty.  error
131b0c40a00SSimon J. Gerraty.endif
132b0c40a00SSimon J. Gerraty.if defined(var)
133b0c40a00SSimon J. Gerraty.  error
134b0c40a00SSimon J. Gerraty.endif
135b0c40a00SSimon J. Gerraty
136b0c40a00SSimon J. Gerratyall: .PHONY
137