var-op-expand.mk revision 1.5 1 1.5 rillig # $NetBSD: var-op-expand.mk,v 1.5 2020/12/27 20:45:52 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the := variable assignment operator, which expands its
4 1.2 rillig # right-hand side.
5 1.1 rillig
6 1.5 rillig
7 1.5 rillig # If the right-hand side does not contain a dollar sign, the ':=' assignment
8 1.5 rillig # operator has the same effect as the '=' assignment operator.
9 1.5 rillig VAR:= value
10 1.5 rillig .if ${VAR} != "value"
11 1.5 rillig . error
12 1.5 rillig .endif
13 1.5 rillig
14 1.5 rillig # When a ':=' assignment is performed, its right-hand side is evaluated and
15 1.5 rillig # expanded as far as possible. Contrary to other situations, '$$' and
16 1.5 rillig # variable expressions based on undefined variables are preserved though.
17 1.5 rillig #
18 1.5 rillig # Whether a variable expression is undefined or not is determined at the end
19 1.5 rillig # of evaluating the expression. The consequence is that ${:Ufallback} expands
20 1.5 rillig # to "fallback"; initially this expression is undefined since it is based on
21 1.5 rillig # the variable named "", which is guaranteed to be never defined, but at the
22 1.5 rillig # end of evaluating the expression ${:Ufallback}, the modifier ':U' has turned
23 1.5 rillig # the expression into a defined expression.
24 1.5 rillig
25 1.5 rillig
26 1.5 rillig # literal dollar signs
27 1.5 rillig VAR:= $$ $$$$ $$$$$$$$
28 1.5 rillig .if ${VAR} != "\$ \$\$ \$\$\$\$"
29 1.5 rillig . error
30 1.5 rillig .endif
31 1.5 rillig
32 1.5 rillig
33 1.5 rillig # reference to a variable containing a literal dollar sign
34 1.5 rillig REF= $$ $$$$ $$$$$$$$
35 1.5 rillig VAR:= ${REF}
36 1.5 rillig REF= too late
37 1.5 rillig .if ${VAR} != "\$ \$\$ \$\$\$\$"
38 1.5 rillig . error
39 1.5 rillig .endif
40 1.5 rillig
41 1.5 rillig
42 1.5 rillig # reference to an undefined variable
43 1.5 rillig .undef UNDEF
44 1.5 rillig VAR:= <${UNDEF}>
45 1.5 rillig UNDEF= after
46 1.5 rillig .if ${VAR} != "<after>"
47 1.5 rillig . error
48 1.5 rillig .endif
49 1.5 rillig
50 1.5 rillig
51 1.5 rillig # reference to a variable whose name is computed from another variable
52 1.5 rillig REF2= referred to
53 1.5 rillig REF= REF2
54 1.5 rillig VAR:= ${${REF}}
55 1.5 rillig REF= too late
56 1.5 rillig .if ${VAR} != "referred to"
57 1.5 rillig . error
58 1.5 rillig .endif
59 1.5 rillig
60 1.5 rillig
61 1.5 rillig # expression with an indirect modifier referring to an undefined variable
62 1.5 rillig .undef UNDEF
63 1.5 rillig VAR:= ${:${UNDEF}}
64 1.5 rillig UNDEF= Uwas undefined
65 1.5 rillig .if ${VAR} != "was undefined"
66 1.5 rillig . error
67 1.5 rillig .endif
68 1.5 rillig
69 1.5 rillig
70 1.5 rillig # expression with an indirect modifier referring to another variable that
71 1.5 rillig # in turn refers to an undefined variable
72 1.5 rillig #
73 1.5 rillig # XXX: Even though this is a ':=' assignment, the '${UNDEF}' in the part of
74 1.5 rillig # the variable modifier is not preserved. To preserve it, ParseModifierPart
75 1.5 rillig # would have to call VarSubstExpr somehow since this is the only piece of
76 1.5 rillig # code that takes care of this global variable.
77 1.5 rillig .undef UNDEF
78 1.5 rillig REF= U${UNDEF}
79 1.5 rillig #.MAKEFLAGS: -dv
80 1.5 rillig VAR:= ${:${REF}}
81 1.5 rillig #.MAKEFLAGS: -d0
82 1.5 rillig REF= too late
83 1.5 rillig UNDEF= Uwas undefined
84 1.5 rillig .if ${VAR} != ""
85 1.5 rillig . error
86 1.5 rillig .endif
87 1.5 rillig
88 1.1 rillig
89 1.3 rillig # XXX: edge case: When a variable name refers to an undefined variable, the
90 1.3 rillig # behavior differs between the '=' and the ':=' assignment operators.
91 1.4 rillig # This bug exists since var.c 1.42 from 2000-05-11.
92 1.3 rillig #
93 1.3 rillig # The '=' operator expands the undefined variable to an empty string, thus
94 1.3 rillig # assigning to VAR_ASSIGN_. In the name of variables to be set, it should
95 1.3 rillig # really be forbidden to refer to undefined variables.
96 1.3 rillig #
97 1.3 rillig # The ':=' operator expands the variable name twice. In one of these
98 1.3 rillig # expansions, the undefined variable expression is preserved (controlled by
99 1.3 rillig # preserveUndefined in VarAssign_EvalSubst), in the other expansion it expands
100 1.3 rillig # to an empty string. This way, 2 variables are created using a single
101 1.3 rillig # variable assignment. It's magic. :-/
102 1.5 rillig .undef UNDEF
103 1.3 rillig .MAKEFLAGS: -dv
104 1.3 rillig VAR_ASSIGN_${UNDEF}= undef value
105 1.3 rillig VAR_SUBST_${UNDEF}:= undef value
106 1.3 rillig .MAKEFLAGS: -d0
107 1.3 rillig
108 1.1 rillig all:
109 1.1 rillig @:;
110