var-op-append.mk revision 1.12 1 1.12 rillig # $NetBSD: var-op-append.mk,v 1.12 2023/11/02 05:46:26 rillig Exp $
2 1.1 rillig #
3 1.10 rillig # Tests for the '+=' variable assignment operator, which appends to a
4 1.10 rillig # variable, creating it if necessary.
5 1.10 rillig #
6 1.10 rillig # See also
7 1.10 rillig # var-op.mk
8 1.10 rillig #
9 1.10 rillig # Standards
10 1.10 rillig # The '+=' variable assignment operator is planned to be added in
11 1.10 rillig # POSIX.1-202x.
12 1.10 rillig #
13 1.10 rillig # This implementation does not support the immediate-expansion macros
14 1.10 rillig # specified in POSIX.1-202x. All variables are delayed-expansion.
15 1.10 rillig #
16 1.10 rillig # History
17 1.10 rillig # The '+=' variable assignment operator was added before 1993-03-21.
18 1.1 rillig
19 1.5 rillig # Appending to an undefined variable is possible.
20 1.5 rillig # The variable is created, and no extra space is added before the value.
21 1.5 rillig VAR+= one
22 1.5 rillig .if ${VAR} != "one"
23 1.5 rillig . error
24 1.5 rillig .endif
25 1.5 rillig
26 1.5 rillig # Appending to an existing variable adds a single space and the value.
27 1.5 rillig VAR+= two
28 1.5 rillig .if ${VAR} != "one two"
29 1.5 rillig . error
30 1.5 rillig .endif
31 1.5 rillig
32 1.5 rillig # Appending an empty string nevertheless adds a single space.
33 1.5 rillig VAR+= # empty
34 1.5 rillig .if ${VAR} != "one two "
35 1.5 rillig . error
36 1.5 rillig .endif
37 1.5 rillig
38 1.5 rillig # Variable names may contain '+', and this character is also part of the
39 1.5 rillig # '+=' assignment operator. As far as possible, the '+' is interpreted as
40 1.5 rillig # part of the assignment operator.
41 1.5 rillig #
42 1.11 rillig # See Parse_Var, AdjustVarassignOp.
43 1.6 rillig C++= value
44 1.5 rillig .if ${C+} != "value" || defined(C++)
45 1.5 rillig . error
46 1.5 rillig .endif
47 1.1 rillig
48 1.8 rillig # Before var.c 1.793 from 2021-02-03, the variable name of a newly created
49 1.8 rillig # variable was expanded two times in a row, which was unexpected but
50 1.8 rillig # irrelevant in practice since variable names containing dollars lead to
51 1.8 rillig # strange side effects in several other places as well.
52 1.7 rillig .MAKEFLAGS: -dv
53 1.7 rillig VAR.${:U\$\$\$\$\$\$\$\$}+= dollars
54 1.7 rillig .MAKEFLAGS: -d0
55 1.8 rillig .if ${VAR.${:U\$\$\$\$\$\$\$\$}} != "dollars"
56 1.7 rillig . error
57 1.7 rillig .endif
58 1.7 rillig
59 1.12 rillig
60 1.12 rillig # Appending to an environment variable in the global scope creates a global
61 1.12 rillig # variable of the same name, taking its initial value from the environment
62 1.12 rillig # variable. After the assignment, the environment variable is left as-is,
63 1.12 rillig # the value of the global variable is not synced back to the environment
64 1.12 rillig # variable.
65 1.12 rillig export ENV_PLUS_GLOBAL=from-env-value
66 1.12 rillig ENV_PLUS_GLOBAL+= appended-value
67 1.12 rillig .if ${ENV_PLUS_GLOBAL} != "from-env-value appended-value"
68 1.12 rillig . error
69 1.12 rillig .endif
70 1.12 rillig EXPORTED!= echo "$$ENV_PLUS_GLOBAL"
71 1.12 rillig .if ${EXPORTED} != "from-env-value"
72 1.12 rillig . error
73 1.12 rillig .endif
74 1.12 rillig
75 1.12 rillig # Appending to an environment variable in the command line scope ignores the
76 1.12 rillig # environment variable.
77 1.12 rillig export ENV_PLUS_COMMAND=from-env-value
78 1.12 rillig .MAKEFLAGS: ENV_PLUS_COMMAND+=appended-command
79 1.12 rillig .if ${ENV_PLUS_COMMAND} != "appended-command"
80 1.12 rillig . error ${ENV_PLUS_COMMAND}
81 1.12 rillig .endif
82 1.12 rillig EXPORTED!= echo "$$ENV_PLUS_GLOBAL"
83 1.12 rillig .if ${EXPORTED} != "from-env-value"
84 1.12 rillig . error
85 1.12 rillig .endif
86 1.12 rillig
87 1.12 rillig
88 1.1 rillig all:
89