xref: /freebsd/contrib/bmake/unit-tests/counter-append.mk (revision b0c40a00a67f611868fc0f10bde6b28eb75931be)
1*b0c40a00SSimon J. Gerraty# $NetBSD: counter-append.mk,v 1.5 2021/04/04 10:13:09 rillig Exp $
2956e45f6SSimon J. Gerraty#
3956e45f6SSimon J. Gerraty# Demonstrates how to let make count the number of times a variable
4956e45f6SSimon J. Gerraty# is actually accessed, using the ::+= variable modifier.
5956e45f6SSimon J. Gerraty#
6956e45f6SSimon J. Gerraty# This works since 2020-09-23.  Before that, the counter ended up at having
7956e45f6SSimon J. Gerraty# 6 words, even though the NEXT variable was only accessed 3 times.
8956e45f6SSimon J. Gerraty# The cause for this surprising behavior was that the ::= variable modifiers
9956e45f6SSimon J. Gerraty# returned an error marker instead of a simple empty string.
10956e45f6SSimon J. Gerraty
11956e45f6SSimon J. GerratyRELEVANT=	yes (load-time part)	# just to filter the output
12956e45f6SSimon J. Gerraty
13956e45f6SSimon J. GerratyCOUNTER=	# zero
14956e45f6SSimon J. Gerraty
15956e45f6SSimon J. GerratyNEXT=		${COUNTER::+=a}${COUNTER:[#]}
16956e45f6SSimon J. Gerraty
17956e45f6SSimon J. Gerraty# This variable is first set to empty and then expanded.
18*b0c40a00SSimon J. Gerraty# See parse.c, function Parse_Var, keyword "!Var_Exists".
19956e45f6SSimon J. GerratyA:=		${NEXT}
20956e45f6SSimon J. GerratyB:=		${NEXT}
21956e45f6SSimon J. GerratyC:=		${NEXT}
22956e45f6SSimon J. Gerraty
23956e45f6SSimon J. GerratyRELEVANT=	no
24956e45f6SSimon J. Gerraty
25956e45f6SSimon J. Gerratyall:
26956e45f6SSimon J. Gerraty	@: ${RELEVANT::=yes (run-time part)}
27956e45f6SSimon J. Gerraty	@echo A=${A:Q} B=${B:Q} C=${C:Q} COUNTER=${COUNTER:[#]:Q}
28956e45f6SSimon J. Gerraty	@: ${RELEVANT::=no}
29