var-op-expand.mk revision 1.6 1 1.6 rillig # $NetBSD: var-op-expand.mk,v 1.6 2020/12/27 21:19:13 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.6 rillig # In variable assignments using the ':=' operator, undefined variables are
90 1.6 rillig # preserved, no matter how indirectly they are referenced.
91 1.6 rillig .undef REF3
92 1.6 rillig REF2= <${REF3}>
93 1.6 rillig REF= ${REF2}
94 1.6 rillig VAR:= ${REF}
95 1.6 rillig REF3= too late
96 1.6 rillig .if ${VAR} != "<too late>"
97 1.6 rillig . error
98 1.6 rillig .endif
99 1.6 rillig
100 1.6 rillig
101 1.6 rillig # In variable assignments using the ':=' operator, '$$' are preserved, no
102 1.6 rillig # matter how indirectly they are referenced.
103 1.6 rillig REF2= REF2:$$ $$$$
104 1.6 rillig REF= REF:$$ $$$$ ${REF2}
105 1.6 rillig VAR:= VAR:$$ $$$$ ${REF}
106 1.6 rillig .if ${VAR} != "VAR:\$ \$\$ REF:\$ \$\$ REF2:\$ \$\$"
107 1.6 rillig . error
108 1.6 rillig .endif
109 1.6 rillig
110 1.6 rillig
111 1.6 rillig # In variable assignments using the ':=' operator, '$$' are preserved in the
112 1.6 rillig # expressions of the top level, but not in expressions that are nested.
113 1.6 rillig VAR:= top:$$ ${:Unest1\:\$\$} ${:Unest2${:U\:\$\$}}
114 1.6 rillig .if ${VAR} != "top:\$ nest1:\$ nest2:\$"
115 1.6 rillig . error
116 1.6 rillig .endif
117 1.6 rillig
118 1.6 rillig
119 1.3 rillig # XXX: edge case: When a variable name refers to an undefined variable, the
120 1.3 rillig # behavior differs between the '=' and the ':=' assignment operators.
121 1.4 rillig # This bug exists since var.c 1.42 from 2000-05-11.
122 1.3 rillig #
123 1.3 rillig # The '=' operator expands the undefined variable to an empty string, thus
124 1.3 rillig # assigning to VAR_ASSIGN_. In the name of variables to be set, it should
125 1.3 rillig # really be forbidden to refer to undefined variables.
126 1.3 rillig #
127 1.3 rillig # The ':=' operator expands the variable name twice. In one of these
128 1.3 rillig # expansions, the undefined variable expression is preserved (controlled by
129 1.3 rillig # preserveUndefined in VarAssign_EvalSubst), in the other expansion it expands
130 1.3 rillig # to an empty string. This way, 2 variables are created using a single
131 1.3 rillig # variable assignment. It's magic. :-/
132 1.5 rillig .undef UNDEF
133 1.3 rillig .MAKEFLAGS: -dv
134 1.3 rillig VAR_ASSIGN_${UNDEF}= undef value
135 1.3 rillig VAR_SUBST_${UNDEF}:= undef value
136 1.3 rillig .MAKEFLAGS: -d0
137 1.3 rillig
138 1.1 rillig all:
139 1.1 rillig @:;
140