cond-func-empty.mk revision 1.9
1# $NetBSD: cond-func-empty.mk,v 1.9 2020/11/07 14:37:31 rillig Exp $
2#
3# Tests for the empty() function in .if conditions, which tests a variable
4# expression for emptiness.
5#
6# Note that the argument in the parentheses is indeed a variable name,
7# optionally followed by variable modifiers.  This is like the defined()
8# function.
9#
10
11.undef UNDEF
12EMPTY=	# empty
13SPACE=	${:U }
14WORD=	word
15
16# An undefined variable is empty.
17.if !empty(UNDEF)
18.  error
19.endif
20
21# An undefined variable has the empty string as the value, and the :M
22# variable modifier does not change that.
23#
24.if !empty(UNDEF:M*)
25.  error
26.endif
27
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).
35#
36# XXX: This is hard to explain to someone who doesn't know these
37# implementation details.
38#
39.if !empty(UNDEF:S,^$,value,W)
40.  error
41.endif
42
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".
46#
47.if empty(UNDEF:S,^$,value,W:Ufallback)
48.  error
49.endif
50
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_.
56#
57# But since the variable was undefined to begin with, the fallback value is
58# used in this expression.
59#
60.if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
61.  error
62.endif
63
64# The variable EMPTY is completely empty (0 characters).
65.if !empty(EMPTY)
66.  error
67.endif
68
69# The variable SPACE has a single space, which counts as being empty.
70.if !empty(SPACE)
71.  error
72.endif
73
74# The variable .newline has a single newline, which counts as being empty.
75.if !empty(.newline)
76.  error
77.endif
78
79# The empty variable named "" gets a fallback value of " ", which counts as
80# empty.
81#
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.
85#
86.if !empty(:U )
87.  error
88.endif
89
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
95# an empty string.
96${:U }=	space
97.if empty( )
98.  error
99.endif
100
101# The value of the following expression is " word", which is not empty.
102.if empty(:U word)
103.  error
104.endif
105
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
108# empty.
109.if empty(VAR:L)
110.  error
111.endif
112
113# The variable WORD has the value "word", which does not count as empty.
114.if empty(WORD)
115.  error
116.endif
117
118# The expression ${} for a variable with the empty name always evaluates
119# to an empty string (see Var_Parse, varUndefined).
120.if !empty()
121.  error
122.endif
123
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.
127#
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.
132.if empty(W${:UOR}D)
133.  error
134.endif
135
136# There may be spaces at the outside of the parentheses.
137# Spaces inside the parentheses are interpreted as part of the variable name.
138.if ! empty ( WORD )
139.  error
140.endif
141
142${:U WORD }=	variable name with spaces
143
144# Now there is a variable named " WORD ", and it is not empty.
145.if empty ( WORD )
146.  error
147.endif
148
149# Parse error: missing closing parenthesis.
150.if empty(WORD
151.  error
152.else
153.  error
154.endif
155
156all:
157	@:;
158