1 # $NetBSD: cond-func-empty.mk,v 1.10 2020/11/15 14:07:53 rillig Exp $
3 # Tests for the empty() function in .if conditions, which tests a variable
4 # expression for emptiness.
6 # Note that the argument in the parentheses is indeed a variable name,
7 # optionally followed by variable modifiers.
15 # An undefined variable is empty.
20 # An undefined variable has the empty string as the value, and the :M
21 # variable modifier does not change that.
27 # The :S modifier replaces the empty value with an actual word. The
28 # expression is now no longer empty, but it is still possible to see whether
29 # the expression was based on an undefined variable. The expression has the
32 # The expression does not have the flag VEF_DEF though, therefore it is still
33 # considered undefined. Yes, indeed, undefined but not empty. There are a
34 # few variable modifiers that turn an undefined expression into a defined
35 # expression, among them :U and :D, but not :S.
37 # XXX: This is hard to explain to someone who doesn't know these
38 # implementation details.
40 .if !empty(UNDEF:S,^$,value,W)
44 # The :U modifier modifies expressions based on undefined variables
45 # (VAR_JUNK) by adding the VAR_KEEP flag, which marks the expression
46 # as "being interesting enough to be further processed".
48 .if empty(UNDEF:S,^$,value,W:Ufallback)
52 # And now to the surprising part. Applying the following :S modifier to the
53 # undefined expression makes it non-empty, but the marker VEF_UNDEF is
54 # preserved nevertheless. The :U modifier that follows only looks at the
55 # VEF_UNDEF flag to decide whether the variable is defined or not. This kind
56 # of makes sense since the :U modifier tests the _variable_, not the
59 # But since the variable was undefined to begin with, the fallback value from
60 # the :U modifier is used in this expression.
62 .if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
66 # The variable EMPTY is completely empty (0 characters).
71 # The variable SPACE has a single space, which counts as being empty.
76 # The variable .newline has a single newline, which counts as being empty.
81 # The empty variable named "" gets a fallback value of " ", which counts as
84 # Contrary to the other functions in conditionals, the trailing space is not
85 # stripped off, as can be seen in the -dv debug log. If the space had been
86 # stripped, it wouldn't make a difference in this case.
92 # Now the variable named " " gets a non-empty value, which demonstrates that
93 # neither leading nor trailing spaces are trimmed in the argument of the
94 # function. If the spaces were trimmed, the variable name would be "" and
95 # that variable is indeed undefined. Since ParseEmptyArg calls Var_Parse
96 # without VARE_UNDEFERR, the value of the undefined variable is returned as
103 # The value of the following expression is " word", which is not empty.
108 # The :L modifier creates a variable expression that has the same value as
109 # its name, which both are "VAR" in this case. The value is therefore not
115 # The variable WORD has the value "word", which does not count as empty.
120 # The expression ${} for a variable with the empty name always evaluates
121 # to an empty string (see Var_Parse, varUndefined).
126 # Ensure that variable expressions that appear as part of the argument are
127 # properly parsed. Typical use cases for this are .for loops, which are
128 # expanded to exactly these ${:U} expressions.
130 # If everything goes well, the argument expands to "WORD", and that variable
131 # is defined at the beginning of this file. The surrounding 'W' and 'D'
132 # ensure that the parser in ParseEmptyArg has the correct position, both
133 # before and after the call to Var_Parse.
138 # There may be spaces at the outside of the parentheses.
139 # Spaces inside the parentheses are interpreted as part of the variable name.
144 ${:U WORD }= variable name with spaces
146 # Now there is a variable named " WORD ", and it is not empty.
151 # Parse error: missing closing parenthesis.