varmod-ifelse.mk revision 1.5 1 1.5 rillig # $NetBSD: varmod-ifelse.mk,v 1.5 2020/10/23 14:24:51 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the ${cond:?then:else} variable modifier, which evaluates either
4 1.2 rillig # the then-expression or the else-expression, depending on the condition.
5 1.5 rillig #
6 1.5 rillig # The modifier was added on 1998-04-01.
7 1.5 rillig #
8 1.5 rillig # Until 2015-10-11, the modifier always evaluated both the "then" and the
9 1.5 rillig # "else" expressions.
10 1.1 rillig
11 1.1 rillig # TODO: Implementation
12 1.1 rillig
13 1.5 rillig # The variable name of the expression is expanded and then taken as the
14 1.5 rillig # condition. In this case it becomes:
15 1.5 rillig #
16 1.5 rillig # variable expression == "variable expression"
17 1.5 rillig #
18 1.5 rillig # This confuses the parser, which expects an operator instead of the bare
19 1.5 rillig # word "expression". If the name were expanded lazily, everything would be
20 1.5 rillig # fine since the condition would be:
21 1.5 rillig #
22 1.5 rillig # ${:Uvariable expression} == "literal"
23 1.5 rillig #
24 1.5 rillig # Evaluating the variable name lazily would require additional code in
25 1.5 rillig # Var_Parse and ParseVarname, it would be more useful and predictable
26 1.5 rillig # though.
27 1.5 rillig .if ${${:Uvariable expression} == "literal":?bad:bad}
28 1.5 rillig . error
29 1.5 rillig .else
30 1.5 rillig . error
31 1.5 rillig .endif
32 1.5 rillig
33 1.5 rillig # In a variable assignment, undefined variables are not an error.
34 1.5 rillig # Because of the early expansion, the whole condition evaluates to
35 1.5 rillig # ' == ""' though, which cannot be parsed because the left-hand side looks
36 1.5 rillig # empty.
37 1.5 rillig COND:= ${${UNDEF} == "":?bad-assign:bad-assign}
38 1.5 rillig
39 1.5 rillig # In a condition, undefined variables generate a "Malformed conditional"
40 1.5 rillig # error. That error message is wrong though. In lint mode, the correct
41 1.5 rillig # "Undefined variable" error message is generated.
42 1.5 rillig # The difference to the ':=' variable assignment is the additional
43 1.5 rillig # "Malformed conditional" error message.
44 1.5 rillig .if ${${UNDEF} == "":?bad-cond:bad-cond}
45 1.5 rillig . error
46 1.5 rillig .else
47 1.5 rillig . error
48 1.5 rillig .endif
49 1.5 rillig
50 1.4 rillig # When the :? is parsed, it is greedy. The else branch spans all the
51 1.4 rillig # text, up until the closing character '}', even if the text looks like
52 1.4 rillig # another modifier.
53 1.4 rillig .if ${1:?then:else:Q} != "then"
54 1.4 rillig . error
55 1.4 rillig .endif
56 1.4 rillig .if ${0:?then:else:Q} != "else:Q"
57 1.4 rillig . error
58 1.4 rillig .endif
59 1.3 rillig
60 1.1 rillig all:
61 1.1 rillig @:;
62