cond-undef-lint.mk revision 1.7 1 # $NetBSD: cond-undef-lint.mk,v 1.7 2025/01/11 20:54:45 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 # See also:
10 # opt-debug-lint.mk
11
12 .MAKEFLAGS: -dL
13
14 # DEF is defined, UNDEF is not.
15 DEF= defined
16
17 # An expression based on a defined variable is fine.
18 .if !${DEF}
19 . error
20 .endif
21
22 # Since the condition fails to evaluate, neither of the branches is taken.
23 # expect+1: Variable "UNDEF" is undefined
24 .if ${UNDEF}
25 . error
26 .else
27 . error
28 .endif
29
30 # The variable name depends on the undefined variable, which is probably a
31 # mistake. The variable UNDEF, as used here, can be easily turned into
32 # an expression that is always defined, using the :U modifier.
33 #
34 # The outer expression does not generate an error message since there was
35 # already an error evaluating this variable's name.
36 #
37 # TODO: Suppress the error message "Variable VAR. is undefined". That part
38 # of the expression must not be evaluated at all.
39 # expect+2: Variable "UNDEF" is undefined
40 # expect+1: Variable "VAR." is undefined
41 .if ${VAR.${UNDEF}}
42 . error
43 .else
44 . error
45 .endif
46
47 # The variable VAR.defined is not defined and thus generates an error message.
48 #
49 # TODO: This pattern looks a lot like CFLAGS.${OPSYS}, which is at least
50 # debatable. Or would any practical use of CFLAGS.${OPSYS} be via an indirect
51 # expression, as in the next example?
52 # expect+1: Variable "VAR.defined" is undefined
53 .if ${VAR.${DEF}}
54 . error
55 .else
56 . error
57 .endif
58
59
60 # Variables that are referenced indirectly may be undefined in a condition.
61 #
62 # A practical example for this is CFLAGS, which consists of CWARNS, COPTS
63 # and a few others. Just because these nested variables are not defined,
64 # this does not make the condition invalid.
65 #
66 # The crucial point is that at the point where the variable appears in the
67 # condition, there is no way to influence the definedness of the nested
68 # variables. In particular, there is no modifier that would turn undefined
69 # nested variables into empty strings, as an equivalent to the :U modifier.
70 INDIRECT= ${NESTED_UNDEF} ${NESTED_DEF}
71 NESTED_DEF= nested-defined
72
73 # Since NESTED_UNDEF is not controllable at this point, it must not generate
74 # an error message, and it doesn't do so, since 2020-09-14.
75 .if !${INDIRECT}
76 . error
77 .endif
78