1# $NetBSD: var-recursive.mk,v 1.4 2022/01/29 10:21:26 rillig Exp $
2#
3# Tests for variable 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.  info still there	# Therefore this line is printed.
21.  info ${DIRECT}	# But expanding the variable is an error.
22
23.elif ${TEST} == indirect
24
25# The chain of variables that refer to each other may be long.
26INDIRECT1=	${INDIRECT2}
27INDIRECT2=	${INDIRECT1}
28.  info ${INDIRECT1}
29
30.elif ${TEST} == conditional
31
32# The variable refers to itself, but only in the branch of a condition that
33# is never satisfied and is thus not evaluated.
34CONDITIONAL=	${1:?ok:${CONDITIONAL}}
35.  info ${CONDITIONAL}
36
37.elif ${TEST} == short
38
39# Short variable names can be expanded using the short-hand $V notation,
40# which takes a different code path in Var_Parse for parsing the variable
41# name.  Ensure that these are checked as well.
42V=	$V
43.  info $V
44
45.elif ${TEST} == target
46
47# If a recursive variable is accessed in a command of a target, the makefiles
48# are not parsed anymore, so there is no location information from the
49# .includes 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.
51VAR=	${VAR}
52target:
53	: OK
54	: ${VAR}
55	: OK
56
57.else
58.  error Unknown test "${TEST}"
59.endif
60
61all:
62