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