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