1 # $NetBSD: cond-undef-lint.mk,v 1.8 2025/01/11 21:21:33 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 # expect+1: Variable "VAR." is undefined 35 .if ${VAR.${UNDEF}} 36 . error 37 .else 38 . error 39 .endif 40 41 # The inner variable DEF is defined, but the resulting name VAR.defined 42 # refers to an undefined variable, thus an error message. 43 # 44 # expect+1: Variable "VAR.defined" is undefined 45 .if ${VAR.${DEF}} 46 . error 47 .else 48 . error 49 .endif 50 51 52 # Variables that are referenced indirectly may be undefined in a condition. 53 # 54 # A practical example for this is CFLAGS, which consists of CWARNS, COPTS 55 # and a few others. Just because these nested variables are not defined, 56 # this does not make the condition invalid. 57 # 58 # The crucial point is that at the point where the variable appears in the 59 # condition, there is no way to influence the definedness of the nested 60 # variables. In particular, there is no modifier that would turn undefined 61 # nested variables into empty strings, as an equivalent to the :U modifier. 62 INDIRECT= ${NESTED_UNDEF} ${NESTED_DEF} 63 NESTED_DEF= nested-defined 64 65 # Since NESTED_UNDEF is not controllable at this point, it must not generate 66 # an error message, and it doesn't do so, since 2020-09-14. 67 .if !${INDIRECT} 68 . error 69 .endif 70