1# $NetBSD: var-recursive.mk,v 1.6 2023/11/19 21:47:52 rillig Exp $
2#
3# Tests for expressions that refer to themselves and thus
4# cannot be evaluated.
5
6TESTS=	direct indirect conditional short target
7
8# Since make exits immediately when it detects a recursive expression,
9# the actual tests are run in sub-makes.
10TEST?=	# none
11.if ${TEST} == ""
12all:
13.for test in ${TESTS}
14	@${.MAKE} -f ${MAKEFILE} TEST=${test} || :
15.endfor
16
17.elif ${TEST} == direct
18
19DIRECT=	${DIRECT}	# Defining a recursive variable is not yet an error.
20# expect+1: still there
21.  info still there	# Therefore this line is printed.
22.  info ${DIRECT}	# But expanding the variable is an error.
23
24.elif ${TEST} == indirect
25
26# The chain of variables that refer to each other may be long.
27INDIRECT1=	${INDIRECT2}
28INDIRECT2=	${INDIRECT1}
29.  info ${INDIRECT1}
30
31.elif ${TEST} == conditional
32
33# The variable refers to itself, but only in the branch of a condition that
34# is never satisfied and is thus not evaluated.
35CONDITIONAL=	${1:?ok:${CONDITIONAL}}
36# expect+1: ok
37.  info ${CONDITIONAL}
38
39.elif ${TEST} == short
40
41# Short variable names can be expanded using the short-hand $V notation,
42# which takes a different code path in Var_Parse for parsing the variable
43# name.  Ensure that these are checked as well.
44V=	$V
45.  info $V
46
47.elif ${TEST} == target
48
49# If a recursive variable is accessed in a command of a target, the makefiles
50# are not parsed anymore, so there is no location information from the
51# .includes and .for directives.  In such a case, use the location of the last
52# command of the target to provide at least a hint to the location.
53VAR=	${VAR}
54target:
55	: OK
56	: ${VAR}
57	: OK
58
59.else
60.  error Unknown test "${TEST}"
61.endif
62
63all:
64