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