xref: /freebsd/contrib/bmake/unit-tests/counter.mk (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1# $NetBSD: counter.mk,v 1.6 2021/04/04 10:13:09 rillig Exp $
2#
3# Demonstrates how to let make count the number of times a variable
4# is actually accessed, using the ::= variable modifier.
5#
6# This works since 2020-09-23.  Before that, the counter ended up at having
7# 4 words, even though the NEXT variable was only accessed 3 times.
8# The cause for this surprising behavior was that the ::= variable modifiers
9# returned an error marker instead of a simple empty string.
10
11RELEVANT=	yes (load-time part)	# just to filter the output
12
13COUNTER=	# zero
14
15NEXT=		${COUNTER::=${COUNTER} a}${COUNTER:[#]}
16
17# This variable is first set to empty and then expanded.
18# See parse.c, function Parse_Var, keyword "!Var_Exists".
19A:=		${NEXT}
20B:=		${NEXT}
21C:=		${NEXT}
22
23RELEVANT=	no
24
25all:
26	@: ${RELEVANT::=yes (run-time part)}
27	@echo A=${A:Q} B=${B:Q} C=${C:Q} COUNTER=${COUNTER:[#]:Q}
28	@: ${RELEVANT::=no}
29