varmod-assign.mk revision 1.11 1 # $NetBSD: varmod-assign.mk,v 1.11 2021/03/15 18:46:05 rillig Exp $
2 #
3 # Tests for the obscure ::= variable modifiers, which perform variable
4 # assignments during evaluation, just like the = operator in C.
5
6 all: mod-assign
7 all: mod-assign-nested
8 all: mod-assign-empty
9 all: mod-assign-parse
10 all: mod-assign-shell-error
11
12 mod-assign:
13 # The ::?= modifier applies the ?= assignment operator 3 times.
14 # The ?= operator only has an effect for the first time, therefore
15 # the variable FIRST ends up with the value 1.
16 @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
17
18 # The ::= modifier applies the = assignment operator 3 times.
19 # The = operator overwrites the previous value, therefore the
20 # variable LAST ends up with the value 3.
21 @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
22
23 # The ::+= modifier applies the += assignment operator 3 times.
24 # The += operator appends 3 times to the variable, therefore
25 # the variable APPENDED ends up with the value "1 2 3".
26 @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
27
28 # The ::!= modifier applies the != assignment operator 3 times.
29 # The side effects of the shell commands are visible in the output.
30 # Just as with the ::= modifier, the last value is stored in the
31 # RAN variable.
32 @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
33
34 # The assignments happen in the global scope and thus are
35 # preserved even after the shell command has been run.
36 @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
37
38 mod-assign-nested:
39 # The condition "1" is true, therefore THEN1 gets assigned a value,
40 # and IT1 as well. Nothing surprising here.
41 @echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
42
43 # The condition "0" is false, therefore ELSE1 gets assigned a value,
44 # and IE1 as well. Nothing surprising here as well.
45 @echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
46
47 # The same effects happen when the variables are defined elsewhere.
48 @echo $@: ${SINK3:Q}
49 @echo $@: ${SINK4:Q}
50 SINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
51 SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
52
53 mod-assign-empty:
54 # Assigning to the empty variable would obviously not work since that
55 # variable is write-protected. Therefore it is rejected early with a
56 # "Bad modifier" message.
57 #
58 # XXX: The error message is hard to read since the variable name is
59 # empty. This leads to a trailing space in the error message.
60 @echo $@: ${::=value}
61
62 # In this variant, it is not as obvious that the name of the
63 # expression is empty. Assigning to it is rejected as well, with the
64 # same "Bad modifier" message.
65 #
66 # XXX: The error message is hard to read since the variable name is
67 # empty. This leads to a trailing space in the error message.
68 @echo $@: ${:Uvalue::=overwritten}
69
70 # The :L modifier sets the value of the expression to its variable
71 # name. The name of the expression is "VAR", therefore assigning to
72 # that variable works.
73 @echo $@: ${VAR:L::=overwritten} VAR=${VAR}
74
75 mod-assign-parse:
76 # The modifier for assignment operators starts with a ':'.
77 # An 'x' after that is an invalid modifier.
78 @echo ${ASSIGN::x} # 'x' is an unknown assignment operator
79
80 # When parsing an assignment operator fails because the operator is
81 # incomplete, make falls back to the SysV modifier.
82 @echo ${SYSV::=sysv\:x}${SYSV::x=:y}
83
84 @echo ${ASSIGN::=value # missing closing brace
85
86 mod-assign-shell-error:
87 # If the command succeeds, the variable is assigned.
88 @${SH_OK::!= echo word; true } echo ok=${SH_OK}
89
90 # If the command fails, the variable keeps its previous value.
91 @${SH_ERR::=previous}
92 @${SH_ERR::!= echo word; false } echo err=${SH_ERR}
93
94 # XXX: The ::= modifier expands its right-hand side exactly once.
95 # This differs subtly from normal assignments such as '+=' or '=', which copy
96 # their right-hand side literally.
97 APPEND.prev= previous
98 APPEND.var= ${APPEND.prev}
99 APPEND.indirect= indirect $${:Unot expanded}
100 APPEND.dollar= $${APPEND.indirect}
101 .if ${APPEND.var::+=${APPEND.dollar}} != ""
102 . error
103 .endif
104 .if ${APPEND.var} != "previous indirect \${:Unot expanded}"
105 . error
106 .endif
107
108
109 # The assignment modifier can be used in a variable expression that is
110 # enclosed in parentheses. In such a case, parsing stops at the first ')',
111 # not at the first '}'.
112 VAR= previous
113 _:= $(VAR::=current})
114 .if ${VAR} != "current}"
115 . error
116 .endif
117
118
119 # Before var.c 1.888 from 2021-03-15, an expression using the modifier '::='
120 # expanded its variable name once too often during evaluation. This was only
121 # relevant for variable names containing a '$' sign in their actual name, not
122 # the usual VAR.${param}.
123 .MAKEFLAGS: -dv
124 param= twice
125 VARNAME= VAR.$${param} # Indirect variable name because of the '$',
126 # to avoid difficult escaping rules.
127
128 ${VARNAME}= initial-value # Sets 'VAR.${param}' to 'expanded'.
129 .if defined(VAR.twice) # At this point, the '$$' is not expanded.
130 . error
131 .endif
132 .if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once
133 . error # too often.
134 .endif
135 .if !defined(VAR.twice)
136 . error # FIXME: This is the unwanted current behavior.
137 .else
138 . info FIXME: don't expand the variable name twice here, $\
139 for symmetry with the usual assignment operators.
140 .endif
141 .if ${${VARNAME}} != "initial-value"
142 . error
143 .endif
144 .MAKEFLAGS: -d0
145