cond-func-empty.mk revision 1.19 1 1.19 rillig # $NetBSD: cond-func-empty.mk,v 1.19 2023/06/01 07:27:30 rillig Exp $
2 1.1 rillig #
3 1.3 rillig # Tests for the empty() function in .if conditions, which tests a variable
4 1.3 rillig # expression for emptiness.
5 1.3 rillig #
6 1.15 rillig # Note that the argument in the parentheses is a variable name, not a variable
7 1.15 rillig # expression, optionally followed by variable modifiers.
8 1.3 rillig #
9 1.3 rillig
10 1.3 rillig .undef UNDEF
11 1.3 rillig EMPTY= # empty
12 1.3 rillig SPACE= ${:U }
13 1.3 rillig WORD= word
14 1.3 rillig
15 1.3 rillig # An undefined variable is empty.
16 1.3 rillig .if !empty(UNDEF)
17 1.3 rillig . error
18 1.3 rillig .endif
19 1.3 rillig
20 1.3 rillig # An undefined variable has the empty string as the value, and the :M
21 1.3 rillig # variable modifier does not change that.
22 1.3 rillig #
23 1.3 rillig .if !empty(UNDEF:M*)
24 1.3 rillig . error
25 1.3 rillig .endif
26 1.3 rillig
27 1.18 rillig # The :S modifier replaces the empty value with an actual word. After
28 1.19 rillig # applying the :S modifier to the expression, its value is 'empty', so it is
29 1.18 rillig # no longer empty, but it is still based on an undefined variable. There are
30 1.18 rillig # a few modifiers that turn an undefined expression into a defined expression,
31 1.18 rillig # among them :U and :D, but not :S. Therefore, at the end of evaluating the
32 1.18 rillig # expression, the expression is still undefined, so its final value becomes an
33 1.18 rillig # empty string.
34 1.3 rillig #
35 1.3 rillig # XXX: This is hard to explain to someone who doesn't know these
36 1.3 rillig # implementation details.
37 1.3 rillig #
38 1.3 rillig .if !empty(UNDEF:S,^$,value,W)
39 1.3 rillig . error
40 1.3 rillig .endif
41 1.3 rillig
42 1.15 rillig # The :U modifier changes the state of a previously undefined expression from
43 1.15 rillig # DEF_UNDEF to DEF_DEFINED. This marks the expression as "being interesting
44 1.15 rillig # enough to be further processed".
45 1.3 rillig #
46 1.3 rillig .if empty(UNDEF:S,^$,value,W:Ufallback)
47 1.3 rillig . error
48 1.3 rillig .endif
49 1.3 rillig
50 1.18 rillig # When an expression is based on an undefined variable, its modifiers interact
51 1.18 rillig # in sometimes surprising ways. Applying the :S modifier to the undefined
52 1.18 rillig # expression makes its value non-empty, but doesn't change that the expression
53 1.18 rillig # is based on an undefined variable. The :U modifier that follows only looks
54 1.18 rillig # at the definedness state to decide whether the variable is defined or not.
55 1.18 rillig # This kind of makes sense since the :U modifier tests the _variable_, not the
56 1.10 rillig # _expression_.
57 1.3 rillig #
58 1.18 rillig # Since the variable was undefined to begin with, the fallback value from the
59 1.18 rillig # :U modifier is used in this expression, instead of keeping the 'value' from
60 1.18 rillig # the :S modifier.
61 1.3 rillig #
62 1.3 rillig .if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
63 1.3 rillig . error
64 1.3 rillig .endif
65 1.3 rillig
66 1.3 rillig # The variable EMPTY is completely empty (0 characters).
67 1.3 rillig .if !empty(EMPTY)
68 1.3 rillig . error
69 1.3 rillig .endif
70 1.3 rillig
71 1.3 rillig # The variable SPACE has a single space, which counts as being empty.
72 1.3 rillig .if !empty(SPACE)
73 1.3 rillig . error
74 1.3 rillig .endif
75 1.3 rillig
76 1.3 rillig # The variable .newline has a single newline, which counts as being empty.
77 1.3 rillig .if !empty(.newline)
78 1.3 rillig . error
79 1.3 rillig .endif
80 1.3 rillig
81 1.15 rillig # The following example constructs an expression with the variable name ""
82 1.15 rillig # and the value " ". This expression counts as empty since the value contains
83 1.15 rillig # only whitespace.
84 1.3 rillig #
85 1.3 rillig # Contrary to the other functions in conditionals, the trailing space is not
86 1.3 rillig # stripped off, as can be seen in the -dv debug log. If the space had been
87 1.15 rillig # stripped, it wouldn't make a difference in this case, but in other cases.
88 1.3 rillig #
89 1.3 rillig .if !empty(:U )
90 1.3 rillig . error
91 1.3 rillig .endif
92 1.3 rillig
93 1.3 rillig # Now the variable named " " gets a non-empty value, which demonstrates that
94 1.3 rillig # neither leading nor trailing spaces are trimmed in the argument of the
95 1.3 rillig # function. If the spaces were trimmed, the variable name would be "" and
96 1.16 rillig # that variable is indeed undefined. Since CondParser_FuncCallEmpty calls
97 1.18 rillig # Var_Parse without VARE_UNDEFERR, the value of the undefined variable ""
98 1.18 rillig # would be returned as an empty string.
99 1.3 rillig ${:U }= space
100 1.3 rillig .if empty( )
101 1.3 rillig . error
102 1.3 rillig .endif
103 1.3 rillig
104 1.3 rillig # The value of the following expression is " word", which is not empty.
105 1.3 rillig .if empty(:U word)
106 1.3 rillig . error
107 1.3 rillig .endif
108 1.3 rillig
109 1.3 rillig # The :L modifier creates a variable expression that has the same value as
110 1.3 rillig # its name, which both are "VAR" in this case. The value is therefore not
111 1.3 rillig # empty.
112 1.3 rillig .if empty(VAR:L)
113 1.3 rillig . error
114 1.3 rillig .endif
115 1.3 rillig
116 1.3 rillig # The variable WORD has the value "word", which does not count as empty.
117 1.3 rillig .if empty(WORD)
118 1.3 rillig . error
119 1.3 rillig .endif
120 1.1 rillig
121 1.3 rillig # The expression ${} for a variable with the empty name always evaluates
122 1.7 rillig # to an empty string (see Var_Parse, varUndefined).
123 1.3 rillig .if !empty()
124 1.3 rillig . error
125 1.3 rillig .endif
126 1.1 rillig
127 1.18 rillig # Ensure that variable expressions that appear as part of the function call
128 1.18 rillig # argument are properly parsed. Typical use cases for this are .for loops,
129 1.18 rillig # which are expanded to exactly these ${:U} expressions.
130 1.5 rillig #
131 1.5 rillig # If everything goes well, the argument expands to "WORD", and that variable
132 1.5 rillig # is defined at the beginning of this file. The surrounding 'W' and 'D'
133 1.16 rillig # ensure that CondParser_FuncCallEmpty keeps track of the parsing position,
134 1.16 rillig # both before and after the call to Var_Parse.
135 1.5 rillig .if empty(W${:UOR}D)
136 1.5 rillig . error
137 1.5 rillig .endif
138 1.5 rillig
139 1.8 rillig # There may be spaces at the outside of the parentheses.
140 1.8 rillig # Spaces inside the parentheses are interpreted as part of the variable name.
141 1.8 rillig .if ! empty ( WORD )
142 1.8 rillig . error
143 1.8 rillig .endif
144 1.8 rillig
145 1.8 rillig ${:U WORD }= variable name with spaces
146 1.8 rillig
147 1.8 rillig # Now there is a variable named " WORD ", and it is not empty.
148 1.8 rillig .if empty ( WORD )
149 1.8 rillig . error
150 1.8 rillig .endif
151 1.8 rillig
152 1.9 rillig # Parse error: missing closing parenthesis.
153 1.9 rillig .if empty(WORD
154 1.9 rillig . error
155 1.9 rillig .else
156 1.9 rillig . error
157 1.9 rillig .endif
158 1.9 rillig
159 1.15 rillig # Since cond.c 1.76 from 2020-06-28 and before var.c 1.226 from 2020-07-02,
160 1.15 rillig # the following example generated a wrong error message "Variable VARNAME is
161 1.15 rillig # recursive".
162 1.15 rillig #
163 1.15 rillig # Since at least 1993, the manual page claimed that irrelevant parts of
164 1.15 rillig # conditions were not evaluated, but that was wrong for a long time. The
165 1.15 rillig # expressions in irrelevant parts of the condition were actually evaluated,
166 1.18 rillig # they just allowed undefined variables to be used in the conditions. These
167 1.18 rillig # unnecessary evaluations were fixed in several commits, starting with var.c
168 1.18 rillig # 1.226 from 2020-07-02.
169 1.15 rillig #
170 1.15 rillig # In this example, the variable "VARNAME2" is not defined, so evaluation of
171 1.15 rillig # the condition should have stopped at this point, and the rest of the
172 1.15 rillig # condition should have been processed in parse-only mode. The right-hand
173 1.15 rillig # side containing the '!empty' was evaluated though, as it had always been.
174 1.11 rillig #
175 1.11 rillig # When evaluating the !empty condition, the variable name was parsed as
176 1.11 rillig # "VARNAME${:U2}", but without expanding any nested variable expression, in
177 1.15 rillig # this case the ${:U2}. The expression '${:U2}' was replaced with an empty
178 1.15 rillig # string, the resulting variable name was thus "VARNAME". This conceptually
179 1.15 rillig # wrong variable name should have been discarded quickly after parsing it, to
180 1.15 rillig # prevent it from doing any harm.
181 1.11 rillig #
182 1.11 rillig # The variable expression was expanded though, and this was wrong. The
183 1.14 rillig # expansion was done without VARE_WANTRES (called VARF_WANTRES back
184 1.11 rillig # then) though. This had the effect that the ${:U1} from the value of VARNAME
185 1.11 rillig # expanded to an empty string. This in turn created the seemingly recursive
186 1.11 rillig # definition VARNAME=${VARNAME}, and that definition was never meant to be
187 1.11 rillig # expanded.
188 1.11 rillig #
189 1.11 rillig # This was fixed by expanding nested variable expressions in the variable name
190 1.14 rillig # only if the flag VARE_WANTRES is given.
191 1.11 rillig VARNAME= ${VARNAME${:U1}}
192 1.11 rillig .if defined(VARNAME${:U2}) && !empty(VARNAME${:U2})
193 1.11 rillig .endif
194 1.11 rillig
195 1.17 rillig
196 1.17 rillig # If the word 'empty' is not followed by '(', it is not a function call but an
197 1.17 rillig # ordinary bare word. This bare word is interpreted as 'defined(empty)', and
198 1.17 rillig # since there is no variable named 'empty', the condition evaluates to false.
199 1.17 rillig .if empty
200 1.17 rillig . error
201 1.17 rillig .endif
202 1.17 rillig
203 1.17 rillig empty= # defined but empty
204 1.17 rillig .if empty
205 1.17 rillig .else
206 1.17 rillig . error
207 1.17 rillig .endif
208