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