var-op-append.mk revision 1.11 1 # $NetBSD: var-op-append.mk,v 1.11 2023/11/02 05:14:58 rillig Exp $
2 #
3 # Tests for the '+=' variable assignment operator, which appends to a
4 # variable, creating it if necessary.
5 #
6 # See also
7 # var-op.mk
8 #
9 # Standards
10 # The '+=' variable assignment operator is planned to be added in
11 # POSIX.1-202x.
12 #
13 # This implementation does not support the immediate-expansion macros
14 # specified in POSIX.1-202x. All variables are delayed-expansion.
15 #
16 # History
17 # The '+=' variable assignment operator was added before 1993-03-21.
18
19 # Appending to an undefined variable is possible.
20 # The variable is created, and no extra space is added before the value.
21 VAR+= one
22 .if ${VAR} != "one"
23 . error
24 .endif
25
26 # Appending to an existing variable adds a single space and the value.
27 VAR+= two
28 .if ${VAR} != "one two"
29 . error
30 .endif
31
32 # Appending an empty string nevertheless adds a single space.
33 VAR+= # empty
34 .if ${VAR} != "one two "
35 . error
36 .endif
37
38 # Variable names may contain '+', and this character is also part of the
39 # '+=' assignment operator. As far as possible, the '+' is interpreted as
40 # part of the assignment operator.
41 #
42 # See Parse_Var, AdjustVarassignOp.
43 C++= value
44 .if ${C+} != "value" || defined(C++)
45 . error
46 .endif
47
48 # Before var.c 1.793 from 2021-02-03, the variable name of a newly created
49 # variable was expanded two times in a row, which was unexpected but
50 # irrelevant in practice since variable names containing dollars lead to
51 # strange side effects in several other places as well.
52 .MAKEFLAGS: -dv
53 VAR.${:U\$\$\$\$\$\$\$\$}+= dollars
54 .MAKEFLAGS: -d0
55 .if ${VAR.${:U\$\$\$\$\$\$\$\$}} != "dollars"
56 . error
57 .endif
58
59 all:
60