1*e2eeea75SSimon J. Gerraty# $NetBSD: directive-for.mk,v 1.9 2020/11/15 20:20:58 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for the .for directive. 4*e2eeea75SSimon J. Gerraty# 5*e2eeea75SSimon J. Gerraty# TODO: Describe naming conventions for the loop variables. 6*e2eeea75SSimon J. Gerraty# .for f in values 7*e2eeea75SSimon J. Gerraty# .for file in values 8*e2eeea75SSimon J. Gerraty# .for _FILE_ in values 9*e2eeea75SSimon J. Gerraty# .for .FILE. in values 10*e2eeea75SSimon J. Gerraty# .for _f_ in values 112c3632d1SSimon J. Gerraty 122c3632d1SSimon J. Gerraty# Using the .for loop, lists of values can be produced. 132c3632d1SSimon J. Gerraty# In simple cases, the :@var@${var}@ variable modifier can be used to 142c3632d1SSimon J. Gerraty# reach the same effects. 152c3632d1SSimon J. Gerraty# 162c3632d1SSimon J. Gerraty.undef NUMBERS 172c3632d1SSimon J. Gerraty.for num in 1 2 3 182c3632d1SSimon J. GerratyNUMBERS+= ${num} 192c3632d1SSimon J. Gerraty.endfor 202c3632d1SSimon J. Gerraty.if ${NUMBERS} != "1 2 3" 212c3632d1SSimon J. Gerraty. error 222c3632d1SSimon J. Gerraty.endif 232c3632d1SSimon J. Gerraty 242c3632d1SSimon J. Gerraty# The .for loop also works for multiple iteration variables. 25*e2eeea75SSimon J. Gerraty# This is something that the variable modifier :@ cannot do. 262c3632d1SSimon J. Gerraty.for name value in VARNAME value NAME2 value2 272c3632d1SSimon J. Gerraty${name}= ${value} 282c3632d1SSimon J. Gerraty.endfor 292c3632d1SSimon J. Gerraty.if ${VARNAME} != "value" || ${NAME2} != "value2" 302c3632d1SSimon J. Gerraty. error 312c3632d1SSimon J. Gerraty.endif 322c3632d1SSimon J. Gerraty 332c3632d1SSimon J. Gerraty# The .for loop splits the items at whitespace, taking quotes into account, 342c3632d1SSimon J. Gerraty# just like the :M or :S variable modifiers. 352c3632d1SSimon J. Gerraty# 362c3632d1SSimon J. Gerraty# Until 2012-06-03, it had split the items exactly at whitespace, without 37*e2eeea75SSimon J. Gerraty# taking the quotes into account. This had resulted in 10 words. 382c3632d1SSimon J. Gerraty# 392c3632d1SSimon J. Gerraty.undef WORDS 402c3632d1SSimon J. Gerraty.for var in one t\ w\ o "three three" 'four four' `five six` 412c3632d1SSimon J. GerratyWORDS+= counted 422c3632d1SSimon J. Gerraty.endfor 432c3632d1SSimon J. Gerraty.if ${WORDS:[#]} != 6 442c3632d1SSimon J. Gerraty. error 452c3632d1SSimon J. Gerraty.endif 462c3632d1SSimon J. Gerraty 472c3632d1SSimon J. Gerraty# In the body of the .for loop, the iteration variables can be accessed 482c3632d1SSimon J. Gerraty# like normal variables, even though they are not really variables. 492c3632d1SSimon J. Gerraty# 502c3632d1SSimon J. Gerraty# Instead, the expression ${var} is transformed into ${:U1}, ${:U2} and so 512c3632d1SSimon J. Gerraty# on, before the loop body is evaluated. 522c3632d1SSimon J. Gerraty# 532c3632d1SSimon J. Gerraty# A notable effect of this implementation technique is that the .for 542c3632d1SSimon J. Gerraty# iteration variables and the normal global variables live in separate 552c3632d1SSimon J. Gerraty# namespaces and do not influence each other. 562c3632d1SSimon J. Gerraty# 572c3632d1SSimon J. Gerratyvar= value before 582c3632d1SSimon J. Gerratyvar2= value before 592c3632d1SSimon J. Gerraty.for var var2 in 1 2 3 4 602c3632d1SSimon J. Gerraty.endfor 612c3632d1SSimon J. Gerraty.if ${var} != "value before" 622c3632d1SSimon J. Gerraty. warning After the .for loop, var must still have its original value. 632c3632d1SSimon J. Gerraty.endif 642c3632d1SSimon J. Gerraty.if ${var2} != "value before" 652c3632d1SSimon J. Gerraty. warning After the .for loop, var2 must still have its original value. 662c3632d1SSimon J. Gerraty.endif 672c3632d1SSimon J. Gerraty 682c3632d1SSimon J. Gerraty# Everything from the paragraph above also applies if the loop body is 692c3632d1SSimon J. Gerraty# empty, even if there is no actual iteration since the loop items are 702c3632d1SSimon J. Gerraty# also empty. 712c3632d1SSimon J. Gerraty# 722c3632d1SSimon J. Gerratyvar= value before 732c3632d1SSimon J. Gerratyvar2= value before 742c3632d1SSimon J. Gerraty.for var var2 in ${:U} 752c3632d1SSimon J. Gerraty.endfor 762c3632d1SSimon J. Gerraty.if ${var} != "value before" 772c3632d1SSimon J. Gerraty. warning After the .for loop, var must still have its original value. 782c3632d1SSimon J. Gerraty.endif 792c3632d1SSimon J. Gerraty.if ${var2} != "value before" 802c3632d1SSimon J. Gerraty. warning After the .for loop, var2 must still have its original value. 812c3632d1SSimon J. Gerraty.endif 822c3632d1SSimon J. Gerraty 832c3632d1SSimon J. Gerraty# Until 2008-12-21, the values of the iteration variables were simply 842c3632d1SSimon J. Gerraty# inserted as plain text and then parsed as usual, which made it possible 852c3632d1SSimon J. Gerraty# to achieve all kinds of strange effects. 862c3632d1SSimon J. Gerraty# 872c3632d1SSimon J. Gerraty# Before that date, the .for loop expanded to: 882c3632d1SSimon J. Gerraty# EXPANSION+= value 892c3632d1SSimon J. Gerraty# Since that date, the .for loop expands to: 902c3632d1SSimon J. Gerraty# EXPANSION${:U+}= value 912c3632d1SSimon J. Gerraty# 922c3632d1SSimon J. GerratyEXPANSION= before 932c3632d1SSimon J. GerratyEXPANSION+ = before 942c3632d1SSimon J. Gerraty.for plus in + 952c3632d1SSimon J. GerratyEXPANSION${plus}= value 962c3632d1SSimon J. Gerraty.endfor 972c3632d1SSimon J. Gerraty.if ${EXPANSION} != "before" 982c3632d1SSimon J. Gerraty. error This must be a make from before 2009. 992c3632d1SSimon J. Gerraty.endif 1002c3632d1SSimon J. Gerraty.if ${EXPANSION+} != "value" 1012c3632d1SSimon J. Gerraty. error This must be a make from before 2009. 1022c3632d1SSimon J. Gerraty.endif 1032c3632d1SSimon J. Gerraty 104956e45f6SSimon J. Gerraty# When the outer .for loop is expanded, it sees the expression ${i} and 105956e45f6SSimon J. Gerraty# expands it. The inner loop then has nothing more to expand. 106956e45f6SSimon J. Gerraty.for i in outer 107956e45f6SSimon J. Gerraty. for i in inner 108956e45f6SSimon J. Gerraty. info ${i} 109956e45f6SSimon J. Gerraty. endfor 110956e45f6SSimon J. Gerraty.endfor 111956e45f6SSimon J. Gerraty 112956e45f6SSimon J. Gerraty# From https://gnats.netbsd.org/29985. 113956e45f6SSimon J. Gerraty# 114956e45f6SSimon J. Gerraty# Until 2008-12-21, the .for loop was expanded by replacing the variable 115956e45f6SSimon J. Gerraty# value literally in the body. This could lead to situations where the 116956e45f6SSimon J. Gerraty# characters from the variable value were interpreted as markup rather than 117956e45f6SSimon J. Gerraty# plain text. 118956e45f6SSimon J. Gerraty# 119956e45f6SSimon J. Gerraty# Until 2012-06-03, the .for loop had split the words at whitespace, without 120956e45f6SSimon J. Gerraty# taking quotes into account. This made it possible to have variable values 121956e45f6SSimon J. Gerraty# like "a:\ a:\file.txt" that ended in a single backslash. Since then, the 122956e45f6SSimon J. Gerraty# variable values have been replaced with expressions of the form ${:U...}, 123956e45f6SSimon J. Gerraty# which are not interpreted as code anymore. 124956e45f6SSimon J. Gerraty# 125956e45f6SSimon J. Gerraty# As of 2020-09-22, a comment in for.c says that it may be possible to 126956e45f6SSimon J. Gerraty# produce an "unwanted substitution", but there is no demonstration code yet. 127956e45f6SSimon J. Gerraty# 128956e45f6SSimon J. Gerraty# The above changes prevent a backslash at the end of a word from being 129956e45f6SSimon J. Gerraty# interpreted as part of the code. Because of this, the trailingBackslash 130956e45f6SSimon J. Gerraty# hack in Var_Subst is no longer needed and as of 2020-09-22, has been 131956e45f6SSimon J. Gerraty# removed. 132956e45f6SSimon J. Gerraty.for path in a:\ a:\file.txt d:\\ d:\\file.txt 133956e45f6SSimon J. Gerraty. info ${path} 134956e45f6SSimon J. Gerraty.endfor 135956e45f6SSimon J. Gerraty 136956e45f6SSimon J. Gerraty# Ensure that braces and parentheses are properly escaped by the .for loop. 137956e45f6SSimon J. Gerraty# Each line must print the same word 3 times. 138956e45f6SSimon J. Gerraty# See GetEscapes. 139956e45f6SSimon J. Gerraty.for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{ 140956e45f6SSimon J. Gerraty. info $v ${v} $(v) 141956e45f6SSimon J. Gerraty.endfor 142956e45f6SSimon J. Gerraty 143956e45f6SSimon J. Gerraty# As of 2020-10-25, the variable names may contain arbitrary characters, 144956e45f6SSimon J. Gerraty# except for whitespace. This allows for creative side effects. Hopefully 145956e45f6SSimon J. Gerraty# nobody is misusing this "feature". 146956e45f6SSimon J. Gerratyvar= outer 147956e45f6SSimon J. Gerraty.for var:Q in value "quoted" 148956e45f6SSimon J. Gerraty. info ${var} ${var:Q} ${var:Q:Q} 149956e45f6SSimon J. Gerraty.endfor 150956e45f6SSimon J. Gerraty 1512c3632d1SSimon J. Gerratyall: 1522c3632d1SSimon J. Gerraty @:; 153