varmod-defined.mk revision 1.4 1 1.4 rillig # $NetBSD: varmod-defined.mk,v 1.4 2020/09/03 18:52:36 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :D variable modifier, which returns the given string
4 1.2 rillig # if the variable is defined. It is closely related to the :U modifier.
5 1.1 rillig
6 1.3 rillig DEF= defined
7 1.3 rillig .undef UNDEF
8 1.3 rillig
9 1.3 rillig # Since DEF is defined, the value of the expression is "value", not
10 1.3 rillig # "defined".
11 1.3 rillig #
12 1.3 rillig .if ${DEF:Dvalue} != "value"
13 1.3 rillig .error
14 1.3 rillig .endif
15 1.3 rillig
16 1.3 rillig # Since UNDEF is not defined, the "value" is ignored. Instead of leaving the
17 1.3 rillig # expression undefined, it is set to "", exactly to allow the expression to
18 1.3 rillig # be used in .if conditions. In this place, other undefined expressions
19 1.3 rillig # would generate an error message.
20 1.3 rillig # XXX: Ideally the error message would be "undefined variable", but as of
21 1.3 rillig # 2020-08-25 it is "Malformed conditional".
22 1.3 rillig #
23 1.3 rillig .if ${UNDEF:Dvalue} != ""
24 1.3 rillig .error
25 1.3 rillig .endif
26 1.1 rillig
27 1.4 rillig # The modifier text may contain plain text as well as expressions.
28 1.4 rillig #
29 1.4 rillig .if ${DEF:D<${DEF}>} != "<defined>"
30 1.4 rillig . error
31 1.4 rillig .endif
32 1.4 rillig
33 1.4 rillig # Special characters that would be interpreted differently can be escaped.
34 1.4 rillig # These are '}' (the closing character of the expression), ':', '$' and '\'.
35 1.4 rillig # Any other backslash sequences are preserved.
36 1.4 rillig #
37 1.4 rillig # The escaping rules for string literals in conditions are completely
38 1.4 rillig # different though. There, any character may be escaped using a backslash.
39 1.4 rillig #
40 1.4 rillig .if ${DEF:D \} \: \$ \\ \) \n } != " } : \$ \\ \\) \\n "
41 1.4 rillig . error
42 1.4 rillig .endif
43 1.4 rillig
44 1.4 rillig # Like in several other places in variable expressions, when
45 1.4 rillig # ApplyModifier_Defined calls Var_Parse, double dollars lead to a parse
46 1.4 rillig # error that is silently ignored. This makes all dollar signs disappear,
47 1.4 rillig # except for the last, which is a well-formed variable expression.
48 1.4 rillig #
49 1.4 rillig .if ${DEF:D$$$$$${DEF}} != "defined"
50 1.4 rillig . error
51 1.4 rillig .endif
52 1.4 rillig
53 1.4 rillig # Any other text is written without any further escaping. In contrast
54 1.4 rillig # to the :M modifier, parentheses and braces do not need to be nested.
55 1.4 rillig # Instead, the :D modifier is implemented sanely by parsing nested
56 1.4 rillig # expressions as such, without trying any shortcuts. See ApplyModifier_Match
57 1.4 rillig # for an inferior variant.
58 1.4 rillig #
59 1.4 rillig .if ${DEF:D!&((((} != "!&(((("
60 1.4 rillig . error
61 1.4 rillig .endif
62 1.4 rillig
63 1.4 rillig # TODO: Add more tests for parsing the plain text part, to cover each branch
64 1.4 rillig # of ApplyModifier_Defined.
65 1.4 rillig
66 1.1 rillig all:
67 1.1 rillig @:;
68