Home | History | Annotate | Line # | Download | only in unit-tests
counter-append.mk revision 1.2
      1 # $NetBSD: counter-append.mk,v 1.2 2020/09/23 07:50:58 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 # 6 words, even though the NEXT variable was only accessed 3 times.
      8 
      9 .MAKEFLAGS: -dv
     10 
     11 RELEVANT=	yes (load-time part)	# just to filter the output
     12 
     13 COUNTER=	# zero
     14 
     15 NEXT=		${COUNTER::+=a}${COUNTER:[#]}
     16 
     17 # This variable is first set to empty and then expanded.
     18 # See parse.c, function Parse_DoVar, keyword "!Var_Exists".
     19 A:=		${NEXT}
     20 B:=		${NEXT}
     21 C:=		${NEXT}
     22 
     23 RELEVANT=	no
     24 
     25 all:
     26 	@: ${RELEVANT::=yes (run-time part)}
     27 	@echo A=${A:Q} B=${B:Q} C=${C:Q} COUNTER=${COUNTER:[#]:Q}
     28 	@: ${RELEVANT::=no}
     29