1*c1d01b5fSSimon J. Gerraty# $NetBSD: directive-for.mk,v 1.20 2023/05/10 13:03:06 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for the .for directive. 4e2eeea75SSimon J. Gerraty# 5e2eeea75SSimon J. Gerraty# TODO: Describe naming conventions for the loop variables. 6e2eeea75SSimon J. Gerraty# .for f in values 7e2eeea75SSimon J. Gerraty# .for file in values 8e2eeea75SSimon J. Gerraty# .for _FILE_ in values 9e2eeea75SSimon J. Gerraty# .for .FILE. in values 10e2eeea75SSimon J. Gerraty# .for _f_ in values 112c3632d1SSimon J. Gerraty# 12*c1d01b5fSSimon J. Gerraty# See also: 13*c1d01b5fSSimon J. Gerraty# varmod-loop.mk The ':@var@...@' modifier 14*c1d01b5fSSimon J. Gerraty 15*c1d01b5fSSimon J. Gerraty# expect-all 16*c1d01b5fSSimon J. Gerraty 17*c1d01b5fSSimon J. Gerraty# A typical use case for a .for loop is to populate a variable with a list of 18*c1d01b5fSSimon J. Gerraty# values depending on other variables. In simple cases, the same effect can 19*c1d01b5fSSimon J. Gerraty# be achieved using the ':@var@${var}@' modifier. 202c3632d1SSimon J. Gerraty.undef NUMBERS 212c3632d1SSimon J. Gerraty.for num in 1 2 3 222c3632d1SSimon J. GerratyNUMBERS+= ${num} 232c3632d1SSimon J. Gerraty.endfor 242c3632d1SSimon J. Gerraty.if ${NUMBERS} != "1 2 3" 252c3632d1SSimon J. Gerraty. error 262c3632d1SSimon J. Gerraty.endif 272c3632d1SSimon J. Gerraty 28*c1d01b5fSSimon J. Gerraty 292c3632d1SSimon J. Gerraty# The .for loop also works for multiple iteration variables. 30*c1d01b5fSSimon J. Gerraty# This is something that the modifier :@ cannot do. 312c3632d1SSimon J. Gerraty.for name value in VARNAME value NAME2 value2 322c3632d1SSimon J. Gerraty${name}= ${value} 332c3632d1SSimon J. Gerraty.endfor 342c3632d1SSimon J. Gerraty.if ${VARNAME} != "value" || ${NAME2} != "value2" 352c3632d1SSimon J. Gerraty. error 362c3632d1SSimon J. Gerraty.endif 372c3632d1SSimon J. Gerraty 38*c1d01b5fSSimon J. Gerraty 392c3632d1SSimon J. Gerraty# The .for loop splits the items at whitespace, taking quotes into account, 40*c1d01b5fSSimon J. Gerraty# just like the :M or :S modifiers. 412c3632d1SSimon J. Gerraty# 42*c1d01b5fSSimon J. Gerraty# Until 2012-06-03, the .for loop had split the items exactly at whitespace, 43*c1d01b5fSSimon J. Gerraty# without taking the quotes into account. This had resulted in 10 words. 442c3632d1SSimon J. Gerraty.undef WORDS 452c3632d1SSimon J. Gerraty.for var in one t\ w\ o "three three" 'four four' `five six` 462c3632d1SSimon J. GerratyWORDS+= counted 472c3632d1SSimon J. Gerraty.endfor 482c3632d1SSimon J. Gerraty.if ${WORDS:[#]} != 6 492c3632d1SSimon J. Gerraty. error 502c3632d1SSimon J. Gerraty.endif 512c3632d1SSimon J. Gerraty 52*c1d01b5fSSimon J. Gerraty 532c3632d1SSimon J. Gerraty# In the body of the .for loop, the iteration variables can be accessed 542c3632d1SSimon J. Gerraty# like normal variables, even though they are not really variables. 552c3632d1SSimon J. Gerraty# 56*c1d01b5fSSimon J. Gerraty# Instead, before interpreting the body of the .for loop, the body is 57*c1d01b5fSSimon J. Gerraty# generated by replacing each expression ${var} with ${:U1}, ${:U2} and so 58*c1d01b5fSSimon J. Gerraty# on. 592c3632d1SSimon J. Gerraty# 60*c1d01b5fSSimon J. Gerraty# A noticeable effect of this implementation technique is that the .for 612c3632d1SSimon J. Gerraty# iteration variables and the normal global variables live in separate 62*c1d01b5fSSimon J. Gerraty# namespaces and do not influence each other. The "scope" of the .for loop 63*c1d01b5fSSimon J. Gerraty# variables is restricted to the current makefile, it does not reach over to 64*c1d01b5fSSimon J. Gerraty# any included makefiles. 652c3632d1SSimon J. Gerratyvar= value before 662c3632d1SSimon J. Gerratyvar2= value before 672c3632d1SSimon J. Gerraty.for var var2 in 1 2 3 4 682c3632d1SSimon J. Gerraty.endfor 692c3632d1SSimon J. Gerraty.if ${var} != "value before" 702c3632d1SSimon J. Gerraty. warning After the .for loop, var must still have its original value. 712c3632d1SSimon J. Gerraty.endif 722c3632d1SSimon J. Gerraty.if ${var2} != "value before" 732c3632d1SSimon J. Gerraty. warning After the .for loop, var2 must still have its original value. 742c3632d1SSimon J. Gerraty.endif 752c3632d1SSimon J. Gerraty 762c3632d1SSimon J. Gerraty# Everything from the paragraph above also applies if the loop body is 77*c1d01b5fSSimon J. Gerraty# empty. In this particular example, the items to be iterated are empty as 78*c1d01b5fSSimon J. Gerraty# well. 792c3632d1SSimon J. Gerratyvar= value before 802c3632d1SSimon J. Gerratyvar2= value before 812c3632d1SSimon J. Gerraty.for var var2 in ${:U} 822c3632d1SSimon J. Gerraty.endfor 832c3632d1SSimon J. Gerraty.if ${var} != "value before" 842c3632d1SSimon J. Gerraty. warning After the .for loop, var must still have its original value. 852c3632d1SSimon J. Gerraty.endif 862c3632d1SSimon J. Gerraty.if ${var2} != "value before" 872c3632d1SSimon J. Gerraty. warning After the .for loop, var2 must still have its original value. 882c3632d1SSimon J. Gerraty.endif 892c3632d1SSimon J. Gerraty 902c3632d1SSimon J. Gerraty# Until 2008-12-21, the values of the iteration variables were simply 912c3632d1SSimon J. Gerraty# inserted as plain text and then parsed as usual, which made it possible 92*c1d01b5fSSimon J. Gerraty# to achieve all kinds of strange effects, such as generating '.if' 93*c1d01b5fSSimon J. Gerraty# directives or inserting '$' characters in random places, thereby changing 94*c1d01b5fSSimon J. Gerraty# how following '$' are interpreted. 952c3632d1SSimon J. Gerraty# 96*c1d01b5fSSimon J. Gerraty# Before that date, the .for loop below expanded to: 972c3632d1SSimon J. Gerraty# EXPANSION+= value 98*c1d01b5fSSimon J. Gerraty# Since that date, the .for loop below expands to: 992c3632d1SSimon J. Gerraty# EXPANSION${:U+}= value 1002c3632d1SSimon J. Gerraty# 1012c3632d1SSimon J. GerratyEXPANSION= before 1022c3632d1SSimon J. GerratyEXPANSION+ = before 1032c3632d1SSimon J. Gerraty.for plus in + 1042c3632d1SSimon J. GerratyEXPANSION${plus}= value 1052c3632d1SSimon J. Gerraty.endfor 1062c3632d1SSimon J. Gerraty.if ${EXPANSION} != "before" 1072c3632d1SSimon J. Gerraty. error This must be a make from before 2009. 1082c3632d1SSimon J. Gerraty.endif 1092c3632d1SSimon J. Gerraty.if ${EXPANSION+} != "value" 1102c3632d1SSimon J. Gerraty. error This must be a make from before 2009. 1112c3632d1SSimon J. Gerraty.endif 1122c3632d1SSimon J. Gerraty 113956e45f6SSimon J. Gerraty# When the outer .for loop is expanded, it sees the expression ${i} and 114*c1d01b5fSSimon J. Gerraty# expands it. The inner loop then only sees the expression ${:Uouter} and 115*c1d01b5fSSimon J. Gerraty# has nothing more to expand. 116956e45f6SSimon J. Gerraty.for i in outer 117956e45f6SSimon J. Gerraty. for i in inner 118*c1d01b5fSSimon J. Gerraty# expect+1: outer 119956e45f6SSimon J. Gerraty. info ${i} 120956e45f6SSimon J. Gerraty. endfor 121956e45f6SSimon J. Gerraty.endfor 122956e45f6SSimon J. Gerraty 123*c1d01b5fSSimon J. Gerraty 124956e45f6SSimon J. Gerraty# From https://gnats.netbsd.org/29985. 125956e45f6SSimon J. Gerraty# 126956e45f6SSimon J. Gerraty# Until 2008-12-21, the .for loop was expanded by replacing the variable 127956e45f6SSimon J. Gerraty# value literally in the body. This could lead to situations where the 128956e45f6SSimon J. Gerraty# characters from the variable value were interpreted as markup rather than 129956e45f6SSimon J. Gerraty# plain text. 130956e45f6SSimon J. Gerraty# 131956e45f6SSimon J. Gerraty# Until 2012-06-03, the .for loop had split the words at whitespace, without 132956e45f6SSimon J. Gerraty# taking quotes into account. This made it possible to have variable values 133956e45f6SSimon J. Gerraty# like "a:\ a:\file.txt" that ended in a single backslash. Since then, the 134956e45f6SSimon J. Gerraty# variable values have been replaced with expressions of the form ${:U...}, 135956e45f6SSimon J. Gerraty# which are not interpreted as code anymore. 136956e45f6SSimon J. Gerraty.for path in a:\ a:\file.txt d:\\ d:\\file.txt 137956e45f6SSimon J. Gerraty. info ${path} 138956e45f6SSimon J. Gerraty.endfor 139*c1d01b5fSSimon J. Gerraty# expect-2: a:\ a:\file.txt 140*c1d01b5fSSimon J. Gerraty# expect-3: d:\\ 141*c1d01b5fSSimon J. Gerraty# expect-4: d:\\file.txt 142*c1d01b5fSSimon J. Gerraty 143956e45f6SSimon J. Gerraty 144956e45f6SSimon J. Gerraty# Ensure that braces and parentheses are properly escaped by the .for loop. 145956e45f6SSimon J. Gerraty# Each line must print the same word 3 times. 1469f45a3c8SSimon J. Gerraty# See ForLoop_SubstBody. 147956e45f6SSimon J. Gerraty.for v in ( [ { ) ] } (()) [[]] {{}} )( ][ }{ 148956e45f6SSimon J. Gerraty. info $v ${v} $(v) 149956e45f6SSimon J. Gerraty.endfor 150*c1d01b5fSSimon J. Gerraty# expect-02: ( ( ( 151*c1d01b5fSSimon J. Gerraty# expect-03: [ [ [ 152*c1d01b5fSSimon J. Gerraty# expect-04: { { { 153*c1d01b5fSSimon J. Gerraty# expect-05: ) ) ) 154*c1d01b5fSSimon J. Gerraty# expect-06: ] ] ] 155*c1d01b5fSSimon J. Gerraty# expect-07: } } } 156*c1d01b5fSSimon J. Gerraty# expect-08: (()) (()) (()) 157*c1d01b5fSSimon J. Gerraty# expect-09: [[]] [[]] [[]] 158*c1d01b5fSSimon J. Gerraty# expect-10: {{}} {{}} {{}} 159*c1d01b5fSSimon J. Gerraty# expect-11: )( )( )( 160*c1d01b5fSSimon J. Gerraty# expect-12: ][ ][ ][ 161*c1d01b5fSSimon J. Gerraty# expect-13: }{ }{ }{ 162956e45f6SSimon J. Gerraty 163*c1d01b5fSSimon J. Gerraty# Before 2023-05-09, the variable names could contain arbitrary characters, 164*c1d01b5fSSimon J. Gerraty# except for whitespace, allowing for creative side effects, as usual for 165*c1d01b5fSSimon J. Gerraty# arbitrary code injection. 166956e45f6SSimon J. Gerratyvar= outer 167*c1d01b5fSSimon J. Gerraty# expect+1: invalid character ':' in .for loop variable name 168956e45f6SSimon J. Gerraty.for var:Q in value "quoted" 169*c1d01b5fSSimon J. Gerraty. info <${var}> <${var:Q}> <${var:Q:Q}> 170*c1d01b5fSSimon J. Gerraty.endfor 171*c1d01b5fSSimon J. Gerraty 172*c1d01b5fSSimon J. Gerraty# Before 2023-05-09, when variable names could contain '$', the short 173*c1d01b5fSSimon J. Gerraty# expression '$$' was preserved, the long expressions were substituted. 174*c1d01b5fSSimon J. Gerraty# expect+1: invalid character '$' in .for loop variable name 175*c1d01b5fSSimon J. Gerraty.for $ in value 176*c1d01b5fSSimon J. Gerraty. info <$$> <${$}> <$($)> 177*c1d01b5fSSimon J. Gerraty.endfor 178*c1d01b5fSSimon J. Gerraty 179*c1d01b5fSSimon J. Gerraty 180*c1d01b5fSSimon J. Gerraty# https://gnats.netbsd.org/53146 mentions the idea of using a dynamic 181*c1d01b5fSSimon J. Gerraty# variable name in .for loops, based on some other variable. The .for loops 182*c1d01b5fSSimon J. Gerraty# are already tricky enough to understand in detail, even without this 183*c1d01b5fSSimon J. Gerraty# possibility, therefore the variable names are restricted to using harmless 184*c1d01b5fSSimon J. Gerraty# characters only. 185*c1d01b5fSSimon J. GerratyINDIRECT= direct 186*c1d01b5fSSimon J. Gerraty# expect+1: invalid character '$' in .for loop variable name 187*c1d01b5fSSimon J. Gerraty.for $(INDIRECT) in value 188*c1d01b5fSSimon J. Gerraty# If the variable name could be chosen dynamically, the iteration variable 189*c1d01b5fSSimon J. Gerraty# might have been 'direct', thereby expanding the expression '${direct}'. 190*c1d01b5fSSimon J. Gerraty. info <$(INDIRECT)> <$(direct)> <$($(INDIRECT))> 191956e45f6SSimon J. Gerraty.endfor 192956e45f6SSimon J. Gerraty 19306b9b3e0SSimon J. Gerraty 19406b9b3e0SSimon J. Gerraty# XXX: A parse error or evaluation error in the items of the .for loop 195*c1d01b5fSSimon J. Gerraty# should skip the whole loop. As of 2023-05-09, the loop is expanded as 196*c1d01b5fSSimon J. Gerraty# usual. 197*c1d01b5fSSimon J. Gerraty# expect+1: Unknown modifier "Z" 19806b9b3e0SSimon J. Gerraty.for var in word1 ${:Uword2:Z} word3 19906b9b3e0SSimon J. Gerraty. info XXX: Not reached ${var} 20006b9b3e0SSimon J. Gerraty.endfor 201*c1d01b5fSSimon J. Gerraty# expect-2: XXX: Not reached word1 202*c1d01b5fSSimon J. Gerraty# expect-3: XXX: Not reached word3 20306b9b3e0SSimon J. Gerraty 2049f45a3c8SSimon J. Gerraty 2059f45a3c8SSimon J. Gerraty# An empty list of variables to the left of the 'in' is a parse error. 2069f45a3c8SSimon J. Gerraty.for in value # expect+0: no iteration variables in for 207*c1d01b5fSSimon J. Gerraty. error 208*c1d01b5fSSimon J. Gerraty.endfor 2099f45a3c8SSimon J. Gerraty 2109f45a3c8SSimon J. Gerraty# An empty list of iteration values to the right of the 'in' is accepted. 2119f45a3c8SSimon J. Gerraty# Unlike in the shell, it is not a parse error. 2129f45a3c8SSimon J. Gerraty.for var in 2139f45a3c8SSimon J. Gerraty. error 2149f45a3c8SSimon J. Gerraty.endfor 2159f45a3c8SSimon J. Gerraty 2169f45a3c8SSimon J. Gerraty# If the iteration values become empty after expanding the expressions, the 2179f45a3c8SSimon J. Gerraty# body of the loop is not evaluated. It is not a parse error. 2189f45a3c8SSimon J. Gerraty.for var in ${:U} 2199f45a3c8SSimon J. Gerraty. error 2209f45a3c8SSimon J. Gerraty.endfor 2219f45a3c8SSimon J. Gerraty 2229f45a3c8SSimon J. Gerraty 2239f45a3c8SSimon J. Gerraty# The loop body can be empty. 2249f45a3c8SSimon J. Gerraty.for var in 1 2 3 2259f45a3c8SSimon J. Gerraty.endfor 2269f45a3c8SSimon J. Gerraty 2279f45a3c8SSimon J. Gerraty 2289f45a3c8SSimon J. Gerraty# A mismatched .if inside a .for loop is detected each time when the loop body 2299f45a3c8SSimon J. Gerraty# is processed. 2309f45a3c8SSimon J. Gerraty.for var in value 2319f45a3c8SSimon J. Gerraty. if 0 2329f45a3c8SSimon J. Gerraty.endfor # expect+0: 1 open conditional 2339f45a3c8SSimon J. Gerraty 2349f45a3c8SSimon J. Gerraty# If there are no iteration values, the loop body is not processed, and the 2359f45a3c8SSimon J. Gerraty# check for mismatched conditionals is not performed. 2369f45a3c8SSimon J. Gerraty.for var in ${:U} 2379f45a3c8SSimon J. Gerraty. if 0 2389f45a3c8SSimon J. Gerraty.endfor 2399f45a3c8SSimon J. Gerraty 2409f45a3c8SSimon J. Gerraty 2419f45a3c8SSimon J. Gerraty# When a .for without the corresponding .endfor occurs in an inactive branch 2429f45a3c8SSimon J. Gerraty# of an .if, the .for directive is just skipped, it does not even need a 2439f45a3c8SSimon J. Gerraty# corresponding .endfor. In other words, the behavior of the parser depends 2449f45a3c8SSimon J. Gerraty# on the actual values of the conditions in the .if clauses. 2459f45a3c8SSimon J. Gerraty.if 0 2469f45a3c8SSimon J. Gerraty. for var in value # does not need a corresponding .endfor 2479f45a3c8SSimon J. Gerraty.endif 2489f45a3c8SSimon J. Gerraty.endfor # expect+0: for-less endfor 2499f45a3c8SSimon J. Gerraty.endif # expect+0: if-less endif 2509f45a3c8SSimon J. Gerraty 2519f45a3c8SSimon J. Gerraty 2529f45a3c8SSimon J. Gerraty# When a .for without the corresponding .endfor occurs in an active branch of 2539f45a3c8SSimon J. Gerraty# an .if, the parser just counts the number of .for and .endfor directives, 2549f45a3c8SSimon J. Gerraty# without looking at any other directives. 2559f45a3c8SSimon J. Gerraty.if 1 2569f45a3c8SSimon J. Gerraty. for var in value 2579f45a3c8SSimon J. Gerraty. endif # expect+0: if-less endif 2589f45a3c8SSimon J. Gerraty. endfor # no 'for-less endfor' 2599f45a3c8SSimon J. Gerraty.endif # no 'if-less endif' 2609f45a3c8SSimon J. Gerraty 2619f45a3c8SSimon J. Gerraty 262*c1d01b5fSSimon J. Gerraty# Before for.c 1.172 from 2023-05-08, when make parsed a .for loop, it 263*c1d01b5fSSimon J. Gerraty# assumed that there was no line continuation between the '.' and the 'for' 264*c1d01b5fSSimon J. Gerraty# or 'endfor', as there is no practical reason to break the line at this 265*c1d01b5fSSimon J. Gerraty# point. 266*c1d01b5fSSimon J. Gerraty# 267*c1d01b5fSSimon J. Gerraty# When make scanned the outer .for loop, it did not recognize the inner .for 268*c1d01b5fSSimon J. Gerraty# loop as such and instead treated it as an unknown directive. The body of 269*c1d01b5fSSimon J. Gerraty# the outer .for loop thus ended above the '.endfor'. 270*c1d01b5fSSimon J. Gerraty# 271*c1d01b5fSSimon J. Gerraty# When make scanned the inner .for loop, it did not recognize the inner 272*c1d01b5fSSimon J. Gerraty# .endfor as such, which led to a parse error 'Unexpected end of file in .for 273*c1d01b5fSSimon J. Gerraty# loop' from the '.endfor' line, followed by a second parse error 'for-less 274*c1d01b5fSSimon J. Gerraty# .endfor' from the '.\\n endfor' line. 2759f45a3c8SSimon J. Gerraty.MAKEFLAGS: -df 2769f45a3c8SSimon J. Gerraty.for outer in o 2779f45a3c8SSimon J. Gerraty.\ 2789f45a3c8SSimon J. Gerraty for inner in i 2799f45a3c8SSimon J. Gerraty.\ 2809f45a3c8SSimon J. Gerraty endfor 2819f45a3c8SSimon J. Gerraty.endfor 2829f45a3c8SSimon J. Gerraty.MAKEFLAGS: -d0 2834fde40d9SSimon J. Gerraty 2844fde40d9SSimon J. Gerraty 2854fde40d9SSimon J. Gerraty# When there is a variable definition 'scope=cmdline' from the command line 2864fde40d9SSimon J. Gerraty# (which has higher precedence than global variables) and a .for loop iterates 2874fde40d9SSimon J. Gerraty# over a variable of the same name, the expression '${scope}' expands to the 2884fde40d9SSimon J. Gerraty# value from the .for loop. This is because when the body of the .for loop is 2894fde40d9SSimon J. Gerraty# expanded, the expression '${scope}' is textually replaced with ${:Uloop}', 2904fde40d9SSimon J. Gerraty# without resolving any other variable names (ForLoop_SubstBody). Later, when 2914fde40d9SSimon J. Gerraty# the body of the .for loop is actually interpreted, the body text doesn't 2924fde40d9SSimon J. Gerraty# contain the word 'scope' anymore. 2934fde40d9SSimon J. Gerraty.MAKEFLAGS: scope=cmdline 2944fde40d9SSimon J. Gerraty.for scope in loop 2954fde40d9SSimon J. Gerraty. if ${scope} != "loop" 2964fde40d9SSimon J. Gerraty. error 2974fde40d9SSimon J. Gerraty. endif 2984fde40d9SSimon J. Gerraty.endfor 299*c1d01b5fSSimon J. Gerraty 300*c1d01b5fSSimon J. Gerraty 301*c1d01b5fSSimon J. Gerraty# Since at least 1993, iteration stops at the first newline. 302*c1d01b5fSSimon J. Gerraty# Back then, the .newline variable didn't exist, therefore it was unlikely 303*c1d01b5fSSimon J. Gerraty# that a newline ever occurred. 304*c1d01b5fSSimon J. Gerraty.for var in a${.newline}b${.newline}c 305*c1d01b5fSSimon J. Gerraty. info newline-item=(${var}) 306*c1d01b5fSSimon J. Gerraty.endfor 307*c1d01b5fSSimon J. Gerraty# expect-2: newline-item=(a) 308