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