1 1.8 rillig # $NetBSD: var-recursive.mk,v 1.8 2024/08/27 04:52:14 rillig Exp $ 2 1.1 rillig # 3 1.8 rillig # Tests for expressions that refer to themselves and thus cannot be 4 1.8 rillig # evaluated, as that would lead to an endless loop. 5 1.1 rillig 6 1.8 rillig .if make(loadtime) 7 1.1 rillig 8 1.8 rillig DIRECT= ${DIRECT} # Defining a recursive variable is not an error. 9 1.8 rillig # expect+2: while evaluating variable "DIRECT" with value "${DIRECT}": Variable DIRECT is recursive. 10 1.8 rillig # expect+1: <> 11 1.8 rillig . info <${DIRECT}> # But expanding such a variable is an error. 12 1.1 rillig 13 1.1 rillig 14 1.1 rillig # The chain of variables that refer to each other may be long. 15 1.1 rillig INDIRECT1= ${INDIRECT2} 16 1.1 rillig INDIRECT2= ${INDIRECT1} 17 1.8 rillig # expect+2: while evaluating variable "INDIRECT1" with value "${INDIRECT2}": while evaluating variable "INDIRECT2" with value "${INDIRECT1}": Variable INDIRECT1 is recursive. 18 1.8 rillig # expect+1: <> 19 1.8 rillig . info <${INDIRECT1}> 20 1.1 rillig 21 1.1 rillig 22 1.1 rillig # The variable refers to itself, but only in the branch of a condition that 23 1.8 rillig # is not satisfied and is thus not evaluated. 24 1.1 rillig CONDITIONAL= ${1:?ok:${CONDITIONAL}} 25 1.8 rillig # expect+1: <ok> 26 1.8 rillig . info <${CONDITIONAL}> 27 1.8 rillig 28 1.8 rillig 29 1.8 rillig # An expression with modifiers is skipped halfway. This can lead to wrong 30 1.8 rillig # follow-up error messages, but recursive variables occur seldom. 31 1.8 rillig MODIFIERS= ${MODIFIERS:Mpattern} 32 1.8 rillig # expect+2: while evaluating variable "MODIFIERS" with value "${MODIFIERS:Mpattern}": Variable MODIFIERS is recursive. 33 1.8 rillig # expect+1: <Mpattern}> 34 1.8 rillig . info <${MODIFIERS}> 35 1.1 rillig 36 1.2 rillig 37 1.2 rillig # Short variable names can be expanded using the short-hand $V notation, 38 1.2 rillig # which takes a different code path in Var_Parse for parsing the variable 39 1.2 rillig # name. Ensure that these are checked as well. 40 1.2 rillig V= $V 41 1.8 rillig # expect+2: while evaluating variable "V" with value "$V": Variable V is recursive. 42 1.8 rillig # expect+1: <> 43 1.8 rillig . info <$V> 44 1.2 rillig 45 1.8 rillig .elif make(runtime) 46 1.3 rillig 47 1.3 rillig # If a recursive variable is accessed in a command of a target, the makefiles 48 1.8 rillig # have already been fully parsed, so there is no location information from the 49 1.8 rillig # .include and .for directives. In such a case, use the location of the last 50 1.4 rillig # command of the target to provide at least a hint to the location. 51 1.3 rillig VAR= ${VAR} 52 1.8 rillig runtime: 53 1.3 rillig : OK 54 1.8 rillig # expect: make: in target "runtime": while evaluating variable "VAR" with value "${VAR}": Variable VAR is recursive. 55 1.8 rillig : <${VAR}> 56 1.3 rillig : OK 57 1.3 rillig 58 1.1 rillig .else 59 1.1 rillig 60 1.1 rillig all: 61 1.8 rillig @${MAKE} -f ${MAKEFILE} loadtime || echo "sub-exit status $$?" 62 1.8 rillig @${MAKE} -f ${MAKEFILE} runtime || echo "sub-exit status $$?" 63 1.8 rillig 64 1.8 rillig .endif 65