counter.mk revision 1.1
1# $NetBSD: counter.mk,v 1.1 2020/08/02 14:53:02 rillig Exp $
2#
3# Demonstrates that it is not easily possible to let make count
4# the number of times a variable is actually accessed.
5#
6# As of 2020-08-02, the counter ends up at having 4 words, even
7# though the NEXT variable is only accessed 3 times.  This is
8# surprising.
9#
10# A hint to this surprising behavior is that the variables don't
11# get fully expanded.  For example, A does not simply contain the
12# value "1" but an additional unexpanded ${COUNTER:...} before it.
13
14RELEVANT=	yes (load-time part)	# just to filter the output
15
16COUNTER=	# zero
17
18NEXT=		${COUNTER::=${COUNTER} a}${COUNTER:[#]}
19
20# This variable is first set to empty and then expanded.
21# See parse.c, function Parse_DoVar, keyword "!Var_Exists".
22A:=		${NEXT}
23B:=		${NEXT}
24C:=		${NEXT}
25
26RELEVANT=	no
27
28all:
29	@: ${RELEVANT::=yes (run-time part)}
30	@echo A=${A:Q} B=${B:Q} C=${C:Q} COUNTER=${COUNTER:[#]:Q}
31	@: ${RELEVANT::=no}
32