1*e2eeea75SSimon J. Gerraty# $NetBSD: cond-undef-lint.mk,v 1.3 2020/11/15 14:58:14 rillig Exp $ 2956e45f6SSimon J. Gerraty# 3956e45f6SSimon J. Gerraty# Tests for defined and undefined variables in .if conditions, in lint mode. 4956e45f6SSimon J. Gerraty# 5956e45f6SSimon J. Gerraty# As of 2020-09-14, lint mode contains experimental code for printing 6956e45f6SSimon J. Gerraty# accurate error messages in case of undefined variables, instead of the 7956e45f6SSimon J. Gerraty# wrong "Malformed condition". 8956e45f6SSimon J. Gerraty# 9956e45f6SSimon J. Gerraty# See also: 10956e45f6SSimon J. Gerraty# opt-debug-lint.mk 11956e45f6SSimon J. Gerraty 12956e45f6SSimon J. Gerraty.MAKEFLAGS: -dL 13956e45f6SSimon J. Gerraty 14956e45f6SSimon J. Gerraty# DEF is defined, UNDEF is not. 15956e45f6SSimon J. GerratyDEF= defined 16956e45f6SSimon J. Gerraty 17956e45f6SSimon J. Gerraty# An expression based on a defined variable is fine. 18956e45f6SSimon J. Gerraty.if !${DEF} 19956e45f6SSimon J. Gerraty. error 20956e45f6SSimon J. Gerraty.endif 21956e45f6SSimon J. Gerraty 22956e45f6SSimon J. Gerraty# Since the condition fails to evaluate, neither of the branches is taken. 23956e45f6SSimon J. Gerraty.if ${UNDEF} 24956e45f6SSimon J. Gerraty. error 25956e45f6SSimon J. Gerraty.else 26956e45f6SSimon J. Gerraty. error 27956e45f6SSimon J. Gerraty.endif 28956e45f6SSimon J. Gerraty 29956e45f6SSimon J. Gerraty# The variable name depends on the undefined variable, which is probably a 30956e45f6SSimon J. Gerraty# mistake. The variable UNDEF, as used here, can be easily turned into 31956e45f6SSimon J. Gerraty# an expression that is always defined, using the :U modifier. 32956e45f6SSimon J. Gerraty# 33956e45f6SSimon J. Gerraty# The outer expression does not generate an error message since there was 34956e45f6SSimon J. Gerraty# already an error evaluating this variable's name. 35956e45f6SSimon J. Gerraty# 36956e45f6SSimon J. Gerraty# TODO: Suppress the error message "Variable VAR. is undefined". That part 37956e45f6SSimon J. Gerraty# of the expression must not be evaluated at all. 38956e45f6SSimon J. Gerraty.if ${VAR.${UNDEF}} 39956e45f6SSimon J. Gerraty. error 40956e45f6SSimon J. Gerraty.else 41956e45f6SSimon J. Gerraty. error 42956e45f6SSimon J. Gerraty.endif 43956e45f6SSimon J. Gerraty 44956e45f6SSimon J. Gerraty# The variable VAR.defined is not defined and thus generates an error message. 45*e2eeea75SSimon J. Gerraty# 46*e2eeea75SSimon J. Gerraty# TODO: This pattern looks a lot like CFLAGS.${OPSYS}, which is at least 47*e2eeea75SSimon J. Gerraty# debatable. Or would any practical use of CFLAGS.${OPSYS} be via an indirect 48*e2eeea75SSimon J. Gerraty# expression, as in the next example? 49956e45f6SSimon J. Gerraty.if ${VAR.${DEF}} 50956e45f6SSimon J. Gerraty. error 51956e45f6SSimon J. Gerraty.else 52956e45f6SSimon J. Gerraty. error 53956e45f6SSimon J. Gerraty.endif 54956e45f6SSimon J. Gerraty 55956e45f6SSimon J. Gerraty 56956e45f6SSimon J. Gerraty# Variables that are referenced indirectly may be undefined in a condition. 57956e45f6SSimon J. Gerraty# 58956e45f6SSimon J. Gerraty# A practical example for this is CFLAGS, which consists of CWARNS, COPTS 59956e45f6SSimon J. Gerraty# and a few others. Just because these nested variables are not defined, 60956e45f6SSimon J. Gerraty# this does not make the condition invalid. 61956e45f6SSimon J. Gerraty# 62956e45f6SSimon J. Gerraty# The crucial point is that at the point where the variable appears in the 63956e45f6SSimon J. Gerraty# condition, there is no way to influence the definedness of the nested 64956e45f6SSimon J. Gerraty# variables. In particular, there is no modifier that would turn undefined 65956e45f6SSimon J. Gerraty# nested variables into empty strings, as an equivalent to the :U modifier. 66956e45f6SSimon J. GerratyINDIRECT= ${NESTED_UNDEF} ${NESTED_DEF} 67956e45f6SSimon J. GerratyNESTED_DEF= nested-defined 68956e45f6SSimon J. Gerraty 69956e45f6SSimon J. Gerraty# Since NESTED_UNDEF is not controllable at this point, it must not generate 70956e45f6SSimon J. Gerraty# an error message, and it doesn't do so, since 2020-09-14. 71956e45f6SSimon J. Gerraty.if !${INDIRECT} 72956e45f6SSimon J. Gerraty. error 73956e45f6SSimon J. Gerraty.endif 74