varmod-assign.mk revision 1.4 1 1.4 rillig # $NetBSD: varmod-assign.mk,v 1.4 2020/08/25 20:49:40 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the obscure ::= variable modifiers, which perform variable
4 1.2 rillig # assignments during evaluation, just like the = operator in C.
5 1.1 rillig
6 1.2 rillig all: mod-assign
7 1.2 rillig all: mod-assign-nested
8 1.4 rillig all: mod-assign-empty
9 1.4 rillig all: mod-assign-parse
10 1.1 rillig
11 1.2 rillig mod-assign:
12 1.3 rillig # The ::?= modifier applies the ?= assignment operator 3 times.
13 1.3 rillig # The ?= operator only has an effect for the first time, therefore
14 1.3 rillig # the variable FIRST ends up with the value 1.
15 1.2 rillig @echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
16 1.3 rillig
17 1.3 rillig # The ::= modifier applies the = assignment operator 3 times.
18 1.3 rillig # The = operator overwrites the previous value, therefore the
19 1.3 rillig # variable LAST ends up with the value 3.
20 1.2 rillig @echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
21 1.3 rillig
22 1.3 rillig # The ::+= modifier applies the += assignment operator 3 times.
23 1.3 rillig # The += operator appends 3 times to the variable, therefore
24 1.3 rillig # the variable APPENDED ends up with the value "1 2 3".
25 1.2 rillig @echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
26 1.3 rillig
27 1.3 rillig # The ::!= modifier applies the != assignment operator 3 times.
28 1.3 rillig # The side effects of the shell commands are visible in the output.
29 1.3 rillig # Just as with the ::= modifier, the last value is stored in the
30 1.3 rillig # RAN variable.
31 1.2 rillig @echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
32 1.3 rillig
33 1.2 rillig # The assignments happen in the global scope and thus are
34 1.2 rillig # preserved even after the shell command has been run.
35 1.2 rillig @echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
36 1.2 rillig
37 1.2 rillig mod-assign-nested:
38 1.3 rillig # The condition "1" is true, therefore THEN1 gets assigned a value,
39 1.3 rillig # and IT1 as well. Nothing surprising here.
40 1.2 rillig @echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
41 1.3 rillig
42 1.3 rillig # The condition "0" is false, therefore ELSE1 gets assigned a value,
43 1.3 rillig # and IE1 as well. Nothing surprising here as well.
44 1.2 rillig @echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
45 1.3 rillig
46 1.3 rillig # The same effects happen when the variables are defined elsewhere.
47 1.2 rillig @echo $@: ${SINK3:Q}
48 1.2 rillig @echo $@: ${SINK4:Q}
49 1.2 rillig SINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
50 1.2 rillig SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
51 1.4 rillig
52 1.4 rillig mod-assign-empty:
53 1.4 rillig # Assigning to the empty variable would obviously not work since that variable
54 1.4 rillig # is write-protected. Therefore it is rejected early as a "bad modifier".
55 1.4 rillig @echo ${::=value}
56 1.4 rillig @echo $@: ${:Uvalue::=overwritten}
57 1.4 rillig
58 1.4 rillig # The :L modifier sets the variable's value to its name.
59 1.4 rillig # Since the name is still "VAR", assigning to that variable works.
60 1.4 rillig @echo $@: ${VAR:L::=overwritten} VAR=${VAR}
61 1.4 rillig
62 1.4 rillig mod-assign-parse:
63 1.4 rillig # The modifier for assignment operators starts with a ':'.
64 1.4 rillig # An 'x' after that is an invalid modifier.
65 1.4 rillig @echo ${ASSIGN::x} # 'x' is an unknown assignment operator
66 1.4 rillig
67 1.4 rillig # When parsing an assignment operator fails because the operator is
68 1.4 rillig # incomplete, make falls back to the SysV modifier.
69 1.4 rillig @echo ${SYSV::=sysv\:x}${SYSV::x=:y}
70 1.4 rillig
71 1.4 rillig @echo ${ASSIGN::=value # missing closing brace
72