1*148ee845SSimon J. Gerraty# $NetBSD: cond-undef-lint.mk,v 1.4 2023/06/01 20:56:35 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. 23*148ee845SSimon J. Gerraty# expect+2: Malformed conditional (${UNDEF}) 24*148ee845SSimon J. Gerraty# expect+1: Variable "UNDEF" is undefined 25956e45f6SSimon J. Gerraty.if ${UNDEF} 26956e45f6SSimon J. Gerraty. error 27956e45f6SSimon J. Gerraty.else 28956e45f6SSimon J. Gerraty. error 29956e45f6SSimon J. Gerraty.endif 30956e45f6SSimon J. Gerraty 31956e45f6SSimon J. Gerraty# The variable name depends on the undefined variable, which is probably a 32956e45f6SSimon J. Gerraty# mistake. The variable UNDEF, as used here, can be easily turned into 33956e45f6SSimon J. Gerraty# an expression that is always defined, using the :U modifier. 34956e45f6SSimon J. Gerraty# 35956e45f6SSimon J. Gerraty# The outer expression does not generate an error message since there was 36956e45f6SSimon J. Gerraty# already an error evaluating this variable's name. 37956e45f6SSimon J. Gerraty# 38956e45f6SSimon J. Gerraty# TODO: Suppress the error message "Variable VAR. is undefined". That part 39956e45f6SSimon J. Gerraty# of the expression must not be evaluated at all. 40*148ee845SSimon J. Gerraty# expect+3: Variable "UNDEF" is undefined 41*148ee845SSimon J. Gerraty# expect+2: Variable "VAR." is undefined 42*148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${VAR.${UNDEF}}) 43956e45f6SSimon J. Gerraty.if ${VAR.${UNDEF}} 44956e45f6SSimon J. Gerraty. error 45956e45f6SSimon J. Gerraty.else 46956e45f6SSimon J. Gerraty. error 47956e45f6SSimon J. Gerraty.endif 48956e45f6SSimon J. Gerraty 49956e45f6SSimon J. Gerraty# The variable VAR.defined is not defined and thus generates an error message. 50e2eeea75SSimon J. Gerraty# 51e2eeea75SSimon J. Gerraty# TODO: This pattern looks a lot like CFLAGS.${OPSYS}, which is at least 52e2eeea75SSimon J. Gerraty# debatable. Or would any practical use of CFLAGS.${OPSYS} be via an indirect 53e2eeea75SSimon J. Gerraty# expression, as in the next example? 54*148ee845SSimon J. Gerraty# expect+2: Variable "VAR.defined" is undefined 55*148ee845SSimon J. Gerraty# expect+1: Malformed conditional (${VAR.${DEF}}) 56956e45f6SSimon J. Gerraty.if ${VAR.${DEF}} 57956e45f6SSimon J. Gerraty. error 58956e45f6SSimon J. Gerraty.else 59956e45f6SSimon J. Gerraty. error 60956e45f6SSimon J. Gerraty.endif 61956e45f6SSimon J. Gerraty 62956e45f6SSimon J. Gerraty 63956e45f6SSimon J. Gerraty# Variables that are referenced indirectly may be undefined in a condition. 64956e45f6SSimon J. Gerraty# 65956e45f6SSimon J. Gerraty# A practical example for this is CFLAGS, which consists of CWARNS, COPTS 66956e45f6SSimon J. Gerraty# and a few others. Just because these nested variables are not defined, 67956e45f6SSimon J. Gerraty# this does not make the condition invalid. 68956e45f6SSimon J. Gerraty# 69956e45f6SSimon J. Gerraty# The crucial point is that at the point where the variable appears in the 70956e45f6SSimon J. Gerraty# condition, there is no way to influence the definedness of the nested 71956e45f6SSimon J. Gerraty# variables. In particular, there is no modifier that would turn undefined 72956e45f6SSimon J. Gerraty# nested variables into empty strings, as an equivalent to the :U modifier. 73956e45f6SSimon J. GerratyINDIRECT= ${NESTED_UNDEF} ${NESTED_DEF} 74956e45f6SSimon J. GerratyNESTED_DEF= nested-defined 75956e45f6SSimon J. Gerraty 76956e45f6SSimon J. Gerraty# Since NESTED_UNDEF is not controllable at this point, it must not generate 77956e45f6SSimon J. Gerraty# an error message, and it doesn't do so, since 2020-09-14. 78956e45f6SSimon J. Gerraty.if !${INDIRECT} 79956e45f6SSimon J. Gerraty. error 80956e45f6SSimon J. Gerraty.endif 81