cond-undef-lint.mk revision 1.2 1 1.2 rillig # $NetBSD: cond-undef-lint.mk,v 1.2 2020/09/14 07:13:29 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for defined and undefined variables in .if conditions, in lint mode.
4 1.1 rillig #
5 1.1 rillig # As of 2020-09-14, lint mode contains experimental code for printing
6 1.1 rillig # accurate error messages in case of undefined variables, instead of the
7 1.1 rillig # wrong "Malformed condition".
8 1.2 rillig #
9 1.2 rillig # See also:
10 1.2 rillig # opt-debug-lint.mk
11 1.1 rillig
12 1.1 rillig .MAKEFLAGS: -dL
13 1.1 rillig
14 1.1 rillig # DEF is defined, UNDEF is not.
15 1.1 rillig DEF= defined
16 1.1 rillig
17 1.1 rillig # An expression based on a defined variable is fine.
18 1.1 rillig .if !${DEF}
19 1.1 rillig . error
20 1.1 rillig .endif
21 1.1 rillig
22 1.1 rillig # Since the condition fails to evaluate, neither of the branches is taken.
23 1.1 rillig .if ${UNDEF}
24 1.1 rillig . error
25 1.1 rillig .else
26 1.1 rillig . error
27 1.1 rillig .endif
28 1.1 rillig
29 1.1 rillig # The variable name depends on the undefined variable, which is probably a
30 1.1 rillig # mistake. The variable UNDEF, as used here, can be easily turned into
31 1.1 rillig # an expression that is always defined, using the :U modifier.
32 1.1 rillig #
33 1.1 rillig # The outer expression does not generate an error message since there was
34 1.1 rillig # already an error evaluating this variable's name.
35 1.1 rillig #
36 1.1 rillig # TODO: Suppress the error message "Variable VAR. is undefined". That part
37 1.1 rillig # of the expression must not be evaluated at all.
38 1.1 rillig .if ${VAR.${UNDEF}}
39 1.1 rillig . error
40 1.1 rillig .else
41 1.1 rillig . error
42 1.1 rillig .endif
43 1.1 rillig
44 1.1 rillig # The variable VAR.defined is not defined and thus generates an error message.
45 1.1 rillig .if ${VAR.${DEF}}
46 1.1 rillig . error
47 1.1 rillig .else
48 1.1 rillig . error
49 1.1 rillig .endif
50 1.1 rillig
51 1.1 rillig
52 1.1 rillig # Variables that are referenced indirectly may be undefined in a condition.
53 1.1 rillig #
54 1.1 rillig # A practical example for this is CFLAGS, which consists of CWARNS, COPTS
55 1.1 rillig # and a few others. Just because these nested variables are not defined,
56 1.1 rillig # this does not make the condition invalid.
57 1.1 rillig #
58 1.1 rillig # The crucial point is that at the point where the variable appears in the
59 1.1 rillig # condition, there is no way to influence the definedness of the nested
60 1.1 rillig # variables. In particular, there is no modifier that would turn undefined
61 1.1 rillig # nested variables into empty strings, as an equivalent to the :U modifier.
62 1.1 rillig INDIRECT= ${NESTED_UNDEF} ${NESTED_DEF}
63 1.1 rillig NESTED_DEF= nested-defined
64 1.1 rillig
65 1.1 rillig # Since NESTED_UNDEF is not controllable at this point, it must not generate
66 1.2 rillig # an error message, and it doesn't do so, since 2020-09-14.
67 1.1 rillig .if !${INDIRECT}
68 1.1 rillig . error
69 1.1 rillig .endif
70