aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/unit-tests/varmod-undefined.mk
blob: fd56ffd35e3091f33ec7c1bfe02b882d9d589ebc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# $NetBSD: varmod-undefined.mk,v 1.9 2023/11/19 21:47:52 rillig Exp $
#
# Tests for the :U variable modifier, which returns the given string
# if the variable is undefined.
#
# See also:
#	directive-for.mk
#	varmod-defined.mk

# The pattern ${:Uword} is heavily used when expanding .for loops.
#
# This is how an expanded .for loop looks like.
# .for word in one
# .  if ${word} != one
.if ${:Uone} != one
# .    error ${word}
.  error ${:Uone}
# .  endif
.endif
# .endfor

# The expressions in the text of the :U modifier may be arbitrarily
# nested.

.if ${:U${:Unested}${${${:Udeeply}}}} != nested
.  error
.endif

# The nested expressions may contain braces, and these braces don't
# need to match pairwise.  In the following example, the :S modifier uses '{'
# as delimiter, which confuses both editors and humans because the opening
# and closing braces don't match anymore.  It's syntactically valid though.
# For more similar examples, see varmod-subst.mk, mod-subst-delimiter.

.if ${:U${:Uvalue:S{a{X{}} != vXlue
.  error
.endif

# The escaping rules for the :U modifier (left-hand side) and condition
# string literals (right-hand side) are completely different.
#
# In the :U modifier, the backslash only escapes very few characters, all
# other backslashes are retained.
#
# In condition string literals, the backslash always escapes the following
# character, no matter whether it would be necessary or not.
#
# In both contexts, \n is an escaped letter n, not a newline; that's what
# the .newline variable is for.
#
# Whitespace at the edges is preserved, on both sides of the comparison.
#
.if ${:U \: \} \$ \\ \a \b \n } != " : } \$ \\ \\a \\b \\n "
.  error
.endif

# Even after the :U modifier has been applied, the expression still remembers
# that it originated from an undefined variable, and the :U modifier can
# be used to overwrite the value of the expression.
#
.if ${UNDEF:Uvalue:S,a,X,} != "vXlue"
.  error
.elif ${UNDEF:Uvalue:S,a,X,:Uwas undefined} != "was undefined"
.  error
.endif

all:
	@:;