1 # $NetBSD: cond-func-empty.mk,v 1.8 2020/09/23 08:11:28 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. This is like the defined()
16 # An undefined variable is empty.
21 # An undefined variable has the empty string as the value, and the :M
22 # variable modifier does not change that.
28 # The :S modifier replaces the empty value with an actual word, and
29 # after that the expression is no longer empty. Because the variable
30 # was undefined in the first place, the expression has the flag VAR_JUNK
31 # but not VAR_KEEP, therefore it is still considered undefined.
32 # Only very few variable modifiers turn an undefined variable expression
33 # into a defined variable expression. The :U and :D modifiers belong to
34 # that group, but :S doesn't (see VAR_KEEP).
36 # XXX: This is hard to explain to someone who doesn't know these
37 # implementation details.
39 .if !empty(UNDEF:S,^$,value,W)
43 # The :U modifier modifies expressions based on undefined variables
44 # (VAR_JUNK) by adding the VAR_KEEP flag, which marks the expression
45 # as "being interesting enough to be further processed".
47 .if empty(UNDEF:S,^$,value,W:Ufallback)
51 # And now to the surprising part. Applying the following :S modifier to the
52 # undefined variable makes it non-empty, but the marker VAR_JUNK is preserved
53 # nevertheless. The :U modifier that follows only looks at VAR_JUNK to decide
54 # whether the variable is defined or not. This kind of makes sense since the
55 # :U modifier tests the _variable_, not the _expression_.
57 # But since the variable was undefined to begin with, the fallback value is
58 # used in this expression.
60 .if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
64 # The variable EMPTY is completely empty (0 characters).
69 # The variable SPACE has a single space, which counts as being empty.
74 # The variable .newline has a single newline, which counts as being empty.
79 # The empty variable named "" gets a fallback value of " ", which counts as
82 # Contrary to the other functions in conditionals, the trailing space is not
83 # stripped off, as can be seen in the -dv debug log. If the space had been
84 # stripped, it wouldn't make a difference in this case.
90 # Now the variable named " " gets a non-empty value, which demonstrates that
91 # neither leading nor trailing spaces are trimmed in the argument of the
92 # function. If the spaces were trimmed, the variable name would be "" and
93 # that variable is indeed undefined. Since ParseEmptyArg calls Var_Parse
94 # without VARE_UNDEFERR, the value of the undefined variable is returned as
101 # The value of the following expression is " word", which is not empty.
106 # The :L modifier creates a variable expression that has the same value as
107 # its name, which both are "VAR" in this case. The value is therefore not
113 # The variable WORD has the value "word", which does not count as empty.
118 # The expression ${} for a variable with the empty name always evaluates
119 # to an empty string (see Var_Parse, varUndefined).
124 # Ensure that variable expressions that appear as part of the argument are
125 # properly parsed. Typical use cases for this are .for loops, which are
126 # expanded to exactly these ${:U} expressions.
128 # If everything goes well, the argument expands to "WORD", and that variable
129 # is defined at the beginning of this file. The surrounding 'W' and 'D'
130 # ensure that the parser in ParseEmptyArg has the correct position, both
131 # before and after the call to Var_ParsePP.
136 # There may be spaces at the outside of the parentheses.
137 # Spaces inside the parentheses are interpreted as part of the variable name.
142 ${:U WORD }= variable name with spaces
144 # Now there is a variable named " WORD ", and it is not empty.