xref: /freebsd/contrib/bmake/unit-tests/cond-func-empty.mk (revision e2eeea75eb8b6dd50c1298067a0655880d186734)
1*e2eeea75SSimon J. Gerraty# $NetBSD: cond-func-empty.mk,v 1.10 2020/11/15 14:07:53 rillig Exp $
22c3632d1SSimon J. Gerraty#
3956e45f6SSimon J. Gerraty# Tests for the empty() function in .if conditions, which tests a variable
4956e45f6SSimon J. Gerraty# expression for emptiness.
5956e45f6SSimon J. Gerraty#
6956e45f6SSimon J. Gerraty# Note that the argument in the parentheses is indeed a variable name,
7*e2eeea75SSimon J. Gerraty# optionally followed by variable modifiers.
8956e45f6SSimon J. Gerraty#
92c3632d1SSimon J. Gerraty
10956e45f6SSimon J. Gerraty.undef UNDEF
11956e45f6SSimon J. GerratyEMPTY=	# empty
12956e45f6SSimon J. GerratySPACE=	${:U }
13956e45f6SSimon J. GerratyWORD=	word
14956e45f6SSimon J. Gerraty
15956e45f6SSimon J. Gerraty# An undefined variable is empty.
16956e45f6SSimon J. Gerraty.if !empty(UNDEF)
17956e45f6SSimon J. Gerraty.  error
18956e45f6SSimon J. Gerraty.endif
19956e45f6SSimon J. Gerraty
20956e45f6SSimon J. Gerraty# An undefined variable has the empty string as the value, and the :M
21956e45f6SSimon J. Gerraty# variable modifier does not change that.
22956e45f6SSimon J. Gerraty#
23956e45f6SSimon J. Gerraty.if !empty(UNDEF:M*)
24956e45f6SSimon J. Gerraty.  error
25956e45f6SSimon J. Gerraty.endif
26956e45f6SSimon J. Gerraty
27*e2eeea75SSimon J. Gerraty# The :S modifier replaces the empty value with an actual word.  The
28*e2eeea75SSimon J. Gerraty# expression is now no longer empty, but it is still possible to see whether
29*e2eeea75SSimon J. Gerraty# the expression was based on an undefined variable.  The expression has the
30*e2eeea75SSimon J. Gerraty# flag VEF_UNDEF.
31*e2eeea75SSimon J. Gerraty#
32*e2eeea75SSimon J. Gerraty# The expression does not have the flag VEF_DEF though, therefore it is still
33*e2eeea75SSimon J. Gerraty# considered undefined.  Yes, indeed, undefined but not empty.  There are a
34*e2eeea75SSimon J. Gerraty# few variable modifiers that turn an undefined expression into a defined
35*e2eeea75SSimon J. Gerraty# expression, among them :U and :D, but not :S.
36956e45f6SSimon J. Gerraty#
37956e45f6SSimon J. Gerraty# XXX: This is hard to explain to someone who doesn't know these
38956e45f6SSimon J. Gerraty# implementation details.
39956e45f6SSimon J. Gerraty#
40956e45f6SSimon J. Gerraty.if !empty(UNDEF:S,^$,value,W)
41956e45f6SSimon J. Gerraty.  error
42956e45f6SSimon J. Gerraty.endif
43956e45f6SSimon J. Gerraty
44956e45f6SSimon J. Gerraty# The :U modifier modifies expressions based on undefined variables
45956e45f6SSimon J. Gerraty# (VAR_JUNK) by adding the VAR_KEEP flag, which marks the expression
46956e45f6SSimon J. Gerraty# as "being interesting enough to be further processed".
47956e45f6SSimon J. Gerraty#
48956e45f6SSimon J. Gerraty.if empty(UNDEF:S,^$,value,W:Ufallback)
49956e45f6SSimon J. Gerraty.  error
50956e45f6SSimon J. Gerraty.endif
51956e45f6SSimon J. Gerraty
52956e45f6SSimon J. Gerraty# And now to the surprising part.  Applying the following :S modifier to the
53*e2eeea75SSimon J. Gerraty# undefined expression makes it non-empty, but the marker VEF_UNDEF is
54*e2eeea75SSimon J. Gerraty# preserved nevertheless.  The :U modifier that follows only looks at the
55*e2eeea75SSimon J. Gerraty# VEF_UNDEF flag to decide whether the variable is defined or not.  This kind
56*e2eeea75SSimon J. Gerraty# of makes sense since the :U modifier tests the _variable_, not the
57*e2eeea75SSimon J. Gerraty# _expression_.
58956e45f6SSimon J. Gerraty#
59*e2eeea75SSimon J. Gerraty# But since the variable was undefined to begin with, the fallback value from
60*e2eeea75SSimon J. Gerraty# the :U modifier is used in this expression.
61956e45f6SSimon J. Gerraty#
62956e45f6SSimon J. Gerraty.if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
63956e45f6SSimon J. Gerraty.  error
64956e45f6SSimon J. Gerraty.endif
65956e45f6SSimon J. Gerraty
66956e45f6SSimon J. Gerraty# The variable EMPTY is completely empty (0 characters).
67956e45f6SSimon J. Gerraty.if !empty(EMPTY)
68956e45f6SSimon J. Gerraty.  error
69956e45f6SSimon J. Gerraty.endif
70956e45f6SSimon J. Gerraty
71956e45f6SSimon J. Gerraty# The variable SPACE has a single space, which counts as being empty.
72956e45f6SSimon J. Gerraty.if !empty(SPACE)
73956e45f6SSimon J. Gerraty.  error
74956e45f6SSimon J. Gerraty.endif
75956e45f6SSimon J. Gerraty
76956e45f6SSimon J. Gerraty# The variable .newline has a single newline, which counts as being empty.
77956e45f6SSimon J. Gerraty.if !empty(.newline)
78956e45f6SSimon J. Gerraty.  error
79956e45f6SSimon J. Gerraty.endif
80956e45f6SSimon J. Gerraty
81956e45f6SSimon J. Gerraty# The empty variable named "" gets a fallback value of " ", which counts as
82956e45f6SSimon J. Gerraty# empty.
83956e45f6SSimon J. Gerraty#
84956e45f6SSimon J. Gerraty# Contrary to the other functions in conditionals, the trailing space is not
85956e45f6SSimon J. Gerraty# stripped off, as can be seen in the -dv debug log.  If the space had been
86956e45f6SSimon J. Gerraty# stripped, it wouldn't make a difference in this case.
87956e45f6SSimon J. Gerraty#
88956e45f6SSimon J. Gerraty.if !empty(:U )
89956e45f6SSimon J. Gerraty.  error
90956e45f6SSimon J. Gerraty.endif
91956e45f6SSimon J. Gerraty
92956e45f6SSimon J. Gerraty# Now the variable named " " gets a non-empty value, which demonstrates that
93956e45f6SSimon J. Gerraty# neither leading nor trailing spaces are trimmed in the argument of the
94956e45f6SSimon J. Gerraty# function.  If the spaces were trimmed, the variable name would be "" and
95956e45f6SSimon J. Gerraty# that variable is indeed undefined.  Since ParseEmptyArg calls Var_Parse
96956e45f6SSimon J. Gerraty# without VARE_UNDEFERR, the value of the undefined variable is returned as
97956e45f6SSimon J. Gerraty# an empty string.
98956e45f6SSimon J. Gerraty${:U }=	space
99956e45f6SSimon J. Gerraty.if empty( )
100956e45f6SSimon J. Gerraty.  error
101956e45f6SSimon J. Gerraty.endif
102956e45f6SSimon J. Gerraty
103956e45f6SSimon J. Gerraty# The value of the following expression is " word", which is not empty.
104956e45f6SSimon J. Gerraty.if empty(:U word)
105956e45f6SSimon J. Gerraty.  error
106956e45f6SSimon J. Gerraty.endif
107956e45f6SSimon J. Gerraty
108956e45f6SSimon J. Gerraty# The :L modifier creates a variable expression that has the same value as
109956e45f6SSimon J. Gerraty# its name, which both are "VAR" in this case.  The value is therefore not
110956e45f6SSimon J. Gerraty# empty.
111956e45f6SSimon J. Gerraty.if empty(VAR:L)
112956e45f6SSimon J. Gerraty.  error
113956e45f6SSimon J. Gerraty.endif
114956e45f6SSimon J. Gerraty
115956e45f6SSimon J. Gerraty# The variable WORD has the value "word", which does not count as empty.
116956e45f6SSimon J. Gerraty.if empty(WORD)
117956e45f6SSimon J. Gerraty.  error
118956e45f6SSimon J. Gerraty.endif
119956e45f6SSimon J. Gerraty
120956e45f6SSimon J. Gerraty# The expression ${} for a variable with the empty name always evaluates
121956e45f6SSimon J. Gerraty# to an empty string (see Var_Parse, varUndefined).
122956e45f6SSimon J. Gerraty.if !empty()
123956e45f6SSimon J. Gerraty.  error
124956e45f6SSimon J. Gerraty.endif
125956e45f6SSimon J. Gerraty
126956e45f6SSimon J. Gerraty# Ensure that variable expressions that appear as part of the argument are
127956e45f6SSimon J. Gerraty# properly parsed.  Typical use cases for this are .for loops, which are
128956e45f6SSimon J. Gerraty# expanded to exactly these ${:U} expressions.
129956e45f6SSimon J. Gerraty#
130956e45f6SSimon J. Gerraty# If everything goes well, the argument expands to "WORD", and that variable
131956e45f6SSimon J. Gerraty# is defined at the beginning of this file.  The surrounding 'W' and 'D'
132956e45f6SSimon J. Gerraty# ensure that the parser in ParseEmptyArg has the correct position, both
133*e2eeea75SSimon J. Gerraty# before and after the call to Var_Parse.
134956e45f6SSimon J. Gerraty.if empty(W${:UOR}D)
135956e45f6SSimon J. Gerraty.  error
136956e45f6SSimon J. Gerraty.endif
137956e45f6SSimon J. Gerraty
138956e45f6SSimon J. Gerraty# There may be spaces at the outside of the parentheses.
139956e45f6SSimon J. Gerraty# Spaces inside the parentheses are interpreted as part of the variable name.
140956e45f6SSimon J. Gerraty.if ! empty ( WORD )
141956e45f6SSimon J. Gerraty.  error
142956e45f6SSimon J. Gerraty.endif
143956e45f6SSimon J. Gerraty
144956e45f6SSimon J. Gerraty${:U WORD }=	variable name with spaces
145956e45f6SSimon J. Gerraty
146956e45f6SSimon J. Gerraty# Now there is a variable named " WORD ", and it is not empty.
147956e45f6SSimon J. Gerraty.if empty ( WORD )
148956e45f6SSimon J. Gerraty.  error
149956e45f6SSimon J. Gerraty.endif
1502c3632d1SSimon J. Gerraty
151*e2eeea75SSimon J. Gerraty# Parse error: missing closing parenthesis.
152*e2eeea75SSimon J. Gerraty.if empty(WORD
153*e2eeea75SSimon J. Gerraty.  error
154*e2eeea75SSimon J. Gerraty.else
155*e2eeea75SSimon J. Gerraty.  error
156*e2eeea75SSimon J. Gerraty.endif
157*e2eeea75SSimon J. Gerraty
1582c3632d1SSimon J. Gerratyall:
1592c3632d1SSimon J. Gerraty	@:;
160