var-op-append.mk revision 1.12 1 # $NetBSD: var-op-append.mk,v 1.12 2023/11/02 05:46:26 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
60 # Appending to an environment variable in the global scope creates a global
61 # variable of the same name, taking its initial value from the environment
62 # variable. After the assignment, the environment variable is left as-is,
63 # the value of the global variable is not synced back to the environment
64 # variable.
65 export ENV_PLUS_GLOBAL=from-env-value
66 ENV_PLUS_GLOBAL+= appended-value
67 .if ${ENV_PLUS_GLOBAL} != "from-env-value appended-value"
68 . error
69 .endif
70 EXPORTED!= echo "$$ENV_PLUS_GLOBAL"
71 .if ${EXPORTED} != "from-env-value"
72 . error
73 .endif
74
75 # Appending to an environment variable in the command line scope ignores the
76 # environment variable.
77 export ENV_PLUS_COMMAND=from-env-value
78 .MAKEFLAGS: ENV_PLUS_COMMAND+=appended-command
79 .if ${ENV_PLUS_COMMAND} != "appended-command"
80 . error ${ENV_PLUS_COMMAND}
81 .endif
82 EXPORTED!= echo "$$ENV_PLUS_GLOBAL"
83 .if ${EXPORTED} != "from-env-value"
84 . error
85 .endif
86
87
88 all:
89