varmod-assign.mk revision 1.3 1 # $NetBSD: varmod-assign.mk,v 1.3 2020/08/25 18:59:30 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
9 mod-assign:
10 # The ::?= modifier applies the ?= assignment operator 3 times.
11 # The ?= operator only has an effect for the first time, therefore
12 # the variable FIRST ends up with the value 1.
13 @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
14
15 # The ::= modifier applies the = assignment operator 3 times.
16 # The = operator overwrites the previous value, therefore the
17 # variable LAST ends up with the value 3.
18 @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
19
20 # The ::+= modifier applies the += assignment operator 3 times.
21 # The += operator appends 3 times to the variable, therefore
22 # the variable APPENDED ends up with the value "1 2 3".
23 @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
24
25 # The ::!= modifier applies the != assignment operator 3 times.
26 # The side effects of the shell commands are visible in the output.
27 # Just as with the ::= modifier, the last value is stored in the
28 # RAN variable.
29 @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
30
31 # The assignments happen in the global scope and thus are
32 # preserved even after the shell command has been run.
33 @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
34
35 mod-assign-nested:
36 # The condition "1" is true, therefore THEN1 gets assigned a value,
37 # and IT1 as well. Nothing surprising here.
38 @echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
39
40 # The condition "0" is false, therefore ELSE1 gets assigned a value,
41 # and IE1 as well. Nothing surprising here as well.
42 @echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
43
44 # The same effects happen when the variables are defined elsewhere.
45 @echo $@: ${SINK3:Q}
46 @echo $@: ${SINK4:Q}
47 SINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
48 SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
49